2022-10-26 13:20:09 +08:00
|
|
|
package stunserver
|
|
|
|
|
|
|
|
import (
|
2022-11-07 23:55:58 +08:00
|
|
|
"context"
|
2022-10-26 13:20:09 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2022-11-07 23:55:58 +08:00
|
|
|
"sync"
|
2022-10-26 13:20:09 +08:00
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
|
|
|
"github.com/gravitl/netmaker/servercfg"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gortc.io/stun"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server is RFC 5389 basic server implementation.
|
|
|
|
//
|
|
|
|
// Current implementation is UDP only and not utilizes FINGERPRINT mechanism,
|
|
|
|
// nor ALTERNATE-SERVER, nor credentials mechanisms. It does not support
|
|
|
|
// backwards compatibility with RFC 3489.
|
|
|
|
type Server struct {
|
2022-11-07 23:55:58 +08:00
|
|
|
Addr string
|
2022-10-26 13:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
software = stun.NewSoftware("netmaker-stun")
|
|
|
|
errNotSTUNMessage = errors.New("not stun message")
|
|
|
|
)
|
|
|
|
|
|
|
|
func basicProcess(addr net.Addr, b []byte, req, res *stun.Message) error {
|
|
|
|
if !stun.IsMessage(b) {
|
|
|
|
return errNotSTUNMessage
|
|
|
|
}
|
|
|
|
if _, err := req.Write(b); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to read message")
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
ip net.IP
|
|
|
|
port int
|
|
|
|
)
|
|
|
|
switch a := addr.(type) {
|
|
|
|
case *net.UDPAddr:
|
|
|
|
ip = a.IP
|
|
|
|
port = a.Port
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown addr: %v", addr))
|
|
|
|
}
|
|
|
|
return res.Build(req,
|
|
|
|
stun.BindingSuccess,
|
|
|
|
software,
|
|
|
|
&stun.XORMappedAddress{
|
|
|
|
IP: ip,
|
|
|
|
Port: port,
|
|
|
|
},
|
|
|
|
stun.Fingerprint,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-25 04:37:53 +08:00
|
|
|
func (s *Server) serveConn(c net.PacketConn, res, req *stun.Message, ctx context.Context) error {
|
2022-10-26 13:20:09 +08:00
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-25 04:37:53 +08:00
|
|
|
go func(ctx context.Context) {
|
|
|
|
<-ctx.Done()
|
|
|
|
if c != nil {
|
|
|
|
// kill connection on server shutdown
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}(ctx)
|
|
|
|
|
2022-10-26 13:20:09 +08:00
|
|
|
buf := make([]byte, 1024)
|
2023-02-25 04:37:53 +08:00
|
|
|
n, addr, err := c.ReadFrom(buf) // this be blocky af
|
2022-10-26 13:20:09 +08:00
|
|
|
if err != nil {
|
2023-02-25 04:37:53 +08:00
|
|
|
if !strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
logger.Log(1, "STUN read error:", err.Error())
|
|
|
|
}
|
2022-10-26 13:20:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-25 04:37:53 +08:00
|
|
|
|
2022-10-26 13:20:09 +08:00
|
|
|
if _, err = req.Write(buf[:n]); err != nil {
|
2023-02-25 04:37:53 +08:00
|
|
|
logger.Log(1, "STUN write error:", err.Error())
|
2022-10-26 13:20:09 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = basicProcess(addr, buf[:n], req, res); err != nil {
|
|
|
|
if err == errNotSTUNMessage {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-25 04:37:53 +08:00
|
|
|
logger.Log(1, "STUN process error:", err.Error())
|
2022-10-26 13:20:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err = c.WriteTo(res.Raw, addr)
|
|
|
|
if err != nil {
|
2023-02-25 04:37:53 +08:00
|
|
|
logger.Log(1, "STUN response write error", err.Error())
|
2022-10-26 13:20:09 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve reads packets from connections and responds to BINDING requests.
|
2023-02-25 04:37:53 +08:00
|
|
|
func (s *Server) serve(c net.PacketConn, ctx context.Context) error {
|
2022-10-26 13:20:09 +08:00
|
|
|
var (
|
|
|
|
res = new(stun.Message)
|
|
|
|
req = new(stun.Message)
|
|
|
|
)
|
|
|
|
for {
|
2022-11-07 23:55:58 +08:00
|
|
|
select {
|
2023-02-25 04:37:53 +08:00
|
|
|
case <-ctx.Done():
|
|
|
|
logger.Log(0, "shut down STUN server")
|
2022-11-07 23:55:58 +08:00
|
|
|
return nil
|
|
|
|
default:
|
2023-02-25 04:37:53 +08:00
|
|
|
if err := s.serveConn(c, res, req, ctx); err != nil {
|
2022-11-07 23:55:58 +08:00
|
|
|
logger.Log(1, "serve: %v", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res.Reset()
|
|
|
|
req.Reset()
|
2022-10-26 13:20:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 23:55:58 +08:00
|
|
|
// listenUDPAndServe listens on laddr and process incoming packets.
|
|
|
|
func listenUDPAndServe(ctx context.Context, serverNet, laddr string) error {
|
2022-10-26 13:20:09 +08:00
|
|
|
c, err := net.ListenPacket(serverNet, laddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s := &Server{
|
2022-11-07 23:55:58 +08:00
|
|
|
Addr: laddr,
|
2022-10-26 13:20:09 +08:00
|
|
|
}
|
2023-02-25 04:37:53 +08:00
|
|
|
return s.serve(c, ctx)
|
2022-10-26 13:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func normalize(address string) string {
|
|
|
|
if len(address) == 0 {
|
|
|
|
address = "0.0.0.0"
|
|
|
|
}
|
|
|
|
if !strings.Contains(address, ":") {
|
|
|
|
address = fmt.Sprintf("%s:%d", address, stun.DefaultPort)
|
|
|
|
}
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
2022-12-20 03:55:24 +08:00
|
|
|
// Start - starts the stun server
|
2023-02-25 04:37:53 +08:00
|
|
|
func Start(wg *sync.WaitGroup, ctx context.Context) {
|
|
|
|
defer wg.Done()
|
2022-12-12 13:03:35 +08:00
|
|
|
normalized := normalize(fmt.Sprintf("0.0.0.0:%d", servercfg.GetStunPort()))
|
2022-10-26 13:20:09 +08:00
|
|
|
logger.Log(0, "netmaker-stun listening on", normalized, "via udp")
|
2023-02-25 04:37:53 +08:00
|
|
|
if err := listenUDPAndServe(ctx, "udp", normalized); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "closed network connection") {
|
|
|
|
logger.Log(0, "shutdown STUN server")
|
|
|
|
} else {
|
|
|
|
logger.Log(0, "server: ", err.Error())
|
|
|
|
}
|
2022-10-26 13:20:09 +08:00
|
|
|
}
|
|
|
|
}
|