Commit 929c3830 by 周海峰

优化

parent 65cde3f0
...@@ -5,6 +5,7 @@ import com.chenyang.nse.bussiness.commmon.json.Response; ...@@ -5,6 +5,7 @@ import com.chenyang.nse.bussiness.commmon.json.Response;
import com.chenyang.nse.bussiness.entity.ip.Ip; import com.chenyang.nse.bussiness.entity.ip.Ip;
import com.chenyang.nse.bussiness.tools.command.CommandTool; import com.chenyang.nse.bussiness.tools.command.CommandTool;
import com.chenyang.nse.bussiness.tools.msg.MesUtil; import com.chenyang.nse.bussiness.tools.msg.MesUtil;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
...@@ -23,6 +24,7 @@ import java.util.Enumeration; ...@@ -23,6 +24,7 @@ import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -47,17 +49,17 @@ public class ChangeIPController { ...@@ -47,17 +49,17 @@ public class ChangeIPController {
@PostMapping({"/update"}) @PostMapping({"/update"})
public Response update(@RequestBody Map<String, String> param) throws UnknownHostException { public Response update(@RequestBody Map<String, String> param) throws UnknownHostException {
String networkInterfaceName = (String)param.get("networkInterface"); String networkInterfaceName = (String) param.get("networkInterface");
if (StringUtils.isEmpty(networkInterfaceName)) { if (StringUtils.isEmpty(networkInterfaceName)) {
return RespHelper.createResp(false, "IP_ERR_001", MesUtil.getMsg("IP_ERR_001", new String[0])); return RespHelper.createResp(false, "IP_ERR_001", MesUtil.getMsg("IP_ERR_001", new String[0]));
} else { } else {
String newIpAddress = (String)param.get("inetAddress"); String newIpAddress = (String) param.get("inetAddress");
if (StringUtils.isEmpty(newIpAddress)) { if (StringUtils.isEmpty(newIpAddress)) {
return RespHelper.createResp(false, "IP_ERR_002", MesUtil.getMsg("IP_ERR_002", new String[0])); return RespHelper.createResp(false, "IP_ERR_002", MesUtil.getMsg("IP_ERR_002", new String[0]));
} else if (!this.validateIP(newIpAddress)) { } else if (!this.validateIP(newIpAddress)) {
return RespHelper.createResp(false, "IP_ERR_004", MesUtil.getMsg("IP_ERR_004", new String[0])); return RespHelper.createResp(false, "IP_ERR_004", MesUtil.getMsg("IP_ERR_004", new String[0]));
} else { } else {
String newSubnetMask = (String)param.get("subnetMask"); String newSubnetMask = (String) param.get("subnetMask");
if (StringUtils.isEmpty(newSubnetMask)) { if (StringUtils.isEmpty(newSubnetMask)) {
return RespHelper.createResp(false, "IP_ERR_003", MesUtil.getMsg("IP_ERR_003", new String[0])); return RespHelper.createResp(false, "IP_ERR_003", MesUtil.getMsg("IP_ERR_003", new String[0]));
} else if (!this.validateSubnetMask(newSubnetMask)) { } else if (!this.validateSubnetMask(newSubnetMask)) {
...@@ -82,13 +84,13 @@ public class ChangeIPController { ...@@ -82,13 +84,13 @@ public class ChangeIPController {
try { try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while(networkInterfaces.hasMoreElements()) { while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = (NetworkInterface)networkInterfaces.nextElement(); NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
if (!networkInterface.isLoopback() && !networkInterface.isVirtual()) { if (!networkInterface.isLoopback() && !networkInterface.isVirtual()) {
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while(inetAddresses.hasMoreElements()) { while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = (InetAddress)inetAddresses.nextElement(); InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
String subnetMask = this.getSubnetMask(networkInterface); String subnetMask = this.getSubnetMask(networkInterface);
if (inetAddress.getAddress().length == 4) { if (inetAddress.getAddress().length == 4) {
Ip ip = new Ip(); Ip ip = new Ip();
...@@ -112,7 +114,7 @@ public class ChangeIPController { ...@@ -112,7 +114,7 @@ public class ChangeIPController {
} }
private String getSubnetMask(NetworkInterface networkInterface) throws SocketException { private String getSubnetMask(NetworkInterface networkInterface) throws SocketException {
for(InterfaceAddress address : networkInterface.getInterfaceAddresses()) { for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
if (address.getAddress() instanceof Inet4Address) { if (address.getAddress() instanceof Inet4Address) {
int maskLength = address.getNetworkPrefixLength(); int maskLength = address.getNetworkPrefixLength();
int mask = -1 << 32 - maskLength; int mask = -1 << 32 - maskLength;
...@@ -125,11 +127,11 @@ public class ChangeIPController { ...@@ -125,11 +127,11 @@ public class ChangeIPController {
} }
public void changeIpFromFile(Map<String, String> params) { public void changeIpFromFile(Map<String, String> params) {
String filePath = "/etc/sysconfig/network-scripts/ifcfg-" + (String)params.get("networkInterface"); String filePath = "/etc/sysconfig/network-scripts/ifcfg-" + (String) params.get("networkInterface");
String newIpAddress = (String)params.get("inetAddress"); String newIpAddress = (String) params.get("inetAddress");
String newSubnetMask = (String)params.get("subnetMask"); String newSubnetMask = (String) params.get("subnetMask");
String gateway = (String)params.get("gateway"); String gateway = (String) params.get("gateway");
String dns = (String)params.get("dns"); String dns = (String) params.get("dns");
try { try {
CommandTool.command("cp " + filePath + " " + filePath + ".bak"); CommandTool.command("cp " + filePath + " " + filePath + ".bak");
...@@ -139,7 +141,7 @@ public class ChangeIPController { ...@@ -139,7 +141,7 @@ public class ChangeIPController {
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String line; String line;
while((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.startsWith("IPADDR=\"")) { if (line.startsWith("IPADDR=\"")) {
line = "IPADDR=\"" + newIpAddress + "\""; line = "IPADDR=\"" + newIpAddress + "\"";
} else if (line.startsWith("PREFIX=\"")) { } else if (line.startsWith("PREFIX=\"")) {
...@@ -170,8 +172,8 @@ public class ChangeIPController { ...@@ -170,8 +172,8 @@ public class ChangeIPController {
byte[] addressBytes = inetAddress.getAddress(); byte[] addressBytes = inetAddress.getAddress();
int cidrNotation = 0; int cidrNotation = 0;
for(byte b : addressBytes) { for (byte b : addressBytes) {
for(int i = 7; i >= 0; --i) { for (int i = 7; i >= 0; --i) {
if ((b >> i & 1) == 0) { if ((b >> i & 1) == 0) {
return cidrNotation; return cidrNotation;
} }
...@@ -194,11 +196,32 @@ public class ChangeIPController { ...@@ -194,11 +196,32 @@ public class ChangeIPController {
} }
private String getDefaultGateway(NetworkInterface networkInterface) throws Exception { private String getDefaultGateway(NetworkInterface networkInterface) throws Exception {
Process process = Runtime.getRuntime().exec("ip route show dev " + networkInterface.getName()); String os = System.getProperty("os.name").toLowerCase();
Process process = null;
if (os.contains("mac")) {
// macOS 获取默认网关
process = Runtime.getRuntime().exec("netstat -rn");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line; String line;
while((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.startsWith("default")) {
String[] parts = line.split("\\s+");
if (parts.length >= 2) {
String gateway = parts[1];
reader.close();
process.destroy();
return gateway;
}
}
}
reader.close();
process.destroy();
} else {
// Linux 获取默认网关
process = Runtime.getRuntime().exec("ip route show dev " + networkInterface.getName());
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("default")) { if (line.startsWith("default")) {
String[] parts = line.split("\\s+"); String[] parts = line.split("\\s+");
String gateway = parts[2]; String gateway = parts[2];
...@@ -207,9 +230,9 @@ public class ChangeIPController { ...@@ -207,9 +230,9 @@ public class ChangeIPController {
return gateway; return gateway;
} }
} }
reader.close(); reader.close();
process.destroy(); process.destroy();
}
return null; return null;
} }
...@@ -219,7 +242,7 @@ public class ChangeIPController { ...@@ -219,7 +242,7 @@ public class ChangeIPController {
StringBuilder dnsServers = new StringBuilder(); StringBuilder dnsServers = new StringBuilder();
String line; String line;
while((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.startsWith("nameserver")) { if (line.startsWith("nameserver")) {
String[] parts = line.split("\\s+"); String[] parts = line.split("\\s+");
if (parts.length >= 2) { if (parts.length >= 2) {
...@@ -247,7 +270,7 @@ public class ChangeIPController { ...@@ -247,7 +270,7 @@ public class ChangeIPController {
InetAddress[] addresses = InetAddress.getAllByName(hostName); InetAddress[] addresses = InetAddress.getAllByName(hostName);
System.out.println("DNS IP 地址:"); System.out.println("DNS IP 地址:");
for(InetAddress address : addresses) { for (InetAddress address : addresses) {
System.out.println(address.getHostAddress()); System.out.println(address.getHostAddress());
} }
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
......
...@@ -14,8 +14,8 @@ jdbc.kingbase.dbname=security ...@@ -14,8 +14,8 @@ jdbc.kingbase.dbname=security
#如果配置数据库类型是H2,则jdbc.dbname的值是public #如果配置数据库类型是H2,则jdbc.dbname的值是public
jdbc.dbname=public jdbc.dbname=public
#jdbc.dbname=encryption #jdbc.dbname=encryption
#basePath=/home/trustz basePath=/home/trustz
basePath=D:/trustz #basePath=D:/trustz
maskingPath=/home/masking maskingPath=/home/masking
#jdbc-mysql #jdbc-mysql
#jdbc.driver=com.mysql.cj.jdbc.Driver #jdbc.driver=com.mysql.cj.jdbc.Driver
...@@ -47,7 +47,7 @@ jdbc.dialect=org.hibernate.dialect.MySQL5Dialect ...@@ -47,7 +47,7 @@ jdbc.dialect=org.hibernate.dialect.MySQL5Dialect
[全局参数] [全局参数]
#license文件路径 #license文件路径
#licensedir=${basePath}/data/infa_file/lic #licensedir=${basePath}/data/infa_file/lic
licensedir=D:/lic licensedir=/Users/zhouhaifeng/Desktop
#是否启用license licenseKey 0:否 1 是 #是否启用license licenseKey 0:否 1 是
isNeedLicenseKey=0 isNeedLicenseKey=0
#错误登录允许次数 #错误登录允许次数
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论