2024-02-05 20:59:37 -05:00
|
|
|
package hy2
|
|
|
|
|
|
|
|
import (
|
2024-02-16 07:46:23 -05:00
|
|
|
"fmt"
|
|
|
|
|
2024-02-05 20:59:37 -05:00
|
|
|
"github.com/InazumaV/V2bX/api/panel"
|
|
|
|
"github.com/InazumaV/V2bX/conf"
|
|
|
|
"github.com/apernet/hysteria/core/server"
|
2024-02-26 08:00:55 -05:00
|
|
|
"github.com/spf13/viper"
|
2024-02-05 20:59:37 -05:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Hysteria2node struct {
|
|
|
|
Hy2server server.Server
|
|
|
|
Tag string
|
|
|
|
Logger *zap.Logger
|
|
|
|
EventLogger server.EventLogger
|
|
|
|
TrafficLogger server.TrafficLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria2) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error {
|
2024-02-16 07:46:23 -05:00
|
|
|
var err error
|
|
|
|
hyconfig := &server.Config{}
|
2024-02-26 08:00:55 -05:00
|
|
|
var c serverConfig
|
|
|
|
v := viper.New()
|
|
|
|
if len(config.Hysteria2ConfigPath) != 0 {
|
|
|
|
v.SetConfigFile(config.Hysteria2ConfigPath)
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
|
|
h.Logger.Fatal("failed to read server config", zap.Error(err))
|
2024-02-16 07:46:23 -05:00
|
|
|
}
|
2024-02-26 08:00:55 -05:00
|
|
|
if err := v.Unmarshal(&c); err != nil {
|
|
|
|
h.Logger.Fatal("failed to parse server config", zap.Error(err))
|
2024-02-16 07:46:23 -05:00
|
|
|
}
|
2024-02-26 08:00:55 -05:00
|
|
|
h.Logger.Debug("loaded server config:")
|
|
|
|
fmt.Printf("%+v", c)
|
2024-02-16 07:46:23 -05:00
|
|
|
}
|
2024-02-05 20:59:37 -05:00
|
|
|
n := Hysteria2node{
|
|
|
|
Tag: tag,
|
|
|
|
Logger: h.Logger,
|
|
|
|
EventLogger: &serverLogger{
|
|
|
|
Tag: tag,
|
|
|
|
logger: h.Logger,
|
|
|
|
},
|
|
|
|
TrafficLogger: &HookServer{
|
|
|
|
Tag: tag,
|
|
|
|
},
|
|
|
|
}
|
2024-02-16 07:46:23 -05:00
|
|
|
|
2024-02-26 08:00:55 -05:00
|
|
|
hyconfig, err = n.getHyConfig(tag, info, config, &c)
|
2024-02-05 20:59:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hyconfig.Authenticator = h.Auth
|
|
|
|
s, err := server.NewServer(hyconfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.Hy2server = s
|
|
|
|
h.Hy2nodes[tag] = n
|
|
|
|
go func() {
|
|
|
|
if err := s.Serve(); err != nil {
|
|
|
|
h.Logger.Error("Server Error", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria2) DelNode(tag string) error {
|
|
|
|
err := h.Hy2nodes[tag].Hy2server.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(h.Hy2nodes, tag)
|
|
|
|
return nil
|
|
|
|
}
|