Add second listener for whitelist/knocking
This commit is contained in:
parent
094df4a3a1
commit
93b372c945
30
knockr.go
30
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)
|
||||
|
Loading…
Reference in New Issue
Block a user