From 094df4a3a14f754de248f3396041c80db4d0e60c Mon Sep 17 00:00:00 2001 From: maride Date: Wed, 27 Sep 2017 15:44:02 +0200 Subject: [PATCH] First quick & dirty Go code --- knockr.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 knockr.go diff --git a/knockr.go b/knockr.go new file mode 100644 index 0000000..b37acb6 --- /dev/null +++ b/knockr.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + "io" + "net" +) + +var whitelist []string + +func main() { + ln, err := net.Listen("tcp", ":8080") + + if err != nil { + fmt.Println("Errur on Listening") + } else { + for { + conn, err := ln.Accept() + if err != nil { + fmt.Println("Errur on Accepting") + } else { + go handle(conn) + } + } + } +} + +func handle(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 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 +}