-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
232 lines (197 loc) · 5.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"bufio"
"flag"
"fmt"
"github.com/b3nguang/ProxyCat-Go/pkg/logger"
"github.com/gin-gonic/gin"
"golang.org/x/net/proxy"
"io"
"net"
"net/http"
"net/url"
"os"
"sync"
"time"
)
var (
proxies []string
proxyIndex int
rotateMode string
rotateInterval time.Duration
mu sync.Mutex
)
func init() {
logger.InitLogger("INFO")
}
// LoadProxies 从指定路径加载代理列表
// filePath: 代理文件路径
func loadProxies(filePath string) []string {
file, err := os.Open(filePath)
if err != nil {
logger.Fatal("🏳 Error loading proxy file:", err)
}
defer file.Close()
var proxies []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
proxies = append(proxies, scanner.Text())
}
if err := scanner.Err(); err != nil {
logger.Fatal("🏳 Error reading proxy file:", err)
}
return proxies
}
// RotateProxies 根据设定的时间间隔和模式轮换代理
// interval: 代理轮换的时间间隔
func rotateProxies(interval time.Duration) {
for {
time.Sleep(interval)
mu.Lock()
if rotateMode == "cycle" {
proxyIndex = (proxyIndex + 1) % len(proxies)
} else if rotateMode == "once" && proxyIndex < len(proxies)-1 {
proxyIndex++
}
logger.Info("🔀 Switched to proxy:", proxies[proxyIndex])
mu.Unlock()
}
}
// GetCurrentProxy 获取当前使用的代理
func getCurrentProxy() string {
mu.Lock()
defer mu.Unlock()
return proxies[proxyIndex]
}
// BuildCompleteURL 构建完整的请求URL
func buildCompleteURL(r *http.Request) string {
if r.URL.IsAbs() {
return r.URL.String()
}
return fmt.Sprintf("%s://%s%s", "http", r.Host, r.URL.RequestURI())
}
// ProxyMiddleware 处理HTTP代理请求的中间件
func proxyMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
currentProxy := getCurrentProxy()
proxyURL, err := url.Parse(currentProxy)
if err != nil {
c.String(http.StatusInternalServerError, "🏳 Invalid proxy URL")
logger.Error("🏳 Invalid proxy URL:", err)
c.Abort()
return
}
logger.Info("🙋 Handling request:", c.Request.Method, c.Request.URL.String(), "via proxy:", currentProxy)
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
client := &http.Client{Transport: transport}
completeURL := buildCompleteURL(c.Request)
req, err := http.NewRequest(c.Request.Method, completeURL, c.Request.Body)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
logger.Error("🏳 Failed to create new request:", err)
c.Abort()
return
}
for name, values := range c.Request.Header {
for _, value := range values {
req.Header.Add(name, value)
}
}
resp, err := client.Do(req)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
logger.Error("🏳 Request failed:", err)
c.Abort()
return
}
defer resp.Body.Close()
for key, values := range resp.Header {
for _, value := range values {
c.Header(key, value)
}
}
c.Status(resp.StatusCode)
_, err = io.Copy(c.Writer, resp.Body)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
logger.Error("🏳 Failed to copy response body:", err)
c.Abort()
return
}
logger.Info("📡 Response:", c.Request.URL.String(), resp.StatusCode)
}
}
// ConnectHandler 处理CONNECT请求
func connectHandler(c *gin.Context) {
currentProxy := getCurrentProxy()
proxyURL, err := url.Parse(currentProxy)
if err != nil {
c.String(http.StatusInternalServerError, "Invalid proxy URL")
logger.Error("🏳 Invalid proxy URL:", err)
return
}
logger.Info("Handling CONNECT request:", c.Request.Host, "via proxy:", currentProxy)
dialer, err := proxy.FromURL(proxyURL, proxy.Direct)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
logger.Error("🏳 Failed to create dialer:", err)
return
}
destConn, err := dialer.Dial("tcp", c.Request.Host)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
logger.Error("🏳 Failed to dial destination:", err)
return
}
defer destConn.Close()
hijacker, ok := c.Writer.(http.Hijacker)
if !ok {
c.String(http.StatusInternalServerError, "Webserver doesn't support hijacking")
logger.Error("🏳 Webserver doesn't support hijacking")
return
}
clientConn, _, err := hijacker.Hijack()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
logger.Error("🏳 Hijacking failed:", err)
return
}
defer clientConn.Close()
c.Status(http.StatusOK)
go transfer(destConn, clientConn)
go transfer(clientConn, destConn)
logger.Info("🎉 CONNECT established for:", c.Request.Host)
}
// Transfer 数据传输
func transfer(destination net.Conn, source net.Conn) {
defer destination.Close()
defer source.Close()
_, err := io.Copy(destination, source)
if err != nil {
logger.Error("🏳 Error during data transfer:", err)
}
}
func main() {
port := flag.Int("p", 1080, "Listening port")
mode := flag.String("m", "cycle", "Proxy rotation mode: cycle or once")
interval := flag.Int("t", 60, "Proxy rotation interval (seconds)")
flag.Parse()
proxies = loadProxies("ip.txt")
if len(proxies) == 0 {
logger.Fatal("No proxies found")
}
rotateMode = *mode
rotateInterval = time.Duration(*interval) * time.Second
go rotateProxies(rotateInterval)
// 创建 Gin Engine 并禁用 Gin 的默认日志中间件
gin.SetMode(gin.ReleaseMode)
r := gin.New()
// 添加自定义的日志中间件
r.Use(proxyMiddleware())
r.Any("/connect", connectHandler)
logger.Info("🚀 Listening on port:", *port, "Proxy rotation mode:", *mode, "Proxy rotation interval:", *interval, "seconds")
logger.Info("🤝 Initial proxy:", getCurrentProxy())
r.Run(fmt.Sprintf(":%d", *port))
}