diff options
author | cyberta <cyberta@riseup.net> | 2020-01-23 00:56:50 -0600 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2020-01-23 00:56:50 -0600 |
commit | 0375f0eccdf2b22a47e0089b4a1868c3a54a527a (patch) | |
tree | a6b568d54b4cf3c7b53c0c5af30f8e5da15298f5 | |
parent | 42b794a24f7b47910d595e713f29db85e3d0da3d (diff) |
add api ip to provider class, implement ip lookup based on given provider host name
7 files changed, 71 insertions, 41 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/CustomProviderSetupActivity.java b/app/src/main/java/se/leap/bitmaskclient/CustomProviderSetupActivity.java index 3763f506..b3ee44ca 100644 --- a/app/src/main/java/se/leap/bitmaskclient/CustomProviderSetupActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/CustomProviderSetupActivity.java @@ -37,7 +37,7 @@ public class CustomProviderSetupActivity extends ProviderSetupBaseActivity { super.onCreate(savedInstanceState); setUpInitialUI(); restoreState(savedInstanceState); - setProvider(new Provider(BuildConfig.customProviderUrl)); + setProvider(new Provider(BuildConfig.customProviderUrl, BuildConfig.customProviderIp, BuildConfig.customProviderApiIp)); } @Override diff --git a/app/src/main/java/se/leap/bitmaskclient/DnsResolver.java b/app/src/main/java/se/leap/bitmaskclient/DnsResolver.java index 069318a4..92f70492 100644 --- a/app/src/main/java/se/leap/bitmaskclient/DnsResolver.java +++ b/app/src/main/java/se/leap/bitmaskclient/DnsResolver.java @@ -1,29 +1,33 @@ package se.leap.bitmaskclient; +import org.jetbrains.annotations.NotNull; + import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import okhttp3.Dns; +import se.leap.bitmaskclient.utils.IPAddress; class DnsResolver implements Dns { @Override - public List<InetAddress> lookup(String hostname) throws UnknownHostException { + public List<InetAddress> lookup(@NotNull String hostname) throws UnknownHostException { try { return Dns.SYSTEM.lookup(hostname); } catch (UnknownHostException e) { ProviderObservable observable = ProviderObservable.getInstance(); Provider currentProvider; - if (observable.getProviderToSetup() != null) { - currentProvider = observable.getProviderToSetup(); + if (observable.getProviderForDns() != null) { + currentProvider = observable.getProviderForDns(); } else { currentProvider = observable.getCurrentProvider(); } - if (currentProvider.hasProviderIp()) { + String ip = currentProvider.getIpForHostname(hostname); + if (!ip.isEmpty()) { ArrayList<InetAddress> addresses = new ArrayList<>(); - addresses.add(InetAddress.getByAddress(hostname, currentProvider.getProviderIpAsBytes())); + addresses.add(InetAddress.getByAddress(hostname, IPAddress.asBytes(ip))); return addresses; } else { throw new UnknownHostException("Hostname " + hostname + " not found"); diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index 3dfee72c..88080a03 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -52,6 +52,7 @@ public final class Provider implements Parcelable { private DefaultedURL mainUrl = new DefaultedURL(); private DefaultedURL apiUrl = new DefaultedURL(); private String providerIp = ""; + private String providerApiIp = ""; private String certificatePin = ""; private String certificatePinEncoding = ""; private String caCert = ""; @@ -77,7 +78,8 @@ public final class Provider implements Parcelable { DESCRIPTION = "description", DOMAIN = "domain", MAIN_URL = "main_url", - PROVIDER_IP = "provider_ip"; + PROVIDER_IP = "provider_ip", + PROVIDER_API_IP = "provider_api_ip"; private static final String API_TERM_NAME = "name"; @@ -91,16 +93,28 @@ public final class Provider implements Parcelable { } } - public Provider(URL mainUrl, String providerIp) { - this.mainUrl.setUrl(mainUrl); - this.providerIp = providerIp; + public Provider(String mainUrl, String providerIp, String providerApiIp) { + try { + this.mainUrl.setUrl(new URL(mainUrl)); + this.providerIp = providerIp; + this.providerApiIp = providerApiIp; + } catch (MalformedURLException e) { + e.printStackTrace(); + } } - public Provider(URL mainUrl, String providerIp, String caCert, String definition) { - this.mainUrl.setUrl(mainUrl); + public Provider(String mainUrl, String providerIp, String providerApiIp, String caCert, String definition) { + try { + this.mainUrl.setUrl(new URL(mainUrl)); + } catch (MalformedURLException e) { + e.printStackTrace(); + } if (this.providerIp != null) { this.providerIp = providerIp; } + if (this.providerApiIp != null) { + this.providerApiIp = providerApiIp; + } if (caCert != null) { this.caCert = caCert; } @@ -154,6 +168,25 @@ public final class Provider implements Parcelable { return false; } + public String getIpForHostname(String host) { + if (host != null) { + if (host.equals(mainUrl.getUrl().getHost())) { + return providerIp; + } else if (host.equals(apiUrl.getUrl().getHost())) { + return providerApiIp; + } + } + return ""; + } + + public String getProviderApiIp() { + return this.providerApiIp; + } + + public void setProviderApiIp(String providerApiIp) { + this.providerApiIp = providerApiIp; + } + public void setProviderIp(String providerIp) { this.providerIp = providerIp; } @@ -162,10 +195,6 @@ public final class Provider implements Parcelable { return this.providerIp; } - public byte[] getProviderIpAsBytes() { - return IPAddress.asBytes(providerIp); - } - public void setMainUrl(URL url) { mainUrl.setUrl(url); } @@ -286,6 +315,7 @@ public final class Provider implements Parcelable { public void writeToParcel(Parcel parcel, int i) { parcel.writeString(getMainUrlString()); parcel.writeString(getProviderIp()); + parcel.writeString(getProviderApiIp()); parcel.writeString(getDefinitionString()); parcel.writeString(getCaCert()); parcel.writeString(getEipServiceJsonString()); @@ -303,6 +333,7 @@ public final class Provider implements Parcelable { eipServiceJson.toString().equals(p.getEipServiceJson().toString())&& mainUrl.equals(p.getMainUrl()) && providerIp.equals(p.getProviderIp()) && + providerApiIp.equals(p.getProviderApiIp()) && apiUrl.equals(p.getApiUrl()) && certificatePin.equals(p.getCertificatePin()) && certificatePinEncoding.equals(p.getCertificatePinEncoding()) && @@ -346,6 +377,10 @@ public final class Provider implements Parcelable { } tmpString = in.readString(); if (!tmpString.isEmpty()) { + providerApiIp = tmpString; + } + tmpString = in.readString(); + if (!tmpString.isEmpty()) { definition = new JSONObject((tmpString)); parseDefinition(definition); } @@ -469,10 +504,6 @@ public final class Provider implements Parcelable { return getCertificatePinEncoding() + ":" + getCertificatePin(); } - public boolean hasProviderIp() { - return !providerIp.isEmpty(); - } - /** * resets everything except the main url and the providerIp */ diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListBaseActivity.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListBaseActivity.java index bae237f6..f403dc00 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderListBaseActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListBaseActivity.java @@ -108,12 +108,8 @@ public abstract class ProviderListBaseActivity extends ProviderSetupBaseActivity } public void showAndSelectProvider(String newURL) { - try { - provider = new Provider(new URL((newURL)), ""); - autoSelectProvider(); - } catch (MalformedURLException e) { - e.printStackTrace(); - } + provider = new Provider(newURL, "", ""); + autoSelectProvider(); } private void autoSelectProvider() { diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java index 134e2d76..ab165450 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java @@ -12,7 +12,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Collection; @@ -22,6 +21,7 @@ import java.util.List; import java.util.Set; import static se.leap.bitmaskclient.Provider.MAIN_URL; +import static se.leap.bitmaskclient.Provider.PROVIDER_API_IP; import static se.leap.bitmaskclient.Provider.PROVIDER_IP; import static se.leap.bitmaskclient.utils.FileHelper.createFile; import static se.leap.bitmaskclient.utils.FileHelper.persistFile; @@ -87,6 +87,7 @@ public class ProviderManager implements AdapteeCollection<Provider> { for (String file : relativeFilePaths) { String mainUrl = null; String providerIp = null; + String providerApiIp = null; String certificate = null; String providerDefinition = null; try { @@ -94,16 +95,13 @@ public class ProviderManager implements AdapteeCollection<Provider> { InputStream providerFile = assetsManager.open(directory + "/" + file); mainUrl = extractKeyFromInputStream(providerFile, MAIN_URL); providerIp = extractKeyFromInputStream(providerFile, PROVIDER_IP); + providerApiIp = extractKeyFromInputStream(providerFile, PROVIDER_API_IP); certificate = loadInputStreamAsString(assetsManager.open(provider + EXT_PEM)); providerDefinition = loadInputStreamAsString(assetsManager.open(provider + EXT_JSON)); } catch (IOException e) { e.printStackTrace(); } - try { - providers.add(new Provider(new URL(mainUrl), providerIp, certificate, providerDefinition)); - } catch (MalformedURLException e) { - e.printStackTrace(); - } + providers.add(new Provider(mainUrl, providerIp, providerApiIp, certificate, providerDefinition)); } return providers; @@ -125,9 +123,10 @@ public class ProviderManager implements AdapteeCollection<Provider> { InputStream inputStream = getInputStreamFrom(externalFilesDir.getAbsolutePath() + "/" + file); String mainUrl = extractKeyFromInputStream(inputStream, MAIN_URL); String providerIp = extractKeyFromInputStream(inputStream, PROVIDER_IP); - providers.add(new Provider(new URL(mainUrl), providerIp)); + String providerApiIp = extractKeyFromInputStream(inputStream, PROVIDER_API_IP); + providers.add(new Provider(mainUrl, providerIp, providerApiIp)); } - } catch (MalformedURLException | FileNotFoundException | NullPointerException e) { + } catch (FileNotFoundException | NullPointerException e) { e.printStackTrace(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderObservable.java b/app/src/main/java/se/leap/bitmaskclient/ProviderObservable.java index f20599ba..50cc37d4 100644 --- a/app/src/main/java/se/leap/bitmaskclient/ProviderObservable.java +++ b/app/src/main/java/se/leap/bitmaskclient/ProviderObservable.java @@ -8,7 +8,7 @@ import java.util.Observable; public class ProviderObservable extends Observable { private static ProviderObservable instance; private Provider currentProvider; - private Provider providerToSetup; + private Provider providerForDns; public static ProviderObservable getInstance() { if (instance == null) { @@ -19,7 +19,7 @@ public class ProviderObservable extends Observable { public synchronized void updateProvider(Provider provider) { instance.currentProvider = provider; - instance.providerToSetup = null; + instance.providerForDns = null; instance.setChanged(); instance.notifyObservers(); } @@ -28,12 +28,12 @@ public class ProviderObservable extends Observable { return instance.currentProvider; } - public void setProviderToSetup(Provider provider) { - this.providerToSetup = provider; + public void setProviderForDns(Provider provider) { + this.providerForDns = provider; } - public Provider getProviderToSetup() { - return instance.providerToSetup; + public Provider getProviderForDns() { + return instance.providerForDns; } } diff --git a/app/src/sharedTest/java/se.leap.bitmaskclient/testutils/TestSetupHelper.java b/app/src/sharedTest/java/se.leap.bitmaskclient/testutils/TestSetupHelper.java index 89833393..c6f4db38 100644 --- a/app/src/sharedTest/java/se.leap.bitmaskclient/testutils/TestSetupHelper.java +++ b/app/src/sharedTest/java/se.leap.bitmaskclient/testutils/TestSetupHelper.java @@ -70,7 +70,7 @@ public class TestSetupHelper { try { Provider p = new Provider( - new URL(domain), + domain, providerIp, getInputAsString(TestSetupHelper.class.getClassLoader().getResourceAsStream(caCertFile)), getInputAsString(TestSetupHelper.class.getClassLoader().getResourceAsStream(providerJson)) |