163 lines
5.3 KiB
Java
163 lines
5.3 KiB
Java
package top.octopusyan.utils;
|
|
|
|
import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.select.Elements;
|
|
import top.octopusyan.common.http.EasyHttp;
|
|
import top.octopusyan.common.http.listener.OnHttpListener;
|
|
import top.octopusyan.common.http.model.ResponseClass;
|
|
import top.octopusyan.config.ProxyConfig.*;
|
|
import top.octopusyan.http.Api;
|
|
import top.octopusyan.http.request.ProxySetup;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* @author : octopus yan
|
|
* @email : octopus_yan@foxmail.com
|
|
* @description : 代理设置工具
|
|
* @create : 2022-4-5 09:56
|
|
*/
|
|
public class ProxyUtil {
|
|
private static String csrf;
|
|
|
|
/**
|
|
* 初始化隧道设置
|
|
*/
|
|
public static ProxySetup initProxy() {
|
|
ProxySetup setup = new ProxySetup();
|
|
setup.setNode(3);// 上海服务器
|
|
setup.setLocal_ip("127.0.0.1");
|
|
setup.setLocal_port(80);
|
|
setup.setProxy_name("默认连接");
|
|
setup.setProxy_type(ProxyType.HTTP.name());
|
|
setup.setUse_compression(true);
|
|
setup.setUse_encryption(true);
|
|
return setup;
|
|
}
|
|
|
|
public static void delete(Integer id) {
|
|
EasyHttp.builder()
|
|
.api(Api.DeleteProxy())
|
|
.pathParam(id, csrf)
|
|
.request(result -> { });
|
|
}
|
|
|
|
/**
|
|
* 获取隧道详情信息
|
|
* @return
|
|
*/
|
|
public static ProxySetup info(ProxySetup setup) {
|
|
|
|
String html;
|
|
try {
|
|
html = EasyHttp.builder()
|
|
.api(Api.ProxyInfo())
|
|
.pathParam(setup.getId())
|
|
.execute(new ResponseClass<String>() {
|
|
});
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
|
|
Document doc = Jsoup.parse(html);
|
|
Elements select = doc.select("tbody > tr > td");
|
|
|
|
String serverStr = select.get(0).text();
|
|
String localIp = select.get(3).text();
|
|
String localPort = select.get(4).text();
|
|
String webPort = select.get(5).text();
|
|
String useEnc = select.get(6).text();
|
|
String useCom = select.get(7).text();
|
|
String domain = select.get(8).text();
|
|
String status = select.get(13).text();
|
|
|
|
Arrays.stream(ProxyServer.values()).forEach(server -> {
|
|
if (serverStr.contains(server.name()))
|
|
setup.setNode(Arrays.asList(ProxyServer.values()).indexOf(server));
|
|
});
|
|
|
|
setup.setLocal_ip(localIp);
|
|
setup.setLocal_port(Integer.parseInt(localPort));
|
|
setup.setRemote_port("无".equals(webPort) ? null : Integer.parseInt(webPort));
|
|
setup.setDomain(domain);
|
|
setup.setUse_compression(useCom.equals("启用"));
|
|
setup.setUse_encryption(useEnc.equals("启用"));
|
|
setup.setStatus(status.equals("启用"));
|
|
|
|
return setup;
|
|
}
|
|
|
|
public static int randomPort(){
|
|
try {
|
|
return EasyHttp.builder()
|
|
.api(Api.RandomPort())
|
|
.execute(new ResponseClass<Integer>() { });
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return (int) (Math.random() * 31000);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取用户隧道列表
|
|
*/
|
|
public static void getList(OnHttpListener<List<ProxySetup>> listener) {
|
|
EasyHttp.builder()
|
|
.api(Api.ProxyList)
|
|
.request(new OnHttpListener<String>() {
|
|
@Override
|
|
public void onSucceed(String result) {
|
|
Document doc = Jsoup.parse(result);
|
|
Elements elementsByClass = doc.select("table > tbody > tr");
|
|
|
|
List<ProxySetup> proxySetupList = elementsByClass.stream().map(element -> {
|
|
ProxySetup proxySetup = new ProxySetup();
|
|
Elements tds = element.getElementsByTag("td");
|
|
proxySetup.setId(Integer.parseInt(tds.get(0).text()));
|
|
proxySetup.setProxy_name(tds.get(1).text());
|
|
proxySetup.setProxy_type(tds.get(2).text());
|
|
proxySetup.setSort(Integer.parseInt(tds.get(5).text()));
|
|
return proxySetup;
|
|
}).collect(Collectors.toList());
|
|
|
|
listener.onSucceed(proxySetupList);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static boolean add(OnHttpListener<ProxySetup> listener, ProxySetup setup) {
|
|
String result = null;
|
|
try {
|
|
result = EasyHttp.builder()
|
|
.api(Api.AddProxy())
|
|
.param(setup)
|
|
.execute(new ResponseClass<String>() {
|
|
});
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
|
|
if(result.equals("隧道创建成功")){
|
|
getList(result1 -> listener.onSucceed(result1.get(result1.size()-1)));
|
|
} else {
|
|
AlertUtil.error(result).show();
|
|
}
|
|
return result.equals("隧道创建成功");
|
|
}
|
|
|
|
public static String getCsrf() {
|
|
return csrf;
|
|
}
|
|
|
|
public static void setCsrf(String htmlStr) {
|
|
int i = htmlStr.indexOf("var csrf_token = \"");
|
|
int i1 = htmlStr.indexOf("\"", i + 18);
|
|
ProxyUtil.csrf = htmlStr.substring(i + 18, i1);
|
|
}
|
|
}
|