mirror of
https://github.com/M66B/NetGuard.git
synced 2025-01-01 12:54:07 +00:00
parent
c4914c5651
commit
9bab61ae3c
5 changed files with 87 additions and 41 deletions
|
@ -31,15 +31,21 @@ import android.view.MenuInflater;
|
|||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ListView;
|
||||
import android.widget.PopupMenu;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ActivityForwarding extends AppCompatActivity {
|
||||
private static final String TAG = "NetGuard.forwarding";
|
||||
|
||||
private boolean running;
|
||||
private ListView lvForwarding;
|
||||
private ForwardingAdapter adapter;
|
||||
|
@ -121,7 +127,32 @@ public class ActivityForwarding extends AppCompatActivity {
|
|||
final EditText etDPort = (EditText) view.findViewById(R.id.etDPort);
|
||||
final EditText etRAddr = (EditText) view.findViewById(R.id.etRAddr);
|
||||
final EditText etRPort = (EditText) view.findViewById(R.id.etRPort);
|
||||
final EditText etRUid = (EditText) view.findViewById(R.id.etRUid);
|
||||
final ProgressBar pbRuid = (ProgressBar) view.findViewById(R.id.pbRUid);
|
||||
final Spinner spRuid = (Spinner) view.findViewById(R.id.spRUid);
|
||||
|
||||
final AsyncTask task = new AsyncTask<Object, Object, List<Rule>>() {
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
pbRuid.setVisibility(View.VISIBLE);
|
||||
spRuid.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Rule> doInBackground(Object... objects) {
|
||||
return Rule.getRules(false, ActivityForwarding.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<Rule> rules) {
|
||||
ArrayAdapter spinnerArrayAdapter =
|
||||
new ArrayAdapter(ActivityForwarding.this,
|
||||
android.R.layout.simple_spinner_item, rules);
|
||||
spRuid.setAdapter(spinnerArrayAdapter);
|
||||
pbRuid.setVisibility(View.GONE);
|
||||
spRuid.setVisibility(View.VISIBLE);
|
||||
}
|
||||
};
|
||||
task.execute();
|
||||
|
||||
dialog = new AlertDialog.Builder(this)
|
||||
.setView(view)
|
||||
|
@ -136,7 +167,7 @@ public class ActivityForwarding extends AppCompatActivity {
|
|||
final int dport = Integer.parseInt(etDPort.getText().toString());
|
||||
final String raddr = etRAddr.getText().toString();
|
||||
final int rport = Integer.parseInt(etRPort.getText().toString());
|
||||
final int ruid = Integer.parseInt(etRUid.getText().toString());
|
||||
final int ruid = ((Rule) spRuid.getSelectedItem()).info.applicationInfo.uid;
|
||||
new AsyncTask<Object, Object, Throwable>() {
|
||||
@Override
|
||||
protected Throwable doInBackground(Object... objects) {
|
||||
|
@ -170,6 +201,7 @@ public class ActivityForwarding extends AppCompatActivity {
|
|||
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
task.cancel(false);
|
||||
dialog.dismiss();
|
||||
}
|
||||
})
|
||||
|
|
|
@ -476,7 +476,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
|
|||
|
||||
@Override
|
||||
protected List<Rule> doInBackground(Object... arg) {
|
||||
return Rule.getRules(false, TAG, ActivityMain.this);
|
||||
return Rule.getRules(false, ActivityMain.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -56,17 +56,17 @@ public class Rule {
|
|||
public boolean enabled;
|
||||
public Intent intent;
|
||||
|
||||
public boolean wifi_default;
|
||||
public boolean other_default;
|
||||
public boolean screen_wifi_default;
|
||||
public boolean screen_other_default;
|
||||
public boolean roaming_default;
|
||||
public boolean wifi_default = false;
|
||||
public boolean other_default = false;
|
||||
public boolean screen_wifi_default = false;
|
||||
public boolean screen_other_default = false;
|
||||
public boolean roaming_default = false;
|
||||
|
||||
public boolean wifi_blocked;
|
||||
public boolean other_blocked;
|
||||
public boolean screen_wifi;
|
||||
public boolean screen_other;
|
||||
public boolean roaming;
|
||||
public boolean wifi_blocked = false;
|
||||
public boolean other_blocked = false;
|
||||
public boolean screen_wifi = false;
|
||||
public boolean screen_other = false;
|
||||
public boolean roaming = false;
|
||||
|
||||
public String[] related = null;
|
||||
|
||||
|
@ -94,6 +94,12 @@ public class Rule {
|
|||
this.internet = true;
|
||||
this.enabled = true;
|
||||
this.intent = null;
|
||||
} else if (info.applicationInfo.uid == 9999) {
|
||||
this.name = context.getString(R.string.title_nobody);
|
||||
this.system = true;
|
||||
this.internet = true;
|
||||
this.enabled = true;
|
||||
this.intent = null;
|
||||
} else {
|
||||
this.name = info.applicationInfo.loadLabel(pm).toString();
|
||||
this.system = Util.isSystem(info.packageName, context);
|
||||
|
@ -115,7 +121,7 @@ public class Rule {
|
|||
}
|
||||
}
|
||||
|
||||
public static List<Rule> getRules(boolean all, String tag, Context context) {
|
||||
public static List<Rule> getRules(boolean all, Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE);
|
||||
SharedPreferences other = context.getSharedPreferences("other", Context.MODE_PRIVATE);
|
||||
|
@ -152,7 +158,6 @@ public class Rule {
|
|||
String pkg = xml.getAttributeValue(null, "package");
|
||||
boolean pblocked = xml.getAttributeBooleanValue(null, "blocked", false);
|
||||
pre_wifi_blocked.put(pkg, pblocked);
|
||||
Log.d(tag, "Wifi " + pkg + " blocked=" + pblocked);
|
||||
|
||||
} else if ("other".equals(xml.getName())) {
|
||||
String pkg = xml.getAttributeValue(null, "package");
|
||||
|
@ -160,26 +165,23 @@ public class Rule {
|
|||
boolean proaming = xml.getAttributeBooleanValue(null, "roaming", default_roaming);
|
||||
pre_other_blocked.put(pkg, pblocked);
|
||||
pre_roaming.put(pkg, proaming);
|
||||
Log.d(tag, "Other " + pkg + " blocked=" + pblocked + " roaming=" + proaming);
|
||||
|
||||
} else if ("relation".equals(xml.getName())) {
|
||||
String pkg = xml.getAttributeValue(null, "package");
|
||||
String[] rel = xml.getAttributeValue(null, "related").split(",");
|
||||
pre_related.put(pkg, rel);
|
||||
Log.d(tag, "Relation " + pkg + " related=" + TextUtils.join(",", rel));
|
||||
|
||||
} else if ("type".equals(xml.getName())) {
|
||||
String pkg = xml.getAttributeValue(null, "package");
|
||||
boolean system = xml.getAttributeBooleanValue(null, "system", true);
|
||||
pre_system.put(pkg, system);
|
||||
Log.d(tag, "Type " + pkg + " system=" + system);
|
||||
}
|
||||
|
||||
|
||||
eventType = xml.next();
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
Log.e(tag, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
Util.sendCrashReport(ex, context);
|
||||
}
|
||||
|
||||
|
@ -209,6 +211,16 @@ public class Rule {
|
|||
media.applicationInfo.icon = 0;
|
||||
listPI.add(media);
|
||||
|
||||
// Add nobody
|
||||
PackageInfo nobody = new PackageInfo();
|
||||
nobody.packageName = "nobody";
|
||||
nobody.versionCode = Build.VERSION.SDK_INT;
|
||||
nobody.versionName = Build.VERSION.RELEASE;
|
||||
nobody.applicationInfo = new ApplicationInfo();
|
||||
nobody.applicationInfo.uid = 9999;
|
||||
nobody.applicationInfo.icon = 0;
|
||||
listPI.add(nobody);
|
||||
|
||||
for (PackageInfo info : listPI) {
|
||||
Rule rule = new Rule(info, context);
|
||||
|
||||
|
@ -221,20 +233,8 @@ public class Rule {
|
|||
(show_disabled || rule.enabled) &&
|
||||
info.applicationInfo.uid != Process.myUid())) {
|
||||
|
||||
if (info.applicationInfo.uid == Process.myUid()) {
|
||||
// Internet access is needed to resolve host names
|
||||
rule.wifi_default = false;
|
||||
rule.other_default = false;
|
||||
rule.screen_wifi_default = false;
|
||||
rule.screen_other_default = false;
|
||||
rule.roaming_default = false;
|
||||
|
||||
rule.wifi_blocked = false;
|
||||
rule.other_blocked = false;
|
||||
rule.screen_wifi = false;
|
||||
rule.screen_other = false;
|
||||
rule.roaming = false;
|
||||
} else {
|
||||
// Internet access is needed to resolve host names
|
||||
if (info.applicationInfo.uid != Process.myUid()) {
|
||||
rule.wifi_default = (pre_wifi_blocked.containsKey(info.packageName) ? pre_wifi_blocked.get(info.packageName) : default_wifi);
|
||||
rule.other_default = (pre_other_blocked.containsKey(info.packageName) ? pre_other_blocked.get(info.packageName) : default_other);
|
||||
rule.screen_wifi_default = default_screen_wifi;
|
||||
|
@ -315,4 +315,9 @@ public class Rule {
|
|||
boolean default_roaming = prefs.getBoolean("whitelist_roaming", true);
|
||||
updateChanged(default_wifi, default_other, default_roaming);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -349,7 +349,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
|
|||
state = State.enforcing;
|
||||
Log.d(TAG, "Start foreground state=" + state.toString());
|
||||
|
||||
List<Rule> listRule = Rule.getRules(true, TAG, SinkholeService.this);
|
||||
List<Rule> listRule = Rule.getRules(true, SinkholeService.this);
|
||||
List<Rule> listAllowed = getAllowedRules(listRule);
|
||||
|
||||
vpn = startVPN(listAllowed);
|
||||
|
@ -381,7 +381,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
|
|||
Log.d(TAG, "Start foreground state=" + state.toString());
|
||||
}
|
||||
|
||||
List<Rule> listRule = Rule.getRules(true, TAG, SinkholeService.this);
|
||||
List<Rule> listRule = Rule.getRules(true, SinkholeService.this);
|
||||
List<Rule> listAllowed = getAllowedRules(listRule);
|
||||
|
||||
if (filter &&
|
||||
|
|
|
@ -114,13 +114,22 @@
|
|||
android:text="@string/title_ruid"
|
||||
android:textAppearance="@style/TextSmall" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etRUid"
|
||||
android:layout_width="match_parent"
|
||||
<ProgressBar
|
||||
android:id="@+id/pbRUid"
|
||||
style="@android:style/Widget.ProgressBar.Small"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="9999"
|
||||
android:inputType="number"
|
||||
android:textAppearance="@style/TextMedium" />
|
||||
android:layout_marginTop="8dp"
|
||||
android:visibility="gone" />
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spRUid"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:prompt="@string/title_ruid"
|
||||
android:textAppearance="@style/TextMedium"
|
||||
android:visibility="gone" />
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
</LinearLayout>
|
||||
|
|
Loading…
Reference in a new issue