解决方案
1、将natapp日志输出
2、写一个程序自动获取并识别最新动态域名
3、替换最新域名地址
应用场景
本次方案成功解决了Chrome登录草料二维码修改技术,使用了RoBot技术,而不用UI自动化Selenium Webdriver技术,因为它会导致登录问题,但是你使用Robot技术可以避开登录Cookie、请求头等问题。
以下代码实现,主要步骤:
1、遍历轮询执行域名解析
2、草料二维码更新
/**
* @ClassName: synReadLog
* @Description:
* @Author belllaaaaaaa
* @Date 2023/10/17
* @Version 1.0
*/
public class synFront38080Log {
public static void main(String[] args) {
while (true) {
doExec();
// 休眠30分钟(30分钟 = 30 * 60 * 1000 毫秒)
try {
Thread.sleep(30 * 60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void doExec(){
// 本地文件路径
String filePath = "E:\\下载\\Compressed\\natapp_windows_amd64_2_3_9\\log38080.txt";
// 指定字符串
String targetString = "Tunnel established at";
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
String lastOccurrence = null;
while ((line = br.readLine()) != null) {
if (line.contains(targetString)) {
lastOccurrence = line;
}
}
br.close();
if (lastOccurrence != null) {
// 使用正则表达式提取HTTP链接
String regex = "http://\\S+";
String httpLink = ReUtil.get(regex, lastOccurrence, 0);
if (httpLink != null) {
System.out.println("获取到的HTTP链接:" + httpLink);
//替换草料动态活码
replaceCaoLiao(httpLink);
} else {
System.out.println("未找到HTTP链接");
}
} else {
System.out.println("未找到包含指定字符串的行");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void replaceCaoLiao(String newContent) {
newContent = newContent + "/pdkweb/";
System.out.println("新的二维码活码链接:"+newContent);
openChrome(newContent);
}
private static void openChrome(String newContent) {
try {
// 打开 Chrome
Runtime.getRuntime().exec("cmd /c start chrome");
// 等待 Chrome 打开
Thread.sleep(5000);
Robot robot = new Robot();
// 输入网址并访问
String url = "https://cli.im/user/active/edit/45257958?p=1&t=jump&from=center&i=1";
type(robot, url);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
// 等待页面加载
Thread.sleep(5000);
//鼠标移到输入框位置并按下左键
robot.mouseMove (969, 468); // 修改坐标为按钮所在位置
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(1000);
// 模拟按下 Ctrl+A (全选)
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(1000);
// 模拟按下 Delete
robot.keyPress(KeyEvent.VK_DELETE);
robot.keyRelease(KeyEvent.VK_DELETE);
Thread.sleep(1000);
// 输入跳转地址
String jumpUrl = newContent;
type(robot, jumpUrl);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
// 等待页面加载
Thread.sleep(1000);
// 移动鼠标并点击“更新内容”按钮
robot.mouseMove (1271, 775);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(3000);
System.out.println("草料二维码地址更新成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
// 辅助方法:用于输入文本
private static void type(Robot robot, String text) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection(text);
clipboard.setContents(stringSelection, null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
}
以下代码,主要解决
1、遍历轮询获取natapp最新动态域名
2、替换在Apache服务启动情况下的Html代码文件,替换请求后端域名地址
public class synBack38090Log {
public static void main(String[] args) {
while (true) {
doexecCommand();
// 休眠30分钟(30分钟 = 29 * 60 * 1000 毫秒)
try {
Thread.sleep(29 * 60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void doexecCommand(){
// 本地文件路径
String filePath = "E:\\下载\\Compressed\\natapp_windows_amd64_2_3_9\\log38090.txt";
// 指定字符串
String targetString = "Tunnel established at";
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
String lastOccurrence = null;
while ((line = br.readLine()) != null) {
if (line.contains(targetString)) {
lastOccurrence = line;
}
}
br.close();
if (lastOccurrence != null) {
// 使用正则表达式提取HTTP链接
String regex = "http://\\S+";
String httpLink = ReUtil.get(regex, lastOccurrence, 0);
if (httpLink != null) {
System.out.println("获取到的HTTP链接:" + httpLink);
//替换本地文件
replaceHtml(httpLink);
} else {
System.out.println("未找到HTTP链接");
}
} else {
System.out.println("未找到包含指定字符串的行");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void replaceHtml(String newContent) {
// 本地文件路径
String filePath = "E:\\apache-tomcat-9.0.70\\webapps\\pdkweb\\index.html";
// 匹配的正则表达式
String regex = "const url = `http://[a-zA-Z0-9.-]+/pdksport/wxsports/refushStep\\?usr=\\$\\{account}&psw=\\$\\{password}&bs=\\$\\{step}`;";
try {
Path path = Paths.get(filePath);
// 读取文件内容,匹配并替换字符串,写回文件
String updatedContent = Files.lines(path)
.map(line -> {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String matchedString = matcher.group(0);
String newUrl = matchedString.replaceFirst("http://[a-zA-Z0-9.-]+", newContent);
System.out.println("新的配置请求:" + newUrl);
return line.replace(matchedString, newUrl);
}
return line;
})
.collect(Collectors.joining(System.lineSeparator()));
Files.write(path, updatedContent.getBytes());
System.out.println("文件内容已成功更新!");
} catch (IOException e) {
e.printStackTrace();
}
}
}