nginx:根据配置文件来读取列表
nginx:通过正则来匹配替换ip
This commit is contained in:
parent
1bacd0690d
commit
58a6279661
2
pom.xml
2
pom.xml
@ -10,7 +10,7 @@
|
||||
</parent>
|
||||
<groupId>com.yutou</groupId>
|
||||
<artifactId>tools</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.2</version>
|
||||
<name>tools</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
|
@ -8,17 +8,32 @@ import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Controller
|
||||
public class UpdateIp {
|
||||
private static final String nasToken = "zIrsh9TUZP2lfRW753PannG49E7VJvor";
|
||||
private static List<String> keys = new ArrayList<>();
|
||||
|
||||
@RequestMapping({"/nas/updateip.do"})
|
||||
public void updateIp(HttpServletRequest request, String token, String ip) {
|
||||
if (!"zIrsh9TUZP2lfRW753PannG49E7VJvor".equals(token)) {
|
||||
/* static {
|
||||
keys.add("nas.yutou233.cn;");
|
||||
keys.add("bt.yutou233.cn;");
|
||||
keys.add("samba445-server");
|
||||
keys.add("samba139-server");
|
||||
keys.add("nas-ssh-server");
|
||||
keys.add("nas-ftp-server");
|
||||
}*/
|
||||
|
||||
@RequestMapping("/nas/updateip.do")
|
||||
public void updateIp(String token, String ip) {
|
||||
if (!nasToken.equals(token)) {
|
||||
System.out.println("验证不通过:" + token);
|
||||
return;
|
||||
}
|
||||
updateList();
|
||||
File file = new File("/etc/nginx/nginx.conf");
|
||||
if (file.exists()) {
|
||||
System.out.println("文件存在,修改 ip>" + ip);
|
||||
@ -26,38 +41,20 @@ public class UpdateIp {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
StringBuilder stringBuffer = new StringBuilder();
|
||||
String line = null;
|
||||
boolean isNas = false;
|
||||
boolean isBt = false;
|
||||
boolean isSamba = false;
|
||||
boolean isSSH = false;
|
||||
boolean isIp = false;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (line.contains("nas.yutou233.cn")) {
|
||||
isNas = true;
|
||||
//System.out.println(line.trim().replace("server_name","").replace("upstream","").trim());
|
||||
if (keys.contains(line.trim().replace("server_name", "").replace("upstream", "").replace("{", "").trim())) {
|
||||
isIp = true;
|
||||
}
|
||||
if (isNas && line.contains("proxy_pass")) {
|
||||
line = "proxy_pass http://" + ip + ":801/;";
|
||||
isNas = false;
|
||||
}
|
||||
if (line.contains("bt.yutou233.cn")) {
|
||||
isBt = true;
|
||||
}
|
||||
if (isBt && line.contains("proxy_pass")) {
|
||||
line = "proxy_pass http://" + ip + ":9091/;";
|
||||
isBt = false;
|
||||
}
|
||||
if (line.contains("samba445-server") || line.contains("samba139-server")) {
|
||||
isSamba = true;
|
||||
}
|
||||
if (isSamba && (line.trim().endsWith(":444;") || line.trim().endsWith(":138;"))) {
|
||||
line = "server " + ip + ":" + (line.split(":")[1]);
|
||||
isSamba = false;
|
||||
}
|
||||
if(line.contains("nas-ssh-server")){
|
||||
isSSH=true;
|
||||
}
|
||||
if(isSSH&&line.endsWith(":23;")){
|
||||
line = "server " + ip + ":" + (line.split(":")[1]);
|
||||
isSSH=false;
|
||||
if (isIp) {
|
||||
String testIp = testIp(line);
|
||||
if (testIp != null) {
|
||||
System.out.println("检测到目标:" + line + " 修改IP " + testIp + " > " + ip);
|
||||
line = line.replace(testIp, ip);
|
||||
|
||||
isIp = false;
|
||||
}
|
||||
}
|
||||
stringBuffer.append(line).append("\n");
|
||||
}
|
||||
@ -73,4 +70,38 @@ public class UpdateIp {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String testIp(String ip) {
|
||||
String pattern = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
|
||||
Pattern p = Pattern.compile(pattern);
|
||||
Matcher matcher = p.matcher(ip);
|
||||
// System.out.println("送检测IP:"+ip);
|
||||
if (matcher.find())
|
||||
return ip.substring(matcher.start(), matcher.end());
|
||||
return null;
|
||||
}
|
||||
|
||||
private void updateList() {
|
||||
File file = new File("ipconfig.txt");
|
||||
try {
|
||||
if (!file.exists()) {
|
||||
boolean create = file.createNewFile();
|
||||
if (create)
|
||||
System.out.println("创建文件完成:" +file.getAbsolutePath());
|
||||
}
|
||||
BufferedReader reader=new BufferedReader(new FileReader(file));
|
||||
String tmp;
|
||||
while ((tmp=reader.readLine())!=null){
|
||||
keys.add(tmp.trim());
|
||||
}
|
||||
reader.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new UpdateIp().updateIp(nasToken, "119.119.120.99");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user