(ipt *iptables.IPTables, sourcePort int, destIP string, destPort int)
| 39 | } |
| 40 | |
| 41 | func AddRules(ipt *iptables.IPTables, sourcePort int, destIP string, destPort int) { |
| 42 | err := ipt.Append( |
| 43 | "nat", |
| 44 | "PREROUTING", |
| 45 | "-p", "tcp", "--dport", strconv.Itoa(sourcePort), "-j", "DNAT", |
| 46 | "--to-destination", fmt.Sprintf("%s:%d", destIP, destPort), |
| 47 | ) |
| 48 | if err != nil { |
| 49 | logrus.Errorf("Error adding a PREROUTING rule for %d->%s:%d - %s", sourcePort, destIP, destPort, err.Error()) |
| 50 | } |
| 51 | logrus.Debugf("Added IP table rule for external traffic any:%d -> %s:%d", sourcePort, destIP, destPort) |
| 52 | |
| 53 | err = ipt.Append( |
| 54 | "nat", |
| 55 | "OUTPUT", |
| 56 | "-p", "tcp", "-o", "lo", "--dport", strconv.Itoa(sourcePort), "-j", "DNAT", |
| 57 | "--to-destination", fmt.Sprintf("%s:%d", destIP, destPort), |
| 58 | ) |
| 59 | if err != nil { |
| 60 | logrus.Errorf("Error adding an OUTPUT rule for %d->%s:%d - %s", sourcePort, destIP, destPort, err.Error()) |
| 61 | } |
| 62 | logrus.Debugf("Added IP table rule for localhost traffic any:%d -> %s:%d", sourcePort, destIP, destPort) |
| 63 | |
| 64 | err = ipt.AppendUnique( |
| 65 | "nat", |
| 66 | "POSTROUTING", |
| 67 | "-j", "MASQUERADE", |
| 68 | ) |
| 69 | if err != nil { |
| 70 | logrus.Errorf("Error adding a POSTROUTING MASQUERADE - %s", err.Error()) |
| 71 | } |
| 72 | |
| 73 | err = exec.Command("sudo", "iptables", "-P", "FORWARD", "ACCEPT").Run() |
| 74 | if err != nil { |
| 75 | logrus.Errorf("Error changing IP routing policy FORWARD ACCEPT - %s", err.Error()) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func DeleteRules(ipt *iptables.IPTables, sourcePort int, destIP string, destPort int) { |
| 80 | err := ipt.Delete( |
no outgoing calls
no test coverage detected