mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-17 19:46:18 +00:00
[PRIVACY] Add a DNS method to fetch new updates
- Use TXT records in order to determine the latest available version. - This addresses a valid privacy issue, as with HTTP requests the server can keep track(estimated) of how many instances are using Forgejo, with DNS that's basically not possible as the server will never receive any data, as the only ones receiving data are DNS resolvers. (cherry picked from commit0baefb546a
) (cherry picked from commite8ee41880b
) (cherry picked from commit7eca4f3bf1
) (cherry picked from commit6dde3992dc
) (cherry picked from commitfb3a37fbfc
) (cherry picked from commit8304af1e9d
) (cherry picked from commit0543a7d12a
) (cherry picked from commitc3a22933b7
) (cherry picked from commite243707694
) (cherry picked from commit7eb6d1bcf7
) (cherry picked from commit1d7b9535cd
)
This commit is contained in:
parent
163df86df5
commit
05920dce67
|
@ -2174,6 +2174,7 @@ ROUTER = console
|
|||
;ENABLE_SUCCESS_NOTICE = false
|
||||
;SCHEDULE = @every 168h
|
||||
;HTTP_ENDPOINT = https://dl.gitea.io/gitea/version.json
|
||||
;DOMAIN_ENDPOINT = release.forgejo.org
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
|
|
@ -1025,6 +1025,7 @@ Default templates for project boards:
|
|||
- `ENABLE_SUCCESS_NOTICE`: **true**: Set to false to switch off success notices.
|
||||
- `SCHEDULE`: **@every 168h**: Cron syntax for scheduling a work, e.g. `@every 168h`.
|
||||
- `HTTP_ENDPOINT`: **https://dl.gitea.io/gitea/version.json**: the endpoint that Gitea will check for newer versions
|
||||
- `DOMAIN_ENDPOINT`: **release.forgejo.org**: the domain that, if specified, Gitea will check for newer versions. This is preferred over `HTTP_ENDPOINT`.
|
||||
|
||||
#### Cron - Delete all old system notices from database (`cron.delete_old_system_notices`)
|
||||
|
||||
|
|
|
@ -4,8 +4,11 @@
|
|||
package updatechecker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/proxy"
|
||||
|
@ -26,7 +29,51 @@ func (r *CheckerState) Name() string {
|
|||
}
|
||||
|
||||
// GiteaUpdateChecker returns error when new version of Gitea is available
|
||||
func GiteaUpdateChecker(httpEndpoint string) error {
|
||||
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(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) {
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: proxy.Proxy(),
|
||||
|
@ -35,16 +82,16 @@ func GiteaUpdateChecker(httpEndpoint string) error {
|
|||
|
||||
req, err := http.NewRequest("GET", httpEndpoint, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
type respType struct {
|
||||
|
@ -55,10 +102,9 @@ func GiteaUpdateChecker(httpEndpoint string) error {
|
|||
respData := respType{}
|
||||
err = json.Unmarshal(body, &respData)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
return UpdateRemoteVersion(respData.Latest.Version)
|
||||
return respData.Latest.Version, nil
|
||||
}
|
||||
|
||||
// UpdateRemoteVersion updates the latest available version of Gitea
|
||||
|
|
16
modules/updatechecker/update_checker_test.go
Normal file
16
modules/updatechecker/update_checker_test.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package updatechecker
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDNSUpdate(t *testing.T) {
|
||||
version, err := getVersionDNS("release.forgejo.org")
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, version)
|
||||
}
|
|
@ -142,7 +142,8 @@ func registerDeleteOldActions() {
|
|||
func registerUpdateGiteaChecker() {
|
||||
type UpdateCheckerConfig struct {
|
||||
BaseConfig
|
||||
HTTPEndpoint string
|
||||
HTTPEndpoint string
|
||||
DomainEndpoint string
|
||||
}
|
||||
RegisterTaskFatal("update_checker", &UpdateCheckerConfig{
|
||||
BaseConfig: BaseConfig{
|
||||
|
@ -150,10 +151,11 @@ func registerUpdateGiteaChecker() {
|
|||
RunAtStart: false,
|
||||
Schedule: "@every 168h",
|
||||
},
|
||||
HTTPEndpoint: "https://dl.gitea.io/gitea/version.json",
|
||||
HTTPEndpoint: "https://dl.gitea.io/gitea/version.json",
|
||||
DomainEndpoint: "release.forgejo.org",
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
updateCheckerConfig := config.(*UpdateCheckerConfig)
|
||||
return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint)
|
||||
return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint, updateCheckerConfig.DomainEndpoint)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue