mirror of
https://github.com/apernet/hysteria.git
synced 2025-04-07 06:27:39 +03:00
Update server.go
This commit is contained in:
parent
00ea03cf80
commit
8ed1be60db
1 changed files with 85 additions and 0 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
|
@ -46,6 +47,7 @@ func init() {
|
|||
}
|
||||
|
||||
type serverConfig struct {
|
||||
V2board *v2boardConfig `mapstructure:"v2board"`
|
||||
Listen string `mapstructure:"listen"`
|
||||
Obfs serverConfigObfs `mapstructure:"obfs"`
|
||||
TLS *serverConfigTLS `mapstructure:"tls"`
|
||||
|
@ -64,6 +66,12 @@ type serverConfig struct {
|
|||
Masquerade serverConfigMasquerade `mapstructure:"masquerade"`
|
||||
}
|
||||
|
||||
type v2boardConfig struct {
|
||||
ApiHost string `mapstructure:"apiHost"`
|
||||
ApiKey string `mapstructure:"apiKey"`
|
||||
NodeID uint `mapstructure:"nodeID"`
|
||||
}
|
||||
|
||||
type serverConfigObfsSalamander struct {
|
||||
Password string `mapstructure:"password"`
|
||||
}
|
||||
|
@ -616,6 +624,17 @@ func (c *serverConfig) fillAuthenticator(hyConfig *server.Config) error {
|
|||
}
|
||||
hyConfig.Authenticator = &auth.CommandAuthenticator{Cmd: c.Auth.Command}
|
||||
return nil
|
||||
case "v2board":
|
||||
// 定时获取用户列表并储存
|
||||
// 判断URL是否存在
|
||||
v2boardConfig := c.V2board
|
||||
if v2boardConfig.ApiHost == "" || v2boardConfig.ApiKey == "" || v2boardConfig.NodeID == 0 {
|
||||
return configError{Field: "auth.v2board", Err: errors.New("v2board config error")}
|
||||
}
|
||||
// 创建定时更新用户UUID协程
|
||||
hyConfig.Authenticator = &auth.V2boardApiProvider{URL: fmt.Sprintf("%s?token=%s&node_id=%d&node_type=hysteria", c.V2board.ApiHost+"/api/v1/server/UniProxy/user", c.V2board.ApiKey, c.V2board.NodeID)}
|
||||
return nil
|
||||
|
||||
default:
|
||||
return configError{Field: "auth.type", Err: errors.New("unsupported auth type")}
|
||||
}
|
||||
|
@ -630,7 +649,14 @@ func (c *serverConfig) fillTrafficLogger(hyConfig *server.Config) error {
|
|||
if c.TrafficStats.Listen != "" {
|
||||
tss := trafficlogger.NewTrafficStatsServer(c.TrafficStats.Secret)
|
||||
hyConfig.TrafficLogger = tss
|
||||
// 添加定时更新用户使用流量协程
|
||||
if c.V2board != nil && c.V2board.ApiHost != "" {
|
||||
go auth.UpdateUsers(fmt.Sprintf("%s?token=%s&node_id=%d&node_type=hysteria", c.V2board.ApiHost+"/api/v1/server/UniProxy/user", c.V2board.ApiKey, c.V2board.NodeID), time.Second*5, hyConfig.TrafficLogger)
|
||||
go hyConfig.TrafficLogger.PushTrafficToV2boardInterval(fmt.Sprintf("%s?token=%s&node_id=%d&node_type=hysteria", c.V2board.ApiHost+"/api/v1/server/UniProxy/push", c.V2board.ApiKey, c.V2board.NodeID), time.Second*60)
|
||||
}
|
||||
go runTrafficStatsServer(c.TrafficStats.Listen, tss)
|
||||
} else {
|
||||
go auth.UpdateUsers(fmt.Sprintf("%s?token=%s&node_id=%d&node_type=hysteria", c.V2board.ApiHost+"/api/v1/server/UniProxy/user", c.V2board.ApiKey, c.V2board.NodeID), time.Second*5, nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -741,6 +767,19 @@ func (c *serverConfig) Config() (*server.Config, error) {
|
|||
return hyConfig, nil
|
||||
}
|
||||
|
||||
type ResponseNodeInfo struct {
|
||||
Host string `json:"host"`
|
||||
ServerPort uint `json:"server_port"`
|
||||
ServerName string `json:"server_name"`
|
||||
UpMbps uint `json:"down_mbps"`
|
||||
DownMbps uint `json:"up_mbps"`
|
||||
Obfs string `json:"obfs"`
|
||||
BaseConfig struct {
|
||||
PushInterval int `json:"push_interval"`
|
||||
PullInterval int `json:"pull_interval"`
|
||||
} `json:"base_config"`
|
||||
}
|
||||
|
||||
func runServer(cmd *cobra.Command, args []string) {
|
||||
logger.Info("server mode")
|
||||
|
||||
|
@ -751,6 +790,52 @@ func runServer(cmd *cobra.Command, args []string) {
|
|||
if err := viper.Unmarshal(&config); err != nil {
|
||||
logger.Fatal("failed to parse server config", zap.Error(err))
|
||||
}
|
||||
// 如果配置了v2board 则自动获取监听端口、obfs
|
||||
if config.V2board != nil && config.V2board.ApiHost != "" {
|
||||
// 创建一个url.Values来存储查询参数
|
||||
queryParams := url.Values{}
|
||||
queryParams.Add("token", config.V2board.ApiKey)
|
||||
queryParams.Add("node_id", strconv.Itoa(int(config.V2board.NodeID)))
|
||||
queryParams.Add("node_type", "hysteria")
|
||||
|
||||
// 创建完整的URL,包括查询参数
|
||||
nodeInfoUrl := config.V2board.ApiHost + "/api/v1/server/UniProxy/config?" + queryParams.Encode()
|
||||
|
||||
// 发起 HTTP GET 请求
|
||||
resp, err := http.Get(nodeInfoUrl)
|
||||
if err != nil {
|
||||
// 处理错误
|
||||
fmt.Println("HTTP GET 请求出错:", err)
|
||||
logger.Fatal("failed to client v2board api to get nodeInfo", zap.Error(err))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// 读取响应数据
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logger.Fatal("failed to read v2board reaponse", zap.Error(err))
|
||||
}
|
||||
// 解析JSON数据
|
||||
var responseNodeInfo ResponseNodeInfo
|
||||
err = json.Unmarshal(body, &responseNodeInfo)
|
||||
if err != nil {
|
||||
logger.Fatal("failed to unmarshal v2board reaponse", zap.Error(err))
|
||||
}
|
||||
// 给 hy的端口、obfs、上行下行进行赋值
|
||||
if responseNodeInfo.ServerPort != 0 {
|
||||
config.Listen = ":" + strconv.Itoa(int(responseNodeInfo.ServerPort))
|
||||
}
|
||||
if responseNodeInfo.DownMbps != 0 {
|
||||
config.Bandwidth.Down = strconv.Itoa(int(responseNodeInfo.DownMbps)) + "Mbps"
|
||||
}
|
||||
if responseNodeInfo.UpMbps != 0 {
|
||||
config.Bandwidth.Up = strconv.Itoa(int(responseNodeInfo.UpMbps)) + "Mbps"
|
||||
}
|
||||
if responseNodeInfo.Obfs != "" {
|
||||
config.Obfs.Type = "salamander"
|
||||
config.Obfs.Salamander.Password = responseNodeInfo.Obfs
|
||||
}
|
||||
|
||||
}
|
||||
hyConfig, err := config.Config()
|
||||
if err != nil {
|
||||
logger.Fatal("failed to load server config", zap.Error(err))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue