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;
...@@ -33,226 +35,247 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -33,226 +35,247 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping({"/ip"}) @RequestMapping({"/ip"})
public class ChangeIPController { public class ChangeIPController {
public static final String QUOTATION_MARK = "\""; public static final String QUOTATION_MARK = "\"";
public static final String IP_START = "IPADDR=\""; public static final String IP_START = "IPADDR=\"";
public static final String SUBNET_MASK_START = "PREFIX=\""; public static final String SUBNET_MASK_START = "PREFIX=\"";
public static final String GATEWAY_START = "GATEWAY=\""; public static final String GATEWAY_START = "GATEWAY=\"";
public static final String DNS_START = "DNS1=\""; public static final String DNS_START = "DNS1=\"";
public static final String LINUX_IP_PATH = "/etc/sysconfig/network-scripts/ifcfg-"; public static final String LINUX_IP_PATH = "/etc/sysconfig/network-scripts/ifcfg-";
@GetMapping({"/show"}) @GetMapping({"/show"})
public Response show() { public Response show() {
return RespHelper.successResp(this.showIpList()); return RespHelper.successResp(this.showIpList());
} }
@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 {
String newSubnetMask = (String)param.get("subnetMask");
if (StringUtils.isEmpty(newSubnetMask)) {
return RespHelper.createResp(false, "IP_ERR_003", MesUtil.getMsg("IP_ERR_003", new String[0]));
} else if (!this.validateSubnetMask(newSubnetMask)) {
return RespHelper.createResp(false, "IP_ERR_005", MesUtil.getMsg("IP_ERR_005", new String[0]));
} else { } else {
Thread thread = new Thread(() -> this.asyncMethodChangeIp(param)); String newSubnetMask = (String) param.get("subnetMask");
thread.start(); if (StringUtils.isEmpty(newSubnetMask)) {
return RespHelper.successResp(); return RespHelper.createResp(false, "IP_ERR_003", MesUtil.getMsg("IP_ERR_003", new String[0]));
} else if (!this.validateSubnetMask(newSubnetMask)) {
return RespHelper.createResp(false, "IP_ERR_005", MesUtil.getMsg("IP_ERR_005", new String[0]));
} else {
Thread thread = new Thread(() -> this.asyncMethodChangeIp(param));
thread.start();
return RespHelper.successResp();
}
} }
} }
} }
}
public void asyncMethodChangeIp(Map<String, String> param) {
public void asyncMethodChangeIp(Map<String, String> param) { this.changeIpFromFile(param);
this.changeIpFromFile(param); CommandTool.command("systemctl restart network");
CommandTool.command("systemctl restart network"); }
}
private List<Ip> showIpList() {
private List<Ip> showIpList() { List<Ip> list = new ArrayList();
List<Ip> list = new ArrayList();
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(); ip.setNetworkInterface(networkInterface.getName());
ip.setNetworkInterface(networkInterface.getName()); ip.setInetAddress(inetAddress.getHostAddress());
ip.setInetAddress(inetAddress.getHostAddress()); ip.setSubnetMask(subnetMask);
ip.setSubnetMask(subnetMask); ip.setGateway(this.getDefaultGateway(networkInterface));
ip.setGateway(this.getDefaultGateway(networkInterface)); ip.setDns(this.getDNSServers());
ip.setDns(this.getDNSServers()); list.add(ip);
list.add(ip); }
} }
} }
} }
} } catch (SocketException e) {
} catch (SocketException e) { e.printStackTrace();
e.printStackTrace(); } catch (Exception e) {
} catch (Exception e) { e.printStackTrace();
e.printStackTrace(); }
}
return list;
return list; }
}
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; String subnetMask = Integer.toString(mask >> 24 & 255) + "." + Integer.toString(mask >> 16 & 255) + "." + Integer.toString(mask >> 8 & 255) + "." + Integer.toString(mask & 255);
String subnetMask = Integer.toString(mask >> 24 & 255) + "." + Integer.toString(mask >> 16 & 255) + "." + Integer.toString(mask >> 8 & 255) + "." + Integer.toString(mask & 255); return subnetMask;
return subnetMask;
}
}
return null;
}
public void changeIpFromFile(Map<String, String> params) {
String filePath = "/etc/sysconfig/network-scripts/ifcfg-" + (String)params.get("networkInterface");
String newIpAddress = (String)params.get("inetAddress");
String newSubnetMask = (String)params.get("subnetMask");
String gateway = (String)params.get("gateway");
String dns = (String)params.get("dns");
try {
CommandTool.command("cp " + filePath + " " + filePath + ".bak");
File file = new File(filePath);
File tempFile = new File(filePath + ".tmp");
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String line;
while((line = reader.readLine()) != null) {
if (line.startsWith("IPADDR=\"")) {
line = "IPADDR=\"" + newIpAddress + "\"";
} else if (line.startsWith("PREFIX=\"")) {
line = "PREFIX=\"" + newSubnetMask + "\"";
} else if (line.startsWith("GATEWAY=\"") && StringUtils.isNotEmpty(gateway)) {
line = "GATEWAY=\"" + gateway + "\"";
} else if (line.startsWith("DNS1=\"") && StringUtils.isNotEmpty(dns)) {
line = "DNS1=\"" + dns + "\"";
} }
}
writer.write(line);
writer.newLine(); return null;
} }
reader.close(); public void changeIpFromFile(Map<String, String> params) {
writer.close(); String filePath = "/etc/sysconfig/network-scripts/ifcfg-" + (String) params.get("networkInterface");
file.delete(); String newIpAddress = (String) params.get("inetAddress");
tempFile.renameTo(file); String newSubnetMask = (String) params.get("subnetMask");
System.out.println("IP地址和子网掩码修改成功!"); String gateway = (String) params.get("gateway");
} catch (IOException e) { String dns = (String) params.get("dns");
System.out.println("发生异常:" + e.getMessage());
} try {
CommandTool.command("cp " + filePath + " " + filePath + ".bak");
} File file = new File(filePath);
File tempFile = new File(filePath + ".tmp");
private int subnetMaskToDecimal(String subnetMask) throws UnknownHostException { BufferedReader reader = new BufferedReader(new FileReader(file));
InetAddress inetAddress = InetAddress.getByName(subnetMask); BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
byte[] addressBytes = inetAddress.getAddress();
int cidrNotation = 0; String line;
while ((line = reader.readLine()) != null) {
for(byte b : addressBytes) { if (line.startsWith("IPADDR=\"")) {
for(int i = 7; i >= 0; --i) { line = "IPADDR=\"" + newIpAddress + "\"";
if ((b >> i & 1) == 0) { } else if (line.startsWith("PREFIX=\"")) {
return cidrNotation; line = "PREFIX=\"" + newSubnetMask + "\"";
} else if (line.startsWith("GATEWAY=\"") && StringUtils.isNotEmpty(gateway)) {
line = "GATEWAY=\"" + gateway + "\"";
} else if (line.startsWith("DNS1=\"") && StringUtils.isNotEmpty(dns)) {
line = "DNS1=\"" + dns + "\"";
}
writer.write(line);
writer.newLine();
} }
++cidrNotation; reader.close();
} writer.close();
} file.delete();
tempFile.renameTo(file);
return cidrNotation; System.out.println("IP地址和子网掩码修改成功!");
} } catch (IOException e) {
System.out.println("发生异常:" + e.getMessage());
public boolean validateIP(String ipAddress) { }
String ipPattern = "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
return Pattern.matches(ipPattern, ipAddress); }
}
private int subnetMaskToDecimal(String subnetMask) throws UnknownHostException {
public boolean validateSubnetMask(String subnetMask) { InetAddress inetAddress = InetAddress.getByName(subnetMask);
String subnetMaskPattern = "^(128|192|224|240|248|252|254|255)\\.((0|128|192|224|240|248|252|254|255)\\.){2}(0|128|192|224|240|248|252|254|255)$"; byte[] addressBytes = inetAddress.getAddress();
return Pattern.matches(subnetMaskPattern, subnetMask); int cidrNotation = 0;
}
for (byte b : addressBytes) {
private String getDefaultGateway(NetworkInterface networkInterface) throws Exception { for (int i = 7; i >= 0; --i) {
Process process = Runtime.getRuntime().exec("ip route show dev " + networkInterface.getName()); if ((b >> i & 1) == 0) {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); return cidrNotation;
}
String line;
while((line = reader.readLine()) != null) { ++cidrNotation;
if (line.startsWith("default")) { }
String[] parts = line.split("\\s+"); }
String gateway = parts[2];
return cidrNotation;
}
public boolean validateIP(String ipAddress) {
String ipPattern = "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
return Pattern.matches(ipPattern, ipAddress);
}
public boolean validateSubnetMask(String subnetMask) {
String subnetMaskPattern = "^(128|192|224|240|248|252|254|255)\\.((0|128|192|224|240|248|252|254|255)\\.){2}(0|128|192|224|240|248|252|254|255)$";
return Pattern.matches(subnetMaskPattern, subnetMask);
}
private String getDefaultGateway(NetworkInterface networkInterface) throws Exception {
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()));
String line;
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")) {
String[] parts = line.split("\\s+");
String gateway = parts[2];
reader.close();
process.destroy();
return gateway;
}
}
reader.close(); reader.close();
process.destroy(); process.destroy();
return gateway; }
} return null;
} }
reader.close(); private String getDNSServers() throws Exception {
process.destroy(); Process process = Runtime.getRuntime().exec("cat /etc/resolv.conf");
return null; BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
} StringBuilder dnsServers = new StringBuilder();
private String getDNSServers() throws Exception { String line;
Process process = Runtime.getRuntime().exec("cat /etc/resolv.conf"); while ((line = reader.readLine()) != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); if (line.startsWith("nameserver")) {
StringBuilder dnsServers = new StringBuilder(); String[] parts = line.split("\\s+");
if (parts.length >= 2) {
String line; String dnsServer = parts[1];
while((line = reader.readLine()) != null) { dnsServers.append(dnsServer).append(", ");
if (line.startsWith("nameserver")) { }
String[] parts = line.split("\\s+");
if (parts.length >= 2) {
String dnsServer = parts[1];
dnsServers.append(dnsServer).append(", ");
} }
} }
}
reader.close();
reader.close(); process.destroy();
process.destroy(); String dns = "";
String dns = ""; if (dnsServers != null && dnsServers.length() > 0) {
if (dnsServers != null && dnsServers.length() > 0) { dns = dnsServers.substring(0, dnsServers.length() - 2).trim();
dns = dnsServers.substring(0, dnsServers.length() - 2).trim(); }
}
return dns;
return dns; }
}
public static void main(String[] args) {
public static void main(String[] args) { try {
try { InetAddress localHost = InetAddress.getLocalHost();
InetAddress localHost = InetAddress.getLocalHost(); String hostName = localHost.getHostName();
String hostName = localHost.getHostName(); System.out.println("主机名: " + hostName);
System.out.println("主机名: " + hostName); 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) { System.out.println("无法获取DNS信息:" + e.getMessage());
System.out.println("无法获取DNS信息:" + e.getMessage()); }
}
}
}
} }
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论