diff --git a/conf/config.yaml b/conf/config.yaml index 62003f6..365da21 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -14,6 +14,9 @@ admin: title: "RustDesk Api Admin" hello-file: "./conf/admin/hello.html" #优先使用file hello: "" + # ID Server and Relay Server ports https://github.com/lejianwen/rustdesk-api/issues/257 + id-server-port: 21116 # ID Server port (for server cmd) + relay-server-port: 21117 # ID Server port (for server cmd) gin: api-addr: "0.0.0.0:21114" mode: "release" #release,debug,test diff --git a/config/config.go b/config/config.go index b176d18..5bc1a17 100644 --- a/config/config.go +++ b/config/config.go @@ -25,9 +25,11 @@ type App struct { BanThreshold int `mapstructure:"ban-threshold"` } type Admin struct { - Title string `mapstructure:"title"` - Hello string `mapstructure:"hello"` - HelloFile string `mapstructure:"hello-file"` + Title string `mapstructure:"title"` + Hello string `mapstructure:"hello"` + HelloFile string `mapstructure:"hello-file"` + IdServerPort int `mapstructure:"id-server-port"` + RelayServerPort int `mapstructure:"relay-server-port"` } type Config struct { Lang string `mapstructure:"lang"` @@ -46,6 +48,15 @@ type Config struct { Ldap Ldap } +func (a *Admin) Init() { + if a.IdServerPort == 0 { + a.IdServerPort = DefaultIdServerPort + } + if a.RelayServerPort == 0 { + a.RelayServerPort = DefaultRelayServerPort + } +} + // Init 初始化配置 func Init(rowVal *Config, path string) *viper.Viper { if path == "" { @@ -80,7 +91,7 @@ func Init(rowVal *Config, path string) *viper.Viper { panic(fmt.Errorf("Fatal error config: %s \n", err)) } rowVal.Rustdesk.LoadKeyFile() - rowVal.Rustdesk.ParsePort() + rowVal.Admin.Init() return v } diff --git a/config/rustdesk.go b/config/rustdesk.go index 2bc1c45..c93cf8f 100644 --- a/config/rustdesk.go +++ b/config/rustdesk.go @@ -2,8 +2,6 @@ package config import ( "os" - "strconv" - "strings" ) const ( @@ -40,19 +38,3 @@ func (rd *Rustdesk) LoadKeyFile() { return } } -func (rd *Rustdesk) ParsePort() { - // Parse port - idres := strings.Split(rd.IdServer, ":") - if len(idres) == 1 { - rd.IdServerPort = DefaultIdServerPort - } else if len(idres) == 2 { - rd.IdServerPort, _ = strconv.Atoi(idres[1]) - } - - relayres := strings.Split(rd.RelayServer, ":") - if len(relayres) == 1 { - rd.RelayServerPort = DefaultRelayServerPort - } else if len(relayres) == 2 { - rd.RelayServerPort, _ = strconv.Atoi(relayres[1]) - } -} diff --git a/http/controller/admin/rustdesk.go b/http/controller/admin/rustdesk.go index 4556692..6a354d1 100644 --- a/http/controller/admin/rustdesk.go +++ b/http/controller/admin/rustdesk.go @@ -119,7 +119,16 @@ func (r *Rustdesk) SendCmd(c *gin.Context) { response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")) return } - res, err := service.AllService.ServerCmdService.SendCmd(rc.Target, rc.Cmd, rc.Option) + + port := 0 + switch rc.Target { + case model.ServerCmdTargetIdServer: + port = global.Config.Admin.IdServerPort - 1 + case model.ServerCmdTargetRelayServer: + port = global.Config.Admin.RelayServerPort + } + + res, err := service.AllService.ServerCmdService.SendCmd(port, rc.Cmd, rc.Option) if err != nil { response.Fail(c, 101, err.Error()) return diff --git a/service/serverCmd.go b/service/serverCmd.go index 9f29499..4fbeb26 100644 --- a/service/serverCmd.go +++ b/service/serverCmd.go @@ -40,14 +40,7 @@ func (is *ServerCmdService) Create(u *model.ServerCmd) error { } // SendCmd 发送命令 -func (is *ServerCmdService) SendCmd(target string, cmd string, arg string) (string, error) { - port := 0 - switch target { - case model.ServerCmdTargetIdServer: - port = Config.Rustdesk.IdServerPort - 1 - case model.ServerCmdTargetRelayServer: - port = Config.Rustdesk.RelayServerPort - } +func (is *ServerCmdService) SendCmd(port int, cmd string, arg string) (string, error) { //组装命令 cmd = cmd + " " + arg res, err := is.SendSocketCmd("v6", port, cmd)