Skip to content

Commit 7ca719d

Browse files
committed
Make https enforcing more fine grained
1 parent a559238 commit 7ca719d

4 files changed

Lines changed: 31 additions & 11 deletions

File tree

library/src/main/java/me/proxer/library/api/HttpsEnforcingInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class HttpsEnforcingInterceptor implements Interceptor {
1616
public Response intercept(final Chain chain) throws IOException {
1717
final Request request = chain.request();
1818

19-
final boolean appliesToUrl = ProxerUrls.hasProxerWebOrCdnHost(request.url())
19+
final boolean appliesToUrl = ProxerUrls.hasProxerWebOrCdnOrStreamHost(request.url())
2020
|| ProxerUrls.hasProxerMangaHost(request.url());
2121

2222
if (appliesToUrl && !request.isHttps()) {

library/src/main/java/me/proxer/library/util/ProxerUrls.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@Accessors(fluent = true)
2020
public final class ProxerUrls {
2121

22-
private static final Pattern PROXER_STREAM_HOST_PATTERN = Pattern.compile("(s[0-9]+\\.)?stream\\.proxer\\.me");
22+
private static final Pattern PROXER_STREAM_FILE_HOST_PATTERN = Pattern.compile("s[0-9]+\\.stream\\.proxer\\.me");
2323
private static final Pattern PROXER_MANGA_HOST_PATTERN = Pattern.compile("manga[0-9]+\\.proxer\\.me");
2424

2525
/**
@@ -52,6 +52,16 @@ public final class ProxerUrls {
5252
.addPathSegment("")
5353
.build();
5454

55+
/**
56+
* Returns the base url for all image links.
57+
*/
58+
@Getter
59+
private final HttpUrl streamBase = new HttpUrl.Builder()
60+
.scheme("https")
61+
.host("stream.proxer.me")
62+
.addPathSegment("")
63+
.build();
64+
5565
/**
5666
* Returns the image link of a {@link NewsArticle}, based on its
5767
* {@code id} and {@code image}.
@@ -240,21 +250,22 @@ public HttpUrl captchaWeb(final Device device) {
240250
* Returns if the passed url has a valid host of proxer.
241251
*/
242252
public boolean hasProxerHost(final HttpUrl url) {
243-
return hasProxerWebOrCdnHost(url) || hasProxerStreamHost(url) || hasProxerMangaHost(url);
253+
return hasProxerWebOrCdnOrStreamHost(url) || hasProxerStreamFileHost(url) || hasProxerMangaHost(url);
244254
}
245255

246256
/**
247257
* Returns if the passed url has a valid host of proxer or the proxer cdn..
248258
*/
249-
public boolean hasProxerWebOrCdnHost(final HttpUrl url) {
250-
return url.host().equals(webBase.host()) || url.host().equals(cdnBase.host());
259+
public boolean hasProxerWebOrCdnOrStreamHost(final HttpUrl url) {
260+
return url.host().equals(webBase.host()) || url.host().equals(cdnBase.host())
261+
|| url.host().equals(streamBase.host());
251262
}
252263

253264
/**
254265
* Returns if the passed url has a valid proxer stream host.
255266
*/
256-
public boolean hasProxerStreamHost(final HttpUrl url) {
257-
return PROXER_STREAM_HOST_PATTERN.matcher(url.host()).matches();
267+
public boolean hasProxerStreamFileHost(final HttpUrl url) {
268+
return PROXER_STREAM_FILE_HOST_PATTERN.matcher(url.host()).matches();
258269
}
259270

260271
/**

library/src/test/java/me/proxer/library/api/HttpsEnforcingInterceptorTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,21 @@ public void testHttpsUpgradeManga() throws IOException, InterruptedException {
3939
}
4040

4141
@Test
42-
public void testHttpsStreamUntouched() throws IOException, InterruptedException {
42+
public void testHttpsUpgradeStream() throws IOException, InterruptedException {
43+
server.enqueue(new MockResponse());
44+
45+
client.newCall(new Request.Builder().url("http://stream.proxer.me").build()).execute();
46+
47+
assertThat(server.takeRequest().getRequestUrl().isHttps()).isEqualTo(true);
48+
}
49+
50+
@Test
51+
public void testHttpsFileStreamUntouched() throws IOException, InterruptedException {
4352
startHttpOnlyServer();
4453

4554
server.enqueue(new MockResponse());
4655

47-
client.newCall(new Request.Builder().url("http://stream.proxer.me").build()).execute();
56+
client.newCall(new Request.Builder().url("http://s1.stream.proxer.me").build()).execute();
4857

4958
assertThat(server.takeRequest().getRequestUrl().isHttps()).isEqualTo(false);
5059
}

library/src/test/java/me/proxer/library/util/ProxerUrlsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ public void testHasProxerHostManga() {
155155
@Test
156156
public void testHasProxerHostStream() {
157157
//noinspection ConstantConditions
158-
assertThat(ProxerUrls.hasProxerHost(HttpUrl.parse("https://stream.proxer.me/files/test.mp4"))).isTrue();
158+
assertThat(ProxerUrls.hasProxerHost(HttpUrl.parse("https://stream.proxer.me/files/embed-abc.html"))).isTrue();
159159
}
160160

161161
@Test
162-
public void testHasProxerHostStreamServer() {
162+
public void testHasProxerHostFileStream() {
163163
//noinspection ConstantConditions
164164
assertThat(ProxerUrls.hasProxerHost(HttpUrl.parse("https://s39.stream.proxer.me/files/test.mp4"))).isTrue();
165165
}

0 commit comments

Comments
 (0)