| 9 | import java.util.Map; |
| 10 | |
| 11 | public class Proxy { |
| 12 | |
| 13 | private static Method method; |
| 14 | private static int port; |
| 15 | |
| 16 | public static Object[] proxy(Map<String, String> params) { |
| 17 | if ("ck".equals(params.get("do"))) return new Object[]{200, "text/plain; charset=utf-8", new ByteArrayInputStream("ok".getBytes(StandardCharsets.UTF_8))}; |
| 18 | return null; |
| 19 | } |
| 20 | |
| 21 | public static void init() { |
| 22 | try { |
| 23 | Class<?> clz = Class.forName("com.github.catvod.Proxy"); |
| 24 | port = (int) clz.getMethod("getPort").invoke(null); |
| 25 | method = clz.getMethod("getUrl", boolean.class); |
| 26 | SpiderDebug.log("本地代理端口:" + port); |
| 27 | } catch (Throwable e) { |
| 28 | findPort(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public static int getPort() { |
| 33 | return port; |
| 34 | } |
| 35 | |
| 36 | public static String getUrl(String siteKey, String param) { |
| 37 | return "proxy://do=csp&siteKey=" + siteKey + param; |
| 38 | } |
| 39 | |
| 40 | public static String getUrl() { |
| 41 | return getUrl(true); |
| 42 | } |
| 43 | |
| 44 | public static String getUrl(boolean local) { |
| 45 | try { |
| 46 | return (String) method.invoke(null, local); |
| 47 | } catch (Throwable e) { |
| 48 | return "http://127.0.0.1:" + port + "/proxy"; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private static void findPort() { |
| 53 | if (port > 0) return; |
| 54 | for (int p = 8964; p < 9999; p++) { |
| 55 | if ("ok".equals(OkHttp.string("http://127.0.0.1:" + p + "/proxy?do=ck", null))) { |
| 56 | SpiderDebug.log("本地代理端口:" + p); |
| 57 | port = p; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
nothing calls this directly
no outgoing calls
no test coverage detected