forked from digibib/tcpforward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickTables.go
38 lines (33 loc) · 1.38 KB
/
quickTables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"flag"
"fmt"
"os/exec"
)
func main() {
port := flag.String("port", "", "Port number to block or unblock")
action := flag.String("action", "block", "Action to perform (block/unblock)")
protocol := flag.String("protocol", "tcp", "Protocol for the rule (tcp/udp)")
flag.Parse()
if(*port == ""){
fmt.Println("The command-line flag \"port\" not found.")
return
}
if *action == "block" {
blockPort(*port, *protocol)
fmt.Printf("Port %s has been blocked\n", *port)
} else if *action == "unblock" {
unblockPort(*port, *protocol)
fmt.Printf("Port %s has been unblocked\n", *port)
}
}
func blockPort(port string, protocol string) {
exec.Command("iptables", "-A", "INPUT", "-p", protocol, "--dport", port,"-s", "127.0.0.1", "-j", "ACCEPT").Run()
exec.Command("iptables", "-A", "INPUT", "-p", protocol, "--dport", port, "-j", "DROP").Run()
exec.Command("ip6tables", "-A", "INPUT", "-p", protocol, "--dport", port,"-s", "::1", "-j", "ACCEPT").Run()
exec.Command("ip6tables", "-A", "INPUT", "-p", protocol, "--dport", port, "-j", "DROP").Run()
}
func unblockPort(port string, protocol string) {
exec.Command("iptables", "-D", "INPUT", "-p", protocol, "--dport", port, "-j", "DROP").Run()
exec.Command("ip6tables", "-D", "INPUT", "-p", protocol, "--dport", port, "-j", "DROP").Run()
}