This commit is contained in:
esrrhs 2019-10-20 16:27:03 +08:00
parent a3036dcc66
commit f275f81308
4 changed files with 109 additions and 70 deletions

View File

@ -9,13 +9,14 @@ import (
"time" "time"
) )
func NewClient(addr string, server string, target string, timeout int, sproto int, rproto int, catch int, key int, tcpmode bool) (*Client, error) { func NewClient(addr string, server string, target string, timeout int, sproto int, rproto int, catch int, key int,
tcpmode int) (*Client, error) {
var ipaddr *net.UDPAddr var ipaddr *net.UDPAddr
var tcpaddr *net.TCPAddr var tcpaddr *net.TCPAddr
var err error var err error
if tcpmode { if tcpmode > 0 {
tcpaddr, err = net.ResolveTCPAddr("tcp", addr) tcpaddr, err = net.ResolveTCPAddr("tcp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -59,7 +60,7 @@ type Client struct {
rproto int rproto int
catch int catch int
key int key int
tcpmode bool tcpmode int
tcpmode_buffersize int tcpmode_buffersize int
tcpmode_maxwin int tcpmode_maxwin int
tcpmode_resend_timems int tcpmode_resend_timems int
@ -129,7 +130,7 @@ func (p *Client) Run() {
defer conn.Close() defer conn.Close()
p.conn = conn p.conn = conn
if p.tcpmode { if p.tcpmode > 0 {
tcplistenConn, err := net.ListenTCP("tcp", p.tcpaddr) tcplistenConn, err := net.ListenTCP("tcp", p.tcpaddr)
if err != nil { if err != nil {
loggo.Error("Error listening for tcp packets: %s", err.Error()) loggo.Error("Error listening for tcp packets: %s", err.Error())
@ -150,7 +151,7 @@ func (p *Client) Run() {
p.localAddrToConnMap = make(map[string]*ClientConn) p.localAddrToConnMap = make(map[string]*ClientConn)
p.localIdToConnMap = make(map[string]*ClientConn) p.localIdToConnMap = make(map[string]*ClientConn)
if p.tcpmode { if p.tcpmode > 0 {
go p.AcceptTcp() go p.AcceptTcp()
} else { } else {
go p.Accept() go p.Accept()
@ -269,7 +270,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn) {
p.sendPacketSize += (uint64)(f.size) p.sendPacketSize += (uint64)(f.size)
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), mb, sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), mb,
p.sproto, p.rproto, p.catch, p.key) p.sproto, p.rproto, p.catch, p.key, p.tcpmode)
} }
} }
@ -311,7 +312,7 @@ func (p *Client) Accept() error {
clientConn.activeTime = now clientConn.activeTime = now
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n],
p.sproto, p.rproto, p.catch, p.key) p.sproto, p.rproto, p.catch, p.key, p.tcpmode)
p.sequence++ p.sequence++
@ -379,7 +380,7 @@ func (p *Client) Close(clientConn *ClientConn) {
func (p *Client) checkTimeoutConn() { func (p *Client) checkTimeoutConn() {
if p.tcpmode { if p.tcpmode > 0 {
return return
} }
@ -404,7 +405,7 @@ func (p *Client) ping() {
now := time.Now() now := time.Now()
b, _ := now.MarshalBinary() b, _ := now.MarshalBinary()
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b,
p.sproto, p.rproto, p.catch, p.key) p.sproto, p.rproto, p.catch, p.key, p.tcpmode)
loggo.Info("ping %s %s %d %d %d %d", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence) loggo.Info("ping %s %s %d %d %d %d", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
p.sequence++ p.sequence++
} }
@ -425,7 +426,7 @@ func (p *Client) sendCatch() {
if p.catch > 0 { if p.catch > 0 {
for _, conn := range p.localIdToConnMap { for _, conn := range p.localIdToConnMap {
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, conn.id, (uint32)(CATCH), make([]byte, 0), sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, conn.id, (uint32)(CATCH), make([]byte, 0),
p.sproto, p.rproto, p.catch, p.key) p.sproto, p.rproto, p.catch, p.key, p.tcpmode)
p.sequence++ p.sequence++
p.sendCatchPacket++ p.sendCatchPacket++
} }

View File

@ -58,7 +58,7 @@ func main() {
rproto := flag.Int("rproto", 0, "recv ping proto") rproto := flag.Int("rproto", 0, "recv ping proto")
catch := flag.Int("catch", 0, "catch mode") catch := flag.Int("catch", 0, "catch mode")
key := flag.Int("key", 0, "key") key := flag.Int("key", 0, "key")
tcpmode := flag.Bool("tcp", false, "tcp mode") tcpmode := flag.Int("tcp", 0, "tcp mode")
flag.Usage = func() { flag.Usage = func() {
fmt.Printf(usage) fmt.Printf(usage)
} }

View File

@ -6,7 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"fmt" "github.com/esrrhs/go-engine/src/loggo"
"golang.org/x/net/icmp" "golang.org/x/net/icmp"
"golang.org/x/net/ipv4" "golang.org/x/net/ipv4"
"io" "io"
@ -19,6 +19,7 @@ const (
DATA uint32 = 0x01010101 DATA uint32 = 0x01010101
PING uint32 = 0x02020202 PING uint32 = 0x02020202
CATCH uint32 = 0x03030303 CATCH uint32 = 0x03030303
FRAME uint32 = 0x04040404
END uint32 = 0xAAAABBBB END uint32 = 0xAAAABBBB
) )
@ -30,6 +31,7 @@ type MyMsg struct {
RPROTO uint16 RPROTO uint16
CATCH uint16 CATCH uint16
KEY uint32 KEY uint32
TCPMODE uint16
ENDTYPE uint32 ENDTYPE uint32
} }
@ -38,7 +40,7 @@ func (p *MyMsg) Len(proto int) int {
if p == nil { if p == nil {
return 0 return 0
} }
return 4 + p.LenString(p.ID) + p.LenString(p.TARGET) + p.LenData(p.Data) + 2 + 2 + 4 + 4 return 4 + p.LenString(p.ID) + p.LenString(p.TARGET) + p.LenData(p.Data) + 2 + 2 + 4 + 2 + 4
} }
func (p *MyMsg) LenString(s string) int { func (p *MyMsg) LenString(s string) int {
@ -71,7 +73,9 @@ func (p *MyMsg) Marshal(proto int) ([]byte, error) {
binary.BigEndian.PutUint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+4:], uint32(p.KEY)) binary.BigEndian.PutUint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+4:], uint32(p.KEY))
binary.BigEndian.PutUint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+8:], uint32(p.ENDTYPE)) binary.BigEndian.PutUint16(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+8:], uint16(p.TCPMODE))
binary.BigEndian.PutUint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+10:], uint32(p.ENDTYPE))
return b, nil return b, nil
} }
@ -110,7 +114,9 @@ func (p *MyMsg) Unmarshal(b []byte) error {
p.KEY = binary.BigEndian.Uint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+4:]) p.KEY = binary.BigEndian.Uint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+4:])
p.ENDTYPE = binary.BigEndian.Uint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+8:]) p.TCPMODE = binary.BigEndian.Uint16(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+8:])
p.ENDTYPE = binary.BigEndian.Uint32(b[4+p.LenString(p.ID)+p.LenString(p.TARGET)+p.LenData(p.Data)+10:])
return nil return nil
} }
@ -136,7 +142,8 @@ func (p *MyMsg) UnmarshalData(b []byte) []byte {
} }
func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, target string, func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, target string,
connId string, msgType uint32, data []byte, sproto int, rproto int, catch int, key int) { connId string, msgType uint32, data []byte, sproto int, rproto int, catch int, key int,
tcpmode int) {
m := &MyMsg{ m := &MyMsg{
ID: connId, ID: connId,
@ -146,6 +153,7 @@ func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, ta
RPROTO: (uint16)(rproto), RPROTO: (uint16)(rproto),
CATCH: (uint16)(catch), CATCH: (uint16)(catch),
KEY: (uint32)(key), KEY: (uint32)(key),
TCPMODE: (uint16)(tcpmode),
ENDTYPE: END, ENDTYPE: END,
} }
@ -165,7 +173,7 @@ func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, ta
bytes, err := msg.Marshal(nil) bytes, err := msg.Marshal(nil)
if err != nil { if err != nil {
fmt.Printf("sendICMP Marshal error %s %s\n", server.String(), err) loggo.Error("sendICMP Marshal error %s %s", server.String(), err)
return return
} }
@ -176,7 +184,7 @@ func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, ta
continue continue
} }
} }
fmt.Printf("sendICMP WriteTo error %s %s\n", server.String(), err) loggo.Error("sendICMP WriteTo error %s %s", server.String(), err)
} }
break break
} }
@ -197,7 +205,7 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) {
// Read timeout // Read timeout
continue continue
} else { } else {
fmt.Printf("Error read icmp message %s\n", err) loggo.Error("Error read icmp message %s", err)
continue continue
} }
} }
@ -209,21 +217,21 @@ func recvICMP(conn icmp.PacketConn, recv chan<- *Packet) {
my := &MyMsg{} my := &MyMsg{}
my.Unmarshal(bytes[8:n]) my.Unmarshal(bytes[8:n])
if (my.TYPE != (uint32)(DATA) && my.TYPE != (uint32)(PING) && my.TYPE != (uint32)(CATCH)) || if (my.TYPE != (uint32)(DATA) && my.TYPE != (uint32)(PING) && my.TYPE != (uint32)(CATCH) && my.TYPE != (uint32)(FRAME)) ||
my.ENDTYPE != (uint32)(END) { my.ENDTYPE != (uint32)(END) {
//fmt.Printf("processPacket diff type %s %d %d \n", my.ID, my.TYPE, my.ENDTYPE) loggo.Info("processPacket diff type %s %d %d ", my.ID, my.TYPE, my.ENDTYPE)
continue continue
} }
if my.Data == nil { if my.Data == nil {
fmt.Printf("processPacket data nil %s\n", my.ID) loggo.Info("processPacket data nil %s", my.ID)
return return
} }
recv <- &Packet{msgType: my.TYPE, data: my.Data, id: my.ID, target: my.TARGET, recv <- &Packet{msgType: my.TYPE, data: my.Data, id: my.ID, target: my.TARGET,
src: srcaddr.(*net.IPAddr), rproto: (int)((int16)(my.RPROTO)), src: srcaddr.(*net.IPAddr), rproto: (int)((int16)(my.RPROTO)),
echoId: echoId, echoSeq: echoSeq, catch: (int)((int16)(my.CATCH)), echoId: echoId, echoSeq: echoSeq, catch: (int)((int16)(my.CATCH)),
key: (int)(my.KEY)} key: (int)(my.KEY), tcpmode: (int)((int16)(my.TCPMODE))}
} }
} }
@ -238,6 +246,7 @@ type Packet struct {
echoSeq int echoSeq int
catch int catch int
key int key int
tcpmode int
} }
func UniqueId() string { func UniqueId() string {

View File

@ -1,7 +1,7 @@
package pingtunnel package pingtunnel
import ( import (
"fmt" "github.com/esrrhs/go-engine/src/loggo"
"golang.org/x/net/icmp" "golang.org/x/net/icmp"
"net" "net"
"time" "time"
@ -37,6 +37,8 @@ type Server struct {
type ServerConn struct { type ServerConn struct {
ipaddrTarget *net.UDPAddr ipaddrTarget *net.UDPAddr
conn *net.UDPConn conn *net.UDPConn
tcpaddrTarget *net.TCPAddr
tcpconn *net.TCPConn
id string id string
activeTime time.Time activeTime time.Time
close bool close bool
@ -49,7 +51,7 @@ func (p *Server) Run() {
conn, err := icmp.ListenPacket("ip4:icmp", "") conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil { if err != nil {
fmt.Printf("Error listening for ICMP packets: %s\n", err.Error()) loggo.Error("Error listening for ICMP packets: %s", err.Error())
return return
} }
p.conn = conn p.conn = conn
@ -85,51 +87,78 @@ func (p *Server) processPacket(packet *Packet) {
if packet.msgType == PING { if packet.msgType == PING {
t := time.Time{} t := time.Time{}
t.UnmarshalBinary(packet.data) t.UnmarshalBinary(packet.data)
fmt.Printf("ping from %s %s %d %d %d\n", packet.src.String(), t.String(), packet.rproto, packet.echoId, packet.echoSeq) loggo.Info("ping from %s %s %d %d %d", packet.src.String(), t.String(), packet.rproto, packet.echoId, packet.echoSeq)
sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(PING), packet.data, sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(PING), packet.data,
packet.rproto, -1, 0, p.key) packet.rproto, -1, 0, p.key, packet.tcpmode)
return return
} }
//fmt.Printf("processPacket %s %s %d\n", packet.id, packet.src.String(), len(packet.data)) loggo.Debug("processPacket %s %s %d", packet.id, packet.src.String(), len(packet.data))
now := time.Now() now := time.Now()
id := packet.id id := packet.id
udpConn := p.localConnMap[id] localConn := p.localConnMap[id]
if udpConn == nil { if localConn == nil {
if packet.tcpmode > 0 {
addr := packet.target
ipaddrTarget, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
loggo.Error("Error ResolveUDPAddr for tcp addr: %s %s", addr, err.Error())
return
}
targetConn, err := net.DialTCP("tcp", nil, ipaddrTarget)
if err != nil {
loggo.Error("Error listening for tcp packets: %s", err.Error())
return
}
catchQueue := make(chan *CatchMsg, packet.catch)
localConn = &ServerConn{tcpconn: targetConn, tcpaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
rproto: packet.rproto, catchQueue: catchQueue}
p.localConnMap[id] = localConn
go p.RecvTCP(localConn, id, packet.src)
} else {
addr := packet.target addr := packet.target
ipaddrTarget, err := net.ResolveUDPAddr("udp", addr) ipaddrTarget, err := net.ResolveUDPAddr("udp", addr)
if err != nil { if err != nil {
fmt.Printf("Error ResolveUDPAddr for udp addr: %s %s\n", addr, err.Error()) loggo.Error("Error ResolveUDPAddr for udp addr: %s %s", addr, err.Error())
return return
} }
targetConn, err := net.DialUDP("udp", nil, ipaddrTarget) targetConn, err := net.DialUDP("udp", nil, ipaddrTarget)
if err != nil { if err != nil {
fmt.Printf("Error listening for udp packets: %s\n", err.Error()) loggo.Error("Error listening for udp packets: %s", err.Error())
return return
} }
catchQueue := make(chan *CatchMsg, 1000) catchQueue := make(chan *CatchMsg, packet.catch)
udpConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false, localConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
rproto: packet.rproto, catchQueue: catchQueue} rproto: packet.rproto, catchQueue: catchQueue}
p.localConnMap[id] = udpConn p.localConnMap[id] = localConn
go p.Recv(udpConn, id, packet.src) go p.Recv(localConn, id, packet.src)
}
} }
udpConn.activeTime = now localConn.activeTime = now
udpConn.catch = packet.catch localConn.catch = packet.catch
if packet.msgType == CATCH { if packet.msgType == CATCH {
select { select {
case re := <-udpConn.catchQueue: case re := <-localConn.catchQueue:
sendICMP(packet.echoId, packet.echoSeq, *p.conn, re.src, "", re.id, (uint32)(CATCH), re.data, sendICMP(packet.echoId, packet.echoSeq, *p.conn, re.src, "", re.id, (uint32)(CATCH), re.data,
re.conn.rproto, -1, 0, p.key) re.conn.rproto, -1, 0, p.key, packet.tcpmode)
p.sendCatchPacket++ p.sendCatchPacket++
case <-time.After(time.Duration(1) * time.Millisecond): case <-time.After(time.Duration(1) * time.Millisecond):
} }
@ -139,10 +168,10 @@ func (p *Server) processPacket(packet *Packet) {
if packet.msgType == DATA { if packet.msgType == DATA {
_, err := udpConn.conn.Write(packet.data) _, err := localConn.conn.Write(packet.data)
if err != nil { if err != nil {
fmt.Printf("WriteToUDP Error %s\n", err) loggo.Error("WriteToUDP Error %s", err)
udpConn.close = true localConn.close = true
return return
} }
@ -153,7 +182,7 @@ func (p *Server) processPacket(packet *Packet) {
func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) { func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
fmt.Printf("server waiting target response %s -> %s %s\n", conn.ipaddrTarget.String(), conn.id, conn.conn.LocalAddr().String()) loggo.Info("server waiting target response %s -> %s %s", conn.ipaddrTarget.String(), conn.id, conn.conn.LocalAddr().String())
for { for {
bytes := make([]byte, 2000) bytes := make([]byte, 2000)
@ -166,7 +195,7 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
// Read timeout // Read timeout
continue continue
} else { } else {
fmt.Printf("ReadFromUDP Error read udp %s\n", err) loggo.Error("ReadFromUDP Error read udp %s", err)
conn.close = true conn.close = true
return return
} }
@ -183,7 +212,7 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
} }
} else { } else {
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n], sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n],
conn.rproto, -1, 0, p.key) conn.rproto, -1, 0, p.key, packet.tcpmode)
} }
p.sendPacket++ p.sendPacket++
@ -210,14 +239,14 @@ func (p *Server) checkTimeoutConn() {
for id, conn := range p.localConnMap { for id, conn := range p.localConnMap {
if conn.close { if conn.close {
fmt.Printf("close inactive conn %s %s\n", id, conn.ipaddrTarget.String()) loggo.Info("close inactive conn %s %s", id, conn.ipaddrTarget.String())
p.Close(conn) p.Close(conn)
} }
} }
} }
func (p *Server) showNet() { func (p *Server) showNet() {
fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s sendCatch %d/s recvCatch %d/s\n", loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s sendCatch %d/s recvCatch %d/s",
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.sendCatchPacket, p.recvCatchPacket) p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.sendCatchPacket, p.recvCatchPacket)
p.sendPacket = 0 p.sendPacket = 0
p.recvPacket = 0 p.recvPacket = 0