Skip to content

Commit 66ee033

Browse files
feat(appstore): append panel version and edition to resource requests
1 parent 3b74ab3 commit 66ee033

8 files changed

Lines changed: 82 additions & 18 deletions

File tree

‎agent/app/service/app.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
945945
}
946946

947947
versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.AppRepoURL(), global.CONF.Base.Mode)
948-
_, versionRes, err := req_helper.HandleRequest(versionUrl, http.MethodGet, constant.TimeOut20s)
948+
_, versionRes, err := req_helper.HandleRequest(versionUrl, http.MethodGet, constant.TimeOut20s, req_helper.WithAppStoreUserAgent)
949949
if err != nil {
950950
return nil, err
951951
}
@@ -989,7 +989,7 @@ func getAppFromRepo(downloadPath string) error {
989989
global.LOG.Infof("[AppStore] download file from %s", downloadUrl)
990990
fileOp := files.NewFileOp()
991991
packagePath := filepath.Join(global.Dir.ResourceDir, filepath.Base(downloadUrl))
992-
if err := files.DownloadFileWithProxy(downloadUrl, packagePath); err != nil {
992+
if err := files.DownloadFileWithProxy(downloadUrl, packagePath, req_helper.WithAppStoreUserAgent); err != nil {
993993
return err
994994
}
995995

‎agent/app/service/app_compose.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func ensureAppDetailCompose(app model.App, detail *model.AppDetail) error {
4444
}
4545
if err == nil {
4646
var status int
47-
status, content, err = req_helper.HandleRequest(composeURL, http.MethodGet, constant.TimeOut20s)
47+
status, content, err = req_helper.HandleRequest(composeURL, http.MethodGet, constant.TimeOut20s, req_helper.WithAppStoreUserAgent)
4848
if status == http.StatusNotFound {
4949
return buserr.New("ErrAppVersionUnavailable")
5050
}

‎agent/app/service/app_utils.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.App
10221022

10231023
filePath := path.Join(appVersionDir, app.Key+"-"+appDetail.Version+".tar.gz")
10241024

1025-
if err = files.DownloadFileWithProxy(downloadURL, filePath); err != nil {
1025+
if err = files.DownloadFileWithProxy(downloadURL, filePath, req_helper.WithAppStoreUserAgent); err != nil {
10261026
if logger == nil {
10271027
global.LOG.Errorf("download app [%s] error %v", app.Name, err)
10281028
} else {

‎agent/global/global.go‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,15 @@ func ResourceURL() string {
8383
}
8484
return "https://resource.1panel.pro/v2/resource"
8585
}
86+
87+
const (
88+
AppRepoHostCN = "apps-assets.fit2cloud.com"
89+
AppRepoHostIntl = "apps.1panel.pro"
90+
)
91+
8692
func AppRepoURL() string {
87-
if CONF.Base.IsEnterprise {
88-
return "https://apps-assets.fit2cloud.com"
89-
}
90-
if CONF.Base.Edition != "intl" {
91-
return "https://apps-assets.fit2cloud.com"
93+
if CONF.Base.IsEnterprise || CONF.Base.Edition != "intl" {
94+
return "https://" + AppRepoHostCN
9295
}
93-
return "https://apps.1panel.pro"
96+
return "https://" + AppRepoHostIntl
9497
}

‎agent/utils/files/utils.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ func IsEmptyDir(dir string) bool {
334334
return err == io.EOF
335335
}
336336

337-
func DownloadFileWithProxy(url, dst string) error {
338-
resp, cancel, err := req_helper.RequestFile(url, http.MethodGet, constant.TimeOut5m)
337+
func DownloadFileWithProxy(url, dst string, options ...req_helper.RequestOption) error {
338+
resp, cancel, err := req_helper.RequestFile(url, http.MethodGet, constant.TimeOut5m, options...)
339339
if err != nil {
340340
return err
341341
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package req_helper
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
7+
"github.com/1Panel-dev/1Panel/agent/global"
8+
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
9+
)
10+
11+
// WithAppStoreUserAgent identifies the node to the official app store origins.
12+
func WithAppStoreUserAgent(req *http.Request) {
13+
target := req.URL
14+
if target.Scheme != "https" || (target.Port() != "" && target.Port() != "443") {
15+
return
16+
}
17+
switch strings.ToLower(target.Hostname()) {
18+
case global.AppRepoHostCN, global.AppRepoHostIntl:
19+
original := req.UserAgent()
20+
if _, set := req.Header["User-Agent"]; !set {
21+
// net/http adds this default only when writing the store's HTTP/1.1 request.
22+
original = "Go-http-client/1.1"
23+
}
24+
req.Header.Set("User-Agent", strings.TrimSpace(original+" "+appStoreUserAgent()))
25+
}
26+
}
27+
28+
func appStoreUserAgent() string {
29+
edition := "community"
30+
if global.CONF.Base.IsEnterprise {
31+
edition = "enterprise"
32+
} else if xpack.MultiNodeProvider.IsXpack() {
33+
edition = "pro"
34+
}
35+
version := strings.TrimPrefix(strings.TrimSpace(global.CONF.Base.Version), "v")
36+
if version == "" {
37+
version = "unknown"
38+
}
39+
return "1Panel/" + version + " (edition=" + edition + ")"
40+
}

‎agent/utils/req_helper/external_http.go‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@ import (
77
"time"
88
)
99

10-
func newExternalHTTPClient(transport *http.Transport, timeout time.Duration) *http.Client {
10+
// RequestOption configures one outgoing request, including each redirect hop.
11+
type RequestOption func(*http.Request)
12+
13+
type requestOptionTransport struct {
14+
*http.Transport
15+
options []RequestOption
16+
}
17+
18+
func (t requestOptionTransport) RoundTrip(req *http.Request) (*http.Response, error) {
19+
// Apply headers to this hop only. net/http copies the original headers on redirects.
20+
req = req.Clone(req.Context())
21+
for _, option := range t.options {
22+
option(req)
23+
}
24+
return t.Transport.RoundTrip(req)
25+
}
26+
27+
func newExternalHTTPClient(transport *http.Transport, timeout time.Duration, options ...RequestOption) *http.Client {
1128
if transport == nil {
1229
transport = http.DefaultTransport.(*http.Transport)
1330
}
@@ -21,11 +38,15 @@ func newExternalHTTPClient(transport *http.Transport, timeout time.Duration) *ht
2138
verified.TLSClientConfig.ServerName = ""
2239
verified.DialTLS = nil
2340
verified.DialTLSContext = nil
24-
return &http.Client{
41+
client := &http.Client{
2542
Timeout: timeout,
2643
Transport: verified,
2744
CheckRedirect: checkExternalRedirect,
2845
}
46+
if len(options) > 0 {
47+
client.Transport = requestOptionTransport{Transport: verified, options: options}
48+
}
49+
return client
2950
}
3051

3152
func checkExternalRedirect(req *http.Request, via []*http.Request) error {

‎agent/utils/req_helper/request.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func HandleGet(url string) (*http.Response, error) {
4747
return resp, nil
4848
}
4949

50-
func HandleRequest(url, method string, timeout int) (int, []byte, error) {
50+
func HandleRequest(url, method string, timeout int, options ...RequestOption) (int, []byte, error) {
5151
transport := xpack.MultiNodeProvider.LoadRequestTransport()
52-
client := newExternalHTTPClient(transport, time.Duration(timeout)*time.Second)
52+
client := newExternalHTTPClient(transport, time.Duration(timeout)*time.Second, options...)
5353
defer client.CloseIdleConnections()
5454
return HandleRequestWithClient(client, url, method, timeout)
5555
}
@@ -125,15 +125,15 @@ func HandleRequestWithHeaders(client *http.Client, url, method string, timeout i
125125
}, nil
126126
}
127127

128-
func RequestFile(url, method string, timeout int) (io.ReadCloser, context.CancelFunc, error) {
128+
func RequestFile(url, method string, timeout int, options ...RequestOption) (io.ReadCloser, context.CancelFunc, error) {
129129
defer func() {
130130
if r := recover(); r != nil {
131131
global.LOG.Errorf("handle request failed, error message: %v", r)
132132
return
133133
}
134134
}()
135135
transport := xpack.MultiNodeProvider.LoadRequestTransport()
136-
client := newExternalHTTPClient(transport, time.Duration(timeout)*time.Second)
136+
client := newExternalHTTPClient(transport, time.Duration(timeout)*time.Second, options...)
137137
defer client.CloseIdleConnections()
138138
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
139139
request, err := http.NewRequestWithContext(ctx, method, url, nil)

0 commit comments

Comments
 (0)