galang-rs/socks5

GitHub: galang-rs/socks5

纯Go实现的模块化SOCKS5代理服务器,内置WireGuard和OpenVPN可插拔后端,采用自研轻量级用户态TCP/IP协议栈。

Stars: 0 | Forks: 0

Go Version License MIT Platform Status

🧦 SOCKS5 Proxy Go

一个模块化、纯 Go 语言实现的 SOCKS5 代理服务器,支持多隧道 VPN 后端。
无 CGo。无系统驱动。可通过 WireGuard、OpenVPN 或直接连接路由流量。

完整实现 RFC 1928 (CONNECT + UDP ASSOCIATE) 和 RFC 1929 (用户名/密码认证) —— 支持可插拔 VPN 后端、多凭据认证、分级日志记录,以及用于隧道连接的自定义轻量级用户态 TCP/IP 协议栈。无 gVisor 依赖。

## ✨ 特性 ### 🛡️ 完整 SOCKS5 协议 - **RFC 1928 合规** — 完整的 SOCKS5 CONNECT 和 UDP ASSOCIATE 实现 - **RFC 1929 认证** — 支持用户名/密码认证 - **方法协商** — 标准的 SOCKS5 认证方法协商握手 - **UDP 中继** — 完整的 UDP ASSOCIATE,支持通过 VPN 隧道进行数据报封装/解封装 - **错误处理** — 针对所有错误情况的标准 SOCKS5 响应代码 - **优雅关闭** — 支持上下文感知的服务器,具备干净的连接拆除和 goroutine 跟踪 ### 🔐 多凭据认证 - **单用户多密码** — 同一用户名可使用不同密码进行认证 - **可变参数凭据设置** — 灵活的 `auth.Credential()` 函数式构建器 - **并发安全** — 线程安全的凭据验证,适用于高吞吐量代理 ### 🌐 可插拔 VPN 后端 - **Direct Backend** — 无 VPN,直接的 `net.Dialer` 连接 - **WireGuard Backend** — 通过 WireGuard 隧道路由所有代理流量 - **OpenVPN Backend** — 通过 OpenVPN 隧道路由所有代理流量 - **Backend 接口** — 干净的 `backend.Backend` 接口,支持自定义实现 - **热插拔** — 无需修改代理服务器代码即可切换后端 ### 🔧 自定义虚拟 NetStack - **轻量级 TCP/IP 协议栈** — 用于 VPN 隧道 I/O 的用户态网络栈 - **DNS 解析** — 通过隧道以 UDP 数据包形式发送 DNS 查询 - **TCP 握手** — 通过 TUN 设备的原始 IP 数据包完成完整 3 次握手 - **虚拟 UDP** — 通过隧道路由的已连接 UDP,带有背压控制 - **数据中继** — 通过隧道进行 TCP 分段和重组 - **连接拆除** — 正确的 FIN/ACK 序列以实现干净断开 - **内存管理** — 关闭时自动回收端口、清理缓冲区和排空 channel - **零 gVisor 依赖** — 自定义实现,无厚重的外部协议栈 ### 📊 分级日志 - **5 个日志级别** — `Disabled`、`Error`、`Warn`、`Info`、`Debug` - **生产就绪** — 默认 `Warn` 级别,静默冗长的数据包跟踪 - **调试模式** — `LogLevelDebug` 启用完整的逐包 UDP 中继跟踪 - **自定义日志器** — 实现 `Logger` 接口以对接自定义日志后端 - **分级前缀** — `[SOCKS5]`、`[SOCKS5:WARN]`、`[SOCKS5:ERR]`、`[SOCKS5:DBG]` ### ⚙️ 架构 - **函数式选项模式** — `socks5.WithAddr()`、`socks5.WithAuth()`、`socks5.WithBackend()`、`socks5.WithLogLevel()` - **模块化包设计** — 分离的 `auth`、`backend`、`netstack`、`socks5` 包 - **并发连接处理** — 每个 SOCKS5 连接在其独立的 goroutine 中处理 - **上下文感知** — 完整支持 `context.Context` 用于取消和超时 ## 📦 安装 ``` go get github.com/galang-rs/socks5 ``` ## 🚀 使用方法 ### 完整示例 ``` package main import ( "context" "log" "os" "os/signal" "syscall" "github.com/galang-rs/socks5/auth" "github.com/galang-rs/socks5/backend" "github.com/galang-rs/socks5/socks5" ) func main() { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() // Multi-auth: same user can have multiple passwords authenticator := auth.New( auth.Credential("user1", "pass1"), auth.Credential("user1", "pass2"), auth.Credential("admin", "secret"), ) // Start WireGuard backend be, err := backend.NewWireGuard(ctx, backend.WireGuardConfig{ ConfigFile: "wg0.conf", }) if err != nil { log.Fatal(err) } defer be.Close() // Start SOCKS5 proxy // LogLevelWarn = only warnings and errors (production) // LogLevelInfo = normal operational logs // LogLevelDebug = verbose packet-level tracing srv := socks5.New( socks5.WithAddr(":1080"), socks5.WithAuth(authenticator), socks5.WithBackend(be), socks5.WithLogLevel(socks5.LogLevelWarn), ) log.Println("SOCKS5 proxy starting on :1080") if err := srv.ListenAndServe(ctx); err != nil { log.Fatal(err) } } ``` ### Direct Backend (无 VPN) ``` be := backend.NewDirect() srv := socks5.New( socks5.WithAddr(":1080"), socks5.WithAuth(authenticator), socks5.WithBackend(be), ) ``` ### WireGuard 后端 ``` be, err := backend.NewWireGuard(ctx, backend.WireGuardConfig{ ConfigFile: "wg0.conf", DNSServers: []string{"1.1.1.1", "8.8.8.8"}, // optional }) if err != nil { log.Fatal(err) } defer be.Close() ``` ### OpenVPN 后端 ``` be, err := backend.NewOpenVPN(ctx, backend.OpenVPNConfig{ ConfigFile: "config.ovpn", AuthFile: "auth.txt", // optional DNSServers: []string{"1.1.1.1"}, // optional }) if err != nil { log.Fatal(err) } defer be.Close() ``` ### 日志级别控制 ``` // Production — silent except warnings and errors (default) socks5.WithLogLevel(socks5.LogLevelWarn) // Normal operation — connection logs socks5.WithLogLevel(socks5.LogLevelInfo) // Troubleshooting — full packet-level UDP relay traces socks5.WithLogLevel(socks5.LogLevelDebug) // Completely silent — no output at all socks5.WithLogLevel(socks5.LogLevelDisabled) ``` ### 自定义日志器 ``` // Implement the socks5.Logger interface for custom backends type myLogger struct{} func (myLogger) Debugf(format string, args ...any) { /* ... */ } func (myLogger) Infof(format string, args ...any) { /* ... */ } func (myLogger) Warnf(format string, args ...any) { /* ... */ } func (myLogger) Errorf(format string, args ...any) { /* ... */ } srv := socks5.New( socks5.WithLogger(myLogger{}), ) ``` ### 运行测试 ``` # 构建 go build ./... # 使用 curl 测试(direct backend) curl --socks5 user1:pass1@localhost:1080 https://ifconfig.me # 使用 curl 测试(VPN backend — 应显示 VPN 出口 IP) curl --socks5 admin:secret@localhost:1080 https://ifconfig.me ``` ## 🏗️ 项目结构 ``` socks5/ ├── auth/ │ └── auth.go # Multi-credential SOCKS5 authenticator ├── backend/ │ ├── backend.go # Backend interface definition │ ├── direct.go # Direct (no VPN) backend │ ├── wireguard.go # WireGuard tunnel backend │ └── openvpn.go # OpenVPN tunnel backend ├── netstack/ │ ├── stack.go # Virtual TCP/IP stack controller + Logger interface │ ├── conn.go # TCP net.Conn implementation over netstack │ ├── virtual_udp.go # UDP net.Conn implementation over netstack │ ├── dns.go # DNS resolver via UDP over tunnel │ ├── ip.go # IPv4 packet builder & parser │ ├── tcp.go # TCP segment builder & parser │ └── udp.go # UDP datagram builder & parser ├── socks5/ │ └── server.go # SOCKS5 proxy server (RFC 1928/1929) + LogLevel system ├── go.mod └── go.sum ``` ## 🏛️ 架构 ``` SOCKS5 Client │ ▼ ┌─────────────┐ │ auth.Multi │ ← Multi-credential username/password authentication └──────┬──────┘ │ ▼ ┌──────────────┐ │ socks5.Server│ ← RFC 1928 CONNECT handler └──────┬───────┘ │ ▼ ┌──────────────────┐ │ backend.Backend │ ← Pluggable backend interface └──────┬───────────┘ │ ├─────────────────┬─────────────────┐ ▼ ▼ ▼ ┌────────────┐ ┌──────────────┐ ┌─────────────┐ │ Direct │ │ WireGuard │ │ OpenVPN │ │ net.Dialer │ │ TUN+netstack │ │ TUN+netstack│ └────────────┘ └──────────────┘ └─────────────┘ ``` `netstack` 包实现了一个轻量级虚拟 TCP/IP 协议栈,将 SOCKS5 TCP 连接桥接到 TUN 设备的原始 IP 数据包: 1. **DNS 解析** — 通过隧道发送 UDP 形式的 DNS 查询 2. **TCP 握手** — 通过原始 IP 数据包进行 3 次握手 3. **数据中继** — 通过 TUN 将 TCP 数据分段为 IP 数据包 4. **UDP 中继** — 用于数据报转发的虚拟已连接 UDP 5. **连接拆除** — 正确的 FIN/ACK 序列 6. **资源清理** — 自动端口回收、缓冲区重置和 channel 排空 ## 🔧 依赖 | Package | Purpose | |---------|---------| | `github.com/galang-rs/wireguard` | WireGuard VPN 隧道实现 | | `github.com/galang-rs/ovpn` | OpenVPN 隧道实现 | | `golang.org/x/crypto` | 加密原语 (间接) | ## 📄 许可证 ``` MIT License Copyright (c) 2026 Galang Reisduanto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --- ADDITIONAL TERMS: 1. Attribution — If you use this software in a product, an acknowledgment in the product documentation or "About" section would be appreciated but is not required. 2. Non-Endorsement — The name "galang-rs" or "Galang Reisduanto" may not be used to endorse or promote products derived from this software without specific prior written permission. 3. Good Faith — This software is shared in good faith for the benefit of the open-source community. Commercial use is permitted and encouraged. ``` ## 📬 功能请求与联系 有任何想法、Bug 报告或自定义功能请求?欢迎随时联系!

Email

📧 Email: galangreisduanto@gmail.com

标签:EVTX分析, Go语言, IP 地址批量处理, OpenVPN, RFC1928, SOCKS5代理, TCP/IP, UDP转发, VPN后端, WireGuard, 中间人通信, 云安全, 命令与控制, 日志审计, 模块化架构, 流量路由, 用户态协议栈, 程序破解, 网络安全, 隐私保护, 隧道技术, 零依赖