forked from jesusprubio/up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers.go
148 lines (140 loc) · 3.17 KB
/
servers.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
package pkg
import (
"crypto/rand"
"fmt"
"math/big"
"net"
"net/url"
)
const tmplRandom = "creating random number: %w"
// RandomCaptivePortal returns a captive portal URL selected randomly from the
// list of well-known companies.
//
// Returns an error if the random number generator fails.
func RandomCaptivePortal() (string, error) {
count := big.NewInt(int64(len(CaptivePortals)))
index, err := rand.Int(rand.Reader, count)
if err != nil {
return "", fmt.Errorf(tmplRandom, err)
}
return CaptivePortals[index.Int64()].String(), nil
}
// CaptivePortals are URLs that well-known companies use inspect the network
// connections of their users.
var CaptivePortals []*url.URL = []*url.URL{
// Google Chrome.
{
Scheme: "http",
Host: "clients3.google.com:80",
Path: "/generate_204",
},
// Mozilla Firefox.
{
Scheme: "http",
Host: "detectportal.firefox.com:80",
Path: "/success.txt",
},
// Apple.
{
Scheme: "http",
Host: "www.apple.com:80",
Path: "/library/test/success.html",
},
// Microsoft.
{
Scheme: "http",
Host: "www.msftconnecttest.com:80",
Path: "/redirect",
},
// Android.
{
Scheme: "http",
Host: "connectivitycheck.android.com:80",
Path: "/generate_204",
},
// Ubuntu.
{
Scheme: "http",
Host: "connectivity-check.ubuntu.com:80",
},
// Debian.
{
Scheme: "http",
Host: "network-test.debian.org:80",
},
}
// RandomDNSServer returns a randomly selected public DNS server address.
//
// Returns an error if the random number generator fails.
func RandomDNSServer() (string, error) {
count := big.NewInt(int64(len(Resolvers)))
index, err := rand.Int(rand.Reader, count)
if err != nil {
return "", fmt.Errorf(tmplRandom, err)
}
return Resolvers[index.Int64()].String(), nil
}
// Resolvers is a list of public DNS server IP addresses.
var Resolvers = []*net.IP{
// Cloudflare
{1, 1, 1, 1},
{1, 0, 0, 1},
// Google
{8, 8, 8, 8},
{8, 8, 4, 4},
// OpenDNS
{208, 67, 222, 222},
{208, 67, 222, 220},
// Control D
{76, 76, 2, 0},
{76, 76, 10, 0},
// AdGuard
{94, 140, 14, 14},
{94, 140, 15, 15},
// CleanBrowsing
{185, 228, 168, 9},
{185, 228, 169, 9},
// Verisign
{64, 6, 64, 6},
{64, 6, 65, 6},
// Quad9
{9, 9, 9, 9},
{149, 112, 112, 112},
// Neustar
{156, 154, 70, 1},
{156, 154, 71, 1},
// Yandex
{77, 88, 8, 8},
{77, 88, 8, 1},
// SafeDNS
{195, 46, 39, 39},
{195, 46, 39, 40},
// Norton ConnectSafe
{199, 85, 126, 10},
{199, 85, 127, 10},
}
// RandomTCPServer returns a TCP host:port selected randomly from the public DNS
// servers.
//
// Returns an error if the random number generator fails.
func RandomTCPServer() (string, error) {
serverAddr, err := RandomDNSServer()
if err != nil {
return "", fmt.Errorf(tmplRandom, err)
}
return net.JoinHostPort(serverAddr, "53"), nil
}
// RandomDomain returns a domain selected randomly from the captive portals.
//
// Returns an error if the random number generator fails.
func RandomDomain() (string, error) {
portalURL, err := RandomCaptivePortal()
if err != nil {
return "", fmt.Errorf(tmplRandom, err)
}
u, err := url.Parse(portalURL)
if err != nil {
return "", fmt.Errorf("parsing URL %s: %w", portalURL, err)
}
return u.Hostname(), nil
}