Skip to content

Commit

Permalink
feat(server): Add Rustdesk Relay Server Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lejianwen committed Jan 4, 2025
1 parent 7db4b03 commit d7f2d54
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/apimain.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func InitGlobal() {
global.Lock = lock.NewLocal()
}
func DatabaseAutoUpdate() {
version := 251
version := 260

db := global.DB

Expand Down
19 changes: 16 additions & 3 deletions http/controller/admin/rustdesk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
51 changes: 44 additions & 7 deletions model/serverCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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},
}
11 changes: 6 additions & 5 deletions service/serverCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"Gwen/global"
"Gwen/model"
"fmt"
"net"
"time"
)
Expand Down Expand Up @@ -40,30 +41,30 @@ 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
}
return "", err
}

// 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
Expand Down

0 comments on commit d7f2d54

Please sign in to comment.