| 1 | package netutil |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // FROM: http://golang.org/src/net/http/client.go |
| 9 | // Given a string of the form "host", "host:port", or "[ipv6::address]:port", |
| 10 | // return true if the string includes a port. |
| 11 | func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") } |
| 12 | |
| 13 | // FROM: http://golang.org/src/net/http/transport.go |
| 14 | var portMap = map[string]string{ |
| 15 | "http": "80", |
| 16 | "https": "443", |
| 17 | "socks5": "1080", |
| 18 | } |
| 19 | |
| 20 | // FROM: http://golang.org/src/net/http/transport.go |
| 21 | // canonicalAddr returns url.Host but always with a ":port" suffix |
| 22 | func CanonicalAddr(url *url.URL) string { |
| 23 | addr := url.Host |
| 24 | if !hasPort(addr) { |
| 25 | return addr + ":" + portMap[url.Scheme] |
| 26 | } |
| 27 | return addr |
| 28 | } |