commit 094df4a3a14f754de248f3396041c80db4d0e60c Author: maride Date: Wed Sep 27 15:44:02 2017 +0200 First quick & dirty Go code 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 +}