package main import ( "fmt" "io" "net" ) var whitelist []string func main() { go listener(9090, whitelist_handler) listener(8080, gateway_handler) } func listener(port int, listen_func func(c net.Conn)) { ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) if err != nil { fmt.Println("[ERR] Creating listener for Port ", port) } else { fmt.Println("[OK ] Creating listener for Port ", port) for { conn, err := ln.Accept() if err != nil { fmt.Println("[ERR] Accepting on Port ", port) } else { go listen_func(conn) } } } } func whitelist_handler(c net.Conn) { host, _, _ := net.SplitHostPort(c.RemoteAddr().String()) io.WriteString(c, fmt.Sprintf("Knock Knock, %s.", host)) add_to_whitelist(host) c.Close() } func gateway_handler(c net.Conn) { host, _, _ := net.SplitHostPort(c.RemoteAddr().String()) if is_whitelisted(host) { fmt.Println("[OK ] Whitelisted host ", host, " connected") proxy(c) } else { fmt.Println("[BLK] Blocking host ", host) } c.Close() } func add_to_whitelist(addr string) { if ! is_whitelisted(addr) { whitelist = append(whitelist, addr) } } func is_whitelisted(addr string) bool { for i:=0; i < len(whitelist); i++ { if whitelist[i] == addr { return true } } return false } func proxy(c net.Conn) { ln, err := net.Dial("tcp", "ip.darknebu.la:443") if err != nil { fmt.Println("[ERR] Proxy connection to server failed") } else { go io.Copy(c, ln) io.Copy(ln, c) } }