Add single proxy
This commit is contained in:
parent
2a6cbc0c7c
commit
4c24ef06a4
|
|
@ -1,24 +1,19 @@
|
|||
package com.github.catvod.net;
|
||||
|
||||
import com.github.catvod.spider.Init;
|
||||
import com.github.catvod.utils.Prefers;
|
||||
import com.google.net.cronet.okhttptransport.CronetInterceptor;
|
||||
|
||||
import org.chromium.net.CronetEngine;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
|
||||
public class Cronet {
|
||||
|
||||
public static final String POST = "POST";
|
||||
public static final String GET = "GET";
|
||||
|
||||
private final OkHttpClient noRedirect;
|
||||
private final OkHttpClient client;
|
||||
private OkHttpClient proxy;
|
||||
private OkHttpClient client;
|
||||
|
||||
private static class Loader {
|
||||
static volatile Cronet INSTANCE = new Cronet();
|
||||
|
|
@ -28,66 +23,94 @@ public class Cronet {
|
|||
return Loader.INSTANCE;
|
||||
}
|
||||
|
||||
public Cronet() {
|
||||
client = getBuilder().build();
|
||||
noRedirect = client.newBuilder().followRedirects(false).followSslRedirects(false).build();
|
||||
public static OkHttpClient client() {
|
||||
if (get().client != null) return get().client;
|
||||
return get().client = getBuilder(OkHttp.getBuilder()).build();
|
||||
}
|
||||
|
||||
private OkHttpClient.Builder getBuilder() {
|
||||
OkHttpClient.Builder builder = OkHttp.getBuilder();
|
||||
public static OkHttpClient proxy() {
|
||||
if (get().proxy != null) return get().proxy;
|
||||
return get().proxy = getBuilder(OkHttp.getBuilder(Prefers.getString("proxy"))).build();
|
||||
}
|
||||
|
||||
public static OkHttpClient client(boolean proxy) {
|
||||
return proxy ? proxy() : client();
|
||||
}
|
||||
|
||||
public static OkHttpClient noRedirect(boolean proxy) {
|
||||
return client(proxy).newBuilder().followRedirects(false).followSslRedirects(false).build();
|
||||
}
|
||||
|
||||
public static String string(String url) {
|
||||
return string(false, url);
|
||||
}
|
||||
|
||||
public static String string(boolean proxy, String url) {
|
||||
return string(proxy, url, null);
|
||||
}
|
||||
|
||||
public static String string(String url, Map<String, String> header) {
|
||||
return string(false, url, header);
|
||||
}
|
||||
|
||||
public static String string(boolean proxy, String url, Map<String, String> header) {
|
||||
return string(proxy, url, null, header);
|
||||
}
|
||||
|
||||
public static String string(String url, Map<String, String> params, Map<String, String> header) {
|
||||
return string(false, url, params, header);
|
||||
}
|
||||
|
||||
public static String string(boolean proxy, String url, Map<String, String> params, Map<String, String> header) {
|
||||
return string(client(proxy), url, params, header);
|
||||
}
|
||||
|
||||
public static String string(OkHttpClient client, String url, Map<String, String> params, Map<String, String> header) {
|
||||
return new OkRequest(OkHttp.GET, url, params, header).execute(client).getBody();
|
||||
}
|
||||
|
||||
public static String post(String url, Map<String, String> params) {
|
||||
return post(false, url, params);
|
||||
}
|
||||
|
||||
public static String post(boolean proxy, String url, Map<String, String> params) {
|
||||
return post(proxy, url, params, null).getBody();
|
||||
}
|
||||
|
||||
public static OkResult post(String url, Map<String, String> params, Map<String, String> header) {
|
||||
return post(false, url, params, header);
|
||||
}
|
||||
|
||||
public static OkResult post(boolean proxy, String url, Map<String, String> params, Map<String, String> header) {
|
||||
return new OkRequest(OkHttp.POST, url, params, header).execute(client(proxy));
|
||||
}
|
||||
|
||||
public static String post(String url, String json) {
|
||||
return post(false, url, json);
|
||||
}
|
||||
|
||||
public static String post(boolean proxy, String url, String json) {
|
||||
return post(proxy, url, json, null).getBody();
|
||||
}
|
||||
|
||||
public static OkResult post(String url, String json, Map<String, String> header) {
|
||||
return post(false, url, json, header);
|
||||
}
|
||||
|
||||
public static OkResult post(boolean proxy, String url, String json, Map<String, String> header) {
|
||||
return new OkRequest(OkHttp.POST, url, json, header).execute(client(proxy));
|
||||
}
|
||||
|
||||
private static OkHttpClient.Builder getBuilder(OkHttpClient.Builder builder) {
|
||||
addInterceptor(builder);
|
||||
return builder;
|
||||
}
|
||||
|
||||
private void addInterceptor(OkHttpClient.Builder builder) {
|
||||
private static void addInterceptor(OkHttpClient.Builder builder) {
|
||||
try {
|
||||
builder.addInterceptor(CronetInterceptor.newBuilder(new CronetEngine.Builder(Init.context()).build()).build());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static OkHttpClient client() {
|
||||
return get().client;
|
||||
}
|
||||
|
||||
private static OkHttpClient noRedirect() {
|
||||
return get().noRedirect;
|
||||
}
|
||||
|
||||
public static String string(String url) {
|
||||
return string(url, null);
|
||||
}
|
||||
|
||||
public static String string(String url, Map<String, String> header) {
|
||||
return string(url, null, header);
|
||||
}
|
||||
|
||||
public static String string(String url, Map<String, String> params, Map<String, String> header) {
|
||||
return string(client(), GET, url, params, header);
|
||||
}
|
||||
|
||||
public static String string(OkHttpClient client, String method, String url, Map<String, String> params, Map<String, String> header) {
|
||||
return new OkRequest(method, url, params, header).execute(client).getBody();
|
||||
}
|
||||
|
||||
public static String post(String url, Map<String, String> params) {
|
||||
return post(url, params, null);
|
||||
}
|
||||
|
||||
public static String post(String url, Map<String, String> params, Map<String, String> header) {
|
||||
return string(client(), POST, url, params, header);
|
||||
}
|
||||
|
||||
public static OkResult postJson(String url, String json) {
|
||||
return postJson(url, json, null);
|
||||
}
|
||||
|
||||
public static OkResult postJson(String url, String json, Map<String, String> header) {
|
||||
return new OkRequest(POST, url, json, header).execute(client());
|
||||
}
|
||||
|
||||
public static String getLocation(String url, Map<String, String> header) throws IOException {
|
||||
return OkHttp.getLocation(noRedirect().newCall(new Request.Builder().url(url).headers(Headers.of(header)).build()).execute().headers().toMultimap());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,6 +84,10 @@ public class OkHttp {
|
|||
return string(proxy, url, null, header);
|
||||
}
|
||||
|
||||
public static String string(String url, Map<String, String> params, Map<String, String> header) {
|
||||
return string(false, url, params, header);
|
||||
}
|
||||
|
||||
public static String string(boolean proxy, String url, Map<String, String> params, Map<String, String> header) {
|
||||
return string(client(proxy), url, params, header);
|
||||
}
|
||||
|
|
@ -100,6 +104,14 @@ public class OkHttp {
|
|||
return post(proxy, url, params, null).getBody();
|
||||
}
|
||||
|
||||
public static OkResult post(String url, Map<String, String> params, Map<String, String> header) {
|
||||
return post(false, url, params, header);
|
||||
}
|
||||
|
||||
public static OkResult post(boolean proxy, String url, Map<String, String> params, Map<String, String> header) {
|
||||
return new OkRequest(POST, url, params, header).execute(client(proxy));
|
||||
}
|
||||
|
||||
public static String post(String url, String json) {
|
||||
return post(false, url, json);
|
||||
}
|
||||
|
|
@ -116,14 +128,6 @@ public class OkHttp {
|
|||
return new OkRequest(POST, url, json, header).execute(client(proxy));
|
||||
}
|
||||
|
||||
public static OkResult post(String url, Map<String, String> params, Map<String, String> header) {
|
||||
return post(false, url, params, header);
|
||||
}
|
||||
|
||||
public static OkResult post(boolean proxy, String url, Map<String, String> params, Map<String, String> header) {
|
||||
return new OkRequest(POST, url, params, header).execute(client(proxy));
|
||||
}
|
||||
|
||||
public static String getLocation(String url, Map<String, String> header) throws IOException {
|
||||
return getLocation(noRedirect(false).newCall(new Request.Builder().url(url).headers(Headers.of(header)).build()).execute().headers().toMultimap());
|
||||
}
|
||||
|
|
@ -139,7 +143,7 @@ public class OkHttp {
|
|||
return new OkHttpClient.Builder().dns(dns()).connectTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).hostnameVerifier(SSLCompat.VERIFIER).sslSocketFactory(new SSLCompat(), SSLCompat.TM);
|
||||
}
|
||||
|
||||
private static OkHttpClient.Builder getBuilder(String proxy) {
|
||||
public static OkHttpClient.Builder getBuilder(String proxy) {
|
||||
Uri uri = Uri.parse(proxy);
|
||||
String userInfo = uri.getUserInfo();
|
||||
OkHttpClient.Builder builder = client().newBuilder();
|
||||
|
|
|
|||
|
|
@ -738,6 +738,11 @@ public class AppYsV2 extends Spider {
|
|||
return Utils.isVideoFormat(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
OkHttp.get().resetProxy();
|
||||
}
|
||||
|
||||
private String getApiUrl() {
|
||||
if (extInfos == null || extInfos.length < 1) return "";
|
||||
return extInfos[0].trim();
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ import com.google.gson.JsonParser;
|
|||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
|
@ -64,14 +63,7 @@ public class Bili extends Spider {
|
|||
audios.put("30216", "64000");
|
||||
}
|
||||
|
||||
private String getCookie() throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
List<String> cookies = OkHttp.newCall("https://www.bilibili.com", getHeader()).headers("set-cookie");
|
||||
for (String cookie : cookies) sb.append(cookie.split(";")[0]).append(";");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private void setCookie() throws IOException {
|
||||
private void setCookie() {
|
||||
cookie = extend.get("cookie").getAsString();
|
||||
if (cookie.startsWith("http")) cookie = OkHttp.string(cookie).trim();
|
||||
if (TextUtils.isEmpty(cookie)) cookie = FileUtil.read(getUserCache());
|
||||
|
|
|
|||
|
|
@ -94,6 +94,11 @@ public class Eighteen extends Spider {
|
|||
return Result.get().url(result.get("url")).string();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
OkHttp.get().resetProxy();
|
||||
}
|
||||
|
||||
private String searchContent(String key, String pg) {
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
params.put("search_keyword", key);
|
||||
|
|
|
|||
|
|
@ -135,4 +135,9 @@ public class Hanime extends Spider {
|
|||
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
|
||||
return Result.get().url(id).header(getHeaders()).string();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
OkHttp.get().resetProxy();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,4 +101,9 @@ public class Jable extends Spider {
|
|||
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
|
||||
return Result.get().url(id).header(getHeaders()).string();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
OkHttp.get().resetProxy();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -97,6 +97,11 @@ public class Miss extends Spider {
|
|||
return Result.get().parse().url(url + id).string();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
OkHttp.get().resetProxy();
|
||||
}
|
||||
|
||||
private String searchContent(String key, String pg) {
|
||||
List<Vod> list = new ArrayList<>();
|
||||
Document doc = Jsoup.parse(OkHttp.string(proxy(), url + "search/" + key + "?page=" + pg));
|
||||
|
|
|
|||
|
|
@ -136,5 +136,10 @@ public class Star extends Spider {
|
|||
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
|
||||
return Result.get().url(id).string();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
OkHttp.get().resetProxy();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -317,6 +317,11 @@ public class XPath extends Spider {
|
|||
return Utils.isVideoFormat(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
OkHttp.get().resetProxy();
|
||||
}
|
||||
|
||||
protected String ext = null;
|
||||
protected Rule rule = null;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue