mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-09-29 16:06:36 +08:00
适配h5
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.admin.common.utils;
|
||||
|
||||
import com.admin.common.dto.GostConfigDto;
|
||||
import com.admin.common.dto.GostDto;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@@ -16,10 +17,35 @@ public class GostUtil {
|
||||
public static GostDto SaveConfig(String addr, String secret) {
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("format", "json");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
String url = "https://" + addr + "/api/config?format=json";
|
||||
return HttpUtils.post(url, data, secret);
|
||||
}
|
||||
|
||||
public static GostConfigDto GetConfig(String addr, String secret) {
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("format", "json");
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
String url = "https://" + addr + "/api/config?format=json";
|
||||
return HttpUtils.get(url, secret);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加限流器配置
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.admin.common.utils;
|
||||
|
||||
import com.admin.common.dto.GostConfigDto;
|
||||
import com.admin.common.dto.GostDto;
|
||||
import com.admin.common.task.SaveConfigAsync;
|
||||
import com.admin.config.RestTemplateConfig;
|
||||
@@ -145,6 +146,32 @@ public class HttpUtils implements ApplicationContextAware {
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public static GostConfigDto get(String url, String secret) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
String auth = secret + ":" + secret;
|
||||
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
||||
headers.set("Authorization", "Basic " + encodedAuth);
|
||||
RestTemplate restTemplate = createRestTemplateWithTimeout();
|
||||
HttpEntity<Object> entity = new HttpEntity<>("", headers);
|
||||
try {
|
||||
ResponseEntity<GostConfigDto> response = restTemplate.exchange(
|
||||
url,
|
||||
HttpMethod.GET,
|
||||
entity,
|
||||
GostConfigDto.class
|
||||
);
|
||||
return response.getBody();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
GostConfigDto gostDto = new GostConfigDto();
|
||||
return gostDto;
|
||||
}
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static GostDto post(String url, Object requestBody, String secret) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
@@ -99,6 +99,7 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
|
||||
|
||||
// 6. 调用Gost服务创建转发
|
||||
R gostResult = createGostServices(forward, tunnel, permissionResult.getLimiter());
|
||||
|
||||
if (gostResult.getCode() != 0) {
|
||||
this.removeById(forward.getId());
|
||||
return gostResult;
|
||||
@@ -538,13 +539,11 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
|
||||
if (tunnel.getType() == TUNNEL_TYPE_TUNNEL_FORWARD) {
|
||||
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, tunnel.getProtocol());
|
||||
if (remoteResult.getCode() != 0) {
|
||||
updateForwardStatusToError(forward);
|
||||
return remoteResult;
|
||||
}
|
||||
|
||||
@@ -553,7 +552,6 @@ public class ForwardServiceImpl extends ServiceImpl<ForwardMapper, Forward> impl
|
||||
// 创建主服务
|
||||
R serviceResult = createMainService(inNode, serviceName, forward, limiter, tunnel.getType());
|
||||
if (serviceResult.getCode() != 0) {
|
||||
updateForwardStatusToError(forward);
|
||||
return serviceResult;
|
||||
}
|
||||
return R.ok();
|
||||
|
||||
@@ -187,14 +187,12 @@ public class SpeedLimitServiceImpl extends ServiceImpl<SpeedLimitMapper, SpeedLi
|
||||
// 3. 获取隧道信息
|
||||
Tunnel tunnel = tunnelService.getById(speedLimit.getTunnelId());
|
||||
if (tunnel == null) {
|
||||
return R.err(ERROR_TUNNEL_NOT_EXISTS);
|
||||
this.removeById(id);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
// 4. 调用Gost API删除限速器
|
||||
R gostResult = deleteGostLimiter(id, tunnel);
|
||||
if (gostResult.getCode() != 0) {
|
||||
return gostResult;
|
||||
}
|
||||
deleteGostLimiter(id, tunnel);
|
||||
|
||||
// 5. 删除限速规则
|
||||
boolean result = this.removeById(id);
|
||||
|
||||
Reference in New Issue
Block a user