First quick & dirty Go code

This commit is contained in:
maride 2017-09-27 15:44:02 +02:00
commit 094df4a3a1

60
knockr.go Normal file
View File

@ -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
}