Skip to content

Commit

Permalink
兼容v1.2版本的部分接口
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyi510 committed Jun 6, 2023
1 parent ae0692f commit 2fef54d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 22 deletions.
7 changes: 3 additions & 4 deletions app/controllers/adressBook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"rustdesk-api-server/app/services"
"rustdesk-api-server/utils/beegoHelper"
"strings"
"time"
)

var Address = new(AddressBookController)
Expand Down Expand Up @@ -64,9 +63,9 @@ func (ctl *AddressBookController) List() {
jdata, _ := json.Marshal(ack)

ctl.JSON(beegoHelper.H{
"error": false,
"data": string(jdata),
"update_at": time.Now().Format("2006-01-02 15:04:05"),
//"error": false,
"data": string(jdata),
//"update_at": time.Now().Format("2006-01-02 15:04:05"),
})
}

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (ctl *BaseController) Prepare() {
controllerName, actionName := ctl.GetControllerAndAction()
ctl.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
ctl.actionName = strings.ToLower(actionName)
log.Println("请求接口", ctl.controllerName, ctl.actionName)
log.Println("请求接口", ctl.Ctx.Input.URL(), ctl.Ctx.Input.Method(), string(ctl.Ctx.Input.RequestBody))
// 获取token
token := ctl.Ctx.Input.Header("Authorization")
if ctl.controllerName != "login" && ctl.controllerName != "index" && !(ctl.controllerName == "user" && (ctl.actionName == "reg" || ctl.actionName == "setpwd")) {
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/heart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controllers

import (
"rustdesk-api-server/utils/beegoHelper"
"time"
)

type HeartController struct {
BaseController
}

// 心跳检测 POST
func (ctl *HeartController) Heart() {

ctl.JSON(beegoHelper.H{
"modified_at": time.Now().Unix(),
})
}
10 changes: 6 additions & 4 deletions app/controllers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,26 @@ func (ctl *LoginController) Login() {
Error: "密码不能为空",
})
}
req.ClientId = strings.TrimSpace(req.ClientId)
if len(req.ClientId) == 0 {
req.Id = strings.TrimSpace(req.Id)
if len(req.Id) == 0 {
ctl.JSON(common.JsonResult{
Code: -1,
Error: "客户端ID不能为空",
})
}

// 查询数据库中的账号密码是否合法
token, err := services.Login.UserLogin(req.Username, req.Password, req.ClientId, req.Uuid, ctl.Ctx)
token, err := services.Login.UserLogin(req.Username, req.Password, req.Id, req.Uuid, ctl.Ctx)
if err != nil {
// return json({"type": "access_token","access_token":token,"user":{"name":username,"email":res['email'],"note":res['note'],"status":res['status'],"grp":res['group'],"is_admin":True if res['is_admin']==1 else False }})
ctl.JSON(common.JsonResult{
Code: -1,
//Code: -1,
Error: err.Error(),
})
}

ctl.JSON(beegoHelper.H{
"type": "access_token",
"access_token": token,
"user": beegoHelper.H{
"name": req.Username,
Expand Down
30 changes: 26 additions & 4 deletions app/dto/login.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
package dto

// {
// "username": "1g",
// "password": "21g2",
// "id": "1089363550",
// "uuid": "M0Y4MkI3N0MtMDMwMy01N0EwLTg5MzAtNDcwNUI4NUNFNUZD",
// "autoLogin": true,
// "type": "account",
// "verificationCode": "",
// "deviceInfo": {
// "os": "macos",
// "type": "client",
// "name": "xiaoyi510deimac.local"
// }
// }
type LoginReq struct {
Username string `json:"username"`
Password string `json:"password"`
ClientId string `json:"id"`
Uuid string `json:"uuid"`
Username string `json:"username"`
Password string `json:"password"`
Id string `json:"id"`
Uuid string `json:"uuid"`
AutoLogin bool `json:"autoLogin"`
Type string `json:"type"`
VerificationCode string `json:"verificationCode"`
DeviceInfo struct {
Os string `json:"os"`
Type string `json:"type"`
Name string `json:"name"`
} `json:"deviceInfo"`
}
2 changes: 1 addition & 1 deletion conf/app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
appname = rustdesk-api-server

# 监听IP
HTTPAddr = 127.0.0.1
HTTPAddr = 0.0.0.0

# 端口号
httpport = 21114
Expand Down
1 change: 1 addition & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {

// 设定路由信息
beego.Router("/", &controllers.IndexController{}, "get:Index")
beego.Router("/api/heartbeat", &controllers.HeartController{}, "post:Heart")
beego.Router("/api/login", &controllers.LoginController{}, "post:Login")
beego.Router("/api/ab", &controllers.AddressBookController{}, "post:Update")
beego.Router("/api/ab/get", &controllers.AddressBookController{}, "post:List")
Expand Down
6 changes: 3 additions & 3 deletions utils/common/json.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package common

type JsonResult struct {
Code int `json:"code"`
Msg string `json:"msg"`
Code int `json:"code,omitempty"`
Msg string `json:"msg,omitempty"`
Error string `json:"error,omitempty"`
Data interface{} `json:"data"`
Data interface{} `json:"data,omitempty"`
Count int64 `json:"count,omitempty"`
}
6 changes: 1 addition & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ package utils

import (
"github.com/dgrijalva/jwt-go"
"time"
)

func GenerateJwtToken(userId int, username, token, clientId, uuid string) (string, error) {
//设置token有效时间
nowTime := time.Now()
expireTime := nowTime.Add(39999999 * time.Hour)

claims := Claims{
UserId: userId,
Expand All @@ -19,7 +15,7 @@ func GenerateJwtToken(userId int, username, token, clientId, uuid string) (strin
//Uuid: uuid,
StandardClaims: jwt.StandardClaims{
// 过期时间
ExpiresAt: expireTime.Unix(),
ExpiresAt: 0,
// 指定token发行人
Issuer: "baozier",
},
Expand Down

0 comments on commit 2fef54d

Please sign in to comment.