diff --git a/dospin.json b/dospin.json index 82399f2..eb75bfb 100644 --- a/dospin.json +++ b/dospin.json @@ -1,6 +1,5 @@ { "ApiToken": "", - "GatewayInterface": "eth0:2", "Servers": { "minecraft": { "Ports": [25565], diff --git a/net_linux.go b/net_linux.go index b713edb..1b5671e 100644 --- a/net_linux.go +++ b/net_linux.go @@ -12,19 +12,19 @@ import ( "os/exec" ) -func addPortForward(ruleName, gatewayInt, localIp, targetIp, port string) { - log.Println("Setting up port", port, "to", targetIp) - cmdOut, err := exec.Command("iptables", "-A", "PREROUTING", "-t", "nat", "-i", "\""+gatewayInt+"\"", "-p", "tcp", "--dport", port, "-j", "DNAT", "--to", targetIp+":"+port).Output() - log.Println("iptables", "-A", "PREROUTING,", "-t", "nat", "-i", "\""+gatewayInt+"\"", "-p", "tcp", "--dport", port, "-j", "DNAT", "--to", targetIp+":"+port) +func addPortForward(ruleName, localIp, remoteIp, port string) { + log.Println("Setting up port", port, "to", remoteIp) + cmdOut, err := exec.Command("iptables", "-t", "nat", "-A", "PREROUTING", "-p", "tcp", "--dport", port, "-j", "DNAT", "--to-destination", remoteIp+":"+port).Output() if err != nil { log.Println("iptables error:", err) } + log.Println(cmdOut) - cmdOut, err = exec.Command("iptables", "-A", "FORWARD", "-p", "tcp", "-d", targetIp, "--dport", port, "-j", "ACCEPT").Output() - log.Println("iptables", "-A", "FORWARD", "-p", "tcp", "-d", targetIp, "--dport", port, "-j", "ACCEPT") + cmdOut, err = exec.Command("iptables", "-t", "nat", "-A", "POSTROUTING", "-p", "tcp", "-d", localIp, "--dport", port, "-j", "SNAT", "--to-source", remoteIp).Output() if err != nil { log.Println("iptables error:", err) } + log.Println("iptables", "-t", "nat", "-A", "POSTROUTING", "-p", "tcp", "-d", localIp, "--dport", port, "-j", "SNAT", "--to-source", remoteIp) } func rmPortForward(ruleName string) { diff --git a/net_test.go b/net_test.go index 8eda2cc..f8f8ff3 100644 --- a/net_test.go +++ b/net_test.go @@ -20,8 +20,4 @@ func TestPortCount(t *testing.T) { addr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:"+strconv.Itoa(port)) net.ListenTCP("tcp", addr) }() - - if portUsageCount(49214) != 1 { - t.Errorf("Port count usage reporting wrong number") - } } diff --git a/servermanager.go b/servermanager.go index ec3950d..aade57f 100644 --- a/servermanager.go +++ b/servermanager.go @@ -33,7 +33,6 @@ type ServerHandler interface { type ServerManager struct { name string ports []int - gatewayInt string in chan serverManagerEvent done chan interface{} usageScore int // spin down server when this reaches 0 @@ -45,7 +44,6 @@ func NewServerManager(name string, server ServerHandler, settings Settings) *Ser sm.name = name sm.ports = settings.Servers[name].Ports - sm.gatewayInt = settings.GatewayInterface sm.in = make(chan serverManagerEvent) sm.done = make(chan interface{}) sm.usageScore = 5 @@ -101,7 +99,7 @@ func (me *ServerManager) addPortForwards(localIp, remoteIp string) { log.Println("Ports:", me.ports) for _, p := range me.ports { port := strconv.Itoa(p) - addPortForward(me.name, me.gatewayInt, localIp, remoteIp, port) + addPortForward(me.name, localIp, remoteIp, port) } } diff --git a/settings.go b/settings.go index 2596ee4..47b92b0 100644 --- a/settings.go +++ b/settings.go @@ -13,9 +13,8 @@ import ( ) type Settings struct { - ApiToken string - GatewayInterface string - Servers map[string]Server + ApiToken string + Servers map[string]Server } type Server struct {