frp/models/nathole/nathole.go

213 lines
4.5 KiB
Go
Raw Normal View History

2019-01-14 17:11:08 +01:00
package nathole
2017-10-24 12:20:07 +02:00
import (
"bytes"
"fmt"
"net"
"sync"
"time"
"github.com/fatedier/frp/models/msg"
"github.com/fatedier/frp/utils/log"
"github.com/fatedier/frp/utils/util"
2018-05-07 20:13:30 +02:00
"github.com/fatedier/golib/errors"
"github.com/fatedier/golib/pool"
2017-10-24 12:20:07 +02:00
)
// Timeout seconds.
var NatHoleTimeout int64 = 10
2019-03-11 08:53:58 +01:00
type SidRequest struct {
Sid string
NotifyCh chan struct{}
}
2017-10-24 12:20:07 +02:00
type NatHoleController struct {
listener *net.UDPConn
clientCfgs map[string]*NatHoleClientCfg
sessions map[string]*NatHoleSession
mu sync.RWMutex
}
func NewNatHoleController(udpBindAddr string) (nc *NatHoleController, err error) {
addr, err := net.ResolveUDPAddr("udp", udpBindAddr)
if err != nil {
return nil, err
}
lconn, err := net.ListenUDP("udp", addr)
if err != nil {
return nil, err
}
nc = &NatHoleController{
listener: lconn,
clientCfgs: make(map[string]*NatHoleClientCfg),
sessions: make(map[string]*NatHoleSession),
}
return nc, nil
}
2019-03-11 08:53:58 +01:00
func (nc *NatHoleController) ListenClient(name string, sk string) (sidCh chan *SidRequest) {
2017-10-24 12:20:07 +02:00
clientCfg := &NatHoleClientCfg{
Name: name,
Sk: sk,
2019-03-11 08:53:58 +01:00
SidCh: make(chan *SidRequest),
2017-10-24 12:20:07 +02:00
}
nc.mu.Lock()
nc.clientCfgs[name] = clientCfg
nc.mu.Unlock()
return clientCfg.SidCh
}
func (nc *NatHoleController) CloseClient(name string) {
nc.mu.Lock()
defer nc.mu.Unlock()
delete(nc.clientCfgs, name)
}
func (nc *NatHoleController) Run() {
for {
buf := pool.GetBuf(1024)
n, raddr, err := nc.listener.ReadFromUDP(buf)
if err != nil {
log.Trace("nat hole listener read from udp error: %v", err)
return
}
rd := bytes.NewReader(buf[:n])
rawMsg, err := msg.ReadMsg(rd)
if err != nil {
log.Trace("read nat hole message error: %v", err)
continue
}
switch m := rawMsg.(type) {
2017-12-04 18:34:33 +01:00
case *msg.NatHoleVisitor:
go nc.HandleVisitor(m, raddr)
2017-10-24 12:20:07 +02:00
case *msg.NatHoleClient:
go nc.HandleClient(m, raddr)
default:
log.Trace("error nat hole message type")
continue
}
pool.PutBuf(buf)
}
}
func (nc *NatHoleController) GenSid() string {
t := time.Now().Unix()
id, _ := util.RandId()
return fmt.Sprintf("%d%s", t, id)
}
2017-12-04 18:34:33 +01:00
func (nc *NatHoleController) HandleVisitor(m *msg.NatHoleVisitor, raddr *net.UDPAddr) {
2017-10-24 12:20:07 +02:00
sid := nc.GenSid()
session := &NatHoleSession{
2017-12-04 18:34:33 +01:00
Sid: sid,
VisitorAddr: raddr,
NotifyCh: make(chan struct{}, 0),
2017-10-24 12:20:07 +02:00
}
nc.mu.Lock()
clientCfg, ok := nc.clientCfgs[m.ProxyName]
if !ok {
2017-10-24 12:20:07 +02:00
nc.mu.Unlock()
errInfo := fmt.Sprintf("xtcp server for [%s] doesn't exist", m.ProxyName)
log.Debug(errInfo)
nc.listener.WriteToUDP(nc.GenNatHoleResponse(nil, errInfo), raddr)
2017-10-24 12:20:07 +02:00
return
}
if m.SignKey != util.GetAuthKey(clientCfg.Sk, m.Timestamp) {
nc.mu.Unlock()
errInfo := fmt.Sprintf("xtcp connection of [%s] auth failed", m.ProxyName)
log.Debug(errInfo)
nc.listener.WriteToUDP(nc.GenNatHoleResponse(nil, errInfo), raddr)
return
}
2017-10-24 12:20:07 +02:00
nc.sessions[sid] = session
nc.mu.Unlock()
2017-12-04 18:34:33 +01:00
log.Trace("handle visitor message, sid [%s]", sid)
2017-10-24 12:20:07 +02:00
defer func() {
nc.mu.Lock()
delete(nc.sessions, sid)
nc.mu.Unlock()
}()
err := errors.PanicToError(func() {
2019-03-11 08:53:58 +01:00
clientCfg.SidCh <- &SidRequest{
Sid: sid,
NotifyCh: session.NotifyCh,
}
2017-10-24 12:20:07 +02:00
})
if err != nil {
return
}
// Wait client connections.
select {
case <-session.NotifyCh:
resp := nc.GenNatHoleResponse(session, "")
2017-12-04 18:34:33 +01:00
log.Trace("send nat hole response to visitor")
2017-10-24 12:20:07 +02:00
nc.listener.WriteToUDP(resp, raddr)
case <-time.After(time.Duration(NatHoleTimeout) * time.Second):
return
}
}
func (nc *NatHoleController) HandleClient(m *msg.NatHoleClient, raddr *net.UDPAddr) {
nc.mu.RLock()
session, ok := nc.sessions[m.Sid]
nc.mu.RUnlock()
if !ok {
return
}
log.Trace("handle client message, sid [%s]", session.Sid)
session.ClientAddr = raddr
resp := nc.GenNatHoleResponse(session, "")
2017-10-24 12:20:07 +02:00
log.Trace("send nat hole response to client")
nc.listener.WriteToUDP(resp, raddr)
}
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()
}
2017-10-24 12:20:07 +02:00
m := &msg.NatHoleResp{
Sid: sid,
VisitorAddr: visitorAddr,
ClientAddr: clientAddr,
Error: errInfo,
2017-10-24 12:20:07 +02:00
}
b := bytes.NewBuffer(nil)
err := msg.WriteMsg(b, m)
if err != nil {
return []byte("")
}
return b.Bytes()
}
type NatHoleSession struct {
2017-12-04 18:34:33 +01:00
Sid string
VisitorAddr *net.UDPAddr
ClientAddr *net.UDPAddr
2017-10-24 12:20:07 +02:00
NotifyCh chan struct{}
}
type NatHoleClientCfg struct {
Name string
Sk string
2019-03-11 08:53:58 +01:00
SidCh chan *SidRequest
2017-10-24 12:20:07 +02:00
}