-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
uri.go
167 lines (143 loc) · 3.8 KB
/
uri.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
159
160
161
162
163
164
165
166
167
package stunner
import (
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
"github.com/l7mp/stunner/internal/util"
stnrv1 "github.com/l7mp/stunner/pkg/apis/v1"
)
// StunnerUri is the specification of a STUNner listener URI
type StunnerUri struct {
Protocol, Address, Username, Password string
Port int
Addr net.Addr
}
// ParseUri parses a STUN/TURN server URI, e.g., "turn://user1:[email protected]:3478?transport=udp"
func ParseUri(uri string) (*StunnerUri, error) {
s := StunnerUri{}
// handle stdin/out
if uri == "-" || uri == "file://-" {
s.Protocol = "file"
// make turncat conf happy
s.Port = 1
s.Addr = &util.FileConnAddr{File: os.Stdin}
return &s, nil
}
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("invalid URI '%s': %s", uri, err)
}
s.Address = u.Hostname()
s.Username = u.User.Username()
password, found := u.User.Password()
if found {
s.Password = password
}
proto, err := getStunnerProtoForURI(u)
if err != nil {
return nil, err
}
s.Protocol = proto
switch strings.ToLower(proto) {
case "udp", "udp4", "udp6", "dtls", "turn-udp", "turn-dtls":
a, err := net.ResolveUDPAddr("udp", s.Address+":"+u.Port())
if err != nil {
return nil, err
}
s.Addr = a
case "tcp", "tcp4", "tcp6", "tls", "turn-tcp", "turn-tls":
a, err := net.ResolveTCPAddr("tcp", s.Address+":"+u.Port())
if err != nil {
return nil, err
}
s.Addr = a
case "ip":
a, err := net.ResolveIPAddr("ip", s.Address+":"+u.Port())
if err != nil {
return nil, err
}
s.Addr = a
case "unix", "unixgram", "unixpacket":
a, err := net.ResolveUnixAddr("unix", s.Address)
if err != nil {
return nil, err
}
s.Addr = a
default:
return nil, fmt.Errorf("invalid protocol: %s", proto)
}
defaultPort := 3478
if strings.ToLower(proto) == "turn-tls" || strings.ToLower(proto) == "turn-dtls" {
defaultPort = 443
}
port, err := strconv.Atoi(u.Port())
if err != nil {
port = defaultPort
}
s.Port = port
return &s, nil
}
func (u *StunnerUri) String() string {
req := stnrv1.ListenerConfig{
Protocol: u.Protocol,
PublicAddr: u.Address,
PublicPort: u.Port,
}
uri, err := GetStandardURLFromListener(&req)
if err != nil {
return ""
}
return uri
}
// GetUriFromListener returns a standard TURN URI as per RFC7065from a listener config.
func GetUriFromListener(req *stnrv1.ListenerConfig) (string, error) {
return req.GetListenerURI(true)
}
// GetStandardURLFromListener returns a standard URL (that can be parsed using net/url) from a listener config.
func GetStandardURLFromListener(req *stnrv1.ListenerConfig) (string, error) {
return req.GetListenerURI(false)
}
// GetUriFromListener returns a standard TURN URI from a listener config
func GetTurnUris(req *stnrv1.StunnerConfig) ([]string, error) {
ret := []string{}
for i := range req.Listeners {
uri, err := GetUriFromListener(&req.Listeners[i])
if err != nil {
return []string{}, err
}
ret = append(ret, uri)
}
return ret, nil
}
func getStunnerProtoForURI(u *url.URL) (string, error) {
scheme := strings.ToLower(u.Scheme)
if scheme == "" {
scheme = "turn"
}
proto := "udp"
q := u.Query()
if len(q["transport"]) > 0 {
proto = strings.ToLower(q["transport"][0])
}
// fully specified protocol names (ignore "turns" scheme for compatibility)
switch proto {
case "tls":
return "TURN-TLS", nil
case "dtls":
return "TURN-DTLS", nil
}
// using RFC7065 compatible URIs
if scheme == "turn" && proto == "udp" {
return "TURN-UDP", nil
} else if scheme == "turn" && proto == "tcp" {
return "TURN-TCP", nil
} else if scheme == "turns" && proto == "udp" {
return "TURN-DTLS", nil
} else if scheme == "turns" && proto == "tcp" {
return "TURN-TLS", nil
}
return "", fmt.Errorf("invalid scheme/protocol in URI %q", u.String())
}