|
|
|
|
@ -1,14 +1,20 @@ |
|
|
|
|
package com.github.catvod.net; |
|
|
|
|
|
|
|
|
|
import android.net.Uri; |
|
|
|
|
import android.text.TextUtils; |
|
|
|
|
|
|
|
|
|
import com.github.catvod.crawler.Spider; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.net.Authenticator; |
|
|
|
|
import java.net.InetSocketAddress; |
|
|
|
|
import java.net.Proxy; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
import okhttp3.Call; |
|
|
|
|
import okhttp3.Credentials; |
|
|
|
|
import okhttp3.Dns; |
|
|
|
|
import okhttp3.Headers; |
|
|
|
|
import okhttp3.OkHttpClient; |
|
|
|
|
@ -37,7 +43,9 @@ public class OkHttp { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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() { |
|
|
|
|
@ -56,11 +64,19 @@ public class OkHttp { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Proxy proxy() { |
|
|
|
|
public static String proxy() { |
|
|
|
|
try { |
|
|
|
|
return (String) Spider.class.getMethod("proxy").invoke(null); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Authenticator auth() { |
|
|
|
|
try { |
|
|
|
|
return (Proxy) Spider.class.getMethod("proxy").invoke(null); |
|
|
|
|
return (Authenticator) Spider.class.getMethod("auth").invoke(null); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return Proxy.NO_PROXY; |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -147,4 +163,19 @@ public class OkHttp { |
|
|
|
|
if (headers.containsKey("Location")) return headers.get("Location").get(0); |
|
|
|
|
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(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|