From c24e1fb24e7d4ea6f1d5f785f33ba8bc65199623 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Tue, 19 May 2020 10:13:42 +0200 Subject: [PATCH] linux raw ip support --- .../hostreplacer/ipmapping/EmptyIpMapper.java | 2 +- .../hostreplacer/ipmapping/IpMapper.java | 21 +++++++++++---- .../ipmapping/IpMapperFactory.java | 1 + .../hostreplacer/ipmapping/LinuxIpMapper.java | 27 +++++++++++++++++++ .../ipmapping/WindowsIpMapper.java | 12 +-------- 5 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/LinuxIpMapper.java diff --git a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/EmptyIpMapper.java b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/EmptyIpMapper.java index 3b825f5..1017b92 100644 --- a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/EmptyIpMapper.java +++ b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/EmptyIpMapper.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.List; // Temporary class for the sake of not getting nullpointers on linux&mac until they have an IpMapper as well -public class EmptyIpMapper implements IpMapper { +public class EmptyIpMapper extends IpMapper { @Override public void enable() { diff --git a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/IpMapper.java b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/IpMapper.java index 360bd95..e5eb9c2 100644 --- a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/IpMapper.java +++ b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/IpMapper.java @@ -1,16 +1,27 @@ package gearth.protocol.hostreplacer.ipmapping; +import java.io.IOException; import java.util.List; // always map to 127.0.0.1, same port -public interface IpMapper { +public abstract class IpMapper { - void enable(); + void runCommand(String... args) { + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(args); + try { + processBuilder.start(); + } catch (IOException e) { + e.printStackTrace(); + } + } - void addMapping(String ip); + abstract public void enable(); - void deleteMapping(String ip); + abstract public void addMapping(String ip); - List getCurrentMappings(); + abstract public void deleteMapping(String ip); + + abstract public List getCurrentMappings(); } diff --git a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/IpMapperFactory.java b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/IpMapperFactory.java index c70ff30..319ef86 100644 --- a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/IpMapperFactory.java +++ b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/IpMapperFactory.java @@ -7,6 +7,7 @@ public class IpMapperFactory { public static IpMapper get() { if (OSValidator.isWindows()) return new WindowsIpMapper(); + if (OSValidator.isUnix()) return new LinuxIpMapper(); return new EmptyIpMapper(); } diff --git a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/LinuxIpMapper.java b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/LinuxIpMapper.java new file mode 100644 index 0000000..f7e1229 --- /dev/null +++ b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/LinuxIpMapper.java @@ -0,0 +1,27 @@ +package gearth.protocol.hostreplacer.ipmapping; + +import java.util.List; + +public class LinuxIpMapper extends IpMapper { + @Override + public void enable() { + + } + + @Override + public void addMapping(String ip) { + runCommand("iptables", "-t", "nat", "-A", "OUTPUT", + "-p", "all", "-d", ip, "-j", "DNAT", "--to-destination", "127.0.0.1"); + } + + @Override + public void deleteMapping(String ip) { + runCommand("iptables", "-t", "nat", "-D", "OUTPUT", + "-p", "all", "-d", ip, "-j", "DNAT", "--to-destination", "127.0.0.1"); + } + + @Override + public List getCurrentMappings() { + return null; + } +} diff --git a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/WindowsIpMapper.java b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/WindowsIpMapper.java index 6271dda..fcf44ed 100644 --- a/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/WindowsIpMapper.java +++ b/G-Earth/src/main/java/gearth/protocol/hostreplacer/ipmapping/WindowsIpMapper.java @@ -4,17 +4,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -public class WindowsIpMapper implements IpMapper { - - private void runCommand(String... args) { - ProcessBuilder processBuilder = new ProcessBuilder(); - processBuilder.command(args); - try { - processBuilder.start(); - } catch (IOException e) { - e.printStackTrace(); - } - } +public class WindowsIpMapper extends IpMapper { @Override public void enable() {