Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuzuki616 committed Jun 9, 2023
1 parent 42bb7bc commit 9827d44
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 80 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ A V2board node server based on multi core, modified from XrayR.

## 功能介绍

| 功能 | v2ray | trojan | shadowsocks |
|-----------|-------|--------|-------------|
| 自动申请tls证书 ||||
| 自动续签tls证书 ||||
| 在线人数统计 ||||
| 审计规则 ||||
| 自定义DNS ||||
| 在线IP数限制 ||||
| 连接数限制 ||||
| 跨节点IP数限制 ||||
| 按照用户限速 ||||
| 动态限速(未测试) ||||
| 功能 | v2ray | trojan | shadowsocks | hysteria |
|-----------|-------|--------|-------------|----------|
| 自动申请tls证书 |||||
| 自动续签tls证书 |||||
| 在线人数统计 |||||
| 审计规则 |||| |
| 自定义DNS |||||
| 在线IP数限制 |||| |
| 连接数限制 |||| |
| 跨节点IP数限制 |||| |
| 按照用户限速 |||| |
| 动态限速(未测试) |||| |

## TODO

Expand Down
17 changes: 10 additions & 7 deletions api/panel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
)

type CommonNodeRsp struct {
Host string `json:"host"`
ServerPort int `json:"server_port"`
ServerName string `json:"server_name"`
Routes []Route `json:"routes"`
BaseConfig BaseConfig `json:"base_config"`
}
Expand Down Expand Up @@ -54,11 +56,11 @@ type NodeInfo struct {
Id int
Type string
Rules []*regexp.Regexp
Host string
Port int
Network string
NetworkSettings json.RawMessage
Tls bool
Host string
ServerName string
UpMbps int
DownMbps int
Expand All @@ -75,16 +77,14 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
if err = c.checkResponse(r, path, err); err != nil {
return
}
err = json.Unmarshal(r.Body(), &node)
if err != nil {
return
}
if c.etag == r.Header().Get("ETag") { // node info not changed
return nil, nil
}
// parse common params
node.Id = c.NodeId
node.Type = c.NodeType
node = &NodeInfo{
Id: c.NodeId,
Type: c.NodeType,
}
common := CommonNodeRsp{}
err = json.Unmarshal(r.Body(), &common)
if err != nil {
Expand All @@ -103,6 +103,9 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
}
}
}
node.ServerName = common.ServerName
node.Host = common.Host
node.Port = common.ServerPort
node.PullInterval = intervalToTime(common.BaseConfig.PullInterval)
node.PushInterval = intervalToTime(common.BaseConfig.PushInterval)
// parse protocol params
Expand Down
6 changes: 3 additions & 3 deletions api/panel/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func New(c *conf.ApiConfig) (*Client, error) {
client.SetBaseURL(c.APIHost)
// Check node type
c.NodeType = strings.ToLower(c.NodeType)
if c.NodeType != "v2ray" &&
c.NodeType != "trojan" &&
c.NodeType != "shadowsocks" {
switch c.NodeType {
case "v2ray", "trojan", "shadowsocks", "hysteria":
default:
return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType)
}
// Create Key for each requests
Expand Down
2 changes: 1 addition & 1 deletion api/panel/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Client) checkResponse(res *resty.Response, path string, err error) erro
}
if res.StatusCode() != 200 {
body := res.Body()
return fmt.Errorf("request %s failed: %s, %s", c.assembleURL(path), string(body), err)
return fmt.Errorf("request %s failed: %s", c.assembleURL(path), string(body))
}
return nil
}
10 changes: 3 additions & 7 deletions common/builder/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func BuildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
case "v2ray":
err = buildV2ray(config, nodeInfo, in)
case "trojan":
err = buildTrojan(config, nodeInfo, in)
err = buildTrojan(config, in)
case "shadowsocks":
err = buildShadowsocks(config, nodeInfo, in)
default:
Expand Down Expand Up @@ -104,7 +104,6 @@ func BuildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
// Support ProxyProtocol for any transport protocol
if *in.StreamSetting.Network != "tcp" &&
*in.StreamSetting.Network != "ws" &&
*in.StreamSetting.Network != "" &&
config.EnableProxyProtocol {
socketConfig := &coreConf.SocketConfig{
AcceptProxyProtocol: config.EnableProxyProtocol,
Expand Down Expand Up @@ -177,7 +176,7 @@ func buildV2ray(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound
return nil
}

func buildTrojan(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
func buildTrojan(config *conf.ControllerConfig, inbound *coreConf.InboundDetourConfig) error {
inbound.Protocol = "trojan"
if config.XrayOptions.EnableFallback {
// Set fallback
Expand All @@ -196,10 +195,7 @@ func buildTrojan(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inboun
s := []byte("{}")
inbound.Settings = (*json.RawMessage)(&s)
}
if nodeInfo.Network == "" {
nodeInfo.Network = "tcp"
}
t := coreConf.TransportProtocol(nodeInfo.Network)
t := coreConf.TransportProtocol("tcp")
inbound.StreamSetting = &coreConf.StreamConfig{Network: &t}
return nil
}
Expand Down
18 changes: 9 additions & 9 deletions conf/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ type ApiConfig struct {
}

type ControllerConfig struct {
DisableUploadTraffic bool `yaml:"DisableUploadTraffic"`
DisableGetRule bool `yaml:"DisableGetRule"`
ListenIP string `yaml:"ListenIP"`
SendIP string `yaml:"SendIP"`
EnableProxyProtocol bool `yaml:"EnableProxyProtocol"`
XrayOptions *XrayOptions `yaml:"XrayOptions"`
HyOptions *HyOptions `yaml:"HyOptions"`
LimitConfig LimitConfig `yaml:"LimitConfig"`
CertConfig *CertConfig `yaml:"CertConfig"`
DisableUploadTraffic bool `yaml:"DisableUploadTraffic"`
DisableGetRule bool `yaml:"DisableGetRule"`
ListenIP string `yaml:"ListenIP"`
SendIP string `yaml:"SendIP"`
EnableProxyProtocol bool `yaml:"EnableProxyProtocol"`
XrayOptions XrayOptions `yaml:"XrayOptions"`
HyOptions HyOptions `yaml:"HyOptions"`
LimitConfig LimitConfig `yaml:"LimitConfig"`
CertConfig *CertConfig `yaml:"CertConfig"`
}

type XrayOptions struct {
Expand Down
7 changes: 4 additions & 3 deletions core/hy/counter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hy

import (
"github.com/apernet/hysteria/core/cs"
"sync"
"sync/atomic"
)
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion core/hy/hy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions core/hy/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 16 additions & 16 deletions core/hy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hy

import (
"crypto/tls"
"errors"
"fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/conf"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down
5 changes: 4 additions & 1 deletion core/hy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {}
}
12 changes: 7 additions & 5 deletions core/hy/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hy

import (
"encoding/base64"
"errors"
"github.com/Yuzuki616/V2bX/core"
)
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 9827d44

Please sign in to comment.