mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Add support for outbound http proxy (#93)
* Add support for outbound http proxy * Simplify logic and fix typos. Co-authored-by: 3nprob <3nprob@3nprob> Co-authored-by: FireMasterK <20838718+FireMasterK@users.noreply.github.com>
This commit is contained in:
parent
a0c5898f4d
commit
ce5881bad1
2 changed files with 23 additions and 5 deletions
|
@ -7,6 +7,9 @@ HTTP_WORKERS: 2
|
||||||
# Proxy
|
# Proxy
|
||||||
PROXY_PART: https://pipedproxy-ams.kavin.rocks
|
PROXY_PART: https://pipedproxy-ams.kavin.rocks
|
||||||
|
|
||||||
|
# Outgoing HTTP Proxy - eg: 127.0.0.1:8118
|
||||||
|
#HTTP_PROXY: 127.0.0.1:8118
|
||||||
|
|
||||||
# Captcha Parameters
|
# Captcha Parameters
|
||||||
CAPTCHA_BASE_URL: https://api.capmonster.cloud/
|
CAPTCHA_BASE_URL: https://api.capmonster.cloud/
|
||||||
CAPTCHA_API_KEY: INSERT_HERE
|
CAPTCHA_API_KEY: INSERT_HERE
|
||||||
|
|
|
@ -1,11 +1,15 @@
|
||||||
package me.kavin.piped.consts;
|
package me.kavin.piped.consts;
|
||||||
|
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.ProxySelector;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpClient.Builder;
|
||||||
import java.net.http.HttpClient.Redirect;
|
import java.net.http.HttpClient.Redirect;
|
||||||
import java.net.http.HttpClient.Version;
|
import java.net.http.HttpClient.Version;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
import org.schabi.newpipe.extractor.Page;
|
import org.schabi.newpipe.extractor.Page;
|
||||||
import org.schabi.newpipe.extractor.StreamingService;
|
import org.schabi.newpipe.extractor.StreamingService;
|
||||||
|
@ -32,11 +36,10 @@ public class Constants {
|
||||||
|
|
||||||
public static final String PUBLIC_URL;
|
public static final String PUBLIC_URL;
|
||||||
|
|
||||||
public static final HttpClient h2client = HttpClient.newBuilder().followRedirects(Redirect.NORMAL)
|
public static final String HTTP_PROXY;
|
||||||
.version(Version.HTTP_2).build();
|
|
||||||
public static final HttpClient h2_no_redir_client = HttpClient.newBuilder().followRedirects(Redirect.NEVER)
|
public static final HttpClient h2client;
|
||||||
.version(Version.HTTP_2).build();
|
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);
|
public static final ObjectMapper mapper = new ObjectMapper().addMixIn(Page.class, PageMixin.class);
|
||||||
|
|
||||||
|
@ -54,11 +57,23 @@ public class Constants {
|
||||||
CAPTCHA_BASE_URL = prop.getProperty("CAPTCHA_BASE_URL");
|
CAPTCHA_BASE_URL = prop.getProperty("CAPTCHA_BASE_URL");
|
||||||
CAPTCHA_API_KEY = prop.getProperty("CAPTCHA_API_KEY");
|
CAPTCHA_API_KEY = prop.getProperty("CAPTCHA_API_KEY");
|
||||||
PUBLIC_URL = prop.getProperty("API_URL");
|
PUBLIC_URL = prop.getProperty("API_URL");
|
||||||
|
HTTP_PROXY = prop.getProperty("HTTP_PROXY");
|
||||||
prop.forEach((_key, _value) -> {
|
prop.forEach((_key, _value) -> {
|
||||||
String key = String.valueOf(_key), value = String.valueOf(_value);
|
String key = String.valueOf(_key), value = String.valueOf(_value);
|
||||||
if (key.startsWith("hibernate"))
|
if (key.startsWith("hibernate"))
|
||||||
hibernateProperties.put(key, value);
|
hibernateProperties.put(key, value);
|
||||||
});
|
});
|
||||||
|
Builder builder = HttpClient.newBuilder().followRedirects(Redirect.NORMAL).version(Version.HTTP_2);
|
||||||
|
Builder builder_noredir = HttpClient.newBuilder().followRedirects(Redirect.NEVER).version(Version.HTTP_2);
|
||||||
|
if (HTTP_PROXY != null && HTTP_PROXY.contains(":")) {
|
||||||
|
String host = StringUtils.substringBefore(HTTP_PROXY, ":");
|
||||||
|
String port = StringUtils.substringAfter(HTTP_PROXY, ":");
|
||||||
|
InetSocketAddress sa = new InetSocketAddress(host, Integer.parseInt(port));
|
||||||
|
ProxySelector ps = ProxySelector.of(sa);
|
||||||
|
ProxySelector.setDefault(ps);
|
||||||
|
}
|
||||||
|
h2client = builder.build();
|
||||||
|
h2_no_redir_client = builder_noredir.build();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue