update sing-box support

This commit is contained in:
Yuzuki616
2023-07-29 18:47:47 +08:00
parent 2812b366b3
commit 96493346f9
34 changed files with 418 additions and 297 deletions

View File

@@ -9,7 +9,7 @@ import (
"github.com/Yuzuki616/V2bX/limiter"
)
func (h *Hy) AddNode(tag string, info *panel.NodeInfo, c *conf.ControllerConfig) error {
func (h *Hy) AddNode(tag string, info *panel.NodeInfo, c *conf.Options) error {
if info.Type != "hysteria" {
return errors.New("the core not support " + info.Type)
}

View File

@@ -49,7 +49,7 @@ func NewServer(tag string, l *limiter.Limiter) *Server {
}
}
func (s *Server) runServer(node *panel.NodeInfo, c *conf.ControllerConfig) error {
func (s *Server) runServer(node *panel.NodeInfo, c *conf.Options) error {
/*if c.HyOptions == nil {
return errors.New("hy options is not vail")
}*/

View File

@@ -22,7 +22,7 @@ func TestServer(t *testing.T) {
UpMbps: 100,
DownMbps: 100,
HyObfs: "atresssdaaaadd",
}, &conf.ControllerConfig{
}, &conf.Options{
ListenIP: "127.0.0.1",
HyOptions: conf.HyOptions{},
CertConfig: &conf.CertConfig{
@@ -36,7 +36,7 @@ func TestServer(t *testing.T) {
time.Sleep(10 * time.Second)
auth := base64.StdEncoding.EncodeToString([]byte("test1111"))
log.Println(auth)
log.Println(s.counter.getCounters(auth).UpCounter.Load())
log.Println(s.counter.GetUpCount(auth))
}
}()
select {}

View File

@@ -7,14 +7,14 @@ import (
type AddUsersParams struct {
Tag string
Config *conf.ControllerConfig
Config *conf.Options
UserInfo []panel.UserInfo
NodeInfo *panel.NodeInfo
}
type Core interface {
Start() error
Close() error
AddNode(tag string, info *panel.NodeInfo, config *conf.ControllerConfig) error
AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error
DelNode(tag string) error
AddUsers(p *AddUsersParams) (added int, err error)
GetUserTraffic(tag, uuid string, reset bool) (up int64, down int64)

View File

@@ -39,7 +39,7 @@ func isSupported(protocol string, protocols []string) bool {
return false
}
func (s *Selector) AddNode(tag string, info *panel.NodeInfo, config *conf.ControllerConfig) error {
func (s *Selector) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error {
for i := range s.cores {
if !isSupported(info.Type, s.cores[i].Protocols()) {
continue

View File

@@ -2,6 +2,12 @@ package sing
import (
"net"
"strings"
"sync"
"github.com/Yuzuki616/V2bX/common/rate"
"github.com/Yuzuki616/V2bX/limiter"
"github.com/Yuzuki616/V2bX/common/counter"
"github.com/inazumav/sing-box/adapter"
@@ -17,7 +23,7 @@ func NewHookServer(logger log.Logger) *HookServer {
return &HookServer{
hooker: &Hooker{
logger: logger,
counter: make(map[string]*counter.TrafficCounter),
counter: sync.Map{},
},
}
}
@@ -40,19 +46,37 @@ func (h *HookServer) Hooker() *Hooker {
type Hooker struct {
logger log.Logger
counter map[string]*counter.TrafficCounter
counter sync.Map
}
func (h *Hooker) RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn {
if c, ok := h.counter[inbound]; ok {
l, err := limiter.GetLimiter(inbound)
if err != nil {
log.Error("get limiter for ", inbound, " error: ", err)
}
ip, _, _ := strings.Cut(conn.RemoteAddr().String(), ":")
if b, r := l.CheckLimit(user, ip, true); r {
conn.Close()
h.logger.Error("[", inbound, "] ", "Limited ", user, " by ip or conn")
return conn
} else if b != nil {
conn = rate.NewConnRateLimiter(conn, b)
}
if c, ok := h.counter.Load(inbound); ok {
return counter.NewConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(user))
} else {
c := counter.NewTrafficCounter()
h.counter.Store(inbound, c)
return counter.NewConnCounter(conn, c.GetCounter(user))
}
return conn
}
func (h *Hooker) RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn {
if c, ok := h.counter[inbound]; ok {
if c, ok := h.counter.Load(inbound); ok {
return counter.NewPacketConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(user))
} else {
c := counter.NewTrafficCounter()
h.counter.Store(inbound, c)
return counter.NewPacketConnCounter(conn, c.GetCounter(user))
}
return conn
}

View File

@@ -21,12 +21,17 @@ import (
)
type WsNetworkConfig struct {
Path string `json:"path"`
Path string `json:"path"`
Headers map[string]string `json:"headers"`
}
func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.ControllerConfig) (option.Inbound, error) {
addr, _ := netip.ParseAddr("0.0.0.0")
func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (option.Inbound, error) {
addr, err := netip.ParseAddr(c.ListenIP)
if err != nil {
return option.Inbound{}, fmt.Errorf("the listen ip not vail")
}
listen := option.ListenOptions{
//ProxyProtocol: true,
Listen: (*option.ListenAddress)(&addr),
ListenPort: uint16(info.Port),
}
@@ -47,6 +52,7 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.ControllerConfi
}
switch info.Network {
case "tcp":
t.Type = ""
case "ws":
network := WsNetworkConfig{}
err := json.Unmarshal(info.NetworkSettings, &network)
@@ -59,14 +65,22 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.ControllerConfi
return option.Inbound{}, fmt.Errorf("parse path error: %s", err)
}
ed, _ := strconv.Atoi(u.Query().Get("ed"))
h := make(map[string]option.Listable[string], len(network.Headers))
for k, v := range network.Headers {
h[k] = option.Listable[string]{
v,
}
}
t.WebsocketOptions = option.V2RayWebsocketOptions{
Path: u.Path,
EarlyDataHeaderName: "Sec-WebSocket-Protocol",
MaxEarlyData: uint32(ed),
Headers: h,
}
case "grpc":
t.GRPCOptions = option.V2RayGRPCOptions{
ServiceName: info.ServerName,
err := json.Unmarshal(info.NetworkSettings, &t.GRPCOptions)
if err != nil {
return option.Inbound{}, fmt.Errorf("decode NetworkSettings error: %s", err)
}
}
in.VMessOptions = option.VMessInboundOptions{
@@ -96,7 +110,7 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.ControllerConfi
return in, nil
}
func (b *Box) AddNode(tag string, info *panel.NodeInfo, config *conf.ControllerConfig) error {
func (b *Box) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error {
c, err := getInboundOptions(tag, info, config)
if err != nil {
return err

View File

@@ -39,8 +39,14 @@ func init() {
vCore.RegisterCore("sing", New)
}
func New(_ *conf.CoreConfig) (vCore.Core, error) {
func New(c *conf.CoreConfig) (vCore.Core, error) {
options := option.Options{}
options.Log = &option.LogOptions{
Disabled: c.SingConfig.LogConfig.Disabled,
Level: c.SingConfig.LogConfig.Level,
Timestamp: c.SingConfig.LogConfig.Timestamp,
Output: c.SingConfig.LogConfig.Output,
}
ctx := context.Background()
createdAt := time.Now()
experimentalOptions := common.PtrValueOrDefault(options.Experimental)

View File

@@ -4,6 +4,7 @@ import (
"errors"
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/common/counter"
"github.com/Yuzuki616/V2bX/core"
"github.com/inazumav/sing-box/inbound"
"github.com/inazumav/sing-box/option"
@@ -36,7 +37,8 @@ func (b *Box) AddUsers(p *core.AddUsersParams) (added int, err error) {
}
func (b *Box) GetUserTraffic(tag, uuid string, reset bool) (up int64, down int64) {
if c, ok := b.hookServer.Hooker().counter[tag]; ok {
if v, ok := b.hookServer.Hooker().counter.Load(tag); ok {
c := v.(*counter.TrafficCounter)
up = c.GetUpCount(uuid)
down = c.GetDownCount(uuid)
if reset {

View File

@@ -17,7 +17,7 @@ import (
)
// BuildInbound build Inbound config for different protocol
func buildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag string) (*core.InboundHandlerConfig, error) {
func buildInbound(config *conf.Options, nodeInfo *panel.NodeInfo, tag string) (*core.InboundHandlerConfig, error) {
in := &coreConf.InboundDetourConfig{}
// Set network protocol
t := coreConf.TransportProtocol(nodeInfo.Network)
@@ -150,7 +150,7 @@ func buildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
return in.Build()
}
func buildV2ray(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
func buildV2ray(config *conf.Options, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
if nodeInfo.ExtraConfig.EnableVless == "true" {
//Set vless
inbound.Protocol = "vless"
@@ -213,7 +213,7 @@ func buildV2ray(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound
return nil
}
func buildTrojan(config *conf.ControllerConfig, inbound *coreConf.InboundDetourConfig) error {
func buildTrojan(config *conf.Options, inbound *coreConf.InboundDetourConfig) error {
inbound.Protocol = "trojan"
if config.XrayOptions.EnableFallback {
// Set fallback
@@ -237,7 +237,7 @@ func buildTrojan(config *conf.ControllerConfig, inbound *coreConf.InboundDetourC
return nil
}
func buildShadowsocks(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
func buildShadowsocks(config *conf.Options, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
inbound.Protocol = "shadowsocks"
settings := &coreConf.ShadowsocksServerConfig{
Cipher: nodeInfo.Cipher,

View File

@@ -11,7 +11,7 @@ import (
"github.com/xtls/xray-core/features/outbound"
)
func (c *Core) AddNode(tag string, info *panel.NodeInfo, config *conf.ControllerConfig) error {
func (c *Core) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error {
inboundConfig, err := buildInbound(config, info, tag)
if err != nil {
return fmt.Errorf("build inbound error: %s", err)

View File

@@ -11,7 +11,7 @@ import (
)
// BuildOutbound build freedom outbund config for addoutbound
func buildOutbound(config *conf2.ControllerConfig, tag string) (*core.OutboundHandlerConfig, error) {
func buildOutbound(config *conf2.Options, tag string) (*core.OutboundHandlerConfig, error) {
outboundDetourConfig := &conf.OutboundDetourConfig{}
outboundDetourConfig.Protocol = "freedom"
outboundDetourConfig.Tag = tag

View File

@@ -39,7 +39,7 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
return &Core{Server: getCore(c.XrayConfig)}, nil
}
func parseConnectionConfig(c *conf.ConnectionConfig) (policy *coreConf.Policy) {
func parseConnectionConfig(c *conf.XrayConnectionConfig) (policy *coreConf.Policy) {
policy = &coreConf.Policy{
StatsUserUplink: true,
StatsUserDownlink: true,