|
1 | 1 | package pkg
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "math/rand" |
| 4 | + "crypto/rand" |
| 5 | + "fmt" |
| 6 | + "math/big" |
5 | 7 | "net"
|
6 | 8 | "net/url"
|
7 | 9 | )
|
8 | 10 |
|
| 11 | +const tmplRandom = "creating random number: %w" |
| 12 | + |
9 | 13 | // RandomCaptivePortal returns a captive portal URL selected randomly from the
|
10 | 14 | // list of well-known companies.
|
11 |
| -func RandomCaptivePortal() string { |
12 |
| - return CaptivePortals[rand.Intn(len(CaptivePortals))].String() |
| 15 | +// |
| 16 | +// Returns an error if the random number generator fails. |
| 17 | +func RandomCaptivePortal() (string, error) { |
| 18 | + count := big.NewInt(int64(len(CaptivePortals))) |
| 19 | + index, err := rand.Int(rand.Reader, count) |
| 20 | + if err != nil { |
| 21 | + return "", fmt.Errorf(tmplRandom, err) |
| 22 | + } |
| 23 | + return CaptivePortals[index.Int64()].String(), nil |
13 | 24 | }
|
14 | 25 |
|
15 | 26 | // CaptivePortals are URLs that well-known companies use inspect the network
|
@@ -57,9 +68,16 @@ var CaptivePortals []*url.URL = []*url.URL{
|
57 | 68 | },
|
58 | 69 | }
|
59 | 70 |
|
60 |
| -// RandomServerDNS returns a randomly selected public DNS server address. |
61 |
| -func RandomServerDNS() string { |
62 |
| - return Resolvers[rand.Intn(len(Resolvers))].String() |
| 71 | +// RandomDNSServer returns a randomly selected public DNS server address. |
| 72 | +// |
| 73 | +// Returns an error if the random number generator fails. |
| 74 | +func RandomDNSServer() (string, error) { |
| 75 | + count := big.NewInt(int64(len(Resolvers))) |
| 76 | + index, err := rand.Int(rand.Reader, count) |
| 77 | + if err != nil { |
| 78 | + return "", fmt.Errorf(tmplRandom, err) |
| 79 | + } |
| 80 | + return Resolvers[index.Int64()].String(), nil |
63 | 81 | }
|
64 | 82 |
|
65 | 83 | // Resolvers is a list of public DNS server IP addresses.
|
@@ -102,15 +120,29 @@ var Resolvers = []*net.IP{
|
102 | 120 | {199, 85, 127, 10},
|
103 | 121 | }
|
104 | 122 |
|
105 |
| -// RandomServerTCP returns a TCP host:port selected randomly from the public DNS |
| 123 | +// RandomTCPServer returns a TCP host:port selected randomly from the public DNS |
106 | 124 | // servers.
|
107 |
| -func RandomServerTCP() string { |
108 |
| - return net.JoinHostPort(RandomServerDNS(), "53") |
| 125 | +// |
| 126 | +// Returns an error if the random number generator fails. |
| 127 | +func RandomTCPServer() (string, error) { |
| 128 | + serverAddr, err := RandomDNSServer() |
| 129 | + if err != nil { |
| 130 | + return "", fmt.Errorf(tmplRandom, err) |
| 131 | + } |
| 132 | + return net.JoinHostPort(serverAddr, "53"), nil |
109 | 133 | }
|
110 | 134 |
|
111 | 135 | // RandomDomain returns a domain selected randomly from the captive portals.
|
112 |
| -func RandomDomain() string { |
113 |
| - // Error ignored because the URLs are hardcoded. |
114 |
| - u, _ := url.Parse(RandomCaptivePortal()) |
115 |
| - return u.Hostname() |
| 136 | +// |
| 137 | +// Returns an error if the random number generator fails. |
| 138 | +func RandomDomain() (string, error) { |
| 139 | + portalURL, err := RandomCaptivePortal() |
| 140 | + if err != nil { |
| 141 | + return "", fmt.Errorf(tmplRandom, err) |
| 142 | + } |
| 143 | + u, err := url.Parse(portalURL) |
| 144 | + if err != nil { |
| 145 | + return "", fmt.Errorf("parsing URL %s: %w", portalURL, err) |
| 146 | + } |
| 147 | + return u.Hostname(), nil |
116 | 148 | }
|
0 commit comments