更新小米路由器API调用接口

更新小米路由器获取公网IP的接口
This commit is contained in:
yutou 2024-06-03 22:01:05 +08:00
parent 6ed42d3e80
commit 120392be17
5 changed files with 93 additions and 15 deletions

View File

@ -63,7 +63,7 @@ public class MiRouterDevices {
}
private String getRemoteAddress(String ip) {
JSONObject data = JSON.parseObject(HttpTools.get(XiaoMiRouter.getDeviceListUrl()));
JSONObject data = JSON.parseObject(HttpTools.get(XiaoMiRouter.getInstance().getDeviceListUrl()));
if (data.getInteger("code") == 0) {
JSONArray array=data.getJSONArray("list");
for (Object o : array) {

View File

@ -10,7 +10,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class QQBotApplication {
public static final String version = "QQBot v.1.7.5";
public static final String version = "QQBot v.1.7.6";
public static void main(String[] args) {
System.out.println("version = " + version);

View File

@ -10,6 +10,7 @@ import com.yutou.qqbot.MessageEvent.AdminMessage;
import com.yutou.qqbot.models.Model;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.RedisTools;
import com.yutou.qqbot.utlis.XiaoMiRouter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@ -89,4 +90,15 @@ public class QQBotController {
}
return "200";
}
@ResponseBody
@RequestMapping("/router/wan1ip.do")
public String getWan1IP(){
return XiaoMiRouter.getInstance().getWan1IPAddress();
}
@ResponseBody
@RequestMapping("/router/wan2ip.do")
public String getWan2IP(){
return XiaoMiRouter.getInstance().getWan2IPAddress();
}
}

View File

@ -64,7 +64,7 @@ public class MiRouter extends Model {
isRunTime = false;
}
private void run()throws Exception{
String url = XiaoMiRouter.getDeviceListUrl();
String url = XiaoMiRouter.getInstance().getDeviceListUrl();
JSONObject json = JSON.parseObject(HttpTools.get(url));
if (json.getInteger("code") == 0) {
String _tmp = RedisTools.get(redis_key);
@ -114,7 +114,7 @@ public class MiRouter extends Model {
}
RedisTools.set(redis_key, devs.toString());
}else {
XiaoMiRouter.setNotToken();
XiaoMiRouter.getInstance().setNotToken();
}
}

View File

@ -7,19 +7,30 @@ import org.apache.commons.codec.digest.DigestUtils;
import java.nio.charset.StandardCharsets;
public class XiaoMiRouter {
private static XiaoMiRouter router;
private final static String key = "a2ffa5c9be07488bbb04a3a47d3c5f6a";
private static String token = null;
private String token = null;
public static void setNotToken() {
private XiaoMiRouter() {
}
public static XiaoMiRouter getInstance() {
if (router == null) {
router = new XiaoMiRouter();
}
return router;
}
public void setNotToken() {
token = null;
}
public static String getToken() {
private String getToken() {
if (token != null) {
return token;
}
String nonce = nonceCreat();
String oldPwd = DigestUtils.sha1Hex(nonce + DigestUtils.sha1Hex("34864394" + key));
String oldPwd = DigestUtils.sha256Hex(nonce + DigestUtils.sha256Hex("34864394" + key));
JSONObject json = new JSONObject();
json.put("username", "admin");
json.put("password", oldPwd);
@ -33,17 +44,72 @@ public class XiaoMiRouter {
return null;
}
public static String getDeviceListUrl() {
return "http://192.168.31.1/cgi-bin/luci/;stok=" + getToken() + "/api/misystem/devicelist";
private String getHomeUrl() {
return getHomeUrl(true);
}
private static String nonceCreat() {
return String.format("%s_%s_%s_%s", 0, HttpTools.getLocalMacAddress(), (int) (System.currentTimeMillis() / 1000), (int) (Math.random() * 10000));
private String getHomeUrl(boolean check) {
if (check && !checkToken()) {
token = null;
}
return "http://192.168.31.1/cgi-bin/luci/;stok=" + getToken();
}
public String getDeviceListUrl() {
return getHomeUrl() + "/api/misystem/devicelist";
}
private String nonceCreat() {
return String.format("%s_%s_%s_%s", 0, "7c:83:34:bd:a3:9f", (int) (System.currentTimeMillis() / 1000), (int) (Math.random() * 10000));
}
private String getWan1StatusUrl() {
return getHomeUrl() + "/api/xqnetwork/get_wan_status";
}
private String getWan2StatusUrl() {
return getHomeUrl() + "/api/xqnetwork/pppoe_status?wan_name=WAN2";
}
public boolean checkToken() {
String url = getHomeUrl(false) + "/api/misystem/messages";
JSONObject json = JSON.parseObject(HttpTools.get(url));
return json.getInteger("code") == 0;
}
public String getWan1IPAddress() {
String url = getWan1StatusUrl();
JSONObject json = JSON.parseObject(HttpTools.get(url));
try {
return json.getJSONObject("ipv4").getJSONObject("ip").getString("address");
} catch (Exception e) {
e.printStackTrace();
return getIPv4();
}
}
public String getWan2IPAddress() {
String url = getWan2StatusUrl();
JSONObject json = JSON.parseObject(HttpTools.get(url));
try {
return json.getJSONObject("ip").getString("address");
} catch (Exception e) {
e.printStackTrace();
return getIPv4();
}
}
public String getIPv4() {
return HttpTools.get("https://4.ipw.cn/");
}
public static void main(String[] args) {
String url = XiaoMiRouter.getDeviceListUrl();
JSONObject json = JSON.parseObject(HttpTools.get(url));
System.out.println("json = " + json);
System.out.println(XiaoMiRouter.getInstance().getWan1IPAddress());
XiaoMiRouter.getInstance().clear();
System.out.println(XiaoMiRouter.getInstance().getWan2IPAddress());
}
public void clear() {
token="";
}
}