Skip to content

Commit

Permalink
Merge pull request #9 from juev/feature/refactoring
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
juev authored Dec 28, 2023
2 parents 43bcb4a + a9255a8 commit 6acc619
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 191 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: '>=1.19.0'
go-version: '>=1.20.0'
check-latest: true
-
name: Install dependencies
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: '>=1.19.0'
go-version: '>=1.20.0'
check-latest: true
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
Expand Down
47 changes: 24 additions & 23 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package main

import (
"fmt"
"io"
"os"
"time"

"github.com/gookit/color"
scanner "github.com/juev/tor-relay-scanner-go"
flag "github.com/spf13/pflag"
)

var Usage = func() {
fmt.Fprintf(os.Stdout, "Usage of tor-relay-scanner-go:\n")
flag.PrintDefaults()
os.Exit(0)
}

func main() {
flag.IntVarP(&poolSize, "num_relays", "n", 30, `The number of concurrent relays tested.`)
usage := func() {
color.Fprintln(os.Stdout, "Usage of tor-relay-scanner-go:")
flag.PrintDefaults()
os.Exit(0)
}

flag.IntVarP(&poolSize, "num_relays", "n", 100, `The number of concurrent relays tested.`)
flag.IntVarP(&goal, "working_relay_num_goal", "g", 5, `Test until at least this number of working relays are found`)
flag.IntVarP(&timeout, "timeout", "t", 1, `Socket connection timeout`)
flag.StringVarP(&outfile, "outfile", "o", "", `Output reachable relays to file`)
Expand All @@ -27,7 +27,7 @@ func main() {
flag.BoolVarP(&ipv4, "ipv4", "4", false, `Use ipv4 only nodes`)
flag.BoolVarP(&ipv6, "ipv6", "6", false, `Use ipv6 only nodes`)
flag.BoolVarP(&jsonRelays, "json", "j", false, `Get available relays in json format`)
flag.Usage = Usage
flag.Usage = usage
flag.Parse()

if timeout < 1 {
Expand All @@ -54,7 +54,7 @@ func main() {
if outfile != "" {
logFile, err := os.OpenFile(outfile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot create file (%s): %v", outfile, err)
color.Fprintf(os.Stderr, "cannot create file (%s): %s", outfile, err.Error())
os.Exit(3)
}
out = io.MultiWriter(os.Stdout, logFile)
Expand All @@ -63,23 +63,24 @@ func main() {
if jsonRelays {
relays, err := sc.GetRelays()
if err != nil {
fmt.Fprintf(os.Stderr, "No relays are reachable this try.\n")
color.Fprintln(os.Stderr, "Tor Relay information can't be downloaded!")
os.Exit(4)
}
fmt.Fprintf(out, "%s\n", relays)
os.Exit(0)
color.Fprintf(out, "%s\n", relays)
return
}

relays := sc.Grab()
if len(relays) > 0 {
fmt.Printf("All reachable relays:\n")
for _, el := range relays {
fmt.Fprintf(out, "%s%s %s\n", prefix, el.Addresses, el.Fingerprint)
}
if torrc {
fmt.Fprintf(out, "UseBridges 1\n")
}
} else {
fmt.Fprintf(os.Stderr, "No relays are reachable this try.\n")
if len(relays) == 0 {
color.Fprintf(os.Stderr, "No relays are reachable this try.\n")
os.Exit(5)
}

color.Printf("All reachable relays:\n")
for _, el := range relays {
color.Fprintf(out, "%s%s %s\n", prefix, el.Address, el.Fingerprint)
}
if torrc {
color.Fprintf(out, "UseBridges 1\n")
}
}
17 changes: 5 additions & 12 deletions funcs.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package scanner

import (
"math/rand"
"time"
"crypto/rand"
"math/big"
)

func shuffle(relays Relays) {
rand.New(rand.NewSource(time.Now().UnixNano())).
Shuffle(len(relays), func(i, j int) {
relays[i], relays[j] = relays[j], relays[i]
})
}

func min(x, y int) int {
if x < y {
return x
for i := len(relays) - 1; i > 0; i-- {
j, _ := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
relays[i], relays[j.Uint64()] = relays[j.Uint64()], relays[i]
}
return y
}
14 changes: 11 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ module github.com/juev/tor-relay-scanner-go
go 1.20

require (
github.com/carlmjohnson/requests v0.23.3
github.com/carlmjohnson/requests v0.23.5
github.com/gookit/color v1.5.4
github.com/json-iterator/go v1.1.12
github.com/spf13/pflag v1.0.5
)

require (
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/net v0.17.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/schollz/progressbar/v3 v3.14.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
)
35 changes: 29 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
github.com/carlmjohnson/requests v0.23.3 h1:22EEJsJqjNWprjQtqw2nLoQ1Sz+I1qJUbvhd0cHSHUg=
github.com/carlmjohnson/requests v0.23.3/go.mod h1:Qzp6tW4DQyainPP+tGwiJTzwxvElTIKm0B191TgTtOA=
github.com/carlmjohnson/requests v0.23.5 h1:NPANcAofwwSuC6SIMwlgmHry2V3pLrSqRiSBKYbNHHA=
github.com/carlmjohnson/requests v0.23.5/go.mod h1:zG9P28thdRnN61aD7iECFhH5iGGKX2jIjKQD9kqYH+o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/schollz/progressbar/v3 v3.14.1 h1:VD+MJPCr4s3wdhTc7OEJ/Z3dAeBzJ7yKH/P4lC5yRTI=
github.com/schollz/progressbar/v3 v3.14.1/go.mod h1:Zc9xXneTzWXF81TGoqL71u0sBPjULtEHYtj/WVgVy8E=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
39 changes: 26 additions & 13 deletions models.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package scanner

import (
"os"
"time"

"github.com/schollz/progressbar/v3"
)

// TorRelayScanner ...
Expand All @@ -12,7 +15,6 @@ type TorRelayScanner interface {

type torRelayScanner struct {
relayInfo RelayInfo
relays Relays
// The number of concurrent relays tested. default=30
poolSize int
// Test until at least this number of working relays are found. default=5
Expand All @@ -24,29 +26,29 @@ type torRelayScanner struct {
// Preferred alternative URL for onionoo relay list. Could be used multiple times
urls []string
// Scan for relays running on specified port number. Could be used multiple times
port []string
ports []string
// Use ipv4 only nodes
ipv4 bool
// Use ipv6 only nodes
ipv6 bool
}

type (
Version string
BuildRevision string
RelaysPublished string
BridgesPublished string
Bridges []any
version string
buildRevision string
relaysPublished string
bridgesPublished string
bridges []any
)

// RelayInfo struct with basics information relay lists
type RelayInfo struct {
Version Version
BuildRevision BuildRevision `json:"build_revision"`
RelaysPublished RelaysPublished `json:"relays_published"`
Version version
BuildRevision buildRevision `json:"build_revision"`
RelaysPublished relaysPublished `json:"relays_published"`
Relays Relays `json:"relays"`
BridgesPublished BridgesPublished `json:"bridges_published"`
Bridges Bridges `json:"bridges"`
BridgesPublished bridgesPublished `json:"bridges_published"`
Bridges bridges `json:"bridges"`
}

// Relays ...
Expand All @@ -61,5 +63,16 @@ type Relay struct {
// ResultRelay ...
type ResultRelay struct {
Fingerprint string `json:"fingerprint"`
Addresses string `json:"or_addresses"`
Address string `json:"or_addresses"`
}

var progressbarOptions = []progressbar.Option{
progressbar.OptionSetDescription("Testing"),
progressbar.OptionSetWidth(15),
progressbar.OptionSetWriter(os.Stderr),
progressbar.OptionShowCount(),
progressbar.OptionClearOnFinish(),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetPredictTime(false),
progressbar.OptionSetRenderBlankState(true),
}
Loading

0 comments on commit 6acc619

Please sign in to comment.