|
|
|
@ -20,13 +20,9 @@ import (
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/g"
|
|
|
|
|
"github.com/fatedier/frp/models/config"
|
|
|
|
|
"github.com/fatedier/frp/models/msg"
|
|
|
|
@ -251,42 +247,31 @@ func (sv *XtcpVisitor) handleConn(userConn frpNet.Conn) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sv.Trace("get natHoleRespMsg, sid [%s], client address [%s]", natHoleRespMsg.Sid, natHoleRespMsg.ClientAddr)
|
|
|
|
|
sv.Trace("get natHoleRespMsg, sid [%s], client address [%s], visitor address [%s]", natHoleRespMsg.Sid, natHoleRespMsg.ClientAddr, natHoleRespMsg.VisitorAddr)
|
|
|
|
|
|
|
|
|
|
// Close visitorConn, so we can use it's local address.
|
|
|
|
|
visitorConn.Close()
|
|
|
|
|
|
|
|
|
|
// Send detect message.
|
|
|
|
|
array := strings.Split(natHoleRespMsg.ClientAddr, ":")
|
|
|
|
|
if len(array) <= 1 {
|
|
|
|
|
sv.Error("get natHoleResp client address error: %s", natHoleRespMsg.ClientAddr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// send sid message to client
|
|
|
|
|
laddr, _ := net.ResolveUDPAddr("udp", visitorConn.LocalAddr().String())
|
|
|
|
|
/*
|
|
|
|
|
for i := 1000; i < 65000; i++ {
|
|
|
|
|
sv.sendDetectMsg(array[0], int64(i), laddr, "a")
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
port, err := strconv.ParseInt(array[1], 10, 64)
|
|
|
|
|
daddr, err := net.ResolveUDPAddr("udp", natHoleRespMsg.ClientAddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
sv.Error("get natHoleResp client address error: %s", natHoleRespMsg.ClientAddr)
|
|
|
|
|
sv.Error("resolve client udp address error: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sv.sendDetectMsg(array[0], int(port), laddr, []byte(natHoleRespMsg.Sid))
|
|
|
|
|
sv.Trace("send all detect msg done")
|
|
|
|
|
|
|
|
|
|
// Listen for visitorConn's address and wait for client connection.
|
|
|
|
|
lConn, err := net.ListenUDP("udp", laddr)
|
|
|
|
|
lConn, err := net.DialUDP("udp", laddr, daddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
sv.Error("listen on visitorConn's local adress error: %v", err)
|
|
|
|
|
sv.Error("dial client udp address error: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer lConn.Close()
|
|
|
|
|
|
|
|
|
|
lConn.SetReadDeadline(time.Now().Add(5 * time.Second))
|
|
|
|
|
lConn.Write([]byte(natHoleRespMsg.Sid))
|
|
|
|
|
|
|
|
|
|
// read ack sid from client
|
|
|
|
|
sidBuf := pool.GetBuf(1024)
|
|
|
|
|
n, _, err = lConn.ReadFromUDP(sidBuf)
|
|
|
|
|
lConn.SetReadDeadline(time.Now().Add(8 * time.Second))
|
|
|
|
|
n, err = lConn.Read(sidBuf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
sv.Warn("get sid from client error: %v", err)
|
|
|
|
|
return
|
|
|
|
@ -296,11 +281,13 @@ func (sv *XtcpVisitor) handleConn(userConn frpNet.Conn) {
|
|
|
|
|
sv.Warn("incorrect sid from client")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sv.Info("nat hole connection make success, sid [%s]", string(sidBuf[:n]))
|
|
|
|
|
pool.PutBuf(sidBuf)
|
|
|
|
|
|
|
|
|
|
sv.Info("nat hole connection make success, sid [%s]", natHoleRespMsg.Sid)
|
|
|
|
|
|
|
|
|
|
// wrap kcp connection
|
|
|
|
|
var remote io.ReadWriteCloser
|
|
|
|
|
remote, err = frpNet.NewKcpConnFromUdp(lConn, false, natHoleRespMsg.ClientAddr)
|
|
|
|
|
remote, err = frpNet.NewKcpConnFromUdp(lConn, true, natHoleRespMsg.ClientAddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
sv.Error("create kcp connection from udp connection error: %v", err)
|
|
|
|
|
return
|
|
|
|
@ -336,22 +323,3 @@ func (sv *XtcpVisitor) handleConn(userConn frpNet.Conn) {
|
|
|
|
|
frpIo.Join(userConn, muxConn)
|
|
|
|
|
sv.Debug("join connections closed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sv *XtcpVisitor) sendDetectMsg(addr string, port int, laddr *net.UDPAddr, content []byte) (err error) {
|
|
|
|
|
daddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", addr, port))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tConn, err := net.DialUDP("udp", laddr, daddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uConn := ipv4.NewConn(tConn)
|
|
|
|
|
uConn.SetTTL(3)
|
|
|
|
|
|
|
|
|
|
tConn.Write(content)
|
|
|
|
|
tConn.Close()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|