mirror of
https://github.com/lejianwen/rustdesk-api.git
synced 2025-09-15 14:28:34 +08:00
feat(server): Add Rustdesk Relay Server Commands
This commit is contained in:
parent
7db4b03634
commit
d7f2d54faa
@ -169,7 +169,7 @@ func InitGlobal() {
|
||||
global.Lock = lock.NewLocal()
|
||||
}
|
||||
func DatabaseAutoUpdate() {
|
||||
version := 251
|
||||
version := 260
|
||||
|
||||
db := global.DB
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ type Rustdesk struct {
|
||||
type RustdeskCmd struct {
|
||||
Cmd string `json:"cmd"`
|
||||
Option string `json:"option"`
|
||||
Target string `json:"target"`
|
||||
}
|
||||
|
||||
func (r *Rustdesk) CmdList(c *gin.Context) {
|
||||
@ -26,7 +27,8 @@ func (r *Rustdesk) CmdList(c *gin.Context) {
|
||||
res := service.AllService.ServerCmdService.List(q.Page, 9999)
|
||||
//在列表前添加系统命令
|
||||
list := make([]*model.ServerCmd, 0)
|
||||
list = append(list, model.SysServerCmds...)
|
||||
list = append(list, model.SysIdServerCmds...)
|
||||
list = append(list, model.SysRelayServerCmds...)
|
||||
list = append(list, res.ServerCmds...)
|
||||
res.ServerCmds = list
|
||||
response.Success(c, res)
|
||||
@ -101,12 +103,23 @@ func (r *Rustdesk) CmdUpdate(c *gin.Context) {
|
||||
|
||||
func (r *Rustdesk) SendCmd(c *gin.Context) {
|
||||
rc := &RustdeskCmd{}
|
||||
c.ShouldBindJSON(rc)
|
||||
if err := c.ShouldBindJSON(rc); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
if rc.Cmd == "" {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
res, err := service.AllService.ServerCmdService.SendCmd(rc.Cmd, rc.Option)
|
||||
if rc.Target == "" {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
if rc.Target != model.ServerCmdTargetIdServer && rc.Target != model.ServerCmdTargetRelayServer {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
res, err := service.AllService.ServerCmdService.SendCmd(rc.Target, rc.Cmd, rc.Option)
|
||||
if err != nil {
|
||||
response.Fail(c, 101, err.Error())
|
||||
return
|
||||
|
||||
@ -6,6 +6,7 @@ type ServerCmd struct {
|
||||
Alias string `json:"alias" gorm:"default:'';not null;"`
|
||||
Option string `json:"option" gorm:"default:'';not null;"`
|
||||
Explain string `json:"explain" gorm:"default:'';not null;"`
|
||||
Target string `json:"target" gorm:"default:'';not null;"`
|
||||
TimeModel
|
||||
}
|
||||
|
||||
@ -14,11 +15,47 @@ type ServerCmdList struct {
|
||||
Pagination
|
||||
}
|
||||
|
||||
var SysServerCmds = []*ServerCmd{
|
||||
{Cmd: "h", Option: "", Explain: "show help"},
|
||||
{Cmd: "relay-servers", Alias: "rs", Option: "<separated by ,>", Explain: "set or show relay servers"},
|
||||
{Cmd: "ip-blocker", Alias: "ib", Option: "[<ip>|<number>] [-]", Explain: "block or unblock ip or show blocked ip"},
|
||||
{Cmd: "ip-changes", Alias: "ic", Option: "[<id>|<number>] [-]", Explain: "ip-changes(ic) [<id>|<number>] [-]"},
|
||||
{Cmd: "always-use-relay", Alias: "aur", Option: "[y|n]", Explain: "always use relay"},
|
||||
{Cmd: "test-geo", Alias: "tg", Option: "<ip1> <ip2>", Explain: "test geo"},
|
||||
const (
|
||||
ServerCmdTargetIdServer = "21115"
|
||||
ServerCmdTargetRelayServer = "21117"
|
||||
)
|
||||
|
||||
var SysIdServerCmds = []*ServerCmd{
|
||||
{Cmd: "h", Option: "", Explain: "show help", Target: ServerCmdTargetIdServer},
|
||||
{Cmd: "relay-servers", Alias: "rs", Option: "<separated by ,>", Explain: "set or show relay servers", Target: ServerCmdTargetIdServer},
|
||||
{Cmd: "ip-blocker", Alias: "ib", Option: "[<ip>|<number>] [-]", Explain: "block or unblock ip or show blocked ip", Target: ServerCmdTargetIdServer},
|
||||
{Cmd: "ip-changes", Alias: "ic", Option: "[<id>|<number>] [-]", Explain: "ip-changes(ic) [<id>|<number>] [-]", Target: ServerCmdTargetIdServer},
|
||||
{Cmd: "always-use-relay", Alias: "aur", Option: "[y|n]", Explain: "always use relay", Target: ServerCmdTargetIdServer},
|
||||
{Cmd: "test-geo", Alias: "tg", Option: "<ip1> <ip2>", Explain: "test geo", Target: ServerCmdTargetIdServer},
|
||||
}
|
||||
|
||||
/*
|
||||
"blacklist-add(ba) <ip>",
|
||||
"blacklist-remove(br) <ip>",
|
||||
"blacklist(b) <ip>",
|
||||
"blocklist-add(Ba) <ip>",
|
||||
"blocklist-remove(Br) <ip>",
|
||||
"blocklist(B) <ip>",
|
||||
"downgrade-threshold(dt) [value]",
|
||||
"downgrade-start-check(t) [value(second)]",
|
||||
"limit-speed(ls) [value(Mb/s)]",
|
||||
"total-bandwidth(tb) [value(Mb/s)]",
|
||||
"single-bandwidth(sb) [value(Mb/s)]",
|
||||
"usage(u)"
|
||||
*/
|
||||
|
||||
var SysRelayServerCmds = []*ServerCmd{
|
||||
{Cmd: "h", Option: "", Explain: "show help", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "blacklist-add", Alias: "ba", Option: "<ip>", Explain: "blacklist-add(ba) <ip>", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "blacklist-remove", Alias: "br", Option: "<ip>", Explain: "blacklist-remove(br) <ip>", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "blacklist", Alias: "b", Option: "<ip>", Explain: "blacklist(b) <ip>", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "blocklist-add", Alias: "Ba", Option: "<ip>", Explain: "blocklist-add(Ba) <ip>", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "blocklist-remove", Alias: "Br", Option: "<ip>", Explain: "blocklist-remove(Br) <ip>", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "blocklist", Alias: "B", Option: "<ip>", Explain: "blocklist(B) <ip>", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "downgrade-threshold", Alias: "dt", Option: "[value]", Explain: "downgrade-threshold(dt) [value]", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "downgrade-start-check", Alias: "t", Option: "[value(second)]", Explain: "downgrade-start-check(t) [value(second)]", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "limit-speed", Alias: "ls", Option: "[value(Mb/s)]", Explain: "limit-speed(ls) [value(Mb/s)]", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "total-bandwidth", Alias: "tb", Option: "[value(Mb/s)]", Explain: "total-bandwidth(tb) [value(Mb/s)]", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "single-bandwidth", Alias: "sb", Option: "[value(Mb/s)]", Explain: "single-bandwidth(sb) [value(Mb/s)]", Target: ServerCmdTargetRelayServer},
|
||||
{Cmd: "usage", Alias: "u", Option: "", Explain: "usage(u)", Target: ServerCmdTargetRelayServer},
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"Gwen/global"
|
||||
"Gwen/model"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
@ -40,15 +41,15 @@ func (is *ServerCmdService) Create(u *model.ServerCmd) error {
|
||||
}
|
||||
|
||||
// SendCmd 发送命令
|
||||
func (is *ServerCmdService) SendCmd(cmd string, arg string) (string, error) {
|
||||
func (is *ServerCmdService) SendCmd(port string, cmd string, arg string) (string, error) {
|
||||
//组装命令
|
||||
cmd = cmd + " " + arg
|
||||
res, err := is.SendSocketCmd("v6", cmd)
|
||||
res, err := is.SendSocketCmd("v6", port, cmd)
|
||||
if err == nil {
|
||||
return res, nil
|
||||
}
|
||||
//v6连接失败,尝试v4
|
||||
res, err = is.SendSocketCmd("v4", cmd)
|
||||
res, err = is.SendSocketCmd("v4", port, cmd)
|
||||
if err == nil {
|
||||
return res, nil
|
||||
}
|
||||
@ -56,14 +57,14 @@ func (is *ServerCmdService) SendCmd(cmd string, arg string) (string, error) {
|
||||
}
|
||||
|
||||
// SendSocketCmd
|
||||
func (is *ServerCmdService) SendSocketCmd(ty string, cmd string) (string, error) {
|
||||
func (is *ServerCmdService) SendSocketCmd(ty string, port string, cmd string) (string, error) {
|
||||
addr := "[::1]"
|
||||
tcp := "tcp6"
|
||||
if ty == "v4" {
|
||||
tcp = "tcp"
|
||||
addr = "127.0.0.1"
|
||||
}
|
||||
conn, err := net.Dial(tcp, addr+":21115")
|
||||
conn, err := net.Dial(tcp, fmt.Sprintf("%s:%s", addr, port))
|
||||
if err != nil {
|
||||
global.Logger.Debugf("%s connect to id server failed: %v", ty, err)
|
||||
return "", err
|
||||
|
||||
Loading…
Reference in New Issue
Block a user