-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
103 lines (85 loc) · 2.52 KB
/
client.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
package turn
import (
"fmt"
"net"
"sync"
"github.com/pion/logging"
"github.com/pion/stun"
"github.com/pion/transport/vnet"
"github.com/pkg/errors"
)
// ClientConfig is a bag of config parameters for Client.
type ClientConfig struct {
ListeningAddress string
LoggerFactory logging.LoggerFactory
Net *vnet.Net
Software *stun.Software
Sender Sender
}
// Client is a STUN server client
type Client struct {
conn net.PacketConn
mux sync.Mutex
net *vnet.Net
log logging.LeveledLogger
software *stun.Software
sender Sender
}
// NewClient returns a new Client instance. listeningAddress is the address and port to listen on, default "0.0.0.0:0"
func NewClient(config *ClientConfig) (*Client, error) {
log := config.LoggerFactory.NewLogger("turnc")
network := "udp4"
if config.Net == nil {
config.Net = vnet.NewNet(nil) // defaults to native operation
} else {
log.Warn("vnet is enabled")
}
if config.Sender == nil {
config.Sender = defaultBuildAndSend
}
c := &Client{
net: config.Net,
log: log,
software: config.Software,
sender: config.Sender,
}
var err error
c.conn, err = c.net.ListenPacket(network, config.ListeningAddress)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to listen on %s", config.ListeningAddress))
}
return c, nil
}
// SendSTUNRequest sends a new STUN request to the serverIP with serverPort
func (c *Client) SendSTUNRequest(serverIP net.IP, serverPort int) (net.Addr, error) {
c.mux.Lock()
defer c.mux.Unlock()
c.log.Debug("sending STUN request")
attrs := []stun.Setter{stun.TransactionID, stun.BindingRequest}
if c.software != nil {
attrs = append(attrs, *c.software)
}
if err := c.sender(c.conn, &net.UDPAddr{IP: serverIP, Port: serverPort}, attrs...); err != nil {
return nil, err
}
packet := make([]byte, 1500)
c.log.Debug("wait for STUN response...")
size, _, err := c.conn.ReadFrom(packet)
if err != nil {
return nil, errors.Wrap(err, "failed to read packet from udp socket")
}
c.log.Debugf("received %d bytes of STUN response", size)
resp := &stun.Message{Raw: append([]byte{}, packet[:size]...)}
if err := resp.Decode(); err != nil {
return nil, errors.Wrap(err, "failed to handle reply")
}
var reflAddr stun.XORMappedAddress
if err := reflAddr.GetFrom(resp); err != nil {
return nil, err
}
//return fmt.Sprintf("pkt_size=%d src_addr=%s refl_addr=%s:%d", size, addr, reflAddr.IP, reflAddr.Port), nil
return &net.UDPAddr{
IP: reflAddr.IP,
Port: reflAddr.Port,
}, nil
}