This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
158 lines (145 loc) · 3.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gortc.io/stun"
)
var (
network = flag.String("net", "udp", "network to listen")
address = flag.String("addr", "0.0.0.0:3478", "address to listen")
profile = flag.Bool("profile", false, "profile")
)
// 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 {
Addr string
LogAllErrors bool
log Logger
}
// Logger is used for logging formatted messages.
type Logger interface {
// Printf must have the same semantics as log.Printf.
Printf(format string, args ...interface{})
}
var (
defaultLogger = logrus.New()
software = stun.NewSoftware("gortc/stund")
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,
)
}
func (s *Server) serveConn(c net.PacketConn, res, req *stun.Message) error {
if c == nil {
return nil
}
buf := make([]byte, 1024)
n, addr, err := c.ReadFrom(buf)
if err != nil {
s.log.Printf("ReadFrom: %v", err)
return nil
}
// s.log().Printf("read %d bytes from %s", n, addr)
if _, err = req.Write(buf[:n]); err != nil {
s.log.Printf("Write: %v", err)
return err
}
if err = basicProcess(addr, buf[:n], req, res); err != nil {
if err == errNotSTUNMessage {
return nil
}
s.log.Printf("basicProcess: %v", err)
return nil
}
_, err = c.WriteTo(res.Raw, addr)
if err != nil {
s.log.Printf("WriteTo: %v", err)
}
return err
}
// Serve reads packets from connections and responds to BINDING requests.
func (s *Server) Serve(c net.PacketConn) error {
var (
res = new(stun.Message)
req = new(stun.Message)
)
for {
if err := s.serveConn(c, res, req); err != nil {
s.log.Printf("serve: %v", err)
return err
}
res.Reset()
req.Reset()
}
}
// ListenUDPAndServe listens on laddr and process incoming packets.
func ListenUDPAndServe(serverNet, laddr string) error {
c, err := net.ListenPacket(serverNet, laddr)
if err != nil {
return err
}
s := &Server{
log: defaultLogger,
}
return s.Serve(c)
}
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
}
func main() {
flag.Parse()
if *profile {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
switch *network {
case "udp":
normalized := normalize(*address)
fmt.Println("gortc/stund listening on", normalized, "via", *network)
log.Fatal(ListenUDPAndServe(*network, normalized))
default:
log.Fatalln("unsupported network:", *network)
}
}