csznet / goforward Goto Github PK
View Code? Open in Web Editor NEW使用golang实现的tcp udp端口转发,支持流量统计、web管理、规则热加载
Home Page: https://www.csz.net
使用golang实现的tcp udp端口转发,支持流量统计、web管理、规则热加载
Home Page: https://www.csz.net
root@pve:/jk/goForward1# ./go* -pass 666
Data: /jk/goForward1/goForward.db
Web管 理 面 板 端 口 :8889
2024/03/17 21:46:27 /home/runner/work/goForward/goForward/sql/sql.go:101 SQL logic error: no such column
: protocal (1)
[0.143ms] [rows:0] SELECT * FROM `connection_stats` WHERE local_port = "8889" And protocal = "udp"
大佬,程序很好用。
看能否提供规则备注功能,时间久了,容易忘记添加的是啥。
另外,目标地址能否增加动态域名的支持呢。
尝试了一下,轻量化很好用,不过直接暴露在公网上还是不太安全,建议先借鉴xui的相关实现
1.自定义路径伪装 ip:port/a1b2c3d4
2.面板访问密码
然后现在用ip:8080/8889都可以访问,第一条规则的存在意义不大,最好有个设置能修改默认端口.然后这个规则目前删除了进程就崩溃,下次开启又会自动创建,可以优化一下
// 定时打印和处理流量变化
func (cs *ConnectionStats) printStats(wg *sync.WaitGroup, ctx context.Context) {
defer wg.Done()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop() // 在函数结束时停止定时器
for {
select {
case <-ticker.C:
cs.TotalBytesLock.Lock()
if cs.TotalBytes > cs.TotalBytesOld {
if cs.Protocol == "tcp" {
cs.TcpTime = 0
}
var total string
if cs.TotalBytes > 0 && float64(cs.TotalBytes)/(1024*1024) < 0.5 {
total = strconv.FormatFloat(float64(cs.TotalBytes)/(1024), 'f', 2, 64) + "KB"
} else {
total = strconv.FormatFloat(float64(cs.TotalBytes)/(1024*1024), 'f', 2, 64) + "MB"
}
fmt.Printf("【%s】端口 %s 统计流量: %s\n", cs.Protocol, cs.LocalPort, total)
//统计更换单位
var gb uint64 = 1073741824
if cs.TotalBytes >= gb {
cs.TotalGigabyte = cs.TotalGigabyte + 1
sql.UpdateForwardGb(cs.Id, cs.TotalGigabyte)
cs.TotalBytes = cs.TotalBytes - gb
}
cs.TotalBytesOld = cs.TotalBytes
sql.UpdateForwardBytes(cs.Id, cs.TotalBytes)
fmt.Printf("【%s】端口 %s 当前连接数: %d\n", cs.Protocol, cs.LocalPort, len(cs.TCPConnections))
} else {
if cs.Protocol == "tcp" {
// fmt.Printf("【%s】端口 %s 当前超时秒: %d\n", cs.Protocol, cs.LocalPort, cs.TcpTime)
if cs.TcpTime >= conf.TcpTimeout {
// fmt.Printf("【%s】端口 %s 超时关闭\n", cs.Protocol, cs.LocalPort)
for i := len(cs.TCPConnections) - 1; i >= 0; i-- {
conn := cs.TCPConnections[i]
conn.Close()
// 从连接列表中移除关闭的连接
cs.TCPConnections = append(cs.TCPConnections[:i], cs.TCPConnections[i+1:]...)
}
} else {
cs.TcpTime = cs.TcpTime + 5
}
}
}
cs.TotalBytesLock.Unlock()
//当协程退出时执行
case <-ctx.Done():
return
}
}
}
以上代码是尚未实现吗
windows系统可以用吗?
是不是默认5s自动关闭连接
× goForward.service - Start goForward on boot
Loaded: loaded (/etc/systemd/system/goForward.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Wed 2024-04-17 21:40:06 CST; 36s ago
Duration: 10.111s
Process: 1822 ExecStart=/root/goForward (code=exited, status=2)
Main PID: 1822 (code=exited, status=2)
CPU: 625ms
Apr 17 21:40:06 C20240408212180 goForward[1822]: panic: interface conversion: interface {} is *[]uint8, not []uint8
Apr 17 21:40:06 C20240408212180 goForward[1822]: goroutine 30 [running]:
Apr 17 21:40:06 C20240408212180 goForward[1822]: csz.net/goForward/forward.(*ConnectionStats).copyBytes(0xc00011c120, {0xdc89d0, 0xc0000124b8}, {0xdc89d0, 0xc0000124a0})
Apr 17 21:40:06 C20240408212180 goForward[1822]: /home/runner/work/goForward/goForward/forward/forward.go:208 +0x394
Apr 17 21:40:06 C20240408212180 goForward[1822]: csz.net/goForward/forward.(*ConnectionStats).handleTCPConnection.func2()
Apr 17 21:40:06 C20240408212180 goForward[1822]: /home/runner/work/goForward/goForward/forward/forward.go:154 +0x6c
Apr 17 21:40:06 C20240408212180 goForward[1822]: created by csz.net/goForward/forward.(*ConnectionStats).handleTCPConnection
Apr 17 21:40:06 C20240408212180 goForward[1822]: /home/runner/work/goForward/goForward/forward/forward.go:152 +0x451
Apr 17 21:40:06 C20240408212180 systemd[1]: goForward.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Apr 17 21:40:06 C20240408212180 systemd[1]: goForward.service: Failed with result 'exit-code'.
root@C20240408212180:~#
启动命令为:ExecStart=/usr/local/bin/goForward -pass [密码] -port 30000
在界面添加ipv6,两个相同的端口后,工具崩溃,可复现,日志如下
Apr 17 02:50:44 cn goForward[15293]: goForward Version v1.1.9 Apr 17 02:50:44 cn goForward[15293]: Data: /usr/local/bin/goForward.db Apr 17 02:50:45 cn goForward[15293]: Web<E7><AE><A1><E7><90><86><E9><9D><A2><E6><9D><BF><E7><AB><AF><E5><8F><A3>:30000 Apr 17 02:50:45 cn goForward[15293]: <E3><80><90>udp<E3><80><91><E7>91><E5><90><AC><E7><AB><AF><E5><8F><A3> 30000 <E8><BD><AC><E5><8F><91><E8><87><B3> 127.0.0.1:30000 Apr 17 02:50:51 cn goForward[15293]: [GIN] 2024/04/17 - 02:50:51 | 200 | 802.27<C2><B5>s | 2001:da8:b000:6801:929c:4aff:fec1:f4db | GET "/" Apr 17 02:51:29 cn goForward[15293]: [GIN] 2024/04/17 - 02:51:29 | 200 | 9.659558ms | 2001:da8:b000:6801:929c:4aff:fec1:f4db | POST "/add" Apr 17 02:51:29 cn goForward[15293]: <E3><80><90>udp<E3><80><91><E7>91><E5><90><AC><E7><AB><AF><E5><8F><A3> 35544 <E8><BD><AC><E5><8F><91><E8><87><B3> 2401:1fe0::eef:35544 Apr 17 02:51:29 cn goForward[15293]: 2024/04/17 02:51:29 <E8><A7><A3><E6><9E><90><E8><BF><9C><E7><A8><8B><E5><9C><B0><E5><9D><80><E6><97><B6><E5><8F><91><E7><94><9F><E9><94><99> > Apr 17 02:51:29 cn systemd[1]: goForward.service: Main process exited, code=exited, status=1/FAILURE Apr 17 02:51:29 cn systemd[1]: goForward.service: Failed with result 'exit-code'.
没找到这个的设置,默认同一个目录放在在 bin 里有点乱
设置端口转发之后,随着时间的流动,内存也越来越大,知道程序奔溃退出
作者您好,我想问下UDP的转发,在1000Mb/s的直连环境下转发性能可以做到多少。
Line 158 in 954807b
还没应用,请问会识别v4 v6吗?
刚刚用上了,太方便了。
不过机器上本地有绑定多个IP,能否在添加时选择要绑定的IP呢,而不是默认的0.0.0.0
感谢。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.