forked from ttacon/uri
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefault_ports.go
More file actions
133 lines (126 loc) · 2.62 KB
/
default_ports.go
File metadata and controls
133 lines (126 loc) · 2.62 KB
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
package uri
import (
"strconv"
"strings"
)
// IsDefaultPort reports whether the port is specified and is different from
// the default port defined for this scheme (if any).
//
// For example, a URI like http://host:8080 would return false, since 80 is the default http port.
func (u uri) IsDefaultPort() bool {
if len(u.authority.port) == 0 {
return true
}
portNum, _ := strconv.ParseUint(u.authority.port, 10, 64)
return defaultPortForScheme(strings.ToLower(u.scheme)) == portNum
}
// DefaultPort returns the default standardized port for the scheme of this URI,
// or zero if no such default is known.
//
// For example, for scheme "https", the default port is 443.
func (u uri) DefaultPort() int {
return int(defaultPortForScheme(strings.ToLower(u.scheme))) //nolint:gosec // uint64 -> int conversion is ok: no port overflows a int
}
// References:
// * https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
// * https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml
//
// Also: https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
//
//nolint:gocyclo,cyclop // essential complexity in well-known scheme port mapping, might be refactored in future
func defaultPortForScheme(scheme string) uint64 {
//nolint:mnd // no need to define default ports with additional constants
switch scheme {
case "aaa":
return 3868
case "aaas":
return 5658
case "acap":
return 674
case "cap":
return 1026
case "coap", "coap+tcp":
return 5683
case "coaps":
return 5684
case "coap+ws":
return 80
case "coaps+ws":
return 443
case "dict":
return 2628
case "dns":
return 53
case "finger":
return 79
case "ftp":
return 21
case "git":
return 9418
case "go":
return 1096
case "gopher":
return 70
case "http":
return 80
case "https":
return 443
case "iax":
return 4569
case "icap":
return 1344
case "imap":
return 143
case "ipp", "ipps":
return 631
case "irc", "irc6":
return 6667
case "ircs":
return 6697
case "ldap":
return 389
case "mailto":
return 25
case "msrp", "msrps":
return 2855
case "nfs":
return 2049
case "nntp":
return 119
case "ntp":
return 123
case "postgresql":
return 5432
case "radius":
return 1812
case "redis":
return 6379
case "rmi":
return 1098
case "rtsp", "rtsps", "rtspu":
return 554
case "rsync":
return 873
case "sftp":
return 22
case "skype":
return 23399
case "smtp":
return 25
case "snmp":
return 161
case "ssh":
return 22
case "steam":
return 7777
case "svn":
return 3690
case "telnet":
return 23
case "vnc":
return 5500
case "wss":
return 6602
}
return 0
}