From 0ba2aa2297eb0fbbcdd3b32e9793db8ee5cf3537 Mon Sep 17 00:00:00 2001 From: esrrhs Date: Thu, 31 Oct 2019 21:48:32 +0800 Subject: [PATCH] add --- client.go | 21 +++++++++++++---- msg.pb.go | 68 +++++++++++++++++++++++++++++-------------------------- msg.proto | 1 + server.go | 14 ++++++++++-- 4 files changed, 65 insertions(+), 39 deletions(-) diff --git a/client.go b/client.go index 393173c..af277ea 100644 --- a/client.go +++ b/client.go @@ -113,6 +113,7 @@ type Client struct { } type ClientConn struct { + exit bool ipaddr *net.UDPAddr tcpaddr *net.TCPAddr id string @@ -286,7 +287,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) { fm := NewFrameMgr(p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems, p.tcpmode_compress, p.tcpmode_stat) now := time.Now() - clientConn := &ClientConn{tcpaddr: tcpsrcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false, + clientConn := &ClientConn{exit: false, tcpaddr: tcpsrcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false, fm: fm} p.addClientConn(uuid, tcpsrcaddr.String(), clientConn) loggo.Info("client accept new local tcp %s %s", uuid, tcpsrcaddr.String()) @@ -294,7 +295,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) { loggo.Info("start connect remote tcp %s %s", uuid, tcpsrcaddr.String()) clientConn.fm.Connect() startConnectTime := time.Now() - for !p.exit { + for !p.exit && !clientConn.exit { if clientConn.fm.IsConnected() { break } @@ -327,7 +328,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) { tcpActiveRecvTime := time.Now() tcpActiveSendTime := time.Now() - for !p.exit { + for !p.exit && !clientConn.exit { now := time.Now() sleep := true @@ -415,7 +416,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) { } startCloseTime := time.Now() - for !p.exit { + for !p.exit && !clientConn.exit { now := time.Now() clientConn.fm.Update() @@ -497,7 +498,7 @@ func (p *Client) Accept() error { continue } uuid := UniqueId() - clientConn = &ClientConn{ipaddr: srcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false} + clientConn = &ClientConn{exit: false, ipaddr: srcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false} p.addClientConn(uuid, srcaddr.String(), clientConn) loggo.Info("client accept new local udp %s %s", uuid, srcaddr.String()) } @@ -539,6 +540,15 @@ func (p *Client) processPacket(packet *Packet) { return } + if packet.my.Type == (int32)(MyMsg_KICK) { + clientConn := p.getClientConnById(packet.my.Id) + if clientConn != nil { + p.close(clientConn) + loggo.Info("remote kick local %s", packet.my.Id) + } + return + } + loggo.Debug("processPacket %s %s %d", packet.my.Id, packet.src.String(), len(packet.my.Data)) clientConn := p.getClientConnById(packet.my.Id) @@ -574,6 +584,7 @@ func (p *Client) processPacket(packet *Packet) { } func (p *Client) close(clientConn *ClientConn) { + clientConn.exit = true p.deleteClientConn(clientConn.id, clientConn.ipaddr.String()) p.deleteClientConn(clientConn.id, clientConn.tcpaddr.String()) } diff --git a/msg.pb.go b/msg.pb.go index 02aa2f6..2884b3f 100644 --- a/msg.pb.go +++ b/msg.pb.go @@ -25,18 +25,21 @@ type MyMsg_TYPE int32 const ( MyMsg_DATA MyMsg_TYPE = 0 MyMsg_PING MyMsg_TYPE = 1 + MyMsg_KICK MyMsg_TYPE = 2 MyMsg_MAGIC MyMsg_TYPE = 57005 ) var MyMsg_TYPE_name = map[int32]string{ 0: "DATA", 1: "PING", + 2: "KICK", 57005: "MAGIC", } var MyMsg_TYPE_value = map[string]int32{ "DATA": 0, "PING": 1, + "KICK": 2, "MAGIC": 57005, } @@ -402,36 +405,37 @@ func init() { func init() { proto.RegisterFile("msg.proto", fileDescriptor_c06e4cca6c2cc899) } var fileDescriptor_c06e4cca6c2cc899 = []byte{ - // 493 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0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 0x5a, 0xcc, 0x03, 0x37, 0x7a, 0xb7, 0x5c, 0x4f, 0xa9, 0x1d, - 0xfc, 0x21, 0xe0, 0x62, 0x94, 0xff, 0xc6, 0xd0, 0xd7, 0x81, 0xf3, 0xc2, 0x20, 0x03, 0x61, 0x98, - 0x8e, 0xa2, 0xbf, 0x7a, 0xc0, 0x18, 0xc5, 0x16, 0x1d, 0x37, 0x17, 0xea, 0xe0, 0xbf, 0xe8, 0x0b, - 0x7d, 0x6c, 0x9e, 0xa2, 0xef, 0x6b, 0x78, 0x0a, 0x27, 0xdd, 0xc3, 0x6f, 0x2e, 0x50, 0x7f, 0xb3, - 0x84, 0xf7, 0x7c, 0x3b, 0x74, 0x85, 0x61, 0xc1, 0xab, 0x7f, 0xa6, 0xd9, 0x07, 0x5b, 0x4c, 0xdf, - 0x53, 0xa2, 0xc1, 0x24, 0x7a, 0x4b, 0xad, 0x6e, 0xbe, 0x36, 0xa2, 0xe5, 0x62, 0x46, 0x9d, 0xb3, - 0xd1, 0x27, 0xa8, 0xb2, 0x22, 0x55, 0xfb, 0xa2, 0x90, 0x5f, 0x2e, 0x7b, 0x78, 0xce, 0x2f, 0xff, - 0x06, 0x00, 0x00, 0xff, 0xff, 0x5b, 0xf2, 0xbf, 0x87, 0x4d, 0x03, 0x00, 0x00, + // 499 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x52, 0xcb, 0x6e, 0xd3, 0x4c, + 0x14, 0xfe, 0xc7, 0x97, 0x24, 0x3e, 0xb9, 0xfc, 0xd3, 0xe1, 0xa2, 0x11, 0x0b, 0x14, 0x2c, 0x21, + 0x99, 0x05, 0x95, 0x28, 0x12, 0xac, 0x53, 0x37, 0x44, 0x51, 0xc9, 0x85, 0x49, 0x58, 0xc0, 0x26, + 0x72, 0xe3, 0xa9, 0x65, 0x81, 0x2f, 0xb2, 0x27, 0x82, 0xf0, 0x04, 0xbc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3e, 0x36, 0x9f, 0xa2, 0x2f, 0xad, 0x7f, 0x06, 0xa7, + 0xed, 0x87, 0xdf, 0xde, 0xa2, 0x7e, 0xa6, 0x31, 0xef, 0x8c, 0xed, 0xc0, 0x15, 0x86, 0xf9, 0xaf, + 0xfe, 0x9a, 0x6b, 0x17, 0x6c, 0x31, 0x7d, 0x47, 0x89, 0x06, 0x13, 0x9c, 0x6a, 0x33, 0x69, 0x1b, + 0xd1, 0x6a, 0x39, 0xa3, 0xce, 0xf9, 0xe0, 0x23, 0x94, 0x69, 0x9e, 0xa8, 0x43, 0x9e, 0xcb, 0xcf, + 0x57, 0x1d, 0x3c, 0xec, 0x97, 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0x15, 0x89, 0x25, 0x68, 0x57, + 0x03, 0x00, 0x00, } diff --git a/msg.proto b/msg.proto index 6a3b671..8571aff 100644 --- a/msg.proto +++ b/msg.proto @@ -5,6 +5,7 @@ message MyMsg { enum TYPE { DATA = 0; PING = 1; + KICK = 2; MAGIC = 0xdead; } diff --git a/server.go b/server.go index 2db9b82..b16267b 100644 --- a/server.go +++ b/server.go @@ -27,7 +27,8 @@ type Server struct { conn *icmp.PacketConn - localConnMap sync.Map + localConnMap sync.Map + remoteConnErrorMap sync.Map sendPacket uint64 recvPacket uint64 @@ -123,6 +124,7 @@ func (p *Server) processPacket(packet *Packet) { if p.maxconn > 0 && p.localConnMapSize >= p.maxconn { loggo.Info("too many connections %d, server connected target fail %s", p.localConnMapSize, packet.my.Target) + p.remoteError(id, packet) return } @@ -133,6 +135,7 @@ func (p *Server) processPacket(packet *Packet) { c, err := net.DialTimeout("tcp", addr, time.Second) if err != nil { loggo.Error("Error listening for tcp packets: %s", err.Error()) + p.remoteError(id, packet) return } targetConn := c.(*net.TCPConn) @@ -155,6 +158,7 @@ func (p *Server) processPacket(packet *Packet) { c, err := net.DialTimeout("udp", addr, time.Second) if err != nil { loggo.Error("Error listening for tcp packets: %s", err.Error()) + p.remoteError(id, packet) return } targetConn := c.(*net.UDPConn) @@ -471,7 +475,6 @@ func (p *Server) showNet() { } func (p *Server) addServerConn(uuid string, serverConn *ServerConn) { - p.localConnMap.Store(uuid, serverConn) } @@ -486,3 +489,10 @@ func (p *Server) getServerConnById(uuid string) *ServerConn { func (p *Server) deleteServerConn(uuid string) { p.localConnMap.Delete(uuid) } + +func (p *Server) remoteError(uuid string, packet *Packet) { + sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", uuid, (uint32)(MyMsg_KICK), packet.my.Data, + (int)(packet.my.Rproto), -1, p.key, + 0, 0, 0, 0, 0, 0, + 0) +}