2021-10-16 11:44:34 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-10-16 11:44:34 +05:30
|
|
|
|
2021-10-21 21:40:49 +05:30
|
|
|
package updatechecker
|
2021-10-16 11:44:34 +05:30
|
|
|
|
|
|
|
import (
|
2023-10-15 21:16:06 +05:30
|
|
|
"context"
|
2023-01-27 05:37:33 +05:30
|
|
|
"errors"
|
2022-01-10 07:18:13 +05:30
|
|
|
"io"
|
2023-01-27 05:37:33 +05:30
|
|
|
"net"
|
2021-10-16 11:44:34 +05:30
|
|
|
"net/http"
|
2023-01-27 05:37:33 +05:30
|
|
|
"strings"
|
2021-10-16 11:44:34 +05:30
|
|
|
|
2021-12-17 06:45:02 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-10-16 11:44:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/proxy"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-10-17 04:59:26 +05:30
|
|
|
"code.gitea.io/gitea/modules/system"
|
2021-10-16 11:44:34 +05:30
|
|
|
|
|
|
|
"github.com/hashicorp/go-version"
|
|
|
|
)
|
|
|
|
|
2021-10-21 21:40:49 +05:30
|
|
|
// CheckerState stores the remote version from the JSON endpoint
|
|
|
|
type CheckerState struct {
|
|
|
|
LatestVersion string
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
|
|
|
|
2021-10-21 21:40:49 +05:30
|
|
|
// Name returns the name of the state item for update checker
|
|
|
|
func (r *CheckerState) Name() string {
|
|
|
|
return "update-checker"
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GiteaUpdateChecker returns error when new version of Gitea is available
|
2023-01-27 05:37:33 +05:30
|
|
|
func GiteaUpdateChecker(httpEndpoint, domainEndpoint string) error {
|
|
|
|
var version string
|
|
|
|
var err error
|
|
|
|
if domainEndpoint != "" {
|
|
|
|
version, err = getVersionDNS(domainEndpoint)
|
|
|
|
} else {
|
|
|
|
version, err = getVersionHTTP(httpEndpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return UpdateRemoteVersion(context.Background(), version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getVersionDNS will request the TXT records for the domain. If a record starts
|
|
|
|
// with "forgejo_versions=" everything after that will be used as the latest
|
|
|
|
// version available.
|
|
|
|
func getVersionDNS(domainEndpoint string) (version string, err error) {
|
|
|
|
records, err := net.LookupTXT(domainEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(records) == 0 {
|
|
|
|
return "", errors.New("no TXT records were found")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
if strings.HasPrefix(record, "forgejo_versions=") {
|
|
|
|
// Get all supported versions, separated by a comma.
|
|
|
|
supportedVersions := strings.Split(strings.TrimPrefix(record, "forgejo_versions="), ",")
|
|
|
|
// For now always return the latest supported version.
|
|
|
|
return supportedVersions[len(supportedVersions)-1], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("there is no TXT record with a valid value")
|
|
|
|
}
|
|
|
|
|
|
|
|
// getVersionHTTP will make an HTTP request to the endpoint, and the returned
|
|
|
|
// content is JSON. The "latest.version" path's value will be used as the latest
|
|
|
|
// version available.
|
|
|
|
func getVersionHTTP(httpEndpoint string) (version string, err error) {
|
2021-10-16 11:44:34 +05:30
|
|
|
httpClient := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: proxy.Proxy(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", httpEndpoint, nil)
|
|
|
|
if err != nil {
|
2023-01-27 05:37:33 +05:30
|
|
|
return "", err
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
2023-01-27 05:37:33 +05:30
|
|
|
return "", err
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2022-01-10 07:18:13 +05:30
|
|
|
body, err := io.ReadAll(resp.Body)
|
2021-10-16 11:44:34 +05:30
|
|
|
if err != nil {
|
2023-01-27 05:37:33 +05:30
|
|
|
return "", err
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
|
|
|
|
2021-10-21 21:40:49 +05:30
|
|
|
type respType struct {
|
2021-10-16 11:44:34 +05:30
|
|
|
Latest struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
} `json:"latest"`
|
|
|
|
}
|
2021-10-21 21:40:49 +05:30
|
|
|
respData := respType{}
|
|
|
|
err = json.Unmarshal(body, &respData)
|
2021-10-16 11:44:34 +05:30
|
|
|
if err != nil {
|
2023-01-27 05:37:33 +05:30
|
|
|
return "", err
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
2023-01-27 05:37:33 +05:30
|
|
|
return respData.Latest.Version, nil
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRemoteVersion updates the latest available version of Gitea
|
2023-10-15 21:16:06 +05:30
|
|
|
func UpdateRemoteVersion(ctx context.Context, version string) (err error) {
|
|
|
|
return system.AppState.Set(ctx, &CheckerState{LatestVersion: version})
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
|
|
|
|
2022-01-10 15:02:37 +05:30
|
|
|
// GetRemoteVersion returns the current remote version (or currently installed version if fail to fetch from DB)
|
2023-10-15 21:16:06 +05:30
|
|
|
func GetRemoteVersion(ctx context.Context) string {
|
2021-10-21 21:40:49 +05:30
|
|
|
item := new(CheckerState)
|
2023-10-15 21:16:06 +05:30
|
|
|
if err := system.AppState.Get(ctx, item); err != nil {
|
2021-10-21 21:40:49 +05:30
|
|
|
return ""
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
2021-10-21 21:40:49 +05:30
|
|
|
return item.LatestVersion
|
2021-10-16 11:44:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetNeedUpdate returns true whether a newer version of Gitea is available
|
2023-10-15 21:16:06 +05:30
|
|
|
func GetNeedUpdate(ctx context.Context) bool {
|
2021-10-16 11:44:34 +05:30
|
|
|
curVer, err := version.NewVersion(setting.AppVer)
|
|
|
|
if err != nil {
|
|
|
|
// return false to fail silently
|
|
|
|
return false
|
|
|
|
}
|
2023-10-15 21:16:06 +05:30
|
|
|
remoteVerStr := GetRemoteVersion(ctx)
|
2021-10-21 21:40:49 +05:30
|
|
|
if remoteVerStr == "" {
|
|
|
|
// no remote version is known
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
remoteVer, err := version.NewVersion(remoteVerStr)
|
2021-10-16 11:44:34 +05:30
|
|
|
if err != nil {
|
|
|
|
// return false to fail silently
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return curVer.LessThan(remoteVer)
|
|
|
|
}
|