mirror of https://github.com/fatedier/frp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
4.4 KiB
213 lines
4.4 KiB
6 years ago
|
package nathole
|
||
7 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
4 years ago
|
"github.com/fatedier/frp/pkg/msg"
|
||
|
"github.com/fatedier/frp/pkg/util/log"
|
||
|
"github.com/fatedier/frp/pkg/util/util"
|
||
7 years ago
|
|
||
|
"github.com/fatedier/golib/errors"
|
||
|
"github.com/fatedier/golib/pool"
|
||
7 years ago
|
)
|
||
|
|
||
|
// Timeout seconds.
|
||
|
var NatHoleTimeout int64 = 10
|
||
|
|
||
6 years ago
|
type SidRequest struct {
|
||
|
Sid string
|
||
|
NotifyCh chan struct{}
|
||
|
}
|
||
|
|
||
5 years ago
|
type Controller struct {
|
||
7 years ago
|
listener *net.UDPConn
|
||
|
|
||
5 years ago
|
clientCfgs map[string]*ClientCfg
|
||
|
sessions map[string]*Session
|
||
7 years ago
|
|
||
|
mu sync.RWMutex
|
||
|
}
|
||
|
|
||
5 years ago
|
func NewController(udpBindAddr string) (nc *Controller, err error) {
|
||
7 years ago
|
addr, err := net.ResolveUDPAddr("udp", udpBindAddr)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
lconn, err := net.ListenUDP("udp", addr)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 years ago
|
nc = &Controller{
|
||
7 years ago
|
listener: lconn,
|
||
5 years ago
|
clientCfgs: make(map[string]*ClientCfg),
|
||
|
sessions: make(map[string]*Session),
|
||
7 years ago
|
}
|
||
|
return nc, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
func (nc *Controller) ListenClient(name string, sk string) (sidCh chan *SidRequest) {
|
||
|
clientCfg := &ClientCfg{
|
||
7 years ago
|
Name: name,
|
||
|
Sk: sk,
|
||
6 years ago
|
SidCh: make(chan *SidRequest),
|
||
7 years ago
|
}
|
||
|
nc.mu.Lock()
|
||
|
nc.clientCfgs[name] = clientCfg
|
||
|
nc.mu.Unlock()
|
||
|
return clientCfg.SidCh
|
||
|
}
|
||
|
|
||
5 years ago
|
func (nc *Controller) CloseClient(name string) {
|
||
7 years ago
|
nc.mu.Lock()
|
||
|
defer nc.mu.Unlock()
|
||
|
delete(nc.clientCfgs, name)
|
||
|
}
|
||
|
|
||
5 years ago
|
func (nc *Controller) Run() {
|
||
7 years ago
|
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) {
|
||
7 years ago
|
case *msg.NatHoleVisitor:
|
||
|
go nc.HandleVisitor(m, raddr)
|
||
7 years ago
|
case *msg.NatHoleClient:
|
||
|
go nc.HandleClient(m, raddr)
|
||
|
default:
|
||
|
log.Trace("error nat hole message type")
|
||
|
continue
|
||
|
}
|
||
|
pool.PutBuf(buf)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func (nc *Controller) GenSid() string {
|
||
7 years ago
|
t := time.Now().Unix()
|
||
5 years ago
|
id, _ := util.RandID()
|
||
7 years ago
|
return fmt.Sprintf("%d%s", t, id)
|
||
|
}
|
||
|
|
||
5 years ago
|
func (nc *Controller) HandleVisitor(m *msg.NatHoleVisitor, raddr *net.UDPAddr) {
|
||
7 years ago
|
sid := nc.GenSid()
|
||
5 years ago
|
session := &Session{
|
||
7 years ago
|
Sid: sid,
|
||
|
VisitorAddr: raddr,
|
||
|
NotifyCh: make(chan struct{}, 0),
|
||
7 years ago
|
}
|
||
|
nc.mu.Lock()
|
||
|
clientCfg, ok := nc.clientCfgs[m.ProxyName]
|
||
7 years ago
|
if !ok {
|
||
7 years ago
|
nc.mu.Unlock()
|
||
7 years ago
|
errInfo := fmt.Sprintf("xtcp server for [%s] doesn't exist", m.ProxyName)
|
||
|
log.Debug(errInfo)
|
||
|
nc.listener.WriteToUDP(nc.GenNatHoleResponse(nil, errInfo), raddr)
|
||
7 years ago
|
return
|
||
|
}
|
||
7 years ago
|
if m.SignKey != util.GetAuthKey(clientCfg.Sk, m.Timestamp) {
|
||
|
nc.mu.Unlock()
|
||
7 years ago
|
errInfo := fmt.Sprintf("xtcp connection of [%s] auth failed", m.ProxyName)
|
||
|
log.Debug(errInfo)
|
||
|
nc.listener.WriteToUDP(nc.GenNatHoleResponse(nil, errInfo), raddr)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
nc.sessions[sid] = session
|
||
|
nc.mu.Unlock()
|
||
7 years ago
|
log.Trace("handle visitor message, sid [%s]", sid)
|
||
7 years ago
|
|
||
|
defer func() {
|
||
|
nc.mu.Lock()
|
||
|
delete(nc.sessions, sid)
|
||
|
nc.mu.Unlock()
|
||
|
}()
|
||
|
|
||
|
err := errors.PanicToError(func() {
|
||
6 years ago
|
clientCfg.SidCh <- &SidRequest{
|
||
|
Sid: sid,
|
||
|
NotifyCh: session.NotifyCh,
|
||
|
}
|
||
7 years ago
|
})
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Wait client connections.
|
||
|
select {
|
||
|
case <-session.NotifyCh:
|
||
7 years ago
|
resp := nc.GenNatHoleResponse(session, "")
|
||
7 years ago
|
log.Trace("send nat hole response to visitor")
|
||
7 years ago
|
nc.listener.WriteToUDP(resp, raddr)
|
||
|
case <-time.After(time.Duration(NatHoleTimeout) * time.Second):
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func (nc *Controller) HandleClient(m *msg.NatHoleClient, raddr *net.UDPAddr) {
|
||
7 years ago
|
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
|
||
|
|
||
7 years ago
|
resp := nc.GenNatHoleResponse(session, "")
|
||
7 years ago
|
log.Trace("send nat hole response to client")
|
||
|
nc.listener.WriteToUDP(resp, raddr)
|
||
|
}
|
||
|
|
||
5 years ago
|
func (nc *Controller) GenNatHoleResponse(session *Session, errInfo string) []byte {
|
||
7 years ago
|
var (
|
||
|
sid string
|
||
|
visitorAddr string
|
||
|
clientAddr string
|
||
|
)
|
||
|
if session != nil {
|
||
|
sid = session.Sid
|
||
|
visitorAddr = session.VisitorAddr.String()
|
||
|
clientAddr = session.ClientAddr.String()
|
||
|
}
|
||
7 years ago
|
m := &msg.NatHoleResp{
|
||
7 years ago
|
Sid: sid,
|
||
|
VisitorAddr: visitorAddr,
|
||
|
ClientAddr: clientAddr,
|
||
|
Error: errInfo,
|
||
7 years ago
|
}
|
||
|
b := bytes.NewBuffer(nil)
|
||
|
err := msg.WriteMsg(b, m)
|
||
|
if err != nil {
|
||
|
return []byte("")
|
||
|
}
|
||
|
return b.Bytes()
|
||
|
}
|
||
|
|
||
5 years ago
|
type Session struct {
|
||
7 years ago
|
Sid string
|
||
|
VisitorAddr *net.UDPAddr
|
||
|
ClientAddr *net.UDPAddr
|
||
7 years ago
|
|
||
|
NotifyCh chan struct{}
|
||
|
}
|
||
|
|
||
5 years ago
|
type ClientCfg struct {
|
||
7 years ago
|
Name string
|
||
|
Sk string
|
||
6 years ago
|
SidCh chan *SidRequest
|
||
7 years ago
|
}
|