Skip to content

Commit c19a4ca

Browse files
committed
内存优化
1 parent e48b432 commit c19a4ca

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

forward/forward.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type LargeConnectionStats struct {
3131
// 复用缓冲区
3232
var bufPool = sync.Pool{
3333
New: func() interface{} {
34-
return make([]byte, 8192)
34+
return make([]byte, 4096)
3535
},
3636
}
3737

@@ -183,8 +183,10 @@ func (cs *ConnectionStats) handleUDPConnection(localConn *net.UDPConn, remoteAdd
183183
cs.TotalBytesLock.Unlock()
184184

185185
// 处理消息的边界和错误情况
186-
go cs.forwardUDPMessage(localConn, remoteAddr, buf[:n])
187-
bufPool.Put(buf[:n])
186+
go func() {
187+
cs.forwardUDPMessage(localConn, remoteAddr, buf[:n])
188+
bufPool.Put(&buf)
189+
}()
188190
}
189191
}
190192
}
@@ -199,11 +201,12 @@ func (cs *ConnectionStats) forwardUDPMessage(localConn *net.UDPConn, remoteAddr
199201
if err != nil {
200202
fmt.Println("写入目标时发生错误:", err)
201203
}
204+
202205
}
203206

204207
func (cs *ConnectionStats) copyBytes(dst, src net.Conn) {
205208
buf := bufPool.Get().([]byte)
206-
defer bufPool.Put(buf)
209+
defer bufPool.Put(&buf)
207210
for {
208211
n, err := src.Read(buf)
209212
if n > 0 {
@@ -231,7 +234,7 @@ func (cs *ConnectionStats) copyBytes(dst, src net.Conn) {
231234

232235
// 定时打印和处理流量变化
233236
func (cs *ConnectionStats) printStats(ctx context.Context) {
234-
ticker := time.NewTicker(5 * time.Second)
237+
ticker := time.NewTicker(10 * time.Second)
235238
defer ticker.Stop() // 在函数结束时停止定时器
236239
for {
237240
select {
@@ -286,8 +289,9 @@ func (cs *ConnectionStats) printStats(ctx context.Context) {
286289
func closeTCPConnections(stats *ConnectionStats) {
287290
stats.TotalBytesLock.Lock()
288291
defer stats.TotalBytesLock.Unlock()
289-
for _, conn := range stats.TCPConnections {
292+
for i, conn := range stats.TCPConnections {
290293
conn.Close()
294+
stats.TCPConnections[i] = nil
291295
}
292296
stats.TCPConnections = nil // 清空切片
293297
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ func init() {
6363
flag.StringVar(&conf.WebPort, "port", "8889", "Web Port")
6464
flag.StringVar(&conf.WebIP, "ip", "0.0.0.0", "Web IP")
6565
flag.StringVar(&conf.WebPass, "pass", "", "Web Password")
66-
flag.IntVar(&conf.TcpTimeout, "tt", 3600, "Tcp Timeout")
66+
flag.IntVar(&conf.TcpTimeout, "tt", 60, "Tcp Timeout")
6767
flag.Parse()
6868
}

readme.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,59 @@
1-
使用golang实现的tcp udp端口转发
1+
使用 golang 实现的 tcp udp 端口转发
22

33
目前已实现:
44

5-
- 规则热加载
6-
- web管理面板
7-
- 流量统计
5+
- 规则热加载
6+
- web 管理面板
7+
- 流量统计
88

9-
支持:Linux、Windows、MacOS(MacOS需要自行编译
9+
支持:Linux、Windows、MacOS(MacOS 需要自行编译
1010

1111
**截图**
1212

1313
![image](https://github.com/csznet/goForward/assets/127601663/2f7840ff-9b34-4f69-a7c1-41feb35e726b)
1414

15-
1615
**使用**
1716

18-
Linux下载
17+
Linux 下载
18+
1919
```
2020
sudo bash -c "$(curl -fsSL https://raw.githubusercontent.com/csznet/goForward/main/get.sh)"
2121
```
22+
2223
运行
24+
2325
```
2426
./goForward
2527
```
2628

27-
**参数**
29+
**参数**
30+
31+
TCP 无传输超时关闭
32+
默认 60,单位秒
2833

29-
TCP无传输超时关闭
30-
默认3600,单位秒
3134
```
3235
./goForward -tt 18000
3336
```
3437

35-
36-
自定义web管理端口
38+
自定义 web 管理端口
3739

3840
```
3941
./goForward -port 8899
4042
```
4143

42-
指定IP绑定
44+
指定 IP 绑定
4345

4446
```
4547
./goForward -ip 1.1.1.1
4648
```
4749

48-
设置web管理访问密码
50+
设置 web 管理访问密码
4951

5052
```
5153
./goForward -pass 666
5254
```
5355

54-
当24H内同一IP密码试错超过3次将会ban掉
56+
当 24H 内同一 IP 密码试错超过 3 次将会 ban 掉
5557

5658
## 开机自启
5759

@@ -74,22 +76,28 @@ ExecStart=/full/path/to/your/goForward -pass 666
7476
WantedBy=default.target
7577
```
7678

77-
其中的```/full/path/to/your/goForward```改为二进制文件地址,后面可接参数
79+
其中的`/full/path/to/your/goForward`改为二进制文件地址,后面可接参数
7880

7981
**重新加载 Systemd 配置**
82+
8083
```
8184
sudo systemctl daemon-reload
8285
```
8386

8487
**启用服务**
88+
8589
```
8690
sudo systemctl enable goForward
8791
```
92+
8893
**启动服务**
94+
8995
```
9096
sudo systemctl start goForward
9197
```
98+
9299
**检查状态**
100+
93101
```
94102
sudo systemctl status goForward.service
95103
```

0 commit comments

Comments
 (0)