Created
May 9, 2017 02:58
-
-
Save GlenDC/06d1c48a8148d6f20dc7f846112162ed to your computer and use it in GitHub Desktop.
Tor Hidden (TCP) Service Example in Golang
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 ( | |
"log" | |
"os" | |
"strings" | |
"golang.org/x/net/proxy" | |
) | |
func main() { | |
args := os.Args[1:] | |
if len(args) != 2 { | |
log.Fatalf("usage: %s toraddress hidserviceaddress", os.Args[0]) | |
} | |
torAddress, serviceAddress := args[0], args[1] | |
dialer, err := proxy.SOCKS5("tcp", torAddress, nil, proxy.Direct) | |
if err != nil { | |
log.Fatal(err) | |
} | |
conn, err := dialer.Dial("tcp", serviceAddress) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer conn.Close() | |
_, err = conn.Write([]byte("PING")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var reply [4]byte | |
_, err = conn.Read(reply[:]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if strings.ToUpper(string(reply[:])) != "PONG" { | |
log.Fatal("received unknown reply:", reply) | |
} | |
log.Println("Connection Works!") | |
} |
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 ( | |
"log" | |
"net" | |
"os" | |
"strings" | |
) | |
func handleConnection(conn net.Conn) { | |
defer conn.Close() | |
var request [4]byte | |
_, err := conn.Read(request[:]) | |
if err != nil { | |
log.Println("error while reading from conn:", err) | |
return | |
} | |
if strings.ToUpper(string(request[:])) != "PING" { | |
log.Println("received unknown request:", request) | |
return | |
} | |
conn.Write([]byte("PONG")) | |
log.Println("Succesfull ping received") | |
} | |
func main() { | |
args := os.Args[1:] | |
if len(args) != 1 { | |
log.Fatalf("usage: %s serveraddress", os.Args[0]) | |
} | |
address := args[0] | |
ln, err := net.Listen("tcp", address) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for { | |
conn, err := ln.Accept() | |
if err != nil { | |
log.Fatal(err) | |
} | |
go handleConnection(conn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment