diff --git a/app/controllers/adressBook.go b/app/controllers/adressBook.go index f235254..821eece 100644 --- a/app/controllers/adressBook.go +++ b/app/controllers/adressBook.go @@ -6,7 +6,6 @@ import ( "rustdesk-api-server/app/services" "rustdesk-api-server/utils/beegoHelper" "strings" - "time" ) var Address = new(AddressBookController) @@ -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"), }) } diff --git a/app/controllers/base.go b/app/controllers/base.go index 4780326..65af0e1 100644 --- a/app/controllers/base.go +++ b/app/controllers/base.go @@ -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")) { diff --git a/app/controllers/heart.go b/app/controllers/heart.go new file mode 100644 index 0000000..85a1a85 --- /dev/null +++ b/app/controllers/heart.go @@ -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(), + }) +} diff --git a/app/controllers/login.go b/app/controllers/login.go index 216cb07..a6b3fc9 100644 --- a/app/controllers/login.go +++ b/app/controllers/login.go @@ -38,8 +38,8 @@ 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不能为空", @@ -47,15 +47,17 @@ func (ctl *LoginController) Login() { } // 查询数据库中的账号密码是否合法 - 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, diff --git a/app/dto/login.go b/app/dto/login.go index 6bc0a44..917a540 100644 --- a/app/dto/login.go +++ b/app/dto/login.go @@ -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"` } diff --git a/conf/app.conf b/conf/app.conf index bb01f39..4989779 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -4,7 +4,7 @@ appname = rustdesk-api-server # 监听IP -HTTPAddr = 127.0.0.1 +HTTPAddr = 0.0.0.0 # 端口号 httpport = 21114 diff --git a/routers/router.go b/routers/router.go index b85d51a..2a96cfc 100644 --- a/routers/router.go +++ b/routers/router.go @@ -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") diff --git a/utils/common/json.go b/utils/common/json.go index 2918941..c0fc704 100644 --- a/utils/common/json.go +++ b/utils/common/json.go @@ -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"` } diff --git a/utils/utils.go b/utils/utils.go index 69548a6..95001ad 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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, @@ -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", },