-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
67 lines (59 loc) · 1.44 KB
/
endpoint.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
package endpoint
import (
"fmt"
"net"
"net/netip"
"github.com/pion/webrtc/v3"
"golang.zx2c4.com/wireguard/conn"
)
type Sender interface {
Send(buf []byte) error
}
type baseEndpoint struct {
id string
}
var _ conn.Endpoint = (*baseEndpoint)(nil)
// used for mac2 cookie calculations
func (ep *baseEndpoint) DstToBytes() []byte {
return []byte(ep.id)
}
func (ep *baseEndpoint) DstToString() string { return getPCRemote(nil) } // returns the destination address (ip:port)
func (*baseEndpoint) ClearSrc() {} // clears the source address
func (*baseEndpoint) SrcToString() string { return "" } // returns the local source address (ip:port)
func (*baseEndpoint) DstIP() netip.Addr { return netip.Addr{} }
func (*baseEndpoint) SrcIP() netip.Addr { return netip.Addr{} }
func getPCRemote(pc *webrtc.PeerConnection) (addr string) {
addr = "[fdd9:f800::]:80"
if pc == nil {
return
}
sctp := pc.SCTP()
if sctp == nil {
return
}
dtls := sctp.Transport()
if dtls == nil {
return
}
ice := dtls.ICETransport()
if ice == nil {
return
}
pair, err := ice.GetSelectedCandidatePair()
if err != nil {
return "[fdd9:f800::1]:80"
}
if pair == nil {
return
}
remote := pair.Remote
if remote == nil {
return "[fdd9:f800::2]:80"
}
ip := net.ParseIP(remote.Address)
isv4 := ip.To4() != nil
if isv4 {
return fmt.Sprintf("%s:%d", remote.Address, remote.Port)
}
return fmt.Sprintf("[%s]:%d", remote.Address, remote.Port)
}