3

In order to open a socket on a port < 1025 i need root access. Exactly how do i give my application this priviledge? I know i can run the su command in a shell to get root access, but is that all i need to do to open a socket on a port < 1025? Or do i need to run some sort of command like su open socket 0.0.0.0:80 or something? I am so confused as there are such little resources out there for this. What is the purpose of google not pre-rooting all of their devices?

Here is what i know i can do to get root access. http://www.youtube.com/watch?v=jmfvX8zvsS0&list=UUk1SpWNzOs4MYmr0uICEntg&index=4&feature=plcp

EDIT: I just wrote this:

public class RootTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            String cmds[] = {"su", "-c", "ls /system/app"};
            Shell shell = new Shell();
            String text = shell.sendShellCommand(cmds);
            TextView textView = (TextView)findViewById(R.id.textView);
            textView.setText(text);
            try {
                ServerSocket socket = new ServerSocket(80);
                Log.d("Root", "Opened socket on port 80!!!");
            }catch(Exception e) {
                Log.d("Root", "Failed to open socket on port 80!!!");
                e.printStackTrace();
            }
        }

    });
  }
}

What this does is it runs the commands su, -c, and then ls /system/app when a button is clicked. Then it gets the output and shows it on a textview (which actually works. I got the su prompt and selected accept and it listed everything in /system/apps/). But then after that i attempt to open a socket on port 80 and it still says Permission Denied. The application has root access, but still cannot open the socket! In the logcat it printed out "Failed to open socket on port 80!!!" as i told it to do when the exception is caught. What can i do to open the socket on port 80 without permission denied?

4

1 回答 1

9

As per https://serverfault.com/questions/112795/how-can-i-run-a-server-on-linux-on-port-80-as-a-normal-user

I ran this command as root:

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

And it worked! I opened a socket on port 8080 and when connecting to my phone on port 80 all traffic got redirected to 8080! WOW!

于 2012-05-24T06:33:45.117 回答