Better checl VPN/DNS address

This commit is contained in:
M66B 2016-02-11 07:50:02 +01:00
parent 85d179f5de
commit 223cf9fbc7
1 changed files with 12 additions and 12 deletions

View File

@ -76,6 +76,7 @@ import java.io.OutputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -524,11 +525,8 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
else if ("vpn4".equals(name)) { else if ("vpn4".equals(name)) {
String vpn4 = prefs.getString("vpn4", null); String vpn4 = prefs.getString("vpn4", null);
try { try {
if (vpn4 == null || TextUtils.isEmpty(vpn4.trim())) checkAddress(vpn4);
throw new IllegalArgumentException("vpn4");
InetAddress.getByName(vpn4);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(TAG, ex.toString());
prefs.edit().remove("vpn4").apply(); prefs.edit().remove("vpn4").apply();
} }
SinkholeService.reload(null, "changed " + name, this); SinkholeService.reload(null, "changed " + name, this);
@ -537,11 +535,8 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
} else if ("vpn6".equals(name)) { } else if ("vpn6".equals(name)) {
String vpn6 = prefs.getString("vpn6", null); String vpn6 = prefs.getString("vpn6", null);
try { try {
if (vpn6 == null || TextUtils.isEmpty(vpn6.trim())) checkAddress(vpn6);
throw new IllegalArgumentException("vpn6");
InetAddress.getByName(vpn6);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(TAG, ex.toString());
prefs.edit().remove("vpn6").apply(); prefs.edit().remove("vpn6").apply();
} }
SinkholeService.reload(null, "changed " + name, this); SinkholeService.reload(null, "changed " + name, this);
@ -550,11 +545,8 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
} else if ("dns".equals(name)) { } else if ("dns".equals(name)) {
String dns = prefs.getString("dns", null); String dns = prefs.getString("dns", null);
try { try {
if (dns == null || TextUtils.isEmpty(dns.trim())) checkAddress(dns);
throw new IllegalArgumentException("dns");
InetAddress.getByName(dns);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(TAG, ex.toString());
prefs.edit().remove("dns").apply(); prefs.edit().remove("dns").apply();
} }
SinkholeService.reload(null, "changed " + name, this); SinkholeService.reload(null, "changed " + name, this);
@ -579,6 +571,14 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
SinkholeService.reload(null, "changed " + name, this); SinkholeService.reload(null, "changed " + name, this);
} }
private void checkAddress(String address) throws IllegalArgumentException, UnknownHostException {
if (address == null || TextUtils.isEmpty(address.trim()))
throw new IllegalArgumentException("Bad address");
InetAddress idns = InetAddress.getByName(address);
if (idns.isLoopbackAddress() || idns.isAnyLocalAddress())
throw new IllegalArgumentException("Bad address");
}
@TargetApi(Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M)
private void checkPermissions() { private void checkPermissions() {
PreferenceScreen screen = getPreferenceScreen(); PreferenceScreen screen = getPreferenceScreen();