You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.1 KiB
77 lines
1.1 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"log"
|
|
"bufio"
|
|
"flag"
|
|
"os/exec"
|
|
)
|
|
|
|
type Options struct {
|
|
Addr string
|
|
}
|
|
|
|
func ParseOptions() Options {
|
|
var opts Options
|
|
|
|
flag.StringVar(&opts.Addr, "addr", "127.0.0.1:9001", "address to bind to recv blocks")
|
|
flag.Parse()
|
|
|
|
return opts
|
|
}
|
|
|
|
func handleConnection(conn net.Conn) {
|
|
defer conn.Close()
|
|
|
|
scan := bufio.NewScanner(conn)
|
|
|
|
for scan.Scan() {
|
|
addr, _, err := net.SplitHostPort(scan.Text())
|
|
if err != nil {
|
|
fmt.Println("Invalid host:port")
|
|
continue
|
|
}
|
|
|
|
if addr == "127.0.0.1" {
|
|
fmt.Println("IGNORE", addr)
|
|
continue
|
|
}
|
|
|
|
fmt.Println("BLOCK: ", addr)
|
|
|
|
cmd := exec.Command("nft",
|
|
"add", "rule", "inet",
|
|
"dentata", "input",
|
|
"ip", "saddr",
|
|
addr, "drop")
|
|
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func listener(addr string) {
|
|
server, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
for {
|
|
conn, err := server.Accept()
|
|
|
|
if err != nil {
|
|
log.Println("ACCEPT ERROR", err)
|
|
} else {
|
|
go handleConnection(conn)
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
opts := ParseOptions()
|
|
|
|
listener(opts.Addr)
|
|
}
|
|
|