修复v6 添加隧道类型选择

This commit is contained in:
qaq
2025-06-18 11:13:41 +08:00
parent d56c94fd43
commit 8ddd65a58c
19 changed files with 568 additions and 88 deletions
@@ -39,4 +39,7 @@ public class TunnelDto {
@NotNull(message = "流量计算类型不能为空")
private Integer flow;
// 协议类型(隧道转发时使用:tls、tcp、mtls),默认为tls
private String protocol;
}
@@ -82,6 +82,8 @@ public class UserTunnelWithDetailDto {
* 出站流量(字节)
*/
private Long outFlow;
private Integer status;
// /**
// * 入口IP
@@ -16,7 +16,7 @@ public class GostUtil {
public static GostDto SaveConfig(String addr, String secret) {
JSONObject data = new JSONObject();
data.put("format", "json");
String url = "http://" + addr + "/api/config?format=json";
String url = "https://" + addr + "/api/config?format=json";
return HttpUtils.post(url, data, secret);
}
@@ -181,9 +181,9 @@ public class GostUtil {
return HttpUtils.post(url, data, secret);
}
public static GostDto AddChains(String addr, String name, String remoteAddr, String secret) {
public static GostDto AddChains(String addr, String name, String remoteAddr, String secret, String protocol) {
JSONObject dialer = new JSONObject();
dialer.put("type", "tls");
dialer.put("type", protocol);
JSONObject connector = new JSONObject();
connector.put("type", "relay");
@@ -212,9 +212,9 @@ public class GostUtil {
return HttpUtils.post(url, data, secret);
}
public static GostDto UpdateChains(String addr, String name, String remoteAddr, String secret) {
public static GostDto UpdateChains(String addr, String name, String remoteAddr, String secret, String protocol) {
JSONObject dialer = new JSONObject();
dialer.put("type", "tls");
dialer.put("type", protocol);
JSONObject connector = new JSONObject();
connector.put("type", "relay");
@@ -249,7 +249,7 @@ public class GostUtil {
return HttpUtils.delete(url, secret);
}
public static GostDto AddRemoteService(String addr, String name, Integer out_port, String remoteAddr, String secret) {
public static GostDto AddRemoteService(String addr, String name, Integer out_port, String remoteAddr, String secret, String protocol) {
JSONObject data = new JSONObject();
data.put("name", name + "_tls");
data.put("addr", ":" + out_port);
@@ -257,7 +257,7 @@ public class GostUtil {
handler.put("type", "relay");
data.put("handler", handler);
JSONObject listener = new JSONObject();
listener.put("type", "tls");
listener.put("type", protocol);
data.put("listener", listener);
JSONObject forwarder = new JSONObject();
JSONArray nodes = new JSONArray();
@@ -388,6 +388,14 @@ public class GostUtil {
* 构建API URL
*/
private static String buildUrl(String addr, String endpoint) {
return "http://" + addr + API_BASE_URL + endpoint;
// 如果是IPv6地址(包含多个冒号且不包含方括号),需要用方括号包裹IP部分
if (!addr.contains("[") && addr.indexOf(':') != addr.lastIndexOf(':')) {
// 这是IPv6地址,找到最后一个冒号(端口分隔符)
int lastColonIndex = addr.lastIndexOf(':');
String ipPart = addr.substring(0, lastColonIndex);
String portPart = addr.substring(lastColonIndex);
addr = "[" + ipPart + "]" + portPart;
}
return "https://" + addr + API_BASE_URL + endpoint;
}
}
@@ -135,17 +135,11 @@ public class HttpUtils implements ApplicationContextAware {
/**
* 创建带超时配置的RestTemplate
*/
@SneakyThrows
private static RestTemplate createRestTemplateWithTimeout() {
// 创建基础的RequestFactory
org.springframework.http.client.SimpleClientHttpRequestFactory factory =
new org.springframework.http.client.SimpleClientHttpRequestFactory();
// 设置连接超时和读取超时
factory.setConnectTimeout(TIMEOUT_MILLISECONDS);
factory.setReadTimeout(TIMEOUT_MILLISECONDS);
// 创建RestTemplate
RestTemplate restTemplate = new RestTemplate(factory);
RestTemplate restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
restTemplate.setErrorHandler(new NoOpResponseErrorHandler());
return restTemplate;
@@ -203,6 +197,7 @@ public class HttpUtils implements ApplicationContextAware {
asyncSaveConfig(url, secret);
return body;
} catch (Exception e) {
e.printStackTrace();
GostDto gostDto = new GostDto();
gostDto.setCode(500);
gostDto.setMsg("请求失败");
@@ -27,11 +27,16 @@ public class RestTemplateConfig {
return new RestTemplate(factory);
}
// 10秒超时配置
private static final int TIMEOUT_SECONDS = 10;
private static final int TIMEOUT_MILLISECONDS = TIMEOUT_SECONDS * 1000;
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
factory.setConnectTimeout(TIMEOUT_MILLISECONDS);
factory.setReadTimeout(TIMEOUT_MILLISECONDS);
return factory;
}
@@ -72,4 +72,9 @@ public class Tunnel extends BaseEntity {
* 流量计算类型(1 单向计算上传。2 双向)
*/
private int flow;
/**
* 协议类型(隧道转发时使用:tls、tcp、mtls)
*/
private String protocol;
}
@@ -536,13 +536,13 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
// 隧道转发需要创建链和远程服务
if (tunnel.getType() == TUNNEL_TYPE_TUNNEL_FORWARD) {
R chainResult = createChainService(inNode, serviceName, tunnel.getOutIp(), forward.getOutPort());
R chainResult = createChainService(inNode, serviceName, tunnel.getOutIp(), forward.getOutPort(), tunnel.getProtocol());
if (chainResult.getCode() != 0) {
updateForwardStatusToError(forward);
return chainResult;
}
R remoteResult = createRemoteService(tunnel.getOutNodeId().intValue(), serviceName, forward);
R remoteResult = createRemoteService(tunnel.getOutNodeId().intValue(), serviceName, forward, tunnel.getProtocol());
if (remoteResult.getCode() != 0) {
updateForwardStatusToError(forward);
return remoteResult;
@@ -568,13 +568,13 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
// 隧道转发需要更新链和远程服务
if (tunnel.getType() == TUNNEL_TYPE_TUNNEL_FORWARD) {
R chainResult = updateChainService(inNode, serviceName, tunnel.getOutIp(), forward.getOutPort());
R chainResult = updateChainService(inNode, serviceName, tunnel.getOutIp(), forward.getOutPort(), tunnel.getProtocol());
if (chainResult.getCode() != 0) {
updateForwardStatusToError(forward);
return chainResult;
}
R remoteResult = updateRemoteService(tunnel.getOutNodeId().intValue(), serviceName, forward);
R remoteResult = updateRemoteService(tunnel.getOutNodeId().intValue(), serviceName, forward, tunnel.getProtocol());
if (remoteResult.getCode() != 0) {
updateForwardStatusToError(forward);
return remoteResult;
@@ -624,20 +624,20 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
/**
* 创建链服务
*/
private R createChainService(Node inNode, String serviceName, String outIp, Integer outPort) {
private R createChainService(Node inNode, String serviceName, String outIp, Integer outPort, String protocol) {
String remoteAddr = outIp + ":" + outPort;
GostDto result = GostUtil.AddChains(inNode.getIp() + ":" + inNode.getPort(), serviceName, remoteAddr, inNode.getSecret());
GostDto result = GostUtil.AddChains(inNode.getIp() + ":" + inNode.getPort(), serviceName, remoteAddr, inNode.getSecret(), protocol);
return isGostOperationSuccess(result) ? R.ok() : R.err(result.getMsg());
}
/**
* 创建远程服务
*/
private R createRemoteService(Integer outNodeId, String serviceName, Forward forward) {
private R createRemoteService(Integer outNodeId, String serviceName, Forward forward, String protocol) {
Node outNode = nodeService.getNodeById(outNodeId.longValue());
GostDto result = GostUtil.AddRemoteService(outNode.getIp() + ":" + outNode.getPort(),
serviceName, forward.getOutPort(),
forward.getRemoteAddr(), outNode.getSecret());
forward.getRemoteAddr(), outNode.getSecret(), protocol);
return isGostOperationSuccess(result) ? R.ok() : R.err(result.getMsg());
}
@@ -654,13 +654,13 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
/**
* 更新链服务
*/
private R updateChainService(Node inNode, String serviceName, String outIp, Integer outPort) {
private R updateChainService(Node inNode, String serviceName, String outIp, Integer outPort, String protocol) {
// 创建新链
String remoteAddr = outIp + ":" + outPort;
GostDto createResult = GostUtil.UpdateChains(inNode.getIp() + ":" + inNode.getPort(), serviceName, remoteAddr, inNode.getSecret());
GostDto createResult = GostUtil.UpdateChains(inNode.getIp() + ":" + inNode.getPort(), serviceName, remoteAddr, inNode.getSecret(), protocol);
if (createResult.getMsg().contains(GOST_NOT_FOUND_MSG)) {
createResult = GostUtil.AddChains(inNode.getIp() + ":" + inNode.getPort(), serviceName, remoteAddr, inNode.getSecret());
createResult = GostUtil.AddChains(inNode.getIp() + ":" + inNode.getPort(), serviceName, remoteAddr, inNode.getSecret(), protocol);
}
return isGostOperationSuccess(createResult) ? R.ok() : R.err(createResult.getMsg());
}
@@ -668,7 +668,7 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
/**
* 更新远程服务
*/
private R updateRemoteService(Integer outNodeId, String serviceName, Forward forward) {
private R updateRemoteService(Integer outNodeId, String serviceName, Forward forward, String protocol) {
Node outNode = nodeService.getNodeById(outNodeId.longValue());
// 创建新远程服务
GostDto createResult = GostUtil.UpdateRemoteService(outNode.getIp() + ":" + outNode.getPort(),
@@ -677,7 +677,7 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
if (createResult.getMsg().contains(GOST_NOT_FOUND_MSG)) {
createResult = GostUtil.AddRemoteService(outNode.getIp() + ":" + outNode.getPort(),
serviceName, forward.getOutPort(),
forward.getRemoteAddr(), outNode.getSecret());
forward.getRemoteAddr(), outNode.getSecret(),protocol);
}
return isGostOperationSuccess(createResult) ? R.ok() : R.err(createResult.getMsg());
}
@@ -257,6 +257,16 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
// 设置流量计算类型
tunnel.setFlow(tunnelDto.getFlow());
// 设置协议类型(仅隧道转发需要)
if (tunnelDto.getType() == TUNNEL_TYPE_TUNNEL_FORWARD) {
// 隧道转发时,设置协议类型,默认为tls
String protocol = StrUtil.isNotBlank(tunnelDto.getProtocol()) ? tunnelDto.getProtocol() : "tls";
tunnel.setProtocol(protocol);
} else {
// 端口转发时,协议类型为null
tunnel.setProtocol(null);
}
return tunnel;
}
@@ -310,6 +320,13 @@ public class TunnelServiceImpl extends ServiceImpl<TunnelMapper, Tunnel> impleme
return R.err(ERROR_SAME_NODE_NOT_ALLOWED);
}
// 验证协议类型
String protocol = tunnelDto.getProtocol();
if (StrUtil.isNotBlank(protocol) &&
!protocol.equals("tls") && !protocol.equals("tcp") && !protocol.equals("mtls")) {
return R.err("协议类型只能为tls、tcp或mtls");
}
// 验证出口节点是否存在
Node outNode = nodeService.getById(tunnelDto.getOutNodeId());
if (outNode == null) {