Add second listener for whitelist/knocking

This commit is contained in:
maride 2017-09-27 16:24:38 +02:00
parent 094df4a3a1
commit 93b372c945

View File

@ -9,40 +9,52 @@ import (
var whitelist []string var whitelist []string
func main() { 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 { if err != nil {
fmt.Println("Errur on Listening") fmt.Println("Errur on Listening")
} else { } else {
fmt.Println("Opened :", port)
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
if err != nil { if err != nil {
fmt.Println("Errur on Accepting") fmt.Println("Errur on Accepting")
} else { } 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()) host, _, _ := net.SplitHostPort(c.RemoteAddr().String())
io.WriteString(c, "Knock Knock, ") io.WriteString(c, "Knock Knock, ")
io.WriteString(c, host) io.WriteString(c, host)
io.WriteString(c, ". ") 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) add_to_whitelist(host)
c.Close() 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) { func add_to_whitelist(addr string) {
if ! is_whitelisted(addr) { if ! is_whitelisted(addr) {
whitelist = append(whitelist, addr) whitelist = append(whitelist, addr)