NetGuard/app/src/main/java/eu/faircode/netguard/ActivityForwardApproval.java

131 lines
4.8 KiB
Java
Raw Normal View History

2016-02-06 15:17:31 +00:00
package eu.faircode.netguard;
/*
This file is part of NetGuard.
NetGuard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
2018-12-31 16:12:49 +00:00
Copyright 2015-2019 by Marcel Bokhorst (M66B)
2016-02-06 15:17:31 +00:00
*/
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
2016-02-08 15:34:54 +00:00
import android.util.Log;
2016-02-06 15:17:31 +00:00
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.net.InetAddress;
2016-02-09 09:53:52 +00:00
public class ActivityForwardApproval extends Activity {
2016-02-06 15:17:31 +00:00
private static final String TAG = "NetGuard.Forward";
private static final String ACTION_START_PORT_FORWARD = "eu.faircode.netguard.START_PORT_FORWARD";
private static final String ACTION_STOP_PORT_FORWARD = "eu.faircode.netguard.STOP_PORT_FORWARD";
static {
2017-10-28 14:08:06 +00:00
try {
System.loadLibrary("netguard");
} catch (UnsatisfiedLinkError ignored) {
System.exit(1);
}
2016-02-06 15:17:31 +00:00
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2016-02-09 09:53:52 +00:00
setContentView(R.layout.forwardapproval);
2016-02-06 15:17:31 +00:00
final int protocol = getIntent().getIntExtra("protocol", 0);
2016-02-08 15:34:54 +00:00
final int dport = getIntent().getIntExtra("dport", 0);
String addr = getIntent().getStringExtra("raddr");
final int rport = getIntent().getIntExtra("rport", 0);
final int ruid = getIntent().getIntExtra("ruid", 0);
final String raddr = (addr == null ? "127.0.0.1" : addr);
2016-02-06 15:17:31 +00:00
try {
InetAddress iraddr = InetAddress.getByName(raddr);
if (rport < 1024 && (iraddr.isLoopbackAddress() || iraddr.isAnyLocalAddress()))
throw new IllegalArgumentException("Port forwarding to privileged port on local address not possible");
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
finish();
}
2016-02-06 15:17:31 +00:00
String pname;
if (protocol == 6)
pname = getString(R.string.menu_protocol_tcp);
else if (protocol == 17)
pname = getString(R.string.menu_protocol_udp);
else
pname = Integer.toString(protocol);
2017-08-05 08:27:27 +00:00
TextView tvForward = findViewById(R.id.tvForward);
2016-02-06 15:17:31 +00:00
if (ACTION_START_PORT_FORWARD.equals(getIntent().getAction()))
2016-02-08 15:34:54 +00:00
tvForward.setText(getString(R.string.msg_start_forward,
pname, dport, raddr, rport,
TextUtils.join(", ", Util.getApplicationNames(ruid, this))));
2016-02-06 15:17:31 +00:00
else
2016-02-08 15:34:54 +00:00
tvForward.setText(getString(R.string.msg_stop_forward, pname, dport));
2016-02-06 15:17:31 +00:00
2017-08-05 08:27:27 +00:00
Button btnOk = findViewById(R.id.btnOk);
Button btnCancel = findViewById(R.id.btnCancel);
2016-02-06 15:17:31 +00:00
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ACTION_START_PORT_FORWARD.equals(getIntent().getAction())) {
2016-02-08 15:34:54 +00:00
/*
am start -a eu.faircode.netguard.START_PORT_FORWARD \
2016-02-09 09:53:52 +00:00
-n eu.faircode.netguard/eu.faircode.netguard.ActivityForwardApproval \
2016-02-08 15:34:54 +00:00
--ei protocol 17 \
--ei dport 53 \
--es raddr 8.8.4.4 \
--ei rport 53 \
--ei ruid 9999 \
2016-02-08 15:34:54 +00:00
--user 0
*/
Log.i(TAG, "Start forwarding protocol " + protocol + " port " + dport + " to " + raddr + "/" + rport + " uid " + ruid);
DatabaseHelper dh = DatabaseHelper.getInstance(ActivityForwardApproval.this);
2016-02-08 15:34:54 +00:00
dh.deleteForward(protocol, dport);
dh.addForward(protocol, dport, raddr, rport, ruid);
2016-02-06 15:17:31 +00:00
} else if (ACTION_STOP_PORT_FORWARD.equals(getIntent().getAction())) {
2016-02-08 15:34:54 +00:00
/*
am start -a eu.faircode.netguard.STOP_PORT_FORWARD \
2016-02-09 09:53:52 +00:00
-n eu.faircode.netguard/eu.faircode.netguard.ActivityForwardApproval \
2016-02-08 15:34:54 +00:00
--ei protocol 17 \
--ei dport 53 \
--user 0
*/
Log.i(TAG, "Stop forwarding protocol " + protocol + " port " + dport);
DatabaseHelper.getInstance(ActivityForwardApproval.this).deleteForward(protocol, dport);
2016-02-06 15:17:31 +00:00
}
2017-07-22 12:48:00 +00:00
ServiceSinkhole.reload("forwarding", ActivityForwardApproval.this, false);
2016-02-08 15:34:54 +00:00
2016-02-06 15:17:31 +00:00
finish();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}