Take the Tor exit list from Onionoo, which has the IPv6 exits (closes #234)
check / check (push) Successful in 3m16s
check / check (push) Successful in 3m16s
check.torproject.org lists IPv4 exits only, so is_tor was false for an IPv6 exit. tor_exits now comes from Onionoo's details document, read by a new format that takes every address in exit_addresses and or_addresses. Model: opus-5-5
This commit is contained in:
1 parent
cf50d195ad
commit
2ee58665ff
6 files changed
+245
-39
No files matched your search
+99
-16
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
||||
@@ -32,12 +33,32 @@ var (
|
||||
errUnknownFormat = errors.New("unknown list format")
|
||||
errNoNetwork = errors.New("no network on the line")
|
||||
errBadRange = errors.New("first and last address make no range")
|
||||
errNoAddresses = errors.New("no relay addresses in the document")
|
||||
)
|
||||
|
||||
// parse reads a list file written in format and returns the addresses it
|
||||
// holds. A line it cannot read fails the whole file, so a file that is not
|
||||
// holds. Anything it cannot read fails the whole file, so a file that is not
|
||||
// the list, such as an error page, never replaces a good copy.
|
||||
func parse(r io.Reader, format Format) (*netipx.IPSet, error) {
|
||||
switch format {
|
||||
case FormatLines:
|
||||
return parseLines(r, addAddressOrNetwork)
|
||||
case FormatSpamhausJSON:
|
||||
return parseLines(r, addSpamhausNetwork)
|
||||
case FormatDShield:
|
||||
return parseLines(r, addDShieldNetwork)
|
||||
case FormatOnionooJSON:
|
||||
return parseOnionoo(r)
|
||||
default:
|
||||
return nil, errUnknownFormat
|
||||
}
|
||||
}
|
||||
|
||||
// parseLines reads a list file line by line, skipping blank lines and
|
||||
// comments, and calls addLine with each other line.
|
||||
func parseLines(
|
||||
r io.Reader, addLine func(*netipx.IPSetBuilder, string) error,
|
||||
) (*netipx.IPSet, error) {
|
||||
var builder netipx.IPSetBuilder
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
@@ -48,7 +69,7 @@ func parse(r io.Reader, format Format) (*netipx.IPSet, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
err := addLine(&builder, line, format)
|
||||
err := addLine(&builder, line)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("line %d: %w", lineNumber, err)
|
||||
}
|
||||
@@ -62,20 +83,6 @@ func parse(r io.Reader, format Format) (*netipx.IPSet, error) {
|
||||
return builder.IPSet()
|
||||
}
|
||||
|
||||
// addLine adds the address or network on one line of a list file to builder.
|
||||
func addLine(builder *netipx.IPSetBuilder, line string, format Format) error {
|
||||
switch format {
|
||||
case FormatLines:
|
||||
return addAddressOrNetwork(builder, line)
|
||||
case FormatSpamhausJSON:
|
||||
return addSpamhausNetwork(builder, line)
|
||||
case FormatDShield:
|
||||
return addDShieldNetwork(builder, line)
|
||||
default:
|
||||
return errUnknownFormat
|
||||
}
|
||||
}
|
||||
|
||||
// addAddressOrNetwork adds an address, such as 192.0.2.1, or a network, such
|
||||
// as 192.0.2.0/24, to builder.
|
||||
func addAddressOrNetwork(builder *netipx.IPSetBuilder, text string) error {
|
||||
@@ -156,3 +163,79 @@ func addDShieldNetwork(builder *netipx.IPSetBuilder, line string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseOnionoo returns every address in the exit_addresses and or_addresses
|
||||
// fields of the relays in an Onionoo details document. A document with no
|
||||
// addresses fails, because the Tor network always has relays.
|
||||
func parseOnionoo(r io.Reader) (*netipx.IPSet, error) {
|
||||
data, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//nolint:tagliatelle // Onionoo's own field names
|
||||
var details struct {
|
||||
Relays []struct {
|
||||
ExitAddresses []string `json:"exit_addresses"`
|
||||
ORAddresses []string `json:"or_addresses"`
|
||||
} `json:"relays"`
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &details)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var builder netipx.IPSetBuilder
|
||||
|
||||
for _, relay := range details.Relays {
|
||||
for _, text := range relay.ExitAddresses {
|
||||
err = addAddress(&builder, text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
for _, text := range relay.ORAddresses {
|
||||
err = addAddressWithPort(&builder, text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addresses, err := builder.IPSet()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(addresses.Ranges()) == 0 {
|
||||
return nil, errNoAddresses
|
||||
}
|
||||
|
||||
return addresses, nil
|
||||
}
|
||||
|
||||
// addAddress adds an address, such as 192.0.2.1, to builder.
|
||||
func addAddress(builder *netipx.IPSetBuilder, text string) error {
|
||||
addr, err := netip.ParseAddr(text)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
builder.Add(addr)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// addAddressWithPort adds the address of text, such as 192.0.2.1:443 or
|
||||
// [2001:db8::1]:443, to builder. Onionoo allows a list of ports after the
|
||||
// address, so the part after the address is not read.
|
||||
func addAddressWithPort(builder *netipx.IPSetBuilder, text string) error {
|
||||
host, _, err := net.SplitHostPort(text)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return addAddress(builder, host)
|
||||
}
|
||||
Reference in new issue
Block a user