小米路由器API
登录页:http://192.168.31.1/cgi-bin/luci/web
//获取key和deviceId
String key = "";
String deviceId = "";
Document document = HttpUtils.getDocument("http://192.168.31.1/cgi-bin/luci/web");
String html = document.toString().replace("\r", "").replace("\n", "").replace("\t", "");
Matcher keyMatcher = Pattern.compile(".*key: \'(.*?)\',").matcher(html);
if(keyMatcher.find()) {
key =keyMatcher.group(1);
}
Matcher deviceIdMatcher = Pattern.compile("deviceId = \'(.*?)\';").matcher(html);
if(deviceIdMatcher.find()) {
deviceId =deviceIdMatcher.group(1);
}
登录:http://192.168.31.1/cgi-bin/luci/api/xqsystem/login
//随机串
public static String getNonce(String deviceId){
Integer type =0;
long time = System.currentTimeMillis() / 1000;
int random = new Random().nextInt(10000);
return type+"_"+deviceId+"_"+time+"_"+random;
}
//密码加密
public static String getPassword(String pwd,String nonce){
String key="a2ffa5c9be07488bbb04a3a47d3c5f6a";
return DigestUtil.sha256Hex(nonce+ DigestUtil.sha256Hex(pwd+key));
}
public void t1(){
String nonce = getNonce("1c:83:41:ca:93:2a");
String password = getPassword("l5w9m6abc",nonce);
String url = String.format("http://192.168.31.1/cgi-bin/luci/api/xqsystem/login");
Connection connect = Jsoup.connect(url);
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0");
connect.headers(headers);
Map<String, String> data = new HashMap<>();
data.put("username", "admin");
data.put("password", password);
data.put("logtype", "2");
data.put("nonce", nonce);
connect.data(data);
Document post = connect.post();
}
//响应
{"url":"/cgi-bin/luci/;stok=fde962455af256e973c565eb844ccd4b/web/home","token":"fde962455af256e973c565eb844ccd4b","code":0}
网络信息:http://192.168.31.1/cgi-bin/luci/;stok=/api/xqnetwork/wan_info
{
"info": {
"mac": "A4:A9:36:11:F5:C6",
"mtu": "1480",
"details": {
"password": "a123456",
"ifname": "eth3",
"dns": [
"180.76.76.76",
"119.29.29.29"
],
"mtu": "1480",
"wanType": "pppoe",
"special": "1",
"username": "0000000000",
"peerdns": "0"
},
"special": "1",
"dnsAddrs1": "119.29.29.29",
"gateWay": "129.210.62.1",
"ipv6_info": {
"ifname": "pppoe-wan",
"dns": [
"240e:50:c800::210",
"240e:50:5000::80"
],
"peerdns": "1",
"wanType": "native",
"ip6addr": [
"240e:382:503:ea00:4c7a:273f:b2e2:621a/64"
],
"status": true,
"ip6gw": "fe80::ce1a:faff:fee7:f760",
"lan_ip6prefix": [
"240e:382:503:ea00::"
],
"lan_ip6addr": [
[
"240e:382:503:ea00::1/64"
]
]
},
"uptime": 9059,
"dnsAddrs": "180.76.76.76",
"status": 0,
"ipv6_show": 1,
"link": 1,
"ipv4": [
{
"mask": "255.255.255.255",
"ip": "129.210.62.28"
}
]
},
"code": 0
}
端口转发
删除端口
http://192.168.31.1/cgi-bin/luci/;stok={token}/api/xqsystem/delete_redirect
//入参
{
port: 1111, //端口
proto: 1 , //协议 1-TCP 2-UDP 3-TCP/UDP
}
//响应
{"code":0}
新增端口
http://192.168.31.1/cgi-bin/luci/;stok={token}/api/xqsystem/add_redirect
//入参
{
name: 测试
proto: 3 //协议 1-TCP 2-UDP 3-TCP/UDP
sport: 1111 //外部端口
ip: 192.168.31.100
dport: 1111 //内部端口
}
//响应
{"code":0}
新增范围端口
http://192.168.31.1/cgi-bin/luci/;stok={token}/api/xqsystem/add_range_redirect
//入参
{
name: 测
proto: //协议 1-TCP 2-UDP 3-TCP/UDP
fport: 10 //开始端口
tport: 20 //结束端口
ip: 192.168.31.102
}
//响应
{"code":0}