kopia lustrzana https://github.com/ryukoposting/Signal-Android
Improve proxy link parsing.
rodzic
3e2349c4ff
commit
0569d0555f
|
@ -18,9 +18,13 @@ import org.whispersystems.signalservice.internal.configuration.SignalProxy;
|
|||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class SignalProxyUtil {
|
||||
|
||||
|
@ -28,6 +32,9 @@ public final class SignalProxyUtil {
|
|||
|
||||
private static final String PROXY_LINK_HOST = "signal.tube";
|
||||
|
||||
private static final Pattern PROXY_LINK_PATTERN = Pattern.compile("^(https|sgnl)://" + PROXY_LINK_HOST + "/#([^:]+).*$");
|
||||
private static final Pattern HOST_PATTERN = Pattern.compile("^([^:]+).*$");
|
||||
|
||||
private SignalProxyUtil() {}
|
||||
|
||||
public static void startListeningToWebsocket() {
|
||||
|
@ -112,31 +119,11 @@ public final class SignalProxyUtil {
|
|||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
URI uri = new URI(proxyLink);
|
||||
Matcher matcher = PROXY_LINK_PATTERN.matcher(proxyLink);
|
||||
|
||||
if (!"https".equalsIgnoreCase(uri.getScheme()) &&
|
||||
!"sgnl".equalsIgnoreCase(uri.getScheme()))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!PROXY_LINK_HOST.equalsIgnoreCase(uri.getHost())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String fragment = uri.getFragment();
|
||||
|
||||
if (Util.isEmpty(fragment)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (fragment.startsWith("#")) {
|
||||
fragment = fragment.substring(1);
|
||||
}
|
||||
|
||||
return Util.isEmpty(fragment) ? null : fragment;
|
||||
} catch (URISyntaxException e) {
|
||||
if (matcher.matches()) {
|
||||
return matcher.group(2);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -147,7 +134,18 @@ public final class SignalProxyUtil {
|
|||
*/
|
||||
public static @NonNull String convertUserEnteredAddressToHost(@NonNull String host) {
|
||||
String parsedHost = SignalProxyUtil.parseHostFromProxyDeepLink(host);
|
||||
return parsedHost != null ? parsedHost : host;
|
||||
if (parsedHost != null) {
|
||||
return parsedHost;
|
||||
}
|
||||
|
||||
Matcher matcher = HOST_PATTERN.matcher(host);
|
||||
|
||||
if (matcher.matches()) {
|
||||
String result = matcher.group(1);
|
||||
return result != null ? result : "";
|
||||
} else {
|
||||
return host;
|
||||
}
|
||||
}
|
||||
|
||||
public static @NonNull String generateProxyUrl(@NonNull String link) {
|
||||
|
@ -158,6 +156,12 @@ public final class SignalProxyUtil {
|
|||
host = parsed;
|
||||
}
|
||||
|
||||
Matcher matcher = HOST_PATTERN.matcher(host);
|
||||
|
||||
if (matcher.matches()) {
|
||||
host = matcher.group(1);
|
||||
}
|
||||
|
||||
return "https://" + PROXY_LINK_HOST + "/#" + host;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class SignalProxyUtilText_convertUserEnteredAddressToHost {
|
||||
|
||||
private final String input;
|
||||
private final String output;
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{ "https://signal.tube/#proxy.parker.org", "proxy.parker.org" },
|
||||
{ "https://signal.tube/#proxy.parker.org:443", "proxy.parker.org" },
|
||||
{ "sgnl://signal.tube/#proxy.parker.org", "proxy.parker.org" },
|
||||
{ "sgnl://signal.tube/#proxy.parker.org:443", "proxy.parker.org" },
|
||||
{ "proxy.parker.org", "proxy.parker.org" },
|
||||
{ "proxy.parker.org:443", "proxy.parker.org" },
|
||||
{ "x", "x" },
|
||||
{ "", "" }
|
||||
});
|
||||
}
|
||||
|
||||
public SignalProxyUtilText_convertUserEnteredAddressToHost(String input, String output) {
|
||||
this.input = input;
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse() {
|
||||
assertEquals(output, SignalProxyUtil.convertUserEnteredAddressToHost(input));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class SignalProxyUtilText_generateProxyUrl {
|
||||
|
||||
private final String input;
|
||||
private final String output;
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{ "https://signal.tube/#proxy.parker.org", "https://signal.tube/#proxy.parker.org" },
|
||||
{ "https://signal.tube/#proxy.parker.org:443", "https://signal.tube/#proxy.parker.org" },
|
||||
{ "sgnl://signal.tube/#proxy.parker.org", "https://signal.tube/#proxy.parker.org" },
|
||||
{ "sgnl://signal.tube/#proxy.parker.org:443", "https://signal.tube/#proxy.parker.org" },
|
||||
{ "proxy.parker.org", "https://signal.tube/#proxy.parker.org" },
|
||||
{ "proxy.parker.org:443", "https://signal.tube/#proxy.parker.org" },
|
||||
{ "x", "https://signal.tube/#x" },
|
||||
{ "", "https://signal.tube/#" }
|
||||
});
|
||||
}
|
||||
|
||||
public SignalProxyUtilText_generateProxyUrl(String input, String output) {
|
||||
this.input = input;
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parse() {
|
||||
assertEquals(output, SignalProxyUtil.generateProxyUrl(input));
|
||||
}
|
||||
}
|
|
@ -18,16 +18,18 @@ public class SignalProxyUtilText_parseHostFromProxyDeepLink {
|
|||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{ "https://signal.tube/#proxy.parker.org", "proxy.parker.org" },
|
||||
{ "sgnl://signal.tube/#proxy.parker.org", "proxy.parker.org" },
|
||||
{ "https://signal.tube/", null },
|
||||
{ "https://signal.tube/#", null },
|
||||
{ "sgnl://signal.tube/", null },
|
||||
{ "sgnl://signal.tube/#", null },
|
||||
{ "http://signal.tube/#proxy.parker.org", null },
|
||||
{ "signal.tube/#proxy.parker.org", null },
|
||||
{ "", null },
|
||||
{ null, null }
|
||||
{ "https://signal.tube/#proxy.parker.org", "proxy.parker.org" },
|
||||
{ "https://signal.tube/#proxy.parker.org:443", "proxy.parker.org" },
|
||||
{ "sgnl://signal.tube/#proxy.parker.org", "proxy.parker.org" },
|
||||
{ "sgnl://signal.tube/#proxy.parker.org:443", "proxy.parker.org" },
|
||||
{ "https://signal.tube/", null },
|
||||
{ "https://signal.tube/#", null },
|
||||
{ "sgnl://signal.tube/", null },
|
||||
{ "sgnl://signal.tube/#", null },
|
||||
{ "http://signal.tube/#proxy.parker.org", null },
|
||||
{ "signal.tube/#proxy.parker.org", null },
|
||||
{ "", null },
|
||||
{ null, null }
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue