Skip to content

Commit

Permalink
Check defaults port idle status to use torrc
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed May 6, 2021
1 parent 886c709 commit bb179a2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions service/anonymity/tor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"context"
"crypto/rand"
"encoding/hex"
"net"
"net/http"
"os"
"os/exec"
"time"

"github.com/cretz/bine/tor"
"github.com/cretz/bine/torutil/ed25519"
Expand Down Expand Up @@ -100,8 +102,27 @@ func (t *Tor) torrc() string {
if config.Opts.TorrcFile() == "" {
return ""
}
if torPortBusy() {
return ""
}
if _, err := os.Open(config.Opts.TorrcFile()); err != nil {
return ""
}
return config.Opts.TorrcFile()
}

func torPortBusy() bool {
addr := net.JoinHostPort("127.0.0.1", "9050")
conn, err := net.DialTimeout("tcp", addr, time.Second)
if err != nil {
logger.Debug("[web] defaults tor port is idle")
return false
}
if conn != nil {
conn.Close()
logger.Debug("[web] defaults tor port is busy")
return true
}

return false
}

0 comments on commit bb179a2

Please sign in to comment.