YanFrp/src/main/java/top/octopusyan/config/ProxyConfig.java

132 lines
3.3 KiB
Java
Raw Normal View History

2022-04-06 17:49:00 +08:00
package top.octopusyan.config;
2022-04-09 03:59:20 +08:00
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import top.octopusyan.model.ProxySetupModel;
2022-04-09 03:59:20 +08:00
import java.util.Arrays;
2022-04-06 17:49:00 +08:00
import java.util.HashMap;
import java.util.Map;
/**
* @author : octopus yan
* @email : octopus_yan@foxmail.com
* @description : 代理设置
* @create : 2022-4-5 18:14
*/
public class ProxyConfig {
2022-04-09 03:59:20 +08:00
private static final Map<String, String> serverPath = new HashMap<>();
private static final Map<String, String> serverIp = new HashMap<>();
private static final Map<String, Integer> typePort = new HashMap<>();
2022-04-06 17:49:00 +08:00
static {
2022-04-21 18:07:26 +08:00
serverPath.put("香港", "example.com");
serverPath.put("北京", "example.com");
serverPath.put("上海", "example.com");
2022-04-06 17:49:00 +08:00
2022-04-21 18:07:26 +08:00
serverIp.put("香港", "127.0.0.1");
serverIp.put("北京", "127.0.0.1");
serverIp.put("上海", "127.0.0.1");
2022-04-09 03:59:20 +08:00
2022-04-06 17:49:00 +08:00
typePort.put("http", 80);
typePort.put("https", 443);
typePort.put("tcp", 25565);
typePort.put("udp", 53);
2022-04-06 17:49:00 +08:00
}
2022-04-09 03:59:20 +08:00
@Getter
2022-04-06 17:49:00 +08:00
public enum ProxyServer {
2022-04-09 03:59:20 +08:00
xg("香港", 1),
bj("北京", 2),
sh("上海", 3);
private final String serverName;
private final int value;
ProxyServer(String serverName, int value) {
this.serverName = serverName;
this.value = value;
}
public static ProxyServer valueOf(int node) {
for (ProxyServer value : values()) {
if (value.value == node) return value;
}
return null;
}
2022-04-06 17:49:00 +08:00
}
public enum ProxyType {
HTTP,
HTTPS,
TCP,
UDP,
STCP,
XTCP,
2022-04-06 17:49:00 +08:00
;
}
public static String getServerPath(String serverName) {
return serverPath.get(serverName);
}
2022-04-09 03:59:20 +08:00
public static int getServerNode(String serverName) {
for (ProxyServer server : ProxyServer.values()) {
if (server.serverName.equals(serverName)) return server.value;
2022-04-09 03:59:20 +08:00
}
return 3;
}
/**
* 获取服务名称
*
2022-04-09 03:59:20 +08:00
* @param node 服务器标签
*/
public static String getServerName(int node) {
ProxyServer proxyServer = ProxyServer.valueOf(node);
assert proxyServer != null;
return proxyServer.getServerName();
}
public static boolean isHttp(ProxySetupModel model) {
return ProxyConfig.getTypeIndex(model.getProxyType()) < 2;
}
public static boolean isP2p(ProxySetupModel model) {
return ProxyConfig.getTypeIndex(model.getProxyType()) > 3;
}
2022-04-09 03:59:20 +08:00
/**
* 获取服务器IP地址
*
2022-04-09 03:59:20 +08:00
* @param serverName 服务器名称
*/
public static String getServerIP(String serverName) {
return serverIp.get(serverName);
}
/**
* 获取服务器IP地址
*
2022-04-09 03:59:20 +08:00
* @param node 服务器标签
*/
public static String getServerIP(int node) {
return serverIp.get(getServerName(node));
}
/**
* 获取代理类型默认端口
*
2022-04-09 03:59:20 +08:00
* @param type 类型名称
*/
2022-04-06 17:49:00 +08:00
public static Integer getTypePort(String type) {
Integer port = typePort.get(type);
return port == null ? 0 : port;
2022-04-06 17:49:00 +08:00
}
2022-04-09 03:59:20 +08:00
public static Integer getTypeIndex(String type) {
return Arrays.asList(ProxyType.values()).indexOf(ProxyType.valueOf(StringUtils.upperCase(type)));
}
2022-04-06 17:49:00 +08:00
}