This commit is contained in:
esrrhs 2019-10-31 21:16:14 +08:00
parent 831a5ee51d
commit 64f8ec24e6
3 changed files with 45 additions and 15 deletions

View File

@ -19,7 +19,7 @@ const (
func NewClient(addr string, server string, target string, timeout int, key int, func NewClient(addr string, server string, target string, timeout int, key int,
tcpmode int, tcpmode_buffersize int, tcpmode_maxwin int, tcpmode_resend_timems int, tcpmode_compress int, tcpmode int, tcpmode_buffersize int, tcpmode_maxwin int, tcpmode_resend_timems int, tcpmode_compress int,
tcpmode_stat int, open_sock5 int) (*Client, error) { tcpmode_stat int, open_sock5 int, maxconn int) (*Client, error) {
var ipaddr *net.UDPAddr var ipaddr *net.UDPAddr
var tcpaddr *net.TCPAddr var tcpaddr *net.TCPAddr
@ -62,6 +62,7 @@ func NewClient(addr string, server string, target string, timeout int, key int,
tcpmode_compress: tcpmode_compress, tcpmode_compress: tcpmode_compress,
tcpmode_stat: tcpmode_stat, tcpmode_stat: tcpmode_stat,
open_sock5: open_sock5, open_sock5: open_sock5,
maxconn: maxconn,
}, nil }, nil
} }
@ -70,6 +71,7 @@ type Client struct {
rtt time.Duration rtt time.Duration
interval *time.Ticker interval *time.Ticker
workResultLock sync.WaitGroup workResultLock sync.WaitGroup
maxconn int
id int id int
sequence int sequence int
@ -272,9 +274,15 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) {
p.workResultLock.Add(1) p.workResultLock.Add(1)
defer p.workResultLock.Done() defer p.workResultLock.Done()
uuid := UniqueId()
tcpsrcaddr := conn.RemoteAddr().(*net.TCPAddr) tcpsrcaddr := conn.RemoteAddr().(*net.TCPAddr)
if p.localIdToConnMapSize >= p.maxconn {
loggo.Info("too many connections %d, client accept new local tcp fail %s", p.localIdToConnMapSize, tcpsrcaddr.String())
return
}
uuid := UniqueId()
fm := NewFrameMgr(p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems, p.tcpmode_compress, p.tcpmode_stat) fm := NewFrameMgr(p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems, p.tcpmode_compress, p.tcpmode_stat)
now := time.Now() now := time.Now()
@ -484,6 +492,10 @@ func (p *Client) Accept() error {
now := time.Now() now := time.Now()
clientConn := p.getClientConnByAddr(srcaddr.String()) clientConn := p.getClientConnByAddr(srcaddr.String())
if clientConn == nil { if clientConn == nil {
if p.localIdToConnMapSize >= p.maxconn {
loggo.Info("too many connections %d, client accept new local udp fail %s", p.localIdToConnMapSize, srcaddr.String())
continue
}
uuid := UniqueId() uuid := UniqueId()
clientConn = &ClientConn{ipaddr: srcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false} clientConn = &ClientConn{ipaddr: srcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false}
p.addClientConn(uuid, srcaddr.String(), clientConn) p.addClientConn(uuid, srcaddr.String(), clientConn)

View File

@ -71,6 +71,9 @@ Usage:
-sock5 开启sock5转发默认0 -sock5 开启sock5转发默认0
Turn on sock5 forwarding, default 0 is off Turn on sock5 forwarding, default 0 is off
-maxconn 最大连接数默认1000
the max num of connections, default 1000
` `
func main() { func main() {
@ -90,6 +93,7 @@ func main() {
tcpmode_stat := flag.Int("tcp_stat", 0, "print tcp stat") tcpmode_stat := flag.Int("tcp_stat", 0, "print tcp stat")
loglevel := flag.String("loglevel", "info", "log level") loglevel := flag.String("loglevel", "info", "log level")
open_sock5 := flag.Int("sock5", 0, "sock5 mode") open_sock5 := flag.Int("sock5", 0, "sock5 mode")
maxconn := flag.Int("maxconn", 0, "max num of connections")
flag.Usage = func() { flag.Usage = func() {
fmt.Printf(usage) fmt.Printf(usage)
} }
@ -132,7 +136,7 @@ func main() {
loggo.Info("key %d", *key) loggo.Info("key %d", *key)
if *t == "server" { if *t == "server" {
s, err := pingtunnel.NewServer(*key) s, err := pingtunnel.NewServer(*key, *maxconn)
if err != nil { if err != nil {
loggo.Error("ERROR: %s", err.Error()) loggo.Error("ERROR: %s", err.Error())
return return
@ -143,8 +147,7 @@ func main() {
loggo.Error("Run ERROR: %s", err.Error()) loggo.Error("Run ERROR: %s", err.Error())
return return
} }
} } else if *t == "client" {
if *t == "client" {
loggo.Info("type %s", *t) loggo.Info("type %s", *t)
loggo.Info("listen %s", *listen) loggo.Info("listen %s", *listen)
@ -161,7 +164,7 @@ func main() {
c, err := pingtunnel.NewClient(*listen, *server, *target, *timeout, *key, c, err := pingtunnel.NewClient(*listen, *server, *target, *timeout, *key,
*tcpmode, *tcpmode_buffersize, *tcpmode_maxwin, *tcpmode_resend_timems, *tcpmode_compress, *tcpmode, *tcpmode_buffersize, *tcpmode_maxwin, *tcpmode_resend_timems, *tcpmode_compress,
*tcpmode_stat, *open_sock5) *tcpmode_stat, *open_sock5, *maxconn)
if err != nil { if err != nil {
loggo.Error("ERROR: %s", err.Error()) loggo.Error("ERROR: %s", err.Error())
return return
@ -173,6 +176,8 @@ func main() {
loggo.Error("Run ERROR: %s", err.Error()) loggo.Error("Run ERROR: %s", err.Error())
return return
} }
} else {
return
} }
for { for {
time.Sleep(time.Hour) time.Sleep(time.Hour)

View File

@ -10,10 +10,11 @@ import (
"time" "time"
) )
func NewServer(key int) (*Server, error) { func NewServer(key int, maxconn int) (*Server, error) {
return &Server{ return &Server{
exit: false, exit: false,
key: key, key: key,
maxconn: maxconn,
}, nil }, nil
} }
@ -22,6 +23,7 @@ type Server struct {
key int key int
interval *time.Ticker interval *time.Ticker
workResultLock sync.WaitGroup workResultLock sync.WaitGroup
maxconn int
conn *icmp.PacketConn conn *icmp.PacketConn
@ -31,6 +33,7 @@ type Server struct {
recvPacket uint64 recvPacket uint64
sendPacketSize uint64 sendPacketSize uint64
recvPacketSize uint64 recvPacketSize uint64
localConnMapSize int
echoId int echoId int
echoSeq int echoSeq int
@ -118,6 +121,11 @@ func (p *Server) processPacket(packet *Packet) {
localConn := p.getServerConnById(id) localConn := p.getServerConnById(id)
if localConn == nil { if localConn == nil {
if p.localConnMapSize >= p.maxconn {
loggo.Info("too many connections %d, server connected target fail %s", p.localConnMapSize, packet.my.Target)
return
}
if packet.my.Tcpmode > 0 { if packet.my.Tcpmode > 0 {
addr := packet.my.Target addr := packet.my.Target
@ -449,8 +457,13 @@ func (p *Server) checkTimeoutConn() {
} }
func (p *Server) showNet() { func (p *Server) showNet() {
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s", p.localConnMapSize = 0
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024) p.localConnMap.Range(func(key, value interface{}) bool {
p.localConnMapSize++
return true
})
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s %dConnections",
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.localConnMapSize)
p.sendPacket = 0 p.sendPacket = 0
p.recvPacket = 0 p.recvPacket = 0
p.sendPacketSize = 0 p.sendPacketSize = 0