forked from netbirdio/netbird
/
main.go
57 lines (49 loc) · 1.11 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
package main
import (
"context"
"os"
"os/signal"
"github.com/FlintyLemming/netbird/sharedsock"
log "github.com/sirupsen/logrus"
)
func main() {
port := 51820
rawSock, err := sharedsock.Listen(port, sharedsock.NewIncomingSTUNFilter())
if err != nil {
panic(err)
}
log.Infof("attached to the raw socket on port %d", port)
ctx, cancel := context.WithCancel(context.Background())
// read packets
go func() {
buf := make([]byte, 1500)
for {
select {
case <-ctx.Done():
log.Debugf("stopped reading from the shared socket")
return
default:
size, addr, err := rawSock.ReadFrom(buf)
if err != nil {
log.Errorf("error while reading packet from the shared socket: %s", err)
continue
}
log.Infof("read a STUN packet of size %d from %s", size, addr.String())
}
}
}()
// terminate the program on ^C
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
log.Infof("received ^C signal, stopping the program")
cancel()
err = rawSock.Close()
if err != nil {
log.Errorf("failed closing raw socket")
}
}
}()
<-ctx.Done()
}