Commit 2a63fe5f authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Added a dialog which asks for root access and redirects a port < 1024

parent b8724db8
......@@ -40,4 +40,5 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:cardview-v7:27.0.2'
compile 'com.android.support:recyclerview-v7:27.0.2'
compile 'eu.chainfire:libsuperuser:1.0.0.+'
}
......@@ -15,9 +15,9 @@ import android.widget.TextView;
import com.frostnerd.dnsserver.R;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.dialogs.RootRequestDialog;
import com.frostnerd.dnsserver.services.ServerService;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
......@@ -100,10 +100,14 @@ public class ServersAdapter extends RecyclerView.Adapter<ServersAdapter.ViewHold
startStopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String cmd = setting.isServerRunning() ? ServerService.COMMAND_STOP_SERVER : ServerService.COMMAND_START_SERVER;
Intent intent = new Intent(context, ServerService.class).putExtra(cmd, setting.getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)context.startForegroundService(intent);
else context.startService(new Intent(context, ServerService.class));
if(setting.getPort() <= 1024){
new RootRequestDialog(context, setting);
}else{
String cmd = setting.isServerRunning() ? ServerService.COMMAND_STOP_SERVER : ServerService.COMMAND_START_SERVER;
Intent intent = new Intent(context, ServerService.class).putExtra(cmd, setting.getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)context.startForegroundService(intent);
else context.startService(intent);
}
}
});
}
......
package com.frostnerd.dnsserver.dialogs;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import com.frostnerd.dnsserver.R;
import com.frostnerd.dnsserver.database.entities.main.DNSServerSetting;
import com.frostnerd.dnsserver.services.ServerService;
import com.frostnerd.utils.networking.NetworkUtil;
import com.frostnerd.utils.preferences.Preferences;
import java.io.IOException;
import java.util.List;
import eu.chainfire.libsuperuser.Shell;
/**
* Copyright Daniel Wolf 2018
* All rights reserved.
* Code may NOT be used without proper permission, neither in binary nor in source form.
* All redistributions of this software in source code must retain this copyright header
* All redistributions of this software in binary form must visibly inform users about usage of this software
* <p>
* development@frostnerd.com
*/
public class RootRequestDialog extends AlertDialog {
private CheckBox dontAskAgain;
public RootRequestDialog(final Context context, final DNSServerSetting server) {
super(context);
if(Preferences.getBoolean(context, "dont_ask_root", false)){
doStuff(server, context);
}else{
setTitle(R.string.dialog_title_root_needed);
setButton(BUTTON_NEGATIVE, context.getString(R.string.cancel), (OnClickListener) null);
setButton(BUTTON_POSITIVE, context.getString(R.string.ok), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(dontAskAgain.isChecked())Preferences.put(context, "dont_ask_root", true);
doStuff(server, context);
}
});
View content = getLayoutInflater().inflate(R.layout.dialog_root_required, null, false);
setView(content);
((TextView)content.findViewById(R.id.text)).setText(R.string.root_needed_explain);
dontAskAgain = content.findViewById(R.id.dont_show_again);
show();
}
}
//Come on, do stuff!
private void doStuff(DNSServerSetting server, Context context){
int port = NetworkUtil.getFirstFreePort(server.isUdp(), 10000, 30000);
String cmd = "iptables -t nat -A PREROUTING -i eth0 -p [trspt] --dport [src] -j REDIRECT --to-port [dst]";
cmd = cmd.replace("[trsprt]", server.isUdp() ? "udp" : "tcp").
replace("[src]", server.getPort() + "").
replace("[dst]", port + "");
server.setLocalRedirectPort(port);
List<String> result = Shell.SU.run(cmd);
if(result != null && result.size() == 0){
Intent intent = new Intent(context, ServerService.class);
intent.putExtra(ServerService.COMMAND_START_SERVER, server.getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)context.startForegroundService(intent);
else context.startService(intent);
}
dismiss();
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<CheckBox
android:id="@+id/dont_show_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_below="@id/text"
android:text="@string/dont_show_again" />
</RelativeLayout>
</ScrollView>
\ No newline at end of file
......@@ -8,4 +8,7 @@
<string name="notification_channel_hiddenicon">Channel ohne Icon</string>
<string name="notification_channel_hiddenicon_description">Dieser Channel zeigt kein Symbol in der Benachrichtigungsleiste</string>
<string name="x_servers_running">[x] servers aktiv</string>
<string name="dialog_title_root_needed">Root benötigt</string>
<string name="root_needed_explain">Rootzugriff wird benötigt, um auf Ports unter 1024 zuzugreifen. Du wirst im nächsten Fenster gefragt dieser App Rootrechte zu erteilen. Ist dein Gerät nicht gerootet oder du verbietest dies wird der ausgewählte Server nicht starten.</string>
<string name="dont_show_again">Nicht nochmal zeigen</string>
</resources>
\ No newline at end of file
......@@ -7,4 +7,7 @@
<string name="notification_channel_default">Default Channel</string>
<string name="notification_channel_default_description">Default channel for posting notifications.</string>
<string name="x_servers_running">[x] servers running</string>
<string name="dialog_title_root_needed">Root needed</string>
<string name="root_needed_explain">Root access is needed to access ports below 1024. You\'ll be asked to grant root permission to this app in the next window. If you deny it or your device is not rooted the selected server won\'t be startet.</string>
<string name="dont_show_again">Don\'t show again</string>
</resources>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment