Thursday, November 24, 2011

Running PAW on Port 80

PAW is only running on ports above 1023, because restricted ports are not permitted for normal apps.
For root users there is a solution by using iptables via the Superuser app.

I'm not sure if the below mentioned solution is working on all rooted devices.
Here is my configuration: Notion Ink Adam running AdamComb v0.3

If this works for you can easily be tested by copying the below code into the BeanShell Console of the PAW web application:

import de.fun2code.android.pawserver.util.*;

execRootShell(String[] commands) {
  shellCmd = "su -c sh";

  sh = Runtime.getRuntime().exec(shellCmd);
  os = sh.getOutputStream();

  for(cmd : commands) {
    os.write((cmd + "\n").getBytes());
  }

  os.write(("exit\n").getBytes());
  os.flush();
  sh.waitFor();  
}

/* ---- Config -------- */
ip = Utils.getLocalIpAddress();
redirectFrom = 80;
redirectTo = 8080;
action = "ADD"; // Can be ADD or DELETE
/* -------------------- */

action = "-" + action.substring(0, 1);

String[] iptablesCmds = new String[] {
  "iptables -t nat " + action + " OUTPUT -d 127.0.0.1 -p tcp --dport " + redirectFrom + " -j REDIRECT --to-ports " + redirectTo,
  "iptables -t nat " + action + " OUTPUT -d " + ip + " -p tcp --dport " + redirectFrom + " -j REDIRECT --to-ports " + redirectTo,
  "iptables -t nat " + action + " PREROUTING -d " + ip + " -p tcp --dport " + redirectFrom + " -j REDIRECT --to-ports " + redirectTo
};

execRootShell(iptablesCmds);

After executing the code, PAW should respond to requests on port 80.
If not, this solution seems not to be working on your configuration.

To forward port 8080 to port 80 every time PAW starts up, create a file called S_forward.bsh inside the /sdcard/paw/etc/init/0 directory.
In order to stop the forwarding when the PAW service shuts down, create an additinal file called K_forward.bsh and copy the same code.
You only have to change the line action = "ADD"; to action = "DELETE"; to stop the forwarding.

Hope that's not just working on the Adam...

PHP Plug-in Update

My cross compiled version of the PHP CGI was not working so well and I didn't manage to fix it.

Fortunately Klaas Biker informed me that Iulian Virtejanu has cross compiled a PHP CGI version that seems to work significantly better.
Here is Iulian's blog entry: PHP and Lighttpd for Android

Iulian gave me the permission to use it in PAW, so I have updated the PHP Plug-in to version 0.3.

There is one known issue:

system(...) does not work.
It is because system('pwd') actually invokes a hardcoded /bin/sh -c 'pwd' - and /bin/sh is not available on Android.
Iulian has submitted a bug report to PHP: https://bugs.php.net/bug.php?id=60081

If you find more bugs, please let me know. I will forward them.