PrivilegedPort.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package de.tudarmstadt.informatik.hostage.system;
  2. import java.io.BufferedReader;
  3. import java.io.FileDescriptor;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import android.net.LocalServerSocket;
  8. import android.net.LocalSocket;
  9. import android.util.Log;
  10. public class PrivilegedPort implements Runnable {
  11. public static enum TYPE {
  12. TCP, UDP
  13. }
  14. private final static String UNIX_PATH = "hostage";
  15. private final String LOG_TAG;
  16. private TYPE type;
  17. private int port;
  18. private FileDescriptor fd;
  19. public PrivilegedPort(TYPE type, int port) {
  20. LOG_TAG = String.format("HosTaGe (PortBinder %s %d)", type.toString(), port);
  21. this.type = type;
  22. this.port = port;
  23. try {
  24. new Thread(this).start();
  25. LocalServerSocket localServer = new LocalServerSocket(UNIX_PATH);
  26. LocalSocket localSocket = localServer.accept();
  27. while (localSocket.getInputStream().read() != -1)
  28. ;
  29. FileDescriptor[] fdArray;
  30. fdArray = localSocket.getAncillaryFileDescriptors();
  31. if (fdArray != null) {
  32. this.fd = fdArray[0];
  33. }
  34. localSocket.close();
  35. localServer.close();
  36. } catch (IOException e) {
  37. }
  38. }
  39. public FileDescriptor getFD() {
  40. return fd;
  41. }
  42. @Override
  43. public void run() {
  44. /*@Shankar TODO: Update the location of PortBinder to GUI-Input*/
  45. String command = String.format("/data/local/bind %s %d", type.toString(), port);
  46. try {
  47. Process p = new ProcessBuilder("su", "-c", command).start();
  48. if (p.waitFor() != 0) {
  49. logError(p.getErrorStream());
  50. }
  51. logOutput(p.getInputStream());
  52. } catch (IOException e) {
  53. } catch (InterruptedException e) {
  54. }
  55. }
  56. private void logOutput(InputStream stdout) throws IOException {
  57. BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
  58. String line;
  59. while ((line = reader.readLine()) != null) {
  60. Log.d(LOG_TAG, line);
  61. }
  62. }
  63. private void logError(InputStream stderr) throws IOException {
  64. BufferedReader reader = new BufferedReader(new InputStreamReader(stderr));
  65. Log.e(LOG_TAG, reader.readLine());
  66. }
  67. }