From 3a6df128443532a886a526310f909b056038cabe Mon Sep 17 00:00:00 2001 From: 3nprob <3nprob@3nprob> Date: Sat, 14 Aug 2021 01:16:01 +0900 Subject: [PATCH] Add support for outbound http proxy --- config.properties | 3 +++ .../java/me/kavin/piped/consts/Constants.java | 25 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/config.properties b/config.properties index ae67038..987bbfc 100644 --- a/config.properties +++ b/config.properties @@ -7,6 +7,9 @@ HTTP_WORKERS: 2 # Proxy PROXY_PART: https://pipedproxy-ams.kavin.rocks +# Ougoing HTTP proxy host:port (for both https and http requests) +HTTP_PROXY: + # Captcha Parameters CAPTCHA_BASE_URL: https://api.capmonster.cloud/ CAPTCHA_API_KEY: INSERT_HERE diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index 50695ab..c070175 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -2,8 +2,12 @@ package me.kavin.piped.consts; import java.io.FileReader; import java.net.http.HttpClient; +import java.net.http.HttpClient.Builder; import java.net.http.HttpClient.Redirect; import java.net.http.HttpClient.Version; +import java.net.ProxySelector; +import java.net.InetSocketAddress; + import java.util.Properties; import org.schabi.newpipe.extractor.NewPipe; @@ -32,10 +36,10 @@ public class Constants { public static final String PUBLIC_URL; - public static final HttpClient h2client = HttpClient.newBuilder().followRedirects(Redirect.NORMAL) - .version(Version.HTTP_2).build(); - public static final HttpClient h2_no_redir_client = HttpClient.newBuilder().followRedirects(Redirect.NEVER) - .version(Version.HTTP_2).build(); + public static final String HTTP_PROXY; + + public static final HttpClient h2client; + public static final HttpClient h2_no_redir_client; // public static final HttpClient h3client = Http3ClientBuilder.newBuilder().followRedirects(Redirect.NORMAL).build(); public static final ObjectMapper mapper = new ObjectMapper().addMixIn(Page.class, PageMixin.class); @@ -54,11 +58,24 @@ public class Constants { CAPTCHA_BASE_URL = prop.getProperty("CAPTCHA_BASE_URL"); CAPTCHA_API_KEY = prop.getProperty("CAPTCHA_API_KEY"); PUBLIC_URL = prop.getProperty("API_URL"); + HTTP_PROXY = prop.getProperty("HTTP_PROXY"); prop.forEach((_key, _value) -> { String key = String.valueOf(_key), value = String.valueOf(_value); if (key.startsWith("hibernate")) hibernateProperties.put(key, value); }); + Builder h2c = HttpClient.newBuilder().followRedirects(Redirect.NORMAL) + .version(Version.HTTP_2); + Builder h2nrc = HttpClient.newBuilder().followRedirects(Redirect.NEVER) + .version(Version.HTTP_2); + if (HTTP_PROXY != null && !HTTP_PROXY.trim().isEmpty()) { + String[] parts = HTTP_PROXY.split(":"); + ProxySelector proxy = ProxySelector.of(new InetSocketAddress(parts[0], Integer.parseInt(parts[1]))); + h2c = h2c.proxy(proxy); + h2nrc = h2nrc.proxy(proxy); + } + h2client = h2c.build(); + h2_no_redir_client = h2nrc.build(); } catch (Exception e) { throw new RuntimeException(e); }