diff --git a/cmd/apimain.go b/cmd/apimain.go index 90744da..331dfd8 100644 --- a/cmd/apimain.go +++ b/cmd/apimain.go @@ -102,9 +102,6 @@ func InitGlobal() { //配置解析 global.Viper = config.Init(&global.Config, global.ConfigPath) - //从配置文件中加载密钥 - config.LoadKeyFile(&global.Config.Rustdesk) - //日志 global.Logger = logger.New(&logger.Config{ Path: global.Config.Logger.Path, diff --git a/config/config.go b/config/config.go index 03a445b..ef2d474 100644 --- a/config/config.go +++ b/config/config.go @@ -40,7 +40,7 @@ type Config struct { } // Init 初始化配置 -func Init(rowVal interface{}, path string) *viper.Viper { +func Init(rowVal *Config, path string) *viper.Viper { if path == "" { path = DefaultConfig } @@ -61,11 +61,14 @@ func Init(rowVal interface{}, path string) *viper.Viper { if err2 := v.Unmarshal(rowVal); err2 != nil { fmt.Println(err2) } + rowVal.Rustdesk.LoadKeyFile() + rowVal.Rustdesk.ParsePort() }) if err := v.Unmarshal(rowVal); err != nil { fmt.Println(err) } - + rowVal.Rustdesk.LoadKeyFile() + rowVal.Rustdesk.ParsePort() return v } diff --git a/config/rustdesk.go b/config/rustdesk.go index f1903d1..5871e4d 100644 --- a/config/rustdesk.go +++ b/config/rustdesk.go @@ -2,31 +2,56 @@ package config import ( "os" + "strconv" + "strings" +) + +const ( + DefaultIdServerPort = 21116 + DefaultRelayServerPort = 21117 ) type Rustdesk struct { - IdServer string `mapstructure:"id-server"` - RelayServer string `mapstructure:"relay-server"` - ApiServer string `mapstructure:"api-server"` - Key string `mapstructure:"key"` - KeyFile string `mapstructure:"key-file"` - Personal int `mapstructure:"personal"` + IdServer string `mapstructure:"id-server"` + IdServerPort int `mapstructure:"-"` + RelayServer string `mapstructure:"relay-server"` + RelayPort int `mapstructure:"-"` + ApiServer string `mapstructure:"api-server"` + Key string `mapstructure:"key"` + KeyFile string `mapstructure:"key-file"` + Personal int `mapstructure:"personal"` //webclient-magic-queryonline WebclientMagicQueryonline int `mapstructure:"webclient-magic-queryonline"` } -func LoadKeyFile(rustdesk *Rustdesk) { +func (rd *Rustdesk) LoadKeyFile() { // Load key file - if rustdesk.Key != "" { + if rd.Key != "" { return } - if rustdesk.KeyFile != "" { + if rd.KeyFile != "" { // Load key from file - b, err := os.ReadFile(rustdesk.KeyFile) + b, err := os.ReadFile(rd.KeyFile) if err != nil { return } - rustdesk.Key = string(b) + rd.Key = string(b) 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.RelayPort = DefaultRelayServerPort + } else if len(relayres) == 2 { + rd.RelayPort, _ = strconv.Atoi(relayres[1]) + } +}