一个简单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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package main

import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"net"
"os"
"strings"
"sync"
"time"
"crypto/tls"

"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v3"
)

// Config 是YAML配置文件的结构体
type Config struct {
Server struct {
Port int `yaml:"port"`
} `yaml:"server"`
Log struct {
Enabled bool `yaml:"enabled"`
File string `yaml:"file"`
MaxSizeMB int `yaml:"maxsize"`
MaxBackups int `yaml:"maxbackups"`
MaxAgeDays int `yaml:"maxage"`
Compress bool `yaml:"compress"`
} `yaml:"log"`
Reload int `yaml:"reload"`
}

var (
port = flag.Int("port", 8888, "监听端口号")
logEnabled = flag.Bool("log", true, "是否启用日志记录")
logFile = flag.String("logfile", "", "日志文件路径")
logMaxSizeMB = flag.Int("log.maxsize", 10, "日志文件大小限制(MB)")
logMaxBackups = flag.Int("log.maxbackups", 3, "保留的最大备份文件数")
logMaxAgeDays = flag.Int("log.maxage", 28, "保留日志文件的最大天数")
logCompress = flag.Bool("log.compress", true, "是否压缩日志文件")
configFilePath = flag.String("config", "", "YAML配置文件路径")
Reload = flag.Int("reload", 30, "配置文件重新加载间隔(秒")
serverCtx, cancel = context.WithCancel(context.Background())
logConfigMutex sync.Mutex
cfg Config
)
var ErrBrokenPipe = errors.New("broken pipe")

var bufferPool = sync.Pool{
New: func() interface{} {
return make([]byte, defaultBufferSize) // 默认缓冲区大小
},
}
const defaultBufferSize = 128 * 1024 // 默认缓冲区大小
func main() {
flag.Parse()

if *configFilePath != "" {
err := loadConfig(*configFilePath)
if err != nil {
log.Fatalf("读取YAML配置文件失败: %v", err)
}
} else {
cfg.Server.Port = *port
cfg.Log.Enabled = *logEnabled
cfg.Log.File = *logFile
cfg.Log.MaxSizeMB = *logMaxSizeMB
cfg.Log.MaxBackups = *logMaxBackups
cfg.Log.MaxAgeDays = *logMaxAgeDays
cfg.Log.Compress = *logCompress
cfg.Reload = *Reload
}

setupLogger()
// 启动配置文件自动加载
go watchConfigFile(*configFilePath)

http.HandleFunc("/", handler)

go func() {
if err := startHTTPServer(serverCtx); err != nil {
log.Fatalf("启动HTTP服务器失败: %v", err)
}
}()

<-serverCtx.Done()
}

func loadConfig(configPath string) error {

yamlData, err := os.ReadFile(configPath)
if err != nil {
return err
}

var newCfg Config
err = yaml.Unmarshal(yamlData, &newCfg)
if err != nil {
return err
}

logConfigMutex.Lock()
cfg = newCfg
logConfigMutex.Unlock()

// 重新设置日志记录器
setupLogger()

return nil
}

func watchConfigFile(configPath string) {
if configPath == "" {
return
}

ticker := time.NewTicker(time.Duration(cfg.Reload) * time.Second) // 定时器间隔可以根据需要调整
defer ticker.Stop()

lastModifiedTime := time.Now()

for range ticker.C {
fileInfo, err := os.Stat(configPath)
if err != nil {
log.Printf("无法获取配置文件信息: %v", err)
continue
}

if fileInfo.ModTime().After(lastModifiedTime) {
lastModifiedTime = fileInfo.ModTime()
log.Println("检测到配置文件修改,重新加载配置...")
err := loadConfig(configPath)
if err != nil {
log.Printf("重新加载配置文件失败: %v", err)
} else {
log.Println("配置文件重新加载完成")

// 重新设置 HTTP 服务器的监听端口
serverCtx, cancel = context.WithCancel(context.Background())
go func() {
if err := startHTTPServer(serverCtx); err != nil {
log.Fatalf("启动HTTP服务器失败: %v", err)
}
}()
}
}
}
}

func setupLogger() {
logConfigMutex.Lock()
defer logConfigMutex.Unlock()

if !cfg.Log.Enabled {
// 如果日志记录被禁用,则将日志输出设置为丢弃
log.SetOutput(io.Discard)
log.Println("日志记录已禁用")
return
}

if cfg.Log.File == "" {
// 如果日志文件路径为空,则默认将日志输出到标准输出
log.SetOutput(os.Stdout)
log.Println("日志记录已启用: 输出到标准输出")
return
}

// 否则,根据配置设置日志记录到指定文件
logger := &lumberjack.Logger{
Filename: cfg.Log.File,
MaxSize: cfg.Log.MaxSizeMB,
MaxBackups: cfg.Log.MaxBackups,
MaxAge: cfg.Log.MaxAgeDays,
Compress: cfg.Log.Compress,
}
log.SetOutput(logger)
log.Printf("日志记录已启用: 输出到文件 %s", cfg.Log.File)
}

func startHTTPServer(ctx context.Context) error {
logConfigMutex.Lock()
addr := fmt.Sprintf(":%d", cfg.Server.Port)
logConfigMutex.Unlock()

fmt.Printf("监听端口: %d...\n", cfg.Server.Port)

srv := &http.Server{Addr: addr}

go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("HTTP服务器启动失败: %v", err)
}
}()

<-ctx.Done()

shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()

if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("HTTP服务器关闭失败: %v", err)
}
return nil
}

func handler(w http.ResponseWriter, r *http.Request) {
// 设置连接关闭标头,告知客户端和代理服务器都关闭连接
r.Header.Set("Connection", "close")
w.Header().Set("Connection", "close")

// 保留原始请求的路径,获取目标路径
targetPath := getTargetPath(r)
// 根据目标路径获取目标 URL
targetURL := getTargetURL(r, targetPath)

// 创建代理请求
proxyReq, err := createProxyRequest(r, targetURL)
if err != nil {
// 请求创建失败,返回 500 错误
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// 发送代理请求
proxyResp, err := sendProxyRequest(proxyReq, r)
if err != nil {
// 发送请求失败,返回 502 错误
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
// 确保代理响应体在函数结束时关闭
defer proxyResp.Body.Close()

// 如果响应是 301(永久移动)或 302(临时移动)重定向
if proxyResp.StatusCode == http.StatusMovedPermanently || proxyResp.StatusCode == http.StatusFound {
// 获取原始请求的协议(Scheme),如果没有则使用默认的 http 协议
scheme := r.URL.Scheme
if scheme == "" {
if r.TLS != nil {
scheme = "https"
} else {
scheme = "http"
}
}
// 获取原始请求的主机(Host),如果没有则使用 r.Host
host := r.URL.Host
if host == "" {
host = r.Host
}
// 构造基础 URL,包含协议和主机
baseURL := fmt.Sprintf("%s://%s", scheme, host)

// 获取 Location 标头(重定向地址)
location := proxyResp.Header.Get("Location")
if location != "" {
// 如果 Location 不完整,拼接基础 URL 和 Location
location = baseURL + "/" + location

// 输出重定向的目标 URL
log.Printf("正在重定向到: %s", location)
// 设置响应的 Location 标头,进行重定向
w.Header().Set("Location", location)
}

// 设置响应状态码并返回
w.WriteHeader(proxyResp.StatusCode)
return
}

// 记录请求和响应信息(可选,日志记录)
logRequestAndResponse(r, targetURL, proxyResp)
// 将代理响应复制到客户端
ctx, cancel := context.WithCancel(context.Background()) // 没有超时限制
defer cancel()

copyResponseToClientWithContext(ctx, w, proxyResp)

}

var client = &http.Client{
Timeout: 0, // 整个请求超时
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 10 * time.Second, // 连接超时
KeepAlive: 30 * time.Second, // 长连接保持时间
DualStack: true,
}).DialContext,
ResponseHeaderTimeout: 10 * time.Second, // 接收响应头超时
TLSClientConfig: &tls.Config{}, // 移除 InsecureSkipVerify,除非绝对必要
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
MaxConnsPerHost: 100, // 可选:限制每个主机的最大连接数
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
const maxRedirects = 10 // 最大重定向次数
redirectCount := len(via)

// 超过最大重定向次数,直接返回错误
if redirectCount >= maxRedirects {
return fmt.Errorf("超出最大重定向次数 (%d 次)", maxRedirects)
}

// 获取上一个请求的 URL
previousURL := via[redirectCount-1].URL

// 根据响应头解析目标重定向 URL
redirectURL, err := url.Parse(req.Response.Header.Get("Location"))
if err != nil {
return fmt.Errorf("无效的重定向 URL: %w", err)
}

// 计算目标 URL,支持相对路径
targetURL := previousURL.ResolveReference(redirectURL)
req.URL = targetURL

log.Printf("从 %s 重定向到 %s", previousURL, targetURL)

// 根据需要选择是否跟随重定向
return http.ErrUseLastResponse // 不跟随重定向
},
}
func sendProxyRequest(proxyReq *http.Request, r *http.Request) (*http.Response, error) {
// 执行代理请求
resp, err := client.Do(proxyReq)
if err != nil {
log.Printf("发送请求失败: %v", err)
return nil, err
}

return resp, nil
}

func getTargetPath(r *http.Request) string {
targetPath := r.URL.Path[1:]
query := r.URL.RawQuery
if query != "" {
targetPath += "?" + query
}
return targetPath
}

func getTargetURL(r *http.Request, targetPath string) string {
if strings.HasPrefix(targetPath, "http:/") {
targetPath = strings.TrimPrefix(targetPath, "http:/")
} else if strings.HasPrefix(targetPath, "https:/") {
targetPath = strings.TrimPrefix(targetPath, "https:/")
}
useHTTPS := strings.HasPrefix(r.URL.Path, "/https:/")

var targetURL string
if useHTTPS {
targetURL = "https://" + r.URL.Host + targetPath
} else {
targetURL = "http://" + r.URL.Host + targetPath
}
return targetURL
}

func createProxyRequest(r *http.Request, targetURL string) (*http.Request, error) {
// 创建新的请求,将原始请求的方法和URL传入
proxyReq, err := http.NewRequest(r.Method, targetURL, nil)
if err != nil {
return nil, err
}

// 处理请求体
if r.Body != nil {
bodyCopy, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
r.Body = io.NopCloser(bytes.NewBuffer(bodyCopy)) // 还原原始请求的 Body
proxyReq.Body = io.NopCloser(bytes.NewBuffer(bodyCopy)) // 设置代理请求的 Body
}

// 复制头部信息
proxyReq.Header = make(http.Header)
copyHeader(proxyReq.Header, r.Header)

// 添加 X-Forwarded-For 头部,包含客户端的 IP
clientIP := strings.Split(r.RemoteAddr, ":")[0]
if prior, ok := proxyReq.Header["X-Forwarded-For"]; ok {
clientIP = prior[0] + ", " + clientIP
}
proxyReq.Header.Set("X-Forwarded-For", clientIP)

return proxyReq, nil
}


func logRequestAndResponse(r *http.Request, targetURL string, proxyResp *http.Response) {
remoteAddr := r.RemoteAddr
forwardedFor := r.Header.Get("X-Forwarded-For")
userAgent := r.Header.Get("User-Agent")

if forwardedFor != "" {
log.Printf("[X-Forwarded-For: %s] [%s] %s %s %s %d [User-Agent: %s]",
forwardedFor, remoteAddr, r.Method, targetURL, r.Proto, proxyResp.StatusCode, userAgent)
} else {
log.Printf("[%s] %s %s %s %d [User-Agent: %s]",
remoteAddr, r.Method, targetURL, r.Proto, proxyResp.StatusCode, userAgent)
}
}

func copyResponseToClientWithContext(ctx context.Context, w http.ResponseWriter, proxyResp *http.Response) {
defer func() {
if err := proxyResp.Body.Close(); err != nil {
log.Printf("关闭响应体错误: %v", err)
}
}()

// 复制响应头
copyHeader(w.Header(), proxyResp.Header)
w.WriteHeader(proxyResp.StatusCode)

buf := bufferPool.Get().([]byte)
defer bufferPool.Put(buf) // 将缓冲区放回池中

done := make(chan struct{}) // 通知完成的通道
go func() {
defer close(done)
if err := copyWithContext(ctx, w, proxyResp.Body, buf); err != nil {
handleCopyError(w, err, proxyResp)
}
}()

select {
case <-ctx.Done(): // 客户端断开连接
log.Println("客户端断开连接")
case <-done:
log.Println("响应成功完成")
}
}


func copyWithContext(ctx context.Context, dst io.Writer, src io.Reader, buf []byte) error {
for {
select {
case <-ctx.Done():
return ctx.Err() // 客户端取消或断开连接
default:
// 从源读取数据
n, readErr := src.Read(buf)
if n > 0 {
written := 0
for written < n {
wn, writeErr := dst.Write(buf[written:n])
if writeErr != nil {
return fmt.Errorf("写入错误: %w", writeErr)
}
written += wn
}
}

if readErr != nil {
if errors.Is(readErr, io.EOF) {
return nil // 流结束(对于 FLV 通常不会发生)
}
return fmt.Errorf("读取错误: %w", readErr)
}
}
}
}


func handleCopyError(w http.ResponseWriter, err error, proxyResp *http.Response) {
if errors.Is(err, context.Canceled) {
log.Printf("传输被取消: %v, URL: %s", err, proxyResp.Request.URL.String())
} else if errors.Is(err, context.DeadlineExceeded) {
log.Printf("传输超时: %v, URL: %s", err, proxyResp.Request.URL.String())
} else {
log.Printf("传输错误: %v, URL: %s", err, proxyResp.Request.URL.String())
}

// 可选:向客户端返回错误
if w.Header().Get("Content-Length") == "" {
http.Error(w, "服务器错误", http.StatusInternalServerError)
}
}

// copyHeader 负责复制请求头
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server:
#监听端口
port: 8888

# 日志输出配置
log:
# 是否输出日志
enabled: true
# 日志输出文件地址
file:
# 日志大小M单位
maxsize: 10
# 压缩文件备份个数
maxbackups: 3
# 日志保留天数
maxage: 28
# 是否压缩
compress: true

# 配置文件重新加载时间秒
reload: 30

编译

1
2
3
4
go mod init proxy
go mod tidy
go build -o proxy 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
# 启动服务
/usr/lib/systemd/system/proxy.service
[Unit]
Description=as - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
LimitCORE=infinity
LimitNOFILE=100000
LimitNPROC=100000
ExecStart=/usr/local/bin/proxy -config=/app/config.yaml
PrivateTmp=true

[Install]
WantedBy=multi-user.target

# 查看配置参数CMD的
/usr/local/bin/proxy -help
# 设置开机启动
systemctl enable proxy
# 启动
systemctl start proxy
# 代理 m3u8
http://代理ip:代理端口/mgsp-tx.live.miguvideo.com/wd_r2/cctv/cctv1hd/1200/01.m3u8
http://代理ip:代理端口/1.v.smtcdns.net/qctv.fengshows.cn/live/0701pcc72.flv
# https 打开记得携带上
http://代理ip:代理端口/https://1.v.smtcdns.net/qctv.fengshows.cn/live/0701pcc72.flv
# git 下载
wget http://代理ip:代理端口/https://github.com/kubernetes/kubernetes/archive/refs/tags/v1.30.0.zip
git clone http://代理ip:代理端口/https://github.com/kubernetes/kubernetes.git
同时还支持别站点下载还原使用