|
|
@ -14,14 +14,16 @@ import (
|
|
|
|
type NatClient struct {
|
|
|
|
type NatClient struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
mu sync.RWMutex
|
|
|
|
cmdTCPConn net.Conn
|
|
|
|
cmdTCPConn net.Conn
|
|
|
|
cmdUDPConn *net.UDPAddr
|
|
|
|
cmdUDPConn *net.UDPConn
|
|
|
|
ServiceTarget string
|
|
|
|
ServiceTarget string
|
|
|
|
CmdTarget string
|
|
|
|
CmdTarget string
|
|
|
|
tcpAlived bool
|
|
|
|
tcpAlived bool
|
|
|
|
DialTimeout int
|
|
|
|
DialTimeout int
|
|
|
|
|
|
|
|
UdpTimeout int
|
|
|
|
enableTCP bool
|
|
|
|
enableTCP bool
|
|
|
|
enableUDP bool
|
|
|
|
enableUDP bool
|
|
|
|
Passwd string
|
|
|
|
Passwd string
|
|
|
|
|
|
|
|
udpAlived bool
|
|
|
|
stopCtx context.Context
|
|
|
|
stopCtx context.Context
|
|
|
|
stopFn context.CancelFunc
|
|
|
|
stopFn context.CancelFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -32,6 +34,12 @@ func (s *NatClient) tcpCmdConn() net.Conn {
|
|
|
|
return s.cmdTCPConn
|
|
|
|
return s.cmdTCPConn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) udpCmdConn() *net.UDPConn {
|
|
|
|
|
|
|
|
s.mu.RLock()
|
|
|
|
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
|
|
|
|
return s.cmdUDPConn
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) tcpCmdConnAlived() bool {
|
|
|
|
func (s *NatClient) tcpCmdConnAlived() bool {
|
|
|
|
s.mu.RLock()
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
@ -44,7 +52,19 @@ func (s *NatClient) setTcpCmdConnAlived(v bool) {
|
|
|
|
s.tcpAlived = v
|
|
|
|
s.tcpAlived = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) Run() {
|
|
|
|
func (s *NatClient) udpCmdConnAlived() bool {
|
|
|
|
|
|
|
|
s.mu.RLock()
|
|
|
|
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
|
|
|
|
return s.udpAlived
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) setUdpCmdConnAlived(v bool) {
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
s.udpAlived = v
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) Run() error {
|
|
|
|
s.stopCtx, s.stopFn = context.WithCancel(context.Background())
|
|
|
|
s.stopCtx, s.stopFn = context.WithCancel(context.Background())
|
|
|
|
if s.DialTimeout == 0 {
|
|
|
|
if s.DialTimeout == 0 {
|
|
|
|
s.DialTimeout = 10000
|
|
|
|
s.DialTimeout = 10000
|
|
|
@ -52,9 +72,23 @@ func (s *NatClient) Run() {
|
|
|
|
if s.Passwd != "" {
|
|
|
|
if s.Passwd != "" {
|
|
|
|
MSG_CMD_HELLO = sha256.New().Sum(append(MSG_CMD_HELLO, []byte(s.Passwd)...))[:16]
|
|
|
|
MSG_CMD_HELLO = sha256.New().Sum(append(MSG_CMD_HELLO, []byte(s.Passwd)...))[:16]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
if s.enableUDP {
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
s.runUdp()
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
if s.enableTCP {
|
|
|
|
if s.enableTCP {
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
s.runTcp()
|
|
|
|
s.runTcp()
|
|
|
|
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) runTcp() error {
|
|
|
|
func (s *NatClient) runTcp() error {
|
|
|
@ -87,6 +121,70 @@ func (s *NatClient) runTcp() error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) runUdp() error {
|
|
|
|
|
|
|
|
starlog.Noticeln("nat client udp module start run")
|
|
|
|
|
|
|
|
if s.UdpTimeout == 0 {
|
|
|
|
|
|
|
|
s.UdpTimeout = 600000
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-s.stopCtx.Done():
|
|
|
|
|
|
|
|
if s.cmdTCPConn != nil {
|
|
|
|
|
|
|
|
s.setUdpCmdConnAlived(false)
|
|
|
|
|
|
|
|
s.cmdUDPConn.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(time.Millisecond * 3000):
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.cmdUDPConn != nil && s.udpCmdConnAlived() {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rmt, err := net.ResolveUDPAddr("udp", s.CmdTarget)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Errorf("dail remote udp cmd server %v fail:%v;will retry\n", s.CmdTarget, err)
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
s.cmdUDPConn, err = net.DialUDP("udp", nil, rmt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Errorf("dail remote udp cmd server %v fail:%v;will retry\n", s.CmdTarget, err)
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
s.cmdTCPConn = nil
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
starlog.Infoln("dail remote udp cmd server ok,remote:", s.CmdTarget)
|
|
|
|
|
|
|
|
s.udpCmdConn().Write(MSG_CMD_HELLO)
|
|
|
|
|
|
|
|
s.setUdpCmdConnAlived(true)
|
|
|
|
|
|
|
|
go s.handleUdpCmdConn(s.udpCmdConn())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NatClient) handleUdpCmdConn(conn *net.UDPConn) {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
header := make([]byte, 16)
|
|
|
|
|
|
|
|
_, err := io.ReadFull(conn, header)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("udp cmd server read fail:", err)
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
s.setUdpCmdConnAlived(false)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(header, MSG_CMD_HELLO_REPLY) {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(header, MSG_NEW_CONN_HELLO) {
|
|
|
|
|
|
|
|
go s.newRemoteUdpConn()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Equal(header, MSG_HEARTBEAT) {
|
|
|
|
|
|
|
|
_, err = conn.Write(MSG_HEARTBEAT)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
s.setUdpCmdConnAlived(false)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) handleTcpCmdConn(conn net.Conn) {
|
|
|
|
func (s *NatClient) handleTcpCmdConn(conn net.Conn) {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
header := make([]byte, 16)
|
|
|
|
header := make([]byte, 16)
|
|
|
@ -125,14 +223,121 @@ func (s *NatClient) newRemoteTcpConn() {
|
|
|
|
_, err = nconn.Write(MSG_NEW_CONN_HELLO)
|
|
|
|
_, err = nconn.Write(MSG_NEW_CONN_HELLO)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
nconn.Close()
|
|
|
|
nconn.Close()
|
|
|
|
log.Errorf("write new client hello to server %v fail:%v\n", s.CmdTarget, err)
|
|
|
|
log.Errorf("write new tcp client hello to server %v fail:%v\n", s.CmdTarget, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cconn, err := net.DialTimeout("tcp", s.ServiceTarget, time.Millisecond*time.Duration(s.DialTimeout))
|
|
|
|
cconn, err := net.DialTimeout("tcp", s.ServiceTarget, time.Millisecond*time.Duration(s.DialTimeout))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("dail remote tcp conn %v fail:%v\n", s.CmdTarget, err)
|
|
|
|
log.Errorf("dail remote tcp conn %v fail:%v\n", s.CmdTarget, err)
|
|
|
|
|
|
|
|
nconn.Close()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go io.Copy(cconn, nconn)
|
|
|
|
go func() {
|
|
|
|
go io.Copy(nconn, cconn)
|
|
|
|
for {
|
|
|
|
|
|
|
|
data := make([]byte, 8192)
|
|
|
|
|
|
|
|
nconn.SetReadDeadline(time.Now().Add(time.Millisecond * time.Duration(s.UdpTimeout)))
|
|
|
|
|
|
|
|
n, err := nconn.Read(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("read from tcp server fail:", nconn.RemoteAddr(), err)
|
|
|
|
|
|
|
|
nconn.Close()
|
|
|
|
|
|
|
|
cconn.Close()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cconn.Write(data[:n])
|
|
|
|
|
|
|
|
//starlog.Debugln("write to udp client:", p, err, cconn.LocalAddr(), cconn.RemoteAddr())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("write to tcp client fail:", cconn.RemoteAddr(), err)
|
|
|
|
|
|
|
|
nconn.Close()
|
|
|
|
|
|
|
|
cconn.Close()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
data := make([]byte, 8192)
|
|
|
|
|
|
|
|
cconn.SetReadDeadline(time.Now().Add(time.Millisecond * time.Duration(s.UdpTimeout)))
|
|
|
|
|
|
|
|
n, err := cconn.Read(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("read from tcp server fail:", cconn.RemoteAddr(), err)
|
|
|
|
|
|
|
|
nconn.Close()
|
|
|
|
|
|
|
|
cconn.Close()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = nconn.Write(data[:n])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("write to tcp client fail:", nconn.RemoteAddr(), err)
|
|
|
|
|
|
|
|
nconn.Close()
|
|
|
|
|
|
|
|
cconn.Close()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *NatClient) newRemoteUdpConn() {
|
|
|
|
|
|
|
|
log := starlog.Std.NewFlag()
|
|
|
|
|
|
|
|
starlog.Infoln("recv request,create new udp conn")
|
|
|
|
|
|
|
|
rmt, err := net.ResolveUDPAddr("udp", s.CmdTarget)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Errorf("dail server udp conn %v fail:%v\n", s.CmdTarget, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
nconn, err := net.DialUDP("udp", nil, rmt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Errorf("dail server udp conn %v fail:%v\n", s.CmdTarget, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("dail server udp conn %v ok\n", s.CmdTarget)
|
|
|
|
|
|
|
|
_, err = nconn.Write(MSG_NEW_CONN_HELLO)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
nconn.Close()
|
|
|
|
|
|
|
|
log.Errorf("write new udp client hello to server %v fail:%v\n", s.CmdTarget, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rmt, err = net.ResolveUDPAddr("udp", s.ServiceTarget)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Errorf("dail server udp conn %v fail:%v\n", s.ServiceTarget, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cconn, err := net.DialUDP("udp", nil, rmt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Errorf("dail remote udp conn %v fail:%v\n", s.ServiceTarget, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("dail remote udp conn %v ok\n", s.ServiceTarget)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
data := make([]byte, 8192)
|
|
|
|
|
|
|
|
nconn.SetReadDeadline(time.Now().Add(time.Millisecond * time.Duration(s.UdpTimeout)))
|
|
|
|
|
|
|
|
n, err := nconn.Read(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("read from udp server fail:", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cconn.Write(data[:n])
|
|
|
|
|
|
|
|
//starlog.Debugln("write to udp client:", p, err, cconn.LocalAddr(), cconn.RemoteAddr())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("write to udp client fail:", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
data := make([]byte, 8192)
|
|
|
|
|
|
|
|
cconn.SetReadDeadline(time.Now().Add(time.Millisecond * time.Duration(s.UdpTimeout)))
|
|
|
|
|
|
|
|
n, err := cconn.Read(data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("read from udp server fail:", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = nconn.Write(data[:n])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
starlog.Infoln("write to udp client fail:", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|