nginx:根据配置文件来读取列表

nginx:通过正则来匹配替换ip
This commit is contained in:
Yutousama 2020-02-19 12:31:27 +08:00
parent 1bacd0690d
commit 58a6279661
2 changed files with 65 additions and 34 deletions

View File

@ -10,7 +10,7 @@
</parent> </parent>
<groupId>com.yutou</groupId> <groupId>com.yutou</groupId>
<artifactId>tools</artifactId> <artifactId>tools</artifactId>
<version>1.0.1</version> <version>1.0.2</version>
<name>tools</name> <name>tools</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>

View File

@ -8,17 +8,32 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Controller @Controller
public class UpdateIp { public class UpdateIp {
private static final String nasToken = "zIrsh9TUZP2lfRW753PannG49E7VJvor"; private static final String nasToken = "zIrsh9TUZP2lfRW753PannG49E7VJvor";
private static List<String> keys = new ArrayList<>();
@RequestMapping({"/nas/updateip.do"}) /* static {
public void updateIp(HttpServletRequest request, String token, String ip) { keys.add("nas.yutou233.cn;");
if (!"zIrsh9TUZP2lfRW753PannG49E7VJvor".equals(token)) { 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); System.out.println("验证不通过:" + token);
return; return;
} }
updateList();
File file = new File("/etc/nginx/nginx.conf"); File file = new File("/etc/nginx/nginx.conf");
if (file.exists()) { if (file.exists()) {
System.out.println("文件存在,修改 ip>" + ip); System.out.println("文件存在,修改 ip>" + ip);
@ -26,38 +41,20 @@ public class UpdateIp {
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder stringBuffer = new StringBuilder(); StringBuilder stringBuffer = new StringBuilder();
String line = null; String line = null;
boolean isNas = false; boolean isIp = false;
boolean isBt = false;
boolean isSamba = false;
boolean isSSH = false;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
if (line.contains("nas.yutou233.cn")) { //System.out.println(line.trim().replace("server_name","").replace("upstream","").trim());
isNas = true; if (keys.contains(line.trim().replace("server_name", "").replace("upstream", "").replace("{", "").trim())) {
isIp = true;
} }
if (isNas && line.contains("proxy_pass")) { if (isIp) {
line = "proxy_pass http://" + ip + ":801/;"; String testIp = testIp(line);
isNas = false; if (testIp != null) {
} System.out.println("检测到目标:" + line + " 修改IP " + testIp + " > " + ip);
if (line.contains("bt.yutou233.cn")) { line = line.replace(testIp, ip);
isBt = true;
} isIp = false;
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;
} }
stringBuffer.append(line).append("\n"); 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");
}
} }