Support proxy
This commit is contained in:
parent
466728a7d7
commit
6e4f3dd77f
|
|
@ -1,14 +1,20 @@
|
||||||
package com.github.catvod.net;
|
package com.github.catvod.net;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.github.catvod.crawler.Spider;
|
import com.github.catvod.crawler.Spider;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.Authenticator;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import okhttp3.Call;
|
import okhttp3.Call;
|
||||||
|
import okhttp3.Credentials;
|
||||||
import okhttp3.Dns;
|
import okhttp3.Dns;
|
||||||
import okhttp3.Headers;
|
import okhttp3.Headers;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
|
@ -37,7 +43,9 @@ public class OkHttp {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OkHttpClient.Builder getBuilder() {
|
public static OkHttpClient.Builder getBuilder() {
|
||||||
return new OkHttpClient.Builder().dns(safeDns()).proxy(proxy()).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).connectTimeout(30, TimeUnit.SECONDS).hostnameVerifier(SSLCompat.VERIFIER).sslSocketFactory(new SSLCompat(), SSLCompat.TM);
|
OkHttpClient.Builder builder = new OkHttpClient.Builder().dns(safeDns()).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).connectTimeout(30, TimeUnit.SECONDS).hostnameVerifier(SSLCompat.VERIFIER).sslSocketFactory(new SSLCompat(), SSLCompat.TM);
|
||||||
|
if (!TextUtils.isEmpty(proxy())) setProxy(builder);
|
||||||
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OkHttpClient client() {
|
public static OkHttpClient client() {
|
||||||
|
|
@ -56,11 +64,19 @@ public class OkHttp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Proxy proxy() {
|
public static String proxy() {
|
||||||
try {
|
try {
|
||||||
return (Proxy) Spider.class.getMethod("proxy").invoke(null);
|
return (String) Spider.class.getMethod("proxy").invoke(null);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return Proxy.NO_PROXY;
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Authenticator auth() {
|
||||||
|
try {
|
||||||
|
return (Authenticator) Spider.class.getMethod("auth").invoke(null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,4 +163,19 @@ public class OkHttp {
|
||||||
if (headers.containsKey("Location")) return headers.get("Location").get(0);
|
if (headers.containsKey("Location")) return headers.get("Location").get(0);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void setProxy(OkHttpClient.Builder builder) {
|
||||||
|
Uri uri = Uri.parse(proxy());
|
||||||
|
String userInfo = uri.getUserInfo();
|
||||||
|
if (uri.getScheme() != null && uri.getScheme().startsWith("socks")) {
|
||||||
|
builder.proxy(new Proxy(Proxy.Type.SOCKS, InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort())));
|
||||||
|
}
|
||||||
|
if (uri.getScheme() != null && uri.getScheme().startsWith("http")) {
|
||||||
|
builder.proxy(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort())));
|
||||||
|
if (userInfo != null && userInfo.contains(":")) builder.proxyAuthenticator((route, response) -> {
|
||||||
|
String credential = Credentials.basic(userInfo.split(":")[0], userInfo.split(":")[1]);
|
||||||
|
return response.request().newBuilder().header("Proxy-Authorization", credential).build();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1 +1 @@
|
||||||
c5b10629e8a887e330888d5907eaa800
|
17be876e842892abafa9dd0306e63cc3
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue