引用github
github地址
https://github.com/GMYXDS/rustdesk-api-server
原因分析:获取数据内容不包含rdp等字段
发现 api v2版问题,经调试 发现请求不正确 做如下修改server_v2.py
更新地址簿和 获取地址的 需要修改如下.
# 更新地址簿
@app.route("/api/ab", methods=["POST"])
async def api_ab(request):
# authorization: Bearer eyJhb
# post:
# {'data': '{"tags":[],"peers":[
# {"id":"123412341","username":"Administrator","hostname":"hostname","platform":"Windows","alias":"test","tags":[""],"forceAlwaysRelay":"false","rdpPort":"","rdpUsername":""},
# ]}'}
# 新增id 一样的
# 改名 一样的
# 修改标签 一样的
# 请求 post
# { data: JSON.stringify(ab) }
# 返回 空 没处理
try:
auth_token = request.token
data = request.json.get("data", "")
if not data:
return json({"code": 99, "data": "数据错误!"})
data2 = json_tools.loads(data)
app = Sanic.get_app()
sql = "SELECT username,uid FROM `rustdesk_v2`.`rustdesk_token` where access_token=%s"
res = await app.ctx.poolmysqlasync.fetchone(sql, auth_token)
if not res:
return json({"code": 99, "data": "登录超时!", "error": "登录超时!"})
uid = res['uid']
tags = data2['tags']
peers = data2['peers']
# 循环添加tags
sql = "DELETE FROM `rustdesk_v2`.`rustdesk_tags` WHERE `uid` = %s"
res = await app.ctx.poolmysqlasync.execute(sql, uid)
for tag in tags:
sql = "INSERT INTO `rustdesk_v2`.`rustdesk_tags` (`uid`, `tag`) VALUES (%s, %s)"
res = await app.ctx.poolmysqlasync.execute(sql, uid, tag)
# 循环 peers
sql = "DELETE FROM `rustdesk_v2`.`rustdesk_peers` WHERE `uid` = %s"
res = await app.ctx.poolmysqlasync.execute(sql, uid)
for peer in peers:
temp_peer = peer
sql = "INSERT INTO `rustdesk_v2`.`rustdesk_peers` (`uid`, `client_id`, `username`, `hostname`, `alias`, `platform`, `tags` ) VALUES (%s,%s,%s,%s,%s,%s,%s)"
res = await app.ctx.poolmysqlasync.execute(sql, uid, temp_peer['id'], temp_peer['username'], temp_peer['hostname'], temp_peer['alias'], temp_peer['platform'], json_tools.dumps(temp_peer['tags']))
return json({"code": 100, "data": "成功"})
except Exception as e:
return json({"code": 99, "data": "失败"})
# 获取地址簿
@app.route("/api/ab", methods=["get"])
async def api_ab_get(request):
# 请求
# None
# 返回
# return json({})
# {"error":"Wrong credentials","msg":"提供的登录信息错误"}
# {"data":json_tools.dumps({"tags":[],"peers":[]})}
# {
# peers: [{id: "abcd", username: "", hostname: "", platform: "", alias: "", tags: ["","", ...],"forceAlwaysRelay":"true","rdpPort":"","rdpUsername":""}],
# tags: [],
# }
try:
auth_token = request.token
app = Sanic.get_app()
sql = "SELECT username,uid FROM `rustdesk_v2`.`rustdesk_token` where access_token=%s"
res = await app.ctx.poolmysqlasync.fetchone(sql, auth_token)
if not res:
return json({"error": "Wrong credentials", "msg": "提供的登录信息错误"})
# 获取全部tags 全部peers
app = Sanic.get_app()
sql = "SELECT tag FROM `rustdesk_v2`.`rustdesk_tags` where uid = %s"
tags = []
rows = await app.ctx.poolmysqlasync.fetchall(sql, res['uid'])
for row in rows:
tags.append(row['tag'])
sql = "SELECT * FROM `rustdesk_v2`.`rustdesk_peers` where uid = %s"
peers = []
rows = await app.ctx.poolmysqlasync.fetchall(sql, res['uid'])
for row in rows:
peers.append({
"id": row['client_id'],
"username": row['username'],
"hostname": row['hostname'],
"alias": row['alias'],
"platform": row['platform'],
"tags": json_tools.loads(row['tags']),
})
# "forceAlwaysRelay": row['forceAlwaysRelay'],
# "rdpPort": row['rdpPort'],
# "rdpUsername": row['rdpUsername'],
return json({"data": json_tools.dumps({"tags": tags, "peers": peers})})
except Exception as e:
return json({"err": e.__str__})
其他文件附加文件参考github.