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("Errur on Listening") } else { fmt.Println("Opened :", port) for { conn, err := ln.Accept() if err != nil { fmt.Println("Errur on Accepting") } else { go listen_func(conn) } } } } func whitelist_handler(c net.Conn) { host, _, _ := net.SplitHostPort(c.RemoteAddr().String()) io.WriteString(c, "Knock Knock, ") io.WriteString(c, host) io.WriteString(c, ". ") 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: ", host) io.WriteString(c, "Hola o/") } else { fmt.Println("BLOCK: ", 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 }