Support using proxy dial-up connection to GitHub. (#46)

This commit is contained in:
三千 2025-02-10 00:45:37 +08:00 committed by GitHub
parent 09163ed4df
commit 4c5d288f03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 86 additions and 9 deletions

61
proxy/dial.go Normal file
View file

@ -0,0 +1,61 @@
package proxy
import (
"ghproxy/config"
"net/http"
"net/url"
"strings"
"golang.org/x/net/proxy"
)
func newProxyDial(proxyUrls string) proxy.Dialer {
var proxyDialer proxy.Dialer = proxy.Direct
for _, proxyUrl := range strings.Split(proxyUrls, ",") {
urlInfo, err := url.Parse(proxyUrl)
if err != nil {
continue
}
if urlInfo.Scheme != "socks5" {
continue
}
var auth *proxy.Auth = nil
if urlInfo.User != nil {
pwd, ok := urlInfo.User.Password()
if !ok {
continue
}
auth = &proxy.Auth{
User: urlInfo.User.Username(),
Password: pwd,
}
}
dialer, err := proxy.SOCKS5("tcp", urlInfo.Host, auth, proxyDialer)
if err == nil {
proxyDialer = dialer
}
}
return proxyDialer
}
func initTransport(cfg *config.Config, transport *http.Transport) {
if !cfg.Proxy.Enabled {
return
}
if cfg.Proxy.Url == "" {
transport.Proxy = http.ProxyFromEnvironment
return
}
proxyInfo, err := url.Parse(cfg.Proxy.Url)
if err == nil {
if strings.HasPrefix(cfg.Proxy.Url, "http") {
transport.Proxy = http.ProxyURL(proxyInfo)
} else {
proxyDialer := newProxyDial(cfg.Proxy.Url)
transport.Dial = proxyDialer.Dial
transport.DialContext = proxyDialer.(proxy.ContextDialer).DialContext
}
}
}