Skip to content

Commit b1555a7

Browse files
committed
feat: ✨ add SOCKS5 proxy support and enhance configuration
- Introduced SOCKS5 proxy functionality with separate ports for random rotation and lowest latency. - Updated `.env.example` and `docker-compose.yml` to include SOCKS5 port configurations. - Enhanced `main.go` to initialize and start SOCKS5 servers alongside existing HTTP proxies. - Revised README to document new SOCKS5 features, including usage examples and testing scripts. - Improved proxy selection logic in the storage layer to support SOCKS5 protocol.
1 parent cede441 commit b1555a7

11 files changed

Lines changed: 1052 additions & 93 deletions

File tree

.env.example

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
CONTAINER_NAME=goproxy
33

44
# 端口配置
5-
STABLE_PORT=7776 # 最低延迟代理端口
6-
RANDOM_PORT=7777 # 随机轮换代理端口
7-
WEBUI_PORT=7778 # WebUI 管理端口
5+
STABLE_PORT=7776 # HTTP 最低延迟代理端口
6+
RANDOM_PORT=7777 # HTTP 随机轮换代理端口
7+
WEBUI_PORT=7778 # WebUI 管理端口
8+
SOCKS5_RANDOM_PORT=7779 # SOCKS5 随机轮换代理端口
9+
SOCKS5_STABLE_PORT=7780 # SOCKS5 最低延迟代理端口
810

911
# 地理过滤配置
1012
# 屏蔽指定国家代码的出口代理(逗号分隔,如 CN,RU,KP)

README.md

Lines changed: 385 additions & 62 deletions
Large diffs are not rendered by default.

config/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ type Config struct {
3434
// 稳定代理端口(最低延迟模式)
3535
StableProxyPort string
3636

37+
// SOCKS5 服务端口(随机轮换模式)
38+
SOCKS5Port string
39+
40+
// 稳定 SOCKS5 端口(最低延迟模式)
41+
StableSOCKS5Port string
42+
3743
// 代理服务认证配置
3844
ProxyAuthEnabled bool // 是否启用代理认证(默认 false)
3945
ProxyAuthUsername string // 代理认证用户名(默认 "proxy")
40-
ProxyAuthPasswordHash string // 代理认证密码 SHA256 哈希
46+
ProxyAuthPassword string // 代理认证密码明文(用于 SOCKS5)
47+
ProxyAuthPasswordHash string // 代理认证密码 SHA256 哈希(用于 HTTP)
4148

4249
// 地理过滤配置
4350
BlockedCountries []string // 屏蔽的国家代码列表(如 ["CN", "RU"],默认 ["CN"])
@@ -139,11 +146,14 @@ func DefaultConfig() *Config {
139146
WebUIPasswordHash: passwordHash(password),
140147
ProxyPort: ":7777",
141148
StableProxyPort: ":7776",
149+
SOCKS5Port: ":7779",
150+
StableSOCKS5Port: ":7780",
142151
DBPath: dataDir() + "proxy.db",
143152

144153
// 代理认证配置
145154
ProxyAuthEnabled: proxyAuthEnabled,
146155
ProxyAuthUsername: proxyAuthUsername,
156+
ProxyAuthPassword: proxyAuthPassword,
147157
ProxyAuthPasswordHash: proxyAuthHash,
148158

149159
// 地理过滤配置

docker-compose.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ services:
1010
container_name: ${CONTAINER_NAME:-goproxy}
1111
restart: unless-stopped
1212
ports:
13-
- "${STABLE_PORT:-7776}:7776" # 稳定代理(最低延迟)
14-
- "${RANDOM_PORT:-7777}:7777" # 随机轮换
15-
- "${WEBUI_PORT:-7778}:7778" # WebUI(有登录认证)
13+
- "${STABLE_PORT:-7776}:7776" # HTTP 最低延迟
14+
- "${RANDOM_PORT:-7777}:7777" # HTTP 随机轮换
15+
- "${WEBUI_PORT:-7778}:7778" # WebUI(有登录认证)
16+
- "${SOCKS5_RANDOM_PORT:-7779}:7779" # SOCKS5 随机轮换
17+
- "${SOCKS5_STABLE_PORT:-7780}:7780" # SOCKS5 最低延迟
1618
volumes:
1719
- goproxy-data:/app/data
1820
environment:

fetcher/fetcher.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ func (f *Fetcher) FetchSmart(mode string, preferredProtocol string) ([]storage.P
7171

7272
switch mode {
7373
case "emergency":
74-
// 紧急模式:使用所有可用源
75-
sources = f.filterAvailableSources(allSources, preferredProtocol)
76-
log.Printf("[fetch] 🚨 紧急模式: 使用 %d 个源", len(sources))
74+
// 紧急模式:忽略断路器,强制使用所有源(包括被禁用的)
75+
sources = f.filterAvailableSources(allSources, preferredProtocol, true)
76+
log.Printf("[fetch] 🚨 紧急模式: 使用 %d 个源(忽略断路器)", len(sources))
7777

7878
case "refill":
7979
// 补充模式:使用快更新源
80-
sources = f.filterAvailableSources(fastUpdateSources, preferredProtocol)
80+
sources = f.filterAvailableSources(fastUpdateSources, preferredProtocol, false)
8181
log.Printf("[fetch] 🔄 补充模式: 使用 %d 个快更新源", len(sources))
8282

8383
case "optimize":
@@ -86,7 +86,7 @@ func (f *Fetcher) FetchSmart(mode string, preferredProtocol string) ([]storage.P
8686
log.Printf("[fetch] ⚡ 优化模式: 使用 %d 个源", len(sources))
8787

8888
default:
89-
sources = f.filterAvailableSources(fastUpdateSources, preferredProtocol)
89+
sources = f.filterAvailableSources(fastUpdateSources, preferredProtocol, false)
9090
}
9191

9292
if len(sources) == 0 {
@@ -97,11 +97,12 @@ func (f *Fetcher) FetchSmart(mode string, preferredProtocol string) ([]storage.P
9797
}
9898

9999
// filterAvailableSources 过滤可用的源(通过断路器)
100-
func (f *Fetcher) filterAvailableSources(sources []Source, preferredProtocol string) []Source {
100+
// ignoreCircuitBreaker: 是否忽略断路器(Emergency 模式下使用)
101+
func (f *Fetcher) filterAvailableSources(sources []Source, preferredProtocol string, ignoreCircuitBreaker bool) []Source {
101102
var available []Source
102103
for _, src := range sources {
103-
// 检查断路器
104-
if f.sourceManager != nil && !f.sourceManager.CanUseSource(src.URL) {
104+
// 检查断路器(紧急模式下忽略)
105+
if !ignoreCircuitBreaker && f.sourceManager != nil && !f.sourceManager.CanUseSource(src.URL) {
105106
continue
106107
}
107108
// 如果指定了协议偏好,优先该协议的源
@@ -115,7 +116,7 @@ func (f *Fetcher) filterAvailableSources(sources []Source, preferredProtocol str
115116

116117
// selectRandomSources 随机选择N个源
117118
func (f *Fetcher) selectRandomSources(sources []Source, count int, preferredProtocol string) []Source {
118-
available := f.filterAvailableSources(sources, preferredProtocol)
119+
available := f.filterAvailableSources(sources, preferredProtocol, false)
119120
if len(available) <= count {
120121
return available
121122
}

main.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ func main() {
7070
totalDeleted += int(deleted)
7171
}
7272

73-
// 创建两个代理服务器:随机轮换 + 最低延迟
73+
// 创建 HTTP 代理服务器:随机轮换 + 最低延迟
7474
randomServer := proxy.New(store, cfg, "random", cfg.ProxyPort)
7575
stableServer := proxy.New(store, cfg, "lowest-latency", cfg.StableProxyPort)
76+
77+
// 创建 SOCKS5 代理服务器:随机轮换 + 最低延迟
78+
socks5RandomServer := proxy.NewSOCKS5(store, cfg, "random", cfg.SOCKS5Port)
79+
socks5StableServer := proxy.NewSOCKS5(store, cfg, "lowest-latency", cfg.StableSOCKS5Port)
7680

7781
// 配置变更通知 channel
7882
configChanged := make(chan struct{}, 1)
@@ -105,16 +109,30 @@ func main() {
105109
// 监听配置变更
106110
go watchConfigChanges(configChanged, poolMgr)
107111

108-
// 启动稳定代理服务(最低延迟模式)
112+
// 启动 HTTP 稳定代理服务(最低延迟模式)
109113
go func() {
110114
if err := stableServer.Start(); err != nil {
111-
log.Fatalf("stable proxy server: %v", err)
115+
log.Fatalf("stable http proxy server: %v", err)
116+
}
117+
}()
118+
119+
// 启动 SOCKS5 稳定代理服务(最低延迟模式)
120+
go func() {
121+
if err := socks5StableServer.Start(); err != nil {
122+
log.Fatalf("stable socks5 proxy server: %v", err)
123+
}
124+
}()
125+
126+
// 启动 SOCKS5 随机代理服务
127+
go func() {
128+
if err := socks5RandomServer.Start(); err != nil {
129+
log.Fatalf("random socks5 proxy server: %v", err)
112130
}
113131
}()
114132

115-
// 启动随机代理服务(阻塞)
133+
// 启动 HTTP 随机代理服务(阻塞)
116134
if err := randomServer.Start(); err != nil {
117-
log.Fatalf("random proxy server: %v", err)
135+
log.Fatalf("random http proxy server: %v", err)
118136
}
119137
}
120138

0 commit comments

Comments
 (0)