add option for modifying iptable chain name

This commit is contained in:
alozxy
2025-01-30 06:52:08 +00:00
parent 218fafe6e6
commit 5bcd0c90ed
3 changed files with 66 additions and 16 deletions

View File

@@ -0,0 +1,47 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/go
{
"name": "Go",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Update the VARIANT arg to pick a version of Go: 1, 1.19, 1.18
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local arm64/Apple Silicon.
"VARIANT": "1.19",
// Options
"NODE_VERSION": "lts/*"
}
},
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"go.toolsManagement.checkForUpdates": "local",
"go.useLanguageServer": true,
"go.gopath": "/go"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
"features": {
"git": "latest"
}
}

View File

@@ -10,9 +10,9 @@ func clean_rule_v4() {
exec.Command("bash", "-c", `iptables-restore --noflush <<-EOF
*nat
-D PREROUTING -m addrtype --dst-type LOCAL -j TRAVERSAL
-F TRAVERSAL
-X TRAVERSAL
-D PREROUTING -m addrtype --dst-type LOCAL -j `+get_conf("chain_name").(string)+`
-F `+get_conf("chain_name").(string)+`
-X `+get_conf("chain_name").(string)+`
COMMIT
EOF`).Run()
}
@@ -21,9 +21,9 @@ func clean_rule_v6() {
exec.Command("bash", "-c", `ip6tables-restore --noflush <<-EOF
*nat
-D PREROUTING -m addrtype --dst-type LOCAL -j TRAVERSAL
-F TRAVERSAL
-X TRAVERSAL
-D PREROUTING -m addrtype --dst-type LOCAL -j `+get_conf("chain_name").(string)+`
-F `+get_conf("chain_name").(string)+`
-X `+get_conf("chain_name").(string)+`
COMMIT
EOF`).Run()
}
@@ -35,10 +35,10 @@ func set_rule_v4() {
if out, err := exec.Command("bash", "-c", `iptables-restore --noflush <<-EOF
*nat
:TRAVERSAL -
-I TRAVERSAL -p tcp -m tcp --dport `+strconv.FormatUint(uint64(local_port), 10)+` -j REDIRECT --to-ports `+strconv.FormatUint(uint64(redir_port), 10)+`
-I TRAVERSAL -p udp -m udp --dport `+strconv.FormatUint(uint64(local_port), 10)+` -j REDIRECT --to-ports `+strconv.FormatUint(uint64(redir_port), 10)+`
-A PREROUTING -m addrtype --dst-type LOCAL -j TRAVERSAL
:`+get_conf("chain_name").(string)+` -
-I `+get_conf("chain_name").(string)+` -p tcp -m tcp --dport `+strconv.FormatUint(uint64(local_port), 10)+` -j REDIRECT --to-ports `+strconv.FormatUint(uint64(redir_port), 10)+`
-I `+get_conf("chain_name").(string)+` -p udp -m udp --dport `+strconv.FormatUint(uint64(local_port), 10)+` -j REDIRECT --to-ports `+strconv.FormatUint(uint64(redir_port), 10)+`
-A PREROUTING -m addrtype --dst-type LOCAL -j `+get_conf("chain_name").(string)+`
COMMIT
EOF`).CombinedOutput(); err != nil {
log.Fatalln("iptablesi-restore return a non-zero value while setting ipv4 rules:", string(out))
@@ -49,8 +49,8 @@ func set_rule_v6() {
if out, err := exec.Command("bash", "-c", `ip6tables-restore --noflush <<-EOF
*nat
:TRAVERSAL -
-A PREROUTING -m addrtype --dst-type LOCAL -j TRAVERSAL
:`+get_conf("chain_name").(string)+` -
-A PREROUTING -m addrtype --dst-type LOCAL -j `+get_conf("chain_name").(string)+`
COMMIT
EOF`).CombinedOutput(); err != nil {
log.Fatalln("ip6tablesi-restore return a non-zero value while setting ipv6 rules:", string(out))
@@ -61,9 +61,9 @@ func modify_rule_v6(external_port uint16, redir_port uint16) {
if out, err := exec.Command("bash", "-c", `ip6tables-restore --noflush <<-EOF
*nat
-F TRAVERSAL
-I TRAVERSAL -p tcp -m tcp --dport `+strconv.FormatUint(uint64(external_port), 10)+` -j REDIRECT --to-ports `+strconv.FormatUint(uint64(redir_port), 10)+`
-I TRAVERSAL -p udp -m udp --dport `+strconv.FormatUint(uint64(external_port), 10)+` -j REDIRECT --to-ports `+strconv.FormatUint(uint64(redir_port), 10)+`
-F `+get_conf("chain_name").(string)+`
-I `+get_conf("chain_name").(string)+` -p tcp -m tcp --dport `+strconv.FormatUint(uint64(external_port), 10)+` -j REDIRECT --to-ports `+strconv.FormatUint(uint64(redir_port), 10)+`
-I `+get_conf("chain_name").(string)+` -p udp -m udp --dport `+strconv.FormatUint(uint64(external_port), 10)+` -j REDIRECT --to-ports `+strconv.FormatUint(uint64(redir_port), 10)+`
COMMIT
EOF`).CombinedOutput(); err != nil {
log.Fatalln("ip6tablesi-restore return a non-zero value while modifying ipv6 rules:", string(out))

View File

@@ -1,10 +1,10 @@
package main
import (
"context"
"flag"
"log"
"net"
"context"
"os"
"os/signal"
"strconv"
@@ -23,6 +23,7 @@ func main() {
var udp_mode bool
var print_version bool
var output string
var chain_name string
flag.StringVar(&stun_server, "s", "stun.mixvoip.com:3478", "stun server address in [addr:port] format, must support stun over tcp.")
flag.Uint64Var(&local_port_64, "l", 12345, "local port")
flag.Uint64Var(&redir_port_64, "r", 14885, "redir port")
@@ -32,6 +33,7 @@ func main() {
flag.BoolVar(&udp_mode, "u", false, "enable udp mode")
flag.BoolVar(&print_version, "v", false, "show current version")
flag.StringVar(&output, "o", "./external.port", "Write output to <file-path>")
flag.StringVar(&chain_name, "n", "TRAV", "iptables chain name, only works on linux")
flag.Parse()
if print_version {
println("trav", version)
@@ -60,6 +62,7 @@ func main() {
set_conf("output", output)
set_conf("server_ip", server_ip)
set_conf("server_port", server_port)
set_conf("chain_name", chain_name)
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)