Repo created

This commit is contained in:
Fr4nz D13trich 2025-11-20 14:05:38 +01:00
parent 51cf8bb4f9
commit ee0cddf35c
548 changed files with 93129 additions and 2 deletions

View file

@ -0,0 +1,77 @@
/*
* Copyright 2012 two forty four a.m. LLC <http://www.twofortyfouram.com>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* <http://www.apache.org/licenses/LICENSE-2.0>
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package dev.ukanth.ufirewall.plugin;
import android.content.Intent;
import android.os.Bundle;
/**
* Helper class to scrub Bundles of invalid extras. This is a workaround for an Android bug:
* <http://code.google.com/p/android/issues/detail?id=16006>.
*/
public final class BundleScrubber
{
/**
* Scrubs Intents for private serializable subclasses in the Intent extras. If the Intent's extras contain
* a private serializable subclass, the Bundle is cleared. The Bundle will not be set to null. If the
* Bundle is null, has no extras, or the extras do not contain a private serializable subclass, the Bundle
* is not mutated.
*
* @param intent {@code Intent} to scrub. This parameter may be mutated if scrubbing is necessary. This
* parameter may be null.
* @return true if the Intent was scrubbed, false if the Intent was not modified.
*/
public static boolean scrub(final Intent intent)
{
if (null == intent)
{
return false;
}
return scrub(intent.getExtras());
}
/**
* Scrubs Bundles for private serializable subclasses in the extras. If the Bundle's extras contain a
* private serializable subclass, the Bundle is cleared. If the Bundle is null, has no extras, or the
* extras do not contain a private serializable subclass, the Bundle is not mutated.
*
* @param bundle {@code Bundle} to scrub. This parameter may be mutated if scrubbing is necessary. This
* parameter may be null.
* @return true if the Bundle was scrubbed, false if the Bundle was not modified.
*/
public static boolean scrub(final Bundle bundle)
{
if (null == bundle)
{
return false;
}
/*
* Note: This is a hack to work around a private serializable classloader attack
*/
try
{
// if a private serializable exists, this will throw an exception
bundle.containsKey(null);
}
catch (final Exception e)
{
bundle.clear();
return true;
}
return false;
}
}

View file

@ -0,0 +1,301 @@
/*
* Copyright 2012 two forty four a.m. LLC <http://www.twofortyfouram.com>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* <http://www.apache.org/licenses/LICENSE-2.0>
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package dev.ukanth.ufirewall.plugin;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import dev.ukanth.ufirewall.Api;
import dev.ukanth.ufirewall.R;
import dev.ukanth.ufirewall.profiles.ProfileData;
import dev.ukanth.ufirewall.profiles.ProfileHelper;
import dev.ukanth.ufirewall.service.RootCommand;
import dev.ukanth.ufirewall.util.G;
/**
* This is the "fire" BroadcastReceiver for a Locale Plug-in setting.
*/
public final class FireReceiver extends BroadcastReceiver {
public static final String TAG = "AFWall";
/**
* @param context {@inheritDoc}.
* @param intent the incoming {@link com.twofortyfouram.locale.Intent#ACTION_FIRE_SETTING} Intent. This
* should contain the {@link com.twofortyfouram.locale.Intent#EXTRA_BUNDLE} that was saved by
* {@link } and later broadcast by Locale.
*/
@Override
public void onReceive(final Context context, final Intent intent) {
/*
* Always be sure to be strict on input parameters! A malicious third-party app could always send an
* empty or otherwise malformed Intent. And since Locale applies settings in the background, the
* plug-in definitely shouldn't crash in the background.
*/
/*
* Locale guarantees that the Intent action will be ACTION_FIRE_SETTING
*/
if (!com.twofortyfouram.locale.Intent.ACTION_FIRE_SETTING.equals(intent.getAction())) {
return;
}
/*
* A hack to prevent a private serializable classloader attack
*/
BundleScrubber.scrub(intent);
BundleScrubber.scrub(intent.getBundleExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE));
final Bundle bundle = intent.getBundleExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);
/*
* Final verification of the plug-in Bundle before firing the setting.
*/
if (PluginBundleManager.isBundleValid(bundle)) {
String index = bundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
String name = null;
if (index.contains("::")) {
String[] msg = index.split("::");
index = msg[0];
name = msg[1];
}
final boolean multimode = G.enableMultiProfile();
final boolean disableToasts = G.disableTaskerToast();
if (!G.isProfileMigrated()) {
if (index != null) {
//int id = Integer.parseInt(index);
switch (index) {
case "0":
Api.applySavedIptablesRules(context, false, new RootCommand()
.setFailureToast(R.string.error_apply)
.setCallback(new RootCommand.Callback() {
@Override
public void cbFunc(RootCommand state) {
Message msg = new Message();
if (state.exitCode == 0) {
msg.arg1 = R.string.rules_applied;
Api.setEnabled(context, true, false);
} else {
// error details are already in logcat
msg.arg1 = R.string.error_apply;
}
sendMessage(msg);
}
}));
break;
case "1":
if (G.protectionLevel().equals("p0")) {
Api.purgeIptables(context, true, new RootCommand()
.setReopenShell(true)
.setCallback(new RootCommand.Callback() {
public void cbFunc(RootCommand state) {
Message msg = new Message();
msg.arg1 = R.string.toast_disabled;
sendMessage(msg);
Api.setEnabled(context, false, false);
}
}));
} else {
Message msg = new Message();
msg.arg1 = R.string.widget_disable_fail;
sendMessage(msg);
}
break;
case "2":
if (multimode) {
G.setProfile(true, "AFWallPrefs");
}
break;
case "3":
if (multimode) {
G.setProfile(true, "AFWallProfile1");
}
break;
case "4":
if (multimode) {
G.setProfile(true, "AFWallProfile2");
}
break;
case "5":
if (multimode) {
G.setProfile(true, "AFWallProfile3");
}
break;
default:
if (multimode) {
G.setProfile(true, name);
}
break;
}
if (Integer.parseInt(index) > 1) {
if (multimode) {
if (Api.isEnabled(context)) {
if (!disableToasts) {
Toast.makeText(context, R.string.tasker_apply, Toast.LENGTH_SHORT).show();
}
Api.applySavedIptablesRules(context, false, new RootCommand()
.setFailureToast(R.string.error_apply)
.setCallback(new RootCommand.Callback() {
@Override
public void cbFunc(RootCommand state) {
Message msg = new Message();
if (state.exitCode == 0) {
msg.arg1 = R.string.tasker_profile_applied;
if (!disableToasts)
sendMessage(msg);
} else {
// error details are already in logcat
msg.arg1 = R.string.error_apply;
}
sendMessage(msg);
}
}));
} else {
Message msg = new Message();
msg.arg1 = R.string.tasker_disabled;
sendMessage(msg);
}
} else {
Message msg = new Message();
msg.arg1 = R.string.tasker_muliprofile;
sendMessage(msg);
}
G.reloadPrefs();
/*if (G.activeNotification()) {
Api.showNotification(Api.isEnabled(context), context);
}*/
Api.updateNotification(Api.isEnabled(context), context);
}
}
} else {
if (index != null) {
//int id = Integer.parseInt(index);
switch (index) {
case "0":
Api.applySavedIptablesRules(context, false, new RootCommand()
.setFailureToast(R.string.error_apply)
.setCallback(new RootCommand.Callback() {
@Override
public void cbFunc(RootCommand state) {
Message msg = new Message();
if (state.exitCode == 0) {
msg.arg1 = R.string.rules_applied;
Api.setEnabled(context, true, false);
} else {
// error details are already in logcat
msg.arg1 = R.string.error_apply;
}
sendMessage(msg);
}
}));
break;
case "1":
if (G.protectionLevel().equals("p0")) {
Api.purgeIptables(context, true, new RootCommand()
.setReopenShell(true)
.setCallback(new RootCommand.Callback() {
public void cbFunc(RootCommand state) {
Message msg = new Message();
msg.arg1 = R.string.toast_disabled;
sendMessage(msg);
Api.setEnabled(context, false, false);
}
}));
/* } else {
msg.arg1 = R.string.toast_error_disabling;
sendMessage(msg);
}*/
} else {
Message msg = new Message();
msg.arg1 = R.string.widget_disable_fail;
sendMessage(msg);
}
break;
case "2":
if (multimode) {
G.setProfile(true, "AFWallPrefs");
}
break;
default:
if (multimode) {
ProfileData data = ProfileHelper.getProfileByName(name);
if (data != null) {
G.setProfile(true, data.getIdentifier());
}
}
break;
}
if (Integer.parseInt(index) > 1) {
if (multimode) {
if (Api.isEnabled(context)) {
if (!disableToasts) {
Toast.makeText(context, R.string.tasker_apply, Toast.LENGTH_SHORT).show();
}
Api.applySavedIptablesRules(context, false, new RootCommand()
.setFailureToast(R.string.error_apply)
.setCallback(new RootCommand.Callback() {
@Override
public void cbFunc(RootCommand state) {
Message msg = new Message();
if (state.exitCode == 0) {
msg.arg1 = R.string.tasker_profile_applied;
if (!disableToasts) sendMessage(msg);
} else {
// error details are already in logcat
msg.arg1 = R.string.error_apply;
}
}
}));
} else {
Message msg = new Message();
msg.arg1 = R.string.tasker_disabled;
sendMessage(msg);
}
} else {
Message msg = new Message();
msg.arg1 = R.string.tasker_muliprofile;
sendMessage(msg);
}
G.reloadPrefs();
/* if (G.activeNotification()) {
Api.showNotification(Api.isEnabled(context), context);
}*/
Api.updateNotification(Api.isEnabled(context), context);
}
}
}
}
}
private void sendMessage(Message msg) {
try {
new Handler() {
public void handleMessage(Message msg) {
if (msg.arg1 != 0)
Toast.makeText(G.getContext(), msg.arg1, Toast.LENGTH_SHORT).show();
}
}.sendMessage(msg);
}catch (Exception e) {
//unable to send toast. but don't crash
}
}
}

View file

@ -0,0 +1,235 @@
package dev.ukanth.ufirewall.plugin;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import java.util.List;
import dev.ukanth.ufirewall.R;
import dev.ukanth.ufirewall.profiles.ProfileData;
import dev.ukanth.ufirewall.profiles.ProfileHelper;
import dev.ukanth.ufirewall.util.G;
public class LocaleEdit extends AppCompatActivity {
//public static final String LOCALE_BRIGHTNESS = "dev.ukanth.ufirewall.plugin.LocaleEdit.ACTIVE_PROFLE";
private boolean mIsCancelled = false;
private final int CUSTOM_PROFILE_ID = 100;
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
BundleScrubber.scrub(getIntent());
BundleScrubber.scrub(getIntent().getBundleExtra(
com.twofortyfouram.locale.Intent.EXTRA_BUNDLE));
setContentView(R.layout.tasker_profile);
Toolbar toolbar = findViewById(R.id.tasker_toolbar);
setSupportActionBar(toolbar);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
RadioButton tasker_enable = findViewById(R.id.tasker_enable);
RadioButton tasker_disable = findViewById(R.id.tasker_disable);
RadioButton button1 = findViewById(R.id.defaultProfile);
String name = prefs.getString("default", getString(R.string.defaultProfile));
button1.setText(name != null && name.length() == 0 ? getString(R.string.defaultProfile) : name);
if (!G.isProfileMigrated()) {
RadioGroup profiles = findViewById(R.id.radioProfiles);
RadioButton button2 = findViewById(R.id.profile1);
RadioButton button3 = findViewById(R.id.profile2);
RadioButton button4 = findViewById(R.id.profile3);
List<String> profilesList = G.getAdditionalProfiles();
//int textColor = Color.parseColor("#000000");
int counter = CUSTOM_PROFILE_ID;
for (String profile : profilesList) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(counter++);
rdbtn.setText(profile);
profiles.addView(rdbtn);
}
name = prefs.getString("profile1", getString(R.string.profile1));
button2.setText(name != null && name.length() == 0 ? getString(R.string.profile1) : name);
name = prefs.getString("profile2", getString(R.string.profile2));
button3.setText(name != null && name.length() == 0 ? getString(R.string.profile2) : name);
name = prefs.getString("profile3", getString(R.string.profile3));
button4.setText(name != null && name.length() == 0 ? getString(R.string.profile3) : name);
setupTitleApi11();
if (null == paramBundle) {
final Bundle forwardedBundle = getIntent().getBundleExtra(
com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);
if (PluginBundleManager.isBundleValid(forwardedBundle)) {
String index = forwardedBundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
if (index.contains("::")) {
index = index.split("::")[0];
}
if (index != null) {
switch (index) {
case "0":
tasker_enable.setChecked(true);
break;
case "1":
tasker_disable.setChecked(true);
break;
case "2":
button1.setChecked(true);
break;
case "3":
button2.setChecked(true);
break;
case "4":
button3.setChecked(true);
break;
case "5":
button4.setChecked(true);
break;
default:
int diff = CUSTOM_PROFILE_ID + (Integer.parseInt(index) - 6);
RadioButton btn = findViewById(diff);
if (btn != null) {
btn.setChecked(true);
}
}
}
}
}
} else {
//TODO: lets do it on new way
RadioGroup profiles = findViewById(R.id.radioProfiles);
//remove the existing profiles
RadioButton button2 = findViewById(R.id.profile1);
RadioButton button3 = findViewById(R.id.profile2);
RadioButton button4 = findViewById(R.id.profile3);
profiles.removeView(button2);
profiles.removeView(button3);
profiles.removeView(button4);
for (ProfileData data : ProfileHelper.getProfiles()) {
if (data != null) {
String profile = data.getName();
Long id = data.getId();
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(id.intValue());
rdbtn.setText(profile);
profiles.addView(rdbtn);
}
}
if (null == paramBundle) {
final Bundle forwardedBundle = getIntent().getBundleExtra(
com.twofortyfouram.locale.Intent.EXTRA_BUNDLE);
if (PluginBundleManager.isBundleValid(forwardedBundle)) {
String index = forwardedBundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
if (index.contains("::")) {
index = index.split("::")[0];
}
if (index != null) {
switch (index) {
case "0":
tasker_enable.setChecked(true);
break;
case "1":
tasker_disable.setChecked(true);
break;
case "2":
button1.setChecked(true);
break;
default:
int diff = Integer.parseInt(index);
RadioButton btn = findViewById(diff);
if (btn != null) {
btn.setChecked(true);
}
}
}
}
}
}
}
private void setupTitleApi11() {
CharSequence callingApplicationLabel = null;
try {
callingApplicationLabel = getPackageManager().getApplicationLabel(
getPackageManager().getApplicationInfo(getCallingPackage(),
0));
} catch (final NameNotFoundException e) {
}
if (null != callingApplicationLabel) {
setTitle(callingApplicationLabel);
}
}
protected void onPause() {
super.onPause();
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
int id = item.getItemId();
if(id == android.R.id.home || id == R.id.twofortyfouram_locale_menu_save ) {
finish();
return true;
} else if (id == R.id.twofortyfouram_locale_menu_dontsave) {
mIsCancelled = true;
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.twofortyfouram_locale_help_save_dontsave, menu);
return true;
}
@Override
public void finish() {
if (mIsCancelled) {
setResult(RESULT_CANCELED);
} else {
RadioGroup group = findViewById(R.id.radioProfiles);
int selectedId = group.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(selectedId);
//int id = Integer.parseInt(radioButton.getHint().toString());
String action = radioButton.getText().toString();
final Intent resultIntent = new Intent();
if (!G.isProfileMigrated()) {
int idx = group.indexOfChild(radioButton);
resultIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE, PluginBundleManager.generateBundle(getApplicationContext(), idx + "::" + action));
} else {
int idx = group.indexOfChild(radioButton);
resultIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE, PluginBundleManager.generateBundle(getApplicationContext(), idx + "::" + action));
}
resultIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_STRING_BLURB, action);
setResult(RESULT_OK, resultIntent);
}
super.finish();
}
}

View file

@ -0,0 +1,92 @@
/*
* Copyright 2012 two forty four a.m. LLC <http://www.twofortyfouram.com>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* <http://www.apache.org/licenses/LICENSE-2.0>
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package dev.ukanth.ufirewall.plugin;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
/**
* Class for managing the {@link com.twofortyfouram.locale.Intent#EXTRA_BUNDLE} for this plug-in.
*/
public final class PluginBundleManager
{
/**
* Type: {@code String}.
* <p>
* String message to display in a Toast message.
*/
public static final String BUNDLE_EXTRA_STRING_MESSAGE = "dev.ukanth.ufirewall.plugin.APPLY_PROFILE"; //$NON-NLS-1$
/**
* Type: {@code int}
* <p>
* versionCode of the plug-in that saved the Bundle.
*/
/*
* This extra is not strictly required, however it makes backward and forward compatibility significantly
* easier. For example, suppose a bug is found in how some version of the plug-in stored its Bundle. By
* having the version, the plug-in can better detect when such bugs occur.
*/
public static final String BUNDLE_EXTRA_INT_VERSION_CODE = "dev.ukanth.ufirewall.plugin.extra.INT_VERSION_CODE"; //$NON-NLS-1$
/**
* Method to verify the content of the bundle are correct.
* <p>
* This method will not mutate {@code bundle}.
*
* @param bundle bundle to verify. May be null, which will always return false.
* @return true if the Bundle is valid, false if the bundle is invalid.
*/
public static boolean isBundleValid(final Bundle bundle)
{
if (null == bundle)
{
return false;
}
/*
* Make sure the expected extras exist
*/
if (!bundle.containsKey(BUNDLE_EXTRA_STRING_MESSAGE))
{
return false;
}
/*
* Make sure the extra isn't null or empty
*/
return !TextUtils.isEmpty(bundle.getString(BUNDLE_EXTRA_STRING_MESSAGE));
}
/**
* @param context Application context.
* @param message The toast message to be displayed by the plug-in. Cannot be null.
* @return A plug-in bundle.
*/
public static Bundle generateBundle(final Context context, final String message)
{
final Bundle result = new Bundle();
result.putString(BUNDLE_EXTRA_STRING_MESSAGE, message);
return result;
}
/**
* Private constructor prevents instantiation
*
* @throws UnsupportedOperationException because this class cannot be instantiated.
*/
private PluginBundleManager()
{
throw new UnsupportedOperationException("This class is non-instantiable"); //$NON-NLS-1$
}
}