61 lines
923 B
Go
61 lines
923 B
Go
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
|
|
}
|