Merge pull request #773 from fatedier/nat

return error quickly if nathole make error
This commit is contained in:
fatedier 2018-05-17 00:10:53 +08:00 committed by GitHub
commit 040d198e36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 8 deletions

View File

@ -272,6 +272,12 @@ func (pxy *XtcpProxy) InWorkConn(conn frpNet.Conn) {
} }
clientConn.SetReadDeadline(time.Time{}) clientConn.SetReadDeadline(time.Time{})
clientConn.Close() clientConn.Close()
if natHoleRespMsg.Error != "" {
pxy.Error("natHoleRespMsg get error info: %s", natHoleRespMsg.Error)
return
}
pxy.Trace("get natHoleRespMsg, sid [%s], client address [%s]", natHoleRespMsg.Sid, natHoleRespMsg.ClientAddr) pxy.Trace("get natHoleRespMsg, sid [%s], client address [%s]", natHoleRespMsg.Sid, natHoleRespMsg.ClientAddr)
// Send sid to visitor udp address. // Send sid to visitor udp address.

View File

@ -235,6 +235,11 @@ func (sv *XtcpVisitor) handleConn(userConn frpNet.Conn) {
visitorConn.SetReadDeadline(time.Time{}) visitorConn.SetReadDeadline(time.Time{})
pool.PutBuf(buf) pool.PutBuf(buf)
if natHoleRespMsg.Error != "" {
sv.Error("natHoleRespMsg get error info: %s", natHoleRespMsg.Error)
return
}
sv.Trace("get natHoleRespMsg, sid [%s], client address [%s]", natHoleRespMsg.Sid, natHoleRespMsg.ClientAddr) sv.Trace("get natHoleRespMsg, sid [%s], client address [%s]", natHoleRespMsg.Sid, natHoleRespMsg.ClientAddr)
// Close visitorConn, so we can use it's local address. // Close visitorConn, so we can use it's local address.

View File

@ -163,6 +163,7 @@ type NatHoleResp struct {
Sid string `json:"sid"` Sid string `json:"sid"`
VisitorAddr string `json:"visitor_addr"` VisitorAddr string `json:"visitor_addr"`
ClientAddr string `json:"client_addr"` ClientAddr string `json:"client_addr"`
Error string `json:"error"`
} }
type NatHoleSid struct { type NatHoleSid struct {

View File

@ -108,12 +108,16 @@ func (nc *NatHoleController) HandleVisitor(m *msg.NatHoleVisitor, raddr *net.UDP
clientCfg, ok := nc.clientCfgs[m.ProxyName] clientCfg, ok := nc.clientCfgs[m.ProxyName]
if !ok { if !ok {
nc.mu.Unlock() nc.mu.Unlock()
log.Debug("xtcp server for [%s] doesn't exist", m.ProxyName) errInfo := fmt.Sprintf("xtcp server for [%s] doesn't exist", m.ProxyName)
log.Debug(errInfo)
nc.listener.WriteToUDP(nc.GenNatHoleResponse(nil, errInfo), raddr)
return return
} }
if m.SignKey != util.GetAuthKey(clientCfg.Sk, m.Timestamp) { if m.SignKey != util.GetAuthKey(clientCfg.Sk, m.Timestamp) {
nc.mu.Unlock() nc.mu.Unlock()
log.Debug("xtcp connection of [%s] auth failed", m.ProxyName) errInfo := fmt.Sprintf("xtcp connection of [%s] auth failed", m.ProxyName)
log.Debug(errInfo)
nc.listener.WriteToUDP(nc.GenNatHoleResponse(nil, errInfo), raddr)
return return
} }
@ -137,7 +141,7 @@ func (nc *NatHoleController) HandleVisitor(m *msg.NatHoleVisitor, raddr *net.UDP
// Wait client connections. // Wait client connections.
select { select {
case <-session.NotifyCh: case <-session.NotifyCh:
resp := nc.GenNatHoleResponse(raddr, session) resp := nc.GenNatHoleResponse(session, "")
log.Trace("send nat hole response to visitor") log.Trace("send nat hole response to visitor")
nc.listener.WriteToUDP(resp, raddr) nc.listener.WriteToUDP(resp, raddr)
case <-time.After(time.Duration(NatHoleTimeout) * time.Second): case <-time.After(time.Duration(NatHoleTimeout) * time.Second):
@ -156,16 +160,27 @@ func (nc *NatHoleController) HandleClient(m *msg.NatHoleClient, raddr *net.UDPAd
session.ClientAddr = raddr session.ClientAddr = raddr
session.NotifyCh <- struct{}{} session.NotifyCh <- struct{}{}
resp := nc.GenNatHoleResponse(raddr, session) resp := nc.GenNatHoleResponse(session, "")
log.Trace("send nat hole response to client") log.Trace("send nat hole response to client")
nc.listener.WriteToUDP(resp, raddr) nc.listener.WriteToUDP(resp, raddr)
} }
func (nc *NatHoleController) GenNatHoleResponse(raddr *net.UDPAddr, session *NatHoleSession) []byte { func (nc *NatHoleController) GenNatHoleResponse(session *NatHoleSession, errInfo string) []byte {
var (
sid string
visitorAddr string
clientAddr string
)
if session != nil {
sid = session.Sid
visitorAddr = session.VisitorAddr.String()
clientAddr = session.ClientAddr.String()
}
m := &msg.NatHoleResp{ m := &msg.NatHoleResp{
Sid: session.Sid, Sid: sid,
VisitorAddr: session.VisitorAddr.String(), VisitorAddr: visitorAddr,
ClientAddr: session.ClientAddr.String(), ClientAddr: clientAddr,
Error: errInfo,
} }
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
err := msg.WriteMsg(b, m) err := msg.WriteMsg(b, m)