package service import ( "errors" "fmt" "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" "hy2xs-admin/dao" "hy2xs-admin/model/bo" "hy2xs-admin/model/constant" "hy2xs-admin/model/entity" "net" "os" "strconv" "strings" ) func UpdateConfig(key string, value string) error { return dao.UpdateConfig([]string{key}, map[string]interface{}{"value": value}) } func GetConfig(key string) (entity.Config, error) { return dao.GetConfig("key = ?", key) } func ListConfig(keys []string) ([]entity.Config, error) { return dao.ListConfig("key in ?", keys) } func ListConfigNotIn(keys []string) ([]entity.Config, error) { return dao.ListConfig("key not in ?", keys) } func GetHysteria2Config() (bo.Hysteria2ServerConfig, error) { var serverConfig bo.Hysteria2ServerConfig config, err := dao.GetConfig("key = ?", constant.Hysteria2Config) if err != nil { return serverConfig, err } if config.Value == nil || strings.TrimSpace(*config.Value) == "" { content, readErr := os.ReadFile(constant.Hysteria2ConfigPath) if readErr != nil { return serverConfig, readErr } if err = yaml.Unmarshal(content, &serverConfig); err != nil { return serverConfig, err } return serverConfig, nil } if err = yaml.Unmarshal([]byte(*config.Value), &serverConfig); err != nil { return serverConfig, err } return serverConfig, nil } func UpsertConfig(configs []entity.Config) error { return dao.UpsertConfig(configs) } func GetHysteria2ApiPort() (int64, error) { hysteria2Config, err := GetHysteria2Config() if err != nil { return 0, err } if hysteria2Config.TrafficStats == nil || hysteria2Config.TrafficStats.Listen == nil { errMsg := "hysteria2 Traffic Stats API (HTTP) Listen is nil" logrus.Errorf(errMsg) return 0, errors.New(errMsg) } apiPort, err := parseTrafficStatsPort(*hysteria2Config.TrafficStats.Listen) if err != nil { errMsg := fmt.Sprintf("apiPort: %s is invalid: %v", *hysteria2Config.TrafficStats.Listen, err) logrus.Errorf(errMsg) return 0, errors.New(errMsg) } return apiPort, nil } func parseTrafficStatsPort(listen string) (int64, error) { trimmed := strings.TrimSpace(listen) if trimmed == "" { return 0, errors.New("empty listen") } hostPort := trimmed if strings.HasPrefix(trimmed, ":") { hostPort = "127.0.0.1" + trimmed } _, portStr, err := net.SplitHostPort(hostPort) if err != nil { return 0, err } port, err := strconv.ParseInt(portStr, 10, 64) if err != nil { return 0, err } if port <= 0 || port > 65535 { return 0, errors.New("port out of range") } return port, nil } func GetPortAndCert() (int64, string, string, error) { configs, err := dao.ListConfig("key in ?", []string{constant.HUIWebPort, constant.HUICrtPath, constant.HUIKeyPath}) if err != nil { return 0, "", "", err } port := "" crtPath := "" keyPath := "" for _, config := range configs { value := *config.Value if *config.Key == constant.HUIWebPort { port = value } else if *config.Key == constant.HUICrtPath { crtPath = value } else if *config.Key == constant.HUIKeyPath { keyPath = value } } portInt, err := strconv.ParseInt(port, 10, 64) if err != nil { logrus.Errorf("port: %s is invalid", port) return 0, "", "", errors.New(fmt.Sprintf("port: %s is invalid", port)) } return portInt, crtPath, keyPath, nil }