工作中使用frp进行端口映射,工作要求统计每个映射的端口流量情况,并根据流量然后编写相关业务逻辑。官网文档只提供 只能通过浏览器登录成功后查看 frp 的状态以及代理统计信息。
但是实际业务中,要求获取每个端口信息放到自己写的程序中。
frps 配置文件如下
[common]
allow_ports = 40200-41000
bind_port = 40000
dashboard_port = 40010
dashboard_user = admin
dashboard_pwd = admin
enable_prometheus = true
如果直接这么请求
import requests
url = 'http://serviceip:40010'
ret = requests.get(url=url)
print(ret.text)
返回的是
Unauthorized
浏览器右键查看网页源代码,js学的不太好,看不懂。
<!DOCTYPE html> <html lang=en> <head> <meta charset=utf-8> <title>frps dashboard</title> <link rel="shortcut icon" href="favicon.ico"></head> <body> <div id=app></div> <script type="text/javascript" src="manifest.js?14bea8276eef86cc7c61"></script><script type="text/javascript" src="vendor.js?51925ec1a77936b64d61"></script></body> </html>
发现模拟请求时请求 /api/proxy/tcp 这个接口并在请求中添加
Authorization: Basic YWRtaW46YWRtaW4=,
url = 'http://serverip:40010/api/proxy/tcp'
header = {
# 'Connection':'keep-alive',
'Authorization':'Basic YWRtaW46YWRtaW4=',
}
ret = requests.get(url=url,headers=header)
print(ret.text)
返回信息
{"proxies":[{"name":"40293","conf":null,"today_traffic_in":0,"today_traffic_out":0,"cur_conns":0,"last_start_time":"03-23 14:14:18","last_close_time":"03-23 14:22:25","status":"offline"},{"name":"40398","conf":null,"today_traffic_in":5751,"today_traffic_out":4022520,"cur_conns":0,"last_start_time":"03-23 13:40:39","last_close_time":"03-23 13:48:57","status":"offline"},{"name":"40546","conf":null,"today_traffic_in":0,"today_traffic_out":0,"cur_conns":0,"last_start_time":"03-23 14:14:18","last_close_time":"03-23 14:22:25","status":"offline"}]}
完美解决
总结
发现 请求头中添加
Authorization: Basic YWRtaW46YWRtaW4=
是对 frps.ini 配置文件中
dashboard_user = admin
dashboard_pwd = admin
设置的账号密码进行的加密。
具体利用什么加密手段我也没研究。