mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
Support using proxy dial-up connection to GitHub.
This commit is contained in:
parent
f5c32915b9
commit
c7aea91c43
8 changed files with 80 additions and 9 deletions
|
|
@ -114,6 +114,10 @@ enabled = false # 是否开启速率限制
|
||||||
rateMethod = "total" # "ip" or "total" 速率限制方式
|
rateMethod = "total" # "ip" or "total" 速率限制方式
|
||||||
ratePerMinute = 180 # 每分钟限制请求数量
|
ratePerMinute = 180 # 每分钟限制请求数量
|
||||||
burst = 5 # 突发请求数量
|
burst = 5 # 突发请求数量
|
||||||
|
|
||||||
|
[proxy]
|
||||||
|
enabled = false # 是否使用代理连接
|
||||||
|
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890" 支持HTTP代理和SOCKS5代理 支持多级SOCKS5代理
|
||||||
```
|
```
|
||||||
|
|
||||||
### 黑名单配置
|
### 黑名单配置
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ type Config struct {
|
||||||
Blacklist BlacklistConfig
|
Blacklist BlacklistConfig
|
||||||
Whitelist WhitelistConfig
|
Whitelist WhitelistConfig
|
||||||
RateLimit RateLimitConfig
|
RateLimit RateLimitConfig
|
||||||
|
Proxy ProxyConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
|
|
@ -61,6 +62,11 @@ type RateLimitConfig struct {
|
||||||
Burst int `toml:"burst"`
|
Burst int `toml:"burst"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ProxyConfig struct {
|
||||||
|
Enabled bool `toml:"enabled"`
|
||||||
|
Url string `toml:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConfig 从 TOML 配置文件加载配置
|
// LoadConfig 从 TOML 配置文件加载配置
|
||||||
func LoadConfig(filePath string) (*Config, error) {
|
func LoadConfig(filePath string) (*Config, error) {
|
||||||
var config Config
|
var config Config
|
||||||
|
|
|
||||||
|
|
@ -35,3 +35,7 @@ enabled = false
|
||||||
rateMethod = "total" # "ip" or "total"
|
rateMethod = "total" # "ip" or "total"
|
||||||
ratePerMinute = 180
|
ratePerMinute = 180
|
||||||
burst = 5
|
burst = 5
|
||||||
|
|
||||||
|
[proxy]
|
||||||
|
enabled = false
|
||||||
|
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890"
|
||||||
2
go.mod
2
go.mod
|
|
@ -6,6 +6,7 @@ require (
|
||||||
github.com/BurntSushi/toml v1.4.0
|
github.com/BurntSushi/toml v1.4.0
|
||||||
github.com/WJQSERVER-STUDIO/go-utils/logger v1.2.0
|
github.com/WJQSERVER-STUDIO/go-utils/logger v1.2.0
|
||||||
github.com/gin-gonic/gin v1.10.0
|
github.com/gin-gonic/gin v1.10.0
|
||||||
|
golang.org/x/net v0.34.0
|
||||||
golang.org/x/time v0.10.0
|
golang.org/x/time v0.10.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -31,7 +32,6 @@ require (
|
||||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
golang.org/x/arch v0.14.0 // indirect
|
golang.org/x/arch v0.14.0 // indirect
|
||||||
golang.org/x/crypto v0.32.0 // indirect
|
golang.org/x/crypto v0.32.0 // indirect
|
||||||
golang.org/x/net v0.34.0 // indirect
|
|
||||||
golang.org/x/sys v0.30.0 // indirect
|
golang.org/x/sys v0.30.0 // indirect
|
||||||
golang.org/x/text v0.22.0 // indirect
|
golang.org/x/text v0.22.0 // indirect
|
||||||
google.golang.org/protobuf v1.36.4 // indirect
|
google.golang.org/protobuf v1.36.4 // indirect
|
||||||
|
|
|
||||||
6
main.go
6
main.go
|
|
@ -91,8 +91,8 @@ func setupRateLimit(cfg *config.Config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitReq() {
|
func InitReq(cfg *config.Config) {
|
||||||
proxy.InitReq()
|
proxy.InitReq(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -100,7 +100,7 @@ func init() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
loadConfig()
|
loadConfig()
|
||||||
setupLogger(cfg)
|
setupLogger(cfg)
|
||||||
InitReq()
|
InitReq(cfg)
|
||||||
loadlist(cfg)
|
loadlist(cfg)
|
||||||
setupRateLimit(cfg)
|
setupRateLimit(cfg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,9 @@ var (
|
||||||
BufferPool *sync.Pool
|
BufferPool *sync.Pool
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitReq() {
|
func InitReq(cfg *config.Config) {
|
||||||
initChunkedHTTPClient()
|
initChunkedHTTPClient(cfg)
|
||||||
initGitHTTPClient()
|
initGitHTTPClient(cfg)
|
||||||
|
|
||||||
// 初始化固定大小的缓存池
|
// 初始化固定大小的缓存池
|
||||||
BufferPool = &sync.Pool{
|
BufferPool = &sync.Pool{
|
||||||
|
|
@ -34,7 +34,7 @@ func InitReq() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initChunkedHTTPClient() {
|
func initChunkedHTTPClient(cfg *config.Config) {
|
||||||
ctr = &http.Transport{
|
ctr = &http.Transport{
|
||||||
MaxIdleConns: 100,
|
MaxIdleConns: 100,
|
||||||
MaxConnsPerHost: 60,
|
MaxConnsPerHost: 60,
|
||||||
|
|
@ -47,6 +47,7 @@ func initChunkedHTTPClient() {
|
||||||
KeepAlive: 30 * time.Second,
|
KeepAlive: 30 * time.Second,
|
||||||
}).DialContext,
|
}).DialContext,
|
||||||
}
|
}
|
||||||
|
initTransport(cfg, ctr)
|
||||||
cclient = &http.Client{
|
cclient = &http.Client{
|
||||||
Transport: ctr,
|
Transport: ctr,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
55
proxy/dial.go
Normal file
55
proxy/dial.go
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"ghproxy/config"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/net/proxy"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newProxyDial(prxoyUrls string) proxy.Dialer {
|
||||||
|
var proxyDialer proxy.Dialer = proxy.Direct
|
||||||
|
for _, prxoyUrl := range strings.Split(prxoyUrls, ",") {
|
||||||
|
urlInfo, err := url.Parse(prxoyUrl)
|
||||||
|
if err != nil {
|
||||||
|
return proxyDialer
|
||||||
|
}
|
||||||
|
var auth *proxy.Auth = nil
|
||||||
|
if urlInfo.User != nil {
|
||||||
|
pwd, _ := urlInfo.User.Password()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,12 +17,13 @@ var (
|
||||||
gtr *http.Transport
|
gtr *http.Transport
|
||||||
)
|
)
|
||||||
|
|
||||||
func initGitHTTPClient() {
|
func initGitHTTPClient(cfg *config.Config) {
|
||||||
gtr = &http.Transport{
|
gtr = &http.Transport{
|
||||||
MaxIdleConns: 30,
|
MaxIdleConns: 30,
|
||||||
MaxConnsPerHost: 30,
|
MaxConnsPerHost: 30,
|
||||||
IdleConnTimeout: 30 * time.Second,
|
IdleConnTimeout: 30 * time.Second,
|
||||||
}
|
}
|
||||||
|
initTransport(cfg, gtr)
|
||||||
gclient = &http.Client{
|
gclient = &http.Client{
|
||||||
Transport: gtr,
|
Transport: gtr,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue