Created
October 26, 2018 11:48
-
-
Save jes/9c7b6eccee4a153b20e2780859416bca to your computer and use it in GitHub Desktop.
Tor measurement program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/net/proxy" | |
"net" | |
"os" | |
"strconv" | |
"time" | |
) | |
func main() { | |
l, err := net.Listen("tcp", "127.0.0.1:9876") | |
if err != nil { | |
fmt.Println("listen: ", err.Error()) | |
os.Exit(1) | |
} | |
messages := make(chan string) | |
go log(messages) | |
go makerequests(messages) | |
for { | |
conn, err := l.Accept() | |
if err != nil { | |
fmt.Println("accept: ", err.Error()) | |
os.Exit(1) | |
} | |
go reply(conn) | |
} | |
} | |
func reply(conn net.Conn) { | |
s := "floobleboard" | |
conn.Write([]byte(s)) | |
conn.Close() | |
} | |
func makerequests(c chan string) { | |
hosts := [...]string{ | |
"vt7iebtdrnvtrjoo.onion", // laptop at home | |
"eyhm46hixoqksupo.onion", // server at home | |
"qk7tnr6f7zmzrman.onion", // apps | |
"qgeuhyuzvugmf7so.onion", // cht1 | |
"uozjnvx4qsxxobpn.onion", // ipfs | |
"pv3en5akblcvrjey.onion", // oracle | |
"55np6truorsw7ku4.onion", // 162 | |
} | |
for { | |
for _, host := range hosts { | |
go makerequest(host, c) | |
} | |
time.Sleep(10 * time.Second) | |
} | |
} | |
func ftoa(n float64) string { | |
return strconv.FormatFloat(n, 'f', 3, 64) | |
} | |
func makerequest(host string, c chan string) { | |
begin := time.Now() | |
logtime := begin.UTC().Format("2006-01-02 15:04:05") | |
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct) | |
if err != nil { | |
fmt.Println("can't get socks5 proxy: ", err.Error()) | |
os.Exit(1) | |
} | |
conn, err := dialer.Dial("tcp", host+":9876") | |
if err != nil { | |
elapsed := time.Since(begin).Seconds() | |
c <- logtime + "\t" + host + "\t" + "error" + "\t" + "Dial: " + err.Error() + "\t" + ftoa(elapsed) | |
return | |
} | |
buf := make([]byte, 1024) | |
n, err := conn.Read(buf) | |
if err != nil { | |
elapsed := time.Since(begin).Seconds() | |
c <- logtime + "\t" + host + "\t" + "error" + "\t" + "Read: " + err.Error() + "\t" + ftoa(elapsed) | |
return | |
} | |
conn.Close() | |
elapsed := time.Since(begin).Seconds() | |
c <- logtime + "\t" + host + "\t" + "ok" + "\t" + string(buf[:n]) + "\t" + ftoa(elapsed) | |
} | |
func log(c chan string) { | |
for { | |
msg := <-c | |
fmt.Println(msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment