Skip to content

Commit

Permalink
Merge pull request #24 from juev/feature/optimization
Browse files Browse the repository at this point in the history
feat: add optimizations
  • Loading branch information
juev authored Dec 8, 2024
2 parents 299216f + 17c7103 commit c60edd8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
6 changes: 5 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
var (
torRc, jsonRelays, silent bool
outfile string
ipv6 bool
)

func main() {
Expand Down Expand Up @@ -68,6 +69,9 @@ func printRelays(sc scanner.TorRelayScanner, out io.Writer) {
}
if torRc {
color.Fprintf(out, "UseBridges 1\n")
if ipv6 {
color.Fprintf(out, "ClientPreferIPv6ORPort 1\n")
}
}
}

Expand All @@ -81,7 +85,7 @@ func createScanner() scanner.TorRelayScanner {
var poolSize, goal int
var timeoutStr, deadlineStr, country string
var urls, port, excludePort []string
var ipv4, ipv6 bool
var ipv4 bool

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`)
Expand Down
29 changes: 15 additions & 14 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,18 @@ func (t *torRelayScanner) getRelays() Relays {
bar := t.createProgressBar()

var relays Relays
loop:
for i := 1; i <= t.goal; i++ {
select {
case el, opened := <-chanRelays:
if !opened {
break loop
return relays
}
relays = append(relays, el)
_ = bar.Add(1)
case <-time.After(t.deadline):
_ = bar.Add(t.goal)
color.Fprintf(os.Stderr, "\nThe program was running for more than the specified time: %.2fs\n", t.deadline.Seconds())
break loop
return relays
}
}

Expand Down Expand Up @@ -239,15 +238,13 @@ func (t *torRelayScanner) filterAddresses(addresses []string) []string {
func (t *torRelayScanner) skipPorts(addr string) bool {
u, _ := url.Parse("//" + addr)
var skip bool
if len(t.ports) > 0 {
if !slices.Contains(t.ports, u.Port()) {
skip = true
}
if len(t.ports) > 0 &&
!slices.Contains(t.ports, u.Port()) {
skip = true
}
if len(t.excludePorts) > 0 {
if slices.Contains(t.excludePorts, u.Port()) {
skip = true
}
if len(t.excludePorts) > 0 &&
slices.Contains(t.excludePorts, u.Port()) {
skip = true
}

return skip
Expand Down Expand Up @@ -279,8 +276,12 @@ func (t *torRelayScanner) grab(addr string) (RelayInfo, error) {

// tcpSocketConnectChecker just checked network connection with specific host:port
func tcpSocketConnectChecker(addr string, timeout time.Duration) bool {
d := net.Dialer{Deadline: time.Now().Add(timeout)}
_, err := d.Dial("tcp", addr)
d := net.Dialer{Timeout: timeout}
conn, err := d.Dial("tcp", addr)
if err != nil {
return false
}
_ = conn.Close()

return err == nil
return true
}

0 comments on commit c60edd8

Please sign in to comment.