From 93b372c945a184e4f6d3727f3e0ec02b953913d6 Mon Sep 17 00:00:00 2001 From: maride Date: Wed, 27 Sep 2017 16:24:38 +0200 Subject: [PATCH] Add second listener for whitelist/knocking --- knockr.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/knockr.go b/knockr.go index b37acb6..6dc1ad1 100644 --- a/knockr.go +++ b/knockr.go @@ -9,40 +9,52 @@ import ( var whitelist []string func main() { - ln, err := net.Listen("tcp", ":8080") + 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 handle(conn) + go listen_func(conn) } } } } -func handle(c net.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, ". ") - if is_whitelisted(host) { - io.WriteString(c, "You're whitelisted.") - } else { - io.WriteString(c, "You're not whitelisted.") - } - 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)