Skip to content

Commit

Permalink
return error quickly if nathole make error
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed May 16, 2018
1 parent ea79e03 commit 1a6cbbb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
6 changes: 6 additions & 0 deletions client/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ func (pxy *XtcpProxy) InWorkConn(conn frpNet.Conn) {
}
clientConn.SetReadDeadline(time.Time{})
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)

// Send sid to visitor udp address.
Expand Down
5 changes: 5 additions & 0 deletions client/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ func (sv *XtcpVisitor) handleConn(userConn frpNet.Conn) {
visitorConn.SetReadDeadline(time.Time{})
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)

// Close visitorConn, so we can use it's local address.
Expand Down
1 change: 1 addition & 0 deletions models/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ type NatHoleResp struct {
Sid string `json:"sid"`
VisitorAddr string `json:"visitor_addr"`
ClientAddr string `json:"client_addr"`
Error string `json:"error"`
}

type NatHoleSid struct {
Expand Down
31 changes: 23 additions & 8 deletions server/nathole.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,16 @@ func (nc *NatHoleController) HandleVisitor(m *msg.NatHoleVisitor, raddr *net.UDP
clientCfg, ok := nc.clientCfgs[m.ProxyName]
if !ok {
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
}
if m.SignKey != util.GetAuthKey(clientCfg.Sk, m.Timestamp) {
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
}

Expand All @@ -137,7 +141,7 @@ func (nc *NatHoleController) HandleVisitor(m *msg.NatHoleVisitor, raddr *net.UDP
// Wait client connections.
select {
case <-session.NotifyCh:
resp := nc.GenNatHoleResponse(raddr, session)
resp := nc.GenNatHoleResponse(session, "")
log.Trace("send nat hole response to visitor")
nc.listener.WriteToUDP(resp, raddr)
case <-time.After(time.Duration(NatHoleTimeout) * time.Second):
Expand All @@ -156,16 +160,27 @@ func (nc *NatHoleController) HandleClient(m *msg.NatHoleClient, raddr *net.UDPAd
session.ClientAddr = raddr
session.NotifyCh <- struct{}{}

resp := nc.GenNatHoleResponse(raddr, session)
resp := nc.GenNatHoleResponse(session, "")
log.Trace("send nat hole response to client")
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{
Sid: session.Sid,
VisitorAddr: session.VisitorAddr.String(),
ClientAddr: session.ClientAddr.String(),
Sid: sid,
VisitorAddr: visitorAddr,
ClientAddr: clientAddr,
Error: errInfo,
}
b := bytes.NewBuffer(nil)
err := msg.WriteMsg(b, m)
Expand Down

0 comments on commit 1a6cbbb

Please sign in to comment.