Browse Source

added cleaning of the nat table after the service is terminated and added a helper function to calculate the redirected port

Daniel Lazar 9 years ago
parent
commit
3df54677bf

+ 1 - 0
src/de/tudarmstadt/informatik/hostage/Listener.java

@@ -169,6 +169,7 @@ public class Listener implements Runnable {
 			running = false;
 			service.notifyUI(this.getClass().getName(),
 					new String[] { service.getString(R.string.broadcast_stopped), protocol.toString(), Integer.toString(port) });
+            HelperUtils.deletePort(protocol.toString(),port);
 		} catch (IOException e) {
 		}
 	}

+ 15 - 0
src/de/tudarmstadt/informatik/hostage/commons/HelperUtils.java

@@ -448,4 +448,19 @@ public final class HelperUtils {
 
         return success;
     }
+
+    public static boolean deletePort(String protocol, int From) throws IOException {
+        assert protocol.equals("tcp") || protocol.equals("udp");
+        int To = getRedirectedPort(From);
+        boolean success = Device.isRooted();
+        
+        new ProcessBuilder("su","-c","iptables -t nat -D PREROUTING -p "+protocol+" --dport "+From+" -j REDIRECT --to-ports "+To).start();
+        new ProcessBuilder("su","-c","iptables -t nat -D OUTPUT -p "+protocol+" --dport "+To+" -j REDIRECT --to-ports "+From).start();
+
+        return success;
+    }
+
+    public static int getRedirectedPort(int port){
+        return port + 1024 + 27113;
+    }
 }