Skip to content

Commit d2fa156

Browse files
committed
feat: use proxy for update if configured
1 parent 350aa9b commit d2fa156

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

internal/service/update.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,20 @@ func NewUpdateService(cfg *config.Config, s *storage.SQLiteStorage, version stri
2929
}
3030
}
3131

32+
// syncProxyConfig syncs proxy configuration to updater
33+
func (u *UpdateService) syncProxyConfig() {
34+
if proxyCfg := u.config.GetProxy(); proxyCfg != nil {
35+
u.updater.SetProxyURL(proxyCfg.URL)
36+
} else {
37+
u.updater.SetProxyURL("")
38+
}
39+
}
40+
3241
// CheckForUpdates checks if a new version is available
3342
func (u *UpdateService) CheckForUpdates() string {
3443
logger.Info("CheckForUpdates called, current version: %s", u.version)
3544

45+
u.syncProxyConfig()
3646
info, err := u.updater.CheckForUpdates()
3747
if err != nil {
3848
logger.Error("CheckForUpdates failed: %v", err)
@@ -109,6 +119,7 @@ func (u *UpdateService) SkipVersion(version string) error {
109119

110120
// DownloadUpdate downloads the update file
111121
func (u *UpdateService) DownloadUpdate(url, filename string) error {
122+
u.syncProxyConfig()
112123
return u.updater.DownloadUpdate(url, filename)
113124
}
114125

internal/updater/downloader.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Downloader struct {
2727
progress DownloadProgress
2828
mu sync.RWMutex
2929
cancelChan chan struct{}
30+
proxyURL string
3031
}
3132

3233
// NewDownloader creates a new downloader
@@ -36,6 +37,13 @@ func NewDownloader() *Downloader {
3637
}
3738
}
3839

40+
// SetProxyURL sets the proxy URL for downloads
41+
func (d *Downloader) SetProxyURL(proxyURL string) {
42+
d.mu.Lock()
43+
d.proxyURL = proxyURL
44+
d.mu.Unlock()
45+
}
46+
3947
// Download downloads a file from URL to destination
4048
func (d *Downloader) Download(url, destPath string) error {
4149
d.mu.Lock()
@@ -44,6 +52,7 @@ func (d *Downloader) Download(url, destPath string) error {
4452
FilePath: destPath,
4553
}
4654
d.cancelChan = make(chan struct{})
55+
proxyURL := d.proxyURL
4756
d.mu.Unlock()
4857

4958
// Create destination directory
@@ -73,6 +82,13 @@ func (d *Downloader) Download(url, destPath string) error {
7382
},
7483
}
7584

85+
// Apply proxy if configured
86+
if proxyURL != "" {
87+
if transport, err := createProxyTransport(proxyURL); err == nil {
88+
client.Transport = transport
89+
}
90+
}
91+
7692
req, err := http.NewRequest("GET", url, nil)
7793
if err != nil {
7894
d.setError(fmt.Sprintf("failed to create request: %v", err))

internal/updater/github.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"net/url"
89
"runtime"
910
"strings"
1011
"time"
12+
13+
"golang.org/x/net/proxy"
1114
)
1215

1316
const (
@@ -32,9 +35,15 @@ type Asset struct {
3235
}
3336

3437
// GetLatestRelease fetches the latest release from GitHub
35-
func GetLatestRelease() (*ReleaseInfo, error) {
38+
func GetLatestRelease(proxyURL string) (*ReleaseInfo, error) {
3639
client := &http.Client{Timeout: httpTimeout}
3740

41+
if proxyURL != "" {
42+
if transport, err := createProxyTransport(proxyURL); err == nil {
43+
client.Transport = transport
44+
}
45+
}
46+
3847
req, err := http.NewRequest("GET", githubAPIURL, nil)
3948
if err != nil {
4049
return nil, fmt.Errorf("failed to create request: %w", err)
@@ -102,3 +111,35 @@ func getPlatformPattern() string {
102111
return fmt.Sprintf("%s-%s", goos, goarch)
103112
}
104113
}
114+
115+
// createProxyTransport creates an http.Transport with proxy support
116+
func createProxyTransport(proxyURL string) (*http.Transport, error) {
117+
parsed, err := url.Parse(proxyURL)
118+
if err != nil {
119+
return nil, err
120+
}
121+
122+
transport := &http.Transport{}
123+
124+
switch parsed.Scheme {
125+
case "socks5", "socks5h":
126+
auth := &proxy.Auth{}
127+
if parsed.User != nil {
128+
auth.User = parsed.User.Username()
129+
auth.Password, _ = parsed.User.Password()
130+
} else {
131+
auth = nil
132+
}
133+
dialer, err := proxy.SOCKS5("tcp", parsed.Host, auth, proxy.Direct)
134+
if err != nil {
135+
return nil, err
136+
}
137+
transport.Dial = dialer.Dial
138+
case "http", "https":
139+
transport.Proxy = http.ProxyURL(parsed)
140+
default:
141+
return nil, fmt.Errorf("unsupported proxy scheme: %s", parsed.Scheme)
142+
}
143+
144+
return transport, nil
145+
}

internal/updater/updater.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Updater struct {
4040
currentVersion string
4141
downloader *Downloader
4242
downloadPath string
43+
proxyURL string
4344
}
4445

4546
// New creates a new updater
@@ -51,9 +52,15 @@ func New(currentVersion string) *Updater {
5152
}
5253
}
5354

55+
// SetProxyURL sets the proxy URL for HTTP requests
56+
func (u *Updater) SetProxyURL(proxyURL string) {
57+
u.proxyURL = proxyURL
58+
u.downloader.SetProxyURL(proxyURL)
59+
}
60+
5461
// CheckForUpdates checks if a new version is available
5562
func (u *Updater) CheckForUpdates() (*UpdateInfo, error) {
56-
release, err := GetLatestRelease()
63+
release, err := GetLatestRelease(u.proxyURL)
5764
if err != nil {
5865
return nil, fmt.Errorf("failed to check updates: %w", err)
5966
}

0 commit comments

Comments
 (0)