feat: 1.4.3 Tor lists and classification, egress provider and peer allowlist
Release / Publish tags (push) Successful in 17s
Release / Plan (reuse the published image?) (push) Successful in 6s
Release / ci (push) Successful in 59s
Release / Build image (amd64) (push) Successful in 24s
Release / Build image (arm64) (push) Successful in 1m29s
CI / Format and vet (push) Successful in 26s
CI / Tests (Linux, race detector) (push) Successful in 33s
CI / Dependency audit (push) Successful in 33s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
Release / Publish tags (push) Successful in 17s
Release / Plan (reuse the published image?) (push) Successful in 6s
Release / ci (push) Successful in 59s
Release / Build image (amd64) (push) Successful in 24s
Release / Build image (arm64) (push) Successful in 1m29s
CI / Format and vet (push) Successful in 26s
CI / Tests (Linux, race detector) (push) Successful in 33s
CI / Dependency audit (push) Successful in 33s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec46fb94ff
commit
045ed53e43
15 files changed
+584
-25
No files matched your search
+182
-1
@@ -3,12 +3,17 @@ package app
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel"
|
||||
@@ -69,7 +74,7 @@ func watchTorExits(ctx context.Context, path string, every time.Duration, log *s
|
||||
log.Warn("tor exit list unreadable", "err", err)
|
||||
return
|
||||
}
|
||||
intel.SetTorExits(addrs)
|
||||
torLists.setManual(addrs)
|
||||
lastMod, lastSize = st.ModTime(), st.Size()
|
||||
log.Info("tor exit list loaded", "n", len(addrs))
|
||||
}
|
||||
@@ -87,3 +92,179 @@ func watchTorExits(ctx context.Context, path string, every time.Duration, log *s
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
const (
|
||||
torExitURL = "https://check.torproject.org/torbulkexitlist"
|
||||
torRelayURL = "https://onionoo.torproject.org/details?running=true&fields=or_addresses,exit_addresses"
|
||||
maxTorList = 32 << 20
|
||||
torFetchTO = 60 * time.Second
|
||||
torUserAgent = "urutau"
|
||||
maxTorRelays = 400000
|
||||
torExitCache = "exits.txt"
|
||||
torRelayCache = "relays.json"
|
||||
)
|
||||
|
||||
// torLists merges the manual exit file (URUTAU_TOR_EXITS) with the downloaded
|
||||
// lists into the intel package.
|
||||
var torLists = &torSource{}
|
||||
|
||||
type torSource struct {
|
||||
mu sync.Mutex
|
||||
manual, auto, rels []netip.Addr
|
||||
}
|
||||
|
||||
func (t *torSource) apply() {
|
||||
all := append(append([]netip.Addr(nil), t.manual...), t.auto...)
|
||||
intel.SetTorExits(all)
|
||||
intel.SetTorRelays(append(all, t.rels...))
|
||||
}
|
||||
|
||||
func (t *torSource) setManual(a []netip.Addr) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
t.manual = a
|
||||
t.apply()
|
||||
}
|
||||
|
||||
func (t *torSource) setAuto(exits, relays []netip.Addr) {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if exits != nil {
|
||||
t.auto = exits
|
||||
}
|
||||
if relays != nil {
|
||||
t.rels = relays
|
||||
}
|
||||
t.apply()
|
||||
}
|
||||
|
||||
func torAddr(s string) (netip.Addr, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
if ap, err := netip.ParseAddrPort(s); err == nil {
|
||||
s = ap.Addr().String()
|
||||
}
|
||||
a, err := netip.ParseAddr(strings.Trim(s, "[]"))
|
||||
if err != nil || a.Zone() != "" {
|
||||
return netip.Addr{}, false
|
||||
}
|
||||
return a.Unmap(), true
|
||||
}
|
||||
|
||||
// ParseTorExitList parses the plain-text exit list (one address per line,
|
||||
// junk and # comments ignored, at most maxTorExits addresses, input limited to
|
||||
// 32 MB).
|
||||
func ParseTorExitList(r io.Reader) []netip.Addr {
|
||||
var out []netip.Addr
|
||||
sc := bufio.NewScanner(io.LimitReader(r, maxTorList))
|
||||
sc.Buffer(make([]byte, 0, 4096), 4096)
|
||||
for sc.Scan() && len(out) < maxTorExits {
|
||||
l := sc.Text()
|
||||
if i := strings.IndexByte(l, '#'); i >= 0 {
|
||||
l = l[:i]
|
||||
}
|
||||
if a, ok := torAddr(l); ok {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ParseOnionoo extracts the relay and exit addresses (v4 and v6) of an Onionoo
|
||||
// "details" document (input limited to 32 MB, at most 400000 addresses).
|
||||
func ParseOnionoo(r io.Reader) ([]netip.Addr, error) {
|
||||
var doc struct {
|
||||
Relays []struct {
|
||||
OR []string `json:"or_addresses"`
|
||||
Exit []string `json:"exit_addresses"`
|
||||
} `json:"relays"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(r, maxTorList)).Decode(&doc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []netip.Addr
|
||||
for _, rl := range doc.Relays {
|
||||
for _, s := range append(rl.OR, rl.Exit...) {
|
||||
if a, ok := torAddr(s); ok && len(out) < maxTorRelays {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func fetchTor(ctx context.Context, url string) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, torFetchTO)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", torUserAgent)
|
||||
resp, err := (&http.Client{Timeout: torFetchTO}).Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("status %d", resp.StatusCode)
|
||||
}
|
||||
b, err := io.ReadAll(io.LimitReader(resp.Body, maxTorList+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(b) > maxTorList {
|
||||
return nil, errors.New("list larger than 32 MB")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// refreshTorLists reads the cache in dir and, when force or the cache is older
|
||||
// than maxAge, downloads the lists again. A failed download keeps the cache.
|
||||
func refreshTorLists(ctx context.Context, dir, exitURL, relayURL string, maxAge time.Duration, log *slog.Logger) {
|
||||
_ = os.MkdirAll(dir, 0o750)
|
||||
one := func(name, url string, parse func([]byte) ([]netip.Addr, error)) []netip.Addr {
|
||||
path := filepath.Join(dir, name)
|
||||
st, statErr := os.Stat(path)
|
||||
if statErr != nil || time.Since(st.ModTime()) >= maxAge {
|
||||
if b, err := fetchTor(ctx, url); err != nil {
|
||||
log.Warn("tor list download failed", "list", name, "err", err)
|
||||
} else if a, err := parse(b); err != nil || len(a) == 0 {
|
||||
log.Warn("tor list invalid, kept the cache", "list", name, "err", err)
|
||||
} else if err := os.WriteFile(path+".tmp", b, 0o640); err == nil && os.Rename(path+".tmp", path) == nil {
|
||||
return a
|
||||
}
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil || len(b) > maxTorList {
|
||||
return nil
|
||||
}
|
||||
a, _ := parse(b)
|
||||
return a
|
||||
}
|
||||
exits := one(torExitCache, exitURL, func(b []byte) ([]netip.Addr, error) {
|
||||
return ParseTorExitList(strings.NewReader(string(b))), nil
|
||||
})
|
||||
relays := one(torRelayCache, relayURL, func(b []byte) ([]netip.Addr, error) {
|
||||
return ParseOnionoo(strings.NewReader(string(b)))
|
||||
})
|
||||
torLists.setAuto(exits, relays)
|
||||
log.Info("tor lists", "exits", len(exits), "relays", len(relays))
|
||||
}
|
||||
|
||||
// watchTorLists refreshes the official lists now (in the background, so
|
||||
// startup is not blocked) and every interval.
|
||||
func watchTorLists(ctx context.Context, dir, exitURL, relayURL string, every time.Duration, log *slog.Logger) {
|
||||
go func() {
|
||||
refreshTorLists(ctx, dir, exitURL, relayURL, every*9/10, log)
|
||||
t := time.NewTicker(every)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
refreshTorLists(ctx, dir, exitURL, relayURL, every*9/10, log)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in new issue
Block a user