You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.3 KiB
45 lines
1.3 KiB
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.io.UnsupportedEncodingException;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
|
|
public class Proxy extends Spider {
|
|
|
|
private static int port = -1;
|
|
|
|
public static Object[] proxy(Map<String, String> params) throws UnsupportedEncodingException {
|
|
switch (Objects.requireNonNull(params.get("do"))) {
|
|
case "ck":
|
|
return new Object[]{200, "text/plain; charset=utf-8", new ByteArrayInputStream("ok".getBytes("UTF-8"))};
|
|
case "ali":
|
|
return Init.getAli().vod(params);
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
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 String getUrl() {
|
|
adjustPort();
|
|
return "http://127.0.0.1:" + port + "/proxy";
|
|
}
|
|
}
|
|
|