216 lines
5.8 KiB
Go
216 lines
5.8 KiB
Go
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"
|
|
"net/url"
|
|
"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 UpdateHysteria2Config(hysteria2ServerConfig bo.Hysteria2ServerConfig) error {
|
|
// Значения по умолчанию
|
|
config, err := dao.ListConfig("key in ?", []string{constant.HUIWebPort, constant.Hysteria2TrafficStatsSecret})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var hUIWebPort string
|
|
var trafficStatsSecret string
|
|
for _, item := range config {
|
|
if *item.Key == constant.HUIWebPort {
|
|
hUIWebPort = *item.Value
|
|
} else if *item.Key == constant.Hysteria2TrafficStatsSecret {
|
|
trafficStatsSecret = *item.Value
|
|
}
|
|
}
|
|
|
|
if hUIWebPort == "" || trafficStatsSecret == "" {
|
|
logrus.Errorf("hUIWebPort or trafficStatsSecret is nil")
|
|
return errors.New(constant.SysError)
|
|
}
|
|
|
|
authHttpUrl, err := GetAuthHttpUrl()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
authType := "http"
|
|
authHttpInsecure := true
|
|
var auth bo.ServerConfigAuth
|
|
auth.Type = &authType
|
|
var http bo.ServerConfigAuthHTTP
|
|
http.URL = &authHttpUrl
|
|
http.Insecure = &authHttpInsecure
|
|
auth.HTTP = &http
|
|
hysteria2ServerConfig.Auth = &auth
|
|
if hysteria2ServerConfig.TrafficStats == nil {
|
|
hysteria2ServerConfig.TrafficStats = &bo.ServerConfigTrafficStats{}
|
|
}
|
|
hysteria2ServerConfig.TrafficStats.Secret = &trafficStatsSecret
|
|
|
|
yamlConfig, err := yaml.Marshal(&hysteria2ServerConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return dao.UpdateConfig([]string{constant.Hysteria2Config}, map[string]interface{}{"value": string(yamlConfig)})
|
|
}
|
|
|
|
func SetHysteria2Config(hysteria2ServerConfig bo.Hysteria2ServerConfig) error {
|
|
config, err := yaml.Marshal(&hysteria2ServerConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return dao.UpdateConfig([]string{constant.Hysteria2Config}, map[string]interface{}{"value": string(config)})
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func GetAuthHttpUrl() (string, error) {
|
|
port, crtPath, keyPath, err := GetPortAndCert()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
protocol := "http"
|
|
if crtPath != "" && keyPath != "" {
|
|
protocol = "https"
|
|
}
|
|
config, err := dao.GetConfig("key = ?", constant.HUIWebContext)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
webContext := ""
|
|
if config.Value != nil && *config.Value != "/" && strings.HasPrefix(*config.Value, "/") {
|
|
webContext = *config.Value
|
|
}
|
|
trafficSecretConfig, err := dao.GetConfig("key = ?", constant.Hysteria2TrafficStatsSecret)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
authURL := fmt.Sprintf("%s://127.0.0.1:%d%s/hui/hysteria2/auth", protocol, port, webContext)
|
|
if trafficSecretConfig.Value != nil && strings.TrimSpace(*trafficSecretConfig.Value) != "" {
|
|
authURL = fmt.Sprintf("%s?access_token=%s", authURL, url.QueryEscape(strings.TrimSpace(*trafficSecretConfig.Value)))
|
|
}
|
|
return authURL, nil
|
|
}
|