Skip to content

Commit 9a889e2

Browse files
authored
chore: fix linting errors (#36)
1 parent d1d15d7 commit 9a889e2

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<img alt="Logo" src="https://media.giphy.com/media/pYyFAHLW0zJL2/giphy.gif" width="40%">
44
</div>
55

6-
Troubleshoot problems with your Internet connection based om different
6+
Troubleshoot problems with your Internet connection based on different
77
[protocols](pkg/protocol.go) and [public servers](pkg/servers.go).
88

99
[![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] ![License](https://img.shields.io/github/license/jesusprubio/up)

pkg/probe.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ func (p Probe) Run(ctx context.Context) error {
7575
return nil
7676
default:
7777
start := time.Now()
78-
rhost := proto.RHost()
78+
rhost, err := proto.RHost()
79+
if err != nil {
80+
return fmt.Errorf("creating remote host: %w", err)
81+
}
7982
p.Logger.Debug(
8083
"New protocol",
8184
"count", count, "protocol", proto.ID, "rhost", rhost,

pkg/protocol.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// Protocols included in the library.
1111
var Protocols = []*Protocol{
1212
{ID: "http", Request: requestHTTP, RHost: RandomCaptivePortal},
13-
{ID: "tcp", Request: requestTCP, RHost: RandomServerTCP},
13+
{ID: "tcp", Request: requestTCP, RHost: RandomTCPServer},
1414
{ID: "dns", Request: requestDNS, RHost: RandomDomain},
1515
}
1616

@@ -31,7 +31,7 @@ type Protocol struct {
3131
// Returns extra information about the attempt or an error if it failed.
3232
Request func(rhost string, timeout time.Duration) (string, error)
3333
// Function to create a random remote
34-
RHost func() string
34+
RHost func() (string, error)
3535
}
3636

3737
// String returns an human-readable representation of the protocol.

pkg/servers.go

+45-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package pkg
22

33
import (
4-
"math/rand"
4+
"crypto/rand"
5+
"fmt"
6+
"math/big"
57
"net"
68
"net/url"
79
)
810

11+
const tmplRandom = "creating random number: %w"
12+
913
// RandomCaptivePortal returns a captive portal URL selected randomly from the
1014
// 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
1324
}
1425

1526
// CaptivePortals are URLs that well-known companies use inspect the network
@@ -57,9 +68,16 @@ var CaptivePortals []*url.URL = []*url.URL{
5768
},
5869
}
5970

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
6381
}
6482

6583
// Resolvers is a list of public DNS server IP addresses.
@@ -102,15 +120,29 @@ var Resolvers = []*net.IP{
102120
{199, 85, 127, 10},
103121
}
104122

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
106124
// 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
109133
}
110134

111135
// 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
116148
}

0 commit comments

Comments
 (0)