fix some bugs

This commit is contained in:
yuzuki999
2023-06-09 12:36:49 +08:00
parent 42bb7bc90f
commit 9827d442d5
14 changed files with 103 additions and 80 deletions

View File

@@ -1,7 +1,6 @@
package hy
import (
"github.com/apernet/hysteria/core/cs"
"sync"
"sync/atomic"
)
@@ -17,8 +16,10 @@ type counters struct {
//ConnGauge atomic.Int64
}
func NewUserTrafficCounter() cs.TrafficCounter {
return new(UserTrafficCounter)
func NewUserTrafficCounter() *UserTrafficCounter {
return &UserTrafficCounter{
counters: map[string]*counters{},
}
}
func (c *UserTrafficCounter) getCounters(auth string) *counters {

View File

@@ -3,15 +3,20 @@ package hy
import (
"fmt"
"github.com/Yuzuki616/V2bX/conf"
vCore "github.com/Yuzuki616/V2bX/core"
"github.com/hashicorp/go-multierror"
"sync"
)
func init() {
vCore.RegisterCore("hy", NewHy)
}
type Hy struct {
servers sync.Map
}
func New(_ *conf.CoreConfig) (*Hy, error) {
func NewHy(_ *conf.CoreConfig) (vCore.Core, error) {
return &Hy{
servers: sync.Map{},
}, nil

View File

@@ -12,6 +12,10 @@ func (h *Hy) AddNode(tag string, info *panel.NodeInfo, c *conf.ControllerConfig)
if info.Type != "hysteria" {
return errors.New("the core not support " + info.Type)
}
switch c.CertConfig.CertMode {
case "reality", "none", "":
return errors.New("hysteria need normal tls cert")
}
s := NewServer(tag)
err := s.runServer(info, c)
if err != nil {

View File

@@ -2,7 +2,6 @@ package hy
import (
"crypto/tls"
"errors"
"fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/conf"
@@ -33,7 +32,7 @@ var serverPacketConnFuncFactoryMap = map[string]pktconns.ServerPacketConnFuncFac
type Server struct {
tag string
counter UserTrafficCounter
counter *UserTrafficCounter
users sync.Map
running atomic.Bool
*cs.Server
@@ -46,9 +45,9 @@ func NewServer(tag string) *Server {
}
func (s *Server) runServer(node *panel.NodeInfo, c *conf.ControllerConfig) error {
if c.HyOptions == nil {
/*if c.HyOptions == nil {
return errors.New("hy options is not vail")
}
}*/
// Resolver
if len(c.HyOptions.Resolver) > 0 {
err := setResolver(c.HyOptions.Resolver)
@@ -136,7 +135,7 @@ func (s *Server) runServer(node *panel.NodeInfo, c *conf.ControllerConfig) error
aclEngine.DefaultAction = acl.ActionDirect
}*/
// Prometheus
trafficCounter := NewUserTrafficCounter()
s.counter = NewUserTrafficCounter()
// Packet conn
pktConnFuncFactory := serverPacketConnFuncFactoryMap[""]
if pktConnFuncFactory == nil {
@@ -155,7 +154,7 @@ func (s *Server) runServer(node *panel.NodeInfo, c *conf.ControllerConfig) error
up, down := SpeedTrans(node.UpMbps, node.DownMbps)
s.Server, err = cs.NewServer(tlsConfig, quicConfig, pktConn,
transport.DefaultServerTransport, up, down, false, aclEngine,
s.connectFunc, s.disconnectFunc, tcpRequestFunc, tcpErrorFunc, udpRequestFunc, udpErrorFunc, trafficCounter)
s.connectFunc, s.disconnectFunc, tcpRequestFunc, tcpErrorFunc, udpRequestFunc, udpErrorFunc, s.counter)
if err != nil {
return fmt.Errorf("new server error: %s", err)
}
@@ -173,25 +172,26 @@ func (s *Server) runServer(node *panel.NodeInfo, c *conf.ControllerConfig) error
return nil
}
func (s *Server) authByUser(addr net.Addr, auth []byte, sSend uint64, sRecv uint64) (bool, string, string) {
if email, ok := s.users.Load(string(auth)); ok {
return true, email.(string), "Done"
func (s *Server) authByUser(addr net.Addr, auth []byte, sSend uint64, sRecv uint64) (bool, string) {
if _, ok := s.users.Load(string(auth)); ok {
return true, "Done"
}
return false, "", "Failed"
return false, "Failed"
}
func (s *Server) connectFunc(addr net.Addr, auth []byte, sSend uint64, sRecv uint64) (bool, string) {
ok, email, msg := s.authByUser(addr, auth, sSend, sRecv)
ok, msg := s.authByUser(addr, auth, sSend, sRecv)
if !ok {
logrus.WithFields(logrus.Fields{
"src": defaultIPMasker.Mask(addr.String()),
}).Info("Authentication failed, client rejected")
} else {
logrus.WithFields(logrus.Fields{
"src": defaultIPMasker.Mask(addr.String()),
"email": email,
}).Info("Client connected")
return false, msg
}
logrus.WithFields(logrus.Fields{
"src": defaultIPMasker.Mask(addr.String()),
"Uuid": string(auth),
"Tag": s.tag,
}).Info("Client connected")
return ok, msg
}

View File

@@ -3,10 +3,12 @@ package hy
import (
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/conf"
"github.com/sirupsen/logrus"
"testing"
)
func TestServer(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
s := NewServer("test")
t.Log(s.runServer(&panel.NodeInfo{
Port: 11415,
@@ -15,11 +17,12 @@ func TestServer(t *testing.T) {
HyObfs: "atresssdaaaadd",
}, &conf.ControllerConfig{
ListenIP: "127.0.0.1",
HyOptions: &conf.HyOptions{},
HyOptions: conf.HyOptions{},
CertConfig: &conf.CertConfig{
CertFile: "../../test_data/1.pem",
KeyFile: "../../test_data/1.key",
},
}))
s.users.Store("test1111", struct{}{})
select {}
}

View File

@@ -1,6 +1,7 @@
package hy
import (
"encoding/base64"
"errors"
"github.com/Yuzuki616/V2bX/core"
)
@@ -18,12 +19,13 @@ func (h *Hy) AddUsers(p *core.AddUsersParams) (int, error) {
}
func (h *Hy) GetUserTraffic(tag, uuid string, reset bool) (up int64, down int64) {
s, _ := h.servers.Load(tag)
c := &s.(*Server).counter
up = c.getCounters(uuid).UpCounter.Load()
down = c.getCounters(uuid).DownCounter.Load()
v, _ := h.servers.Load(tag)
s := v.(*Server)
auth := base64.StdEncoding.EncodeToString([]byte(uuid))
up = s.counter.getCounters(auth).UpCounter.Load()
down = s.counter.getCounters(uuid).DownCounter.Load()
if reset {
c.Reset(uuid)
s.counter.Reset(uuid)
}
return
}