|
|
|
|
@ -1,15 +1,16 @@ |
|
|
|
|
package com.github.catvod.spider; |
|
|
|
|
|
|
|
|
|
import com.github.catvod.crawler.Spider; |
|
|
|
|
import com.github.catvod.crawler.SpiderDebug; |
|
|
|
|
import com.github.catvod.net.OkHttp; |
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
public class Proxy extends Spider { |
|
|
|
|
public class Proxy { |
|
|
|
|
|
|
|
|
|
private static int port = -1; |
|
|
|
|
private static Method method; |
|
|
|
|
private static int port; |
|
|
|
|
|
|
|
|
|
public static Object[] proxy(Map<String, String> params) throws Exception { |
|
|
|
|
switch (params.get("do")) { |
|
|
|
|
@ -26,27 +27,34 @@ public class Proxy extends Spider { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void adjustPort() { |
|
|
|
|
if (Proxy.port > 0) return; |
|
|
|
|
int port = 9978; |
|
|
|
|
while (port < 10000) { |
|
|
|
|
String resp = OkHttp.string("http://127.0.0.1:" + port + "/proxy?do=ck", null); |
|
|
|
|
if (resp.equals("ok")) { |
|
|
|
|
SpiderDebug.log("Found local server port " + port); |
|
|
|
|
Proxy.port = port; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
port++; |
|
|
|
|
public static void init() { |
|
|
|
|
try { |
|
|
|
|
method = Class.forName("com.github.catvod.Proxy").getMethod("getUrl", boolean.class); |
|
|
|
|
} catch (Throwable e) { |
|
|
|
|
findPort(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static int getPort() { |
|
|
|
|
adjustPort(); |
|
|
|
|
return port; |
|
|
|
|
public static String getUrl(boolean local) { |
|
|
|
|
try { |
|
|
|
|
return (String) method.invoke(null, local); |
|
|
|
|
} catch (Throwable e) { |
|
|
|
|
return "http://127.0.0.1:" + port + "/proxy"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getUrl() { |
|
|
|
|
adjustPort(); |
|
|
|
|
return "http://127.0.0.1:" + port + "/proxy"; |
|
|
|
|
return getUrl(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void findPort() { |
|
|
|
|
if (port > 0) return; |
|
|
|
|
for (int p = 8964; p < 9999; p++) { |
|
|
|
|
if ("ok".equals(OkHttp.string("http://127.0.0.1:" + p + "/proxy?do=ck", null))) { |
|
|
|
|
SpiderDebug.log("本地代理端口:" + p); |
|
|
|
|
port = p; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|