diff --git a/neutrino-proxy-admin/src/api/portPool.js b/neutrino-proxy-admin/src/api/portPool.js index 4a8ee331..15baf75f 100644 --- a/neutrino-proxy-admin/src/api/portPool.js +++ b/neutrino-proxy-admin/src/api/portPool.js @@ -68,3 +68,11 @@ export function deleteBatchPortPool(ids) { } }) } + +export function portAvailable(query) { + return request({ + url: '/port-pool/port-available', + method: 'get', + params: query + }) +} diff --git a/neutrino-proxy-admin/src/views/proxy/portMapping.vue b/neutrino-proxy-admin/src/views/proxy/portMapping.vue index ec5f643a..6ce51537 100644 --- a/neutrino-proxy-admin/src/views/proxy/portMapping.vue +++ b/neutrino-proxy-admin/src/views/proxy/portMapping.vue @@ -142,12 +142,6 @@ - import { fetchList, createUserPortMapping, updateUserPortMapping, updateEnableStatus, deletePortMapping } from '@/api/portMapping' -import { portPoolList, availablePortList } from '@/api/portPool' +import { portPoolList, availablePortList, portAvailable } from '@/api/portPool' import { licenseList, licenseAuthList } from '@/api/license' import { protocalList } from '@/api/protocal' import { userList } from '@/api/user' @@ -234,6 +228,16 @@ export default { loadSelect }, data() { + let isPortAvailable = (rule, value, callback) => { + if(value!=null){ + let param = {port: value} + portAvailable(param).then(res => { + if(!res.data.data){ + return callback(new Error('该端口被占用')); + } + }); + } + }; return { tableKey: 0, list: null, @@ -287,7 +291,8 @@ export default { pvData: [], rules: { licenseId: [{ required: true, message: '请选择License', trigger: 'blur,change' }], - serverPort: [{ required: true, message: '请输入服务端端口', trigger: 'blur' }], + serverPort: [{ required: true, message: '请输入服务端端口', trigger: 'blur' }, + { validator: isPortAvailable, trigger: 'change' }], clientIp: [{ required: true, message: '请输入客户端IP', trigger: 'blur' }], clientPort: [{ required: true, message: '请输入客户端端口', trigger: 'blur' }], protocal: [{ required: true, message: '请选择协议', trigger: 'blur' }] @@ -572,7 +577,7 @@ export default { resolve(); }); }); - }, + } } } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/PortPoolController.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/PortPoolController.java index 76ac35fe..60b1aa30 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/PortPoolController.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/controller/PortPoolController.java @@ -133,4 +133,12 @@ public class PortPoolController { portPoolService.deleteBatch(req.getIds()); } + + @Get + @Mapping("/port-available") + public boolean portAvailable(Integer port) { + ParamCheckUtil.checkNotNull(port, "port"); + + return portPoolService.portAvailable(port); + } } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/service/PortPoolService.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/service/PortPoolService.java index a8e42f00..97cb2fba 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/service/PortPoolService.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/service/PortPoolService.java @@ -28,6 +28,7 @@ import org.apache.ibatis.solon.annotation.Db; import org.dromara.neutrinoproxy.server.controller.req.system.*; import org.dromara.neutrinoproxy.server.controller.res.system.*; import org.dromara.neutrinoproxy.server.dal.entity.PortGroupDO; +import org.dromara.neutrinoproxy.server.util.PortAvailableUtil; import org.noear.solon.annotation.Component; import org.noear.solon.annotation.Inject; @@ -198,4 +199,13 @@ public class PortPoolService { visitorChannelService.updateVisitorChannelByPortPool(portPoolDO.getPort(), EnableStatusEnum.DISABLE.getStatus()); }); } + + /** + * 检查端口是否被占用 + * @param port + * @return + */ + public boolean portAvailable(Integer port) { + return PortAvailableUtil.isPortAvailable(port); + } } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/util/PortAvailableUtil.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/util/PortAvailableUtil.java new file mode 100644 index 00000000..bca0796a --- /dev/null +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/util/PortAvailableUtil.java @@ -0,0 +1,39 @@ +package org.dromara.neutrinoproxy.server.util; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; + +/** + * 检查端口是否被占用 + * 文章参考:https://blog.csdn.net/xingluxiaoseng/article/details/40148527 + */ +public class PortAvailableUtil { + + private static void bindPort(String host, int port) throws IOException { + Socket s = new Socket(); + s.bind(new InetSocketAddress(host, port)); + s.close(); + } + + /** + * 端口占用判断,若是端口被占用,则会抛出IOException异常,表示端口被占用 + * @param port + * @return + */ + public static boolean isPortAvailable(int port) { + try { + bindPort("0.0.0.0", port); + bindPort(InetAddress.getLocalHost().getHostAddress(), port); + return true; + } catch (Exception e) { + return false; + } + } + + public static void main(String[] args) { + System.out.println("端口被占用:"+isPortAvailable(9527)); + } + +}