Compare commits

..

46 Commits

Author SHA1 Message Date
wyx2685
39dfd8b6dd fix: xray内核使用自有连接时报空指针BUG 2025-07-23 22:02:48 +09:00
wyx2685
eb51d3e13c fix: sing内核reality server不能正常连接 2025-07-23 21:40:07 +09:00
wyx2685
dadeb6304b update xray core 2025-07-23 02:06:11 +09:00
wyx2685
4c7b9f5eb9 remove trash example 2025-07-19 04:05:59 +09:00
wyx2685
63d88843b6 test: Xray内核删除用户时尝试关闭连接 2025-07-16 15:49:17 +09:00
wyx2685
8d225f811b fix: 用户限速信息不能同步更新BUG 2025-07-13 09:23:06 +09:00
wyx2685
f6b587b275 prefer msgpack for userlist 2025-07-04 11:37:23 +09:00
wyx2685
d9b3d24465 update cores 2025-06-10 01:42:31 +09:00
wyx2685
fc284b3b9f fix: 设备数限制某些情况误拒绝旧IP链接BUG 2025-05-29 00:23:21 +09:00
wyx2685
eb92c4912d fix: reality 2025-05-24 18:53:11 +09:00
wyx2685
a68378670f test: add anytls 2025-05-24 08:33:36 +09:00
wyx2685
d200a3336e test: Add Tuic 2025-04-30 07:50:41 +09:00
wyx2685
6a95d576f1 Merge branch 'dev_new' of github.com:wyx2685/V2bX into dev_new 2025-04-01 06:03:05 +09:00
wyx2685
dbe529bd48 update xray-core v25.3.31 2025-04-01 06:01:52 +09:00
wyx2685
8254e49297 Update Dockerfile 2025-03-06 23:50:24 +08:00
wyx2685
9e8ad2619a Update Dockerfile 2025-03-06 23:48:14 +08:00
wyx2685
d5fff6c433 Update Dockerfile 2025-03-06 23:45:34 +08:00
wyx2685
fe896a61a3 update Dockerfile 2025-03-07 00:38:36 +09:00
wyx2685
a63198c20b Update release.yml 2025-03-06 23:34:14 +08:00
wyx2685
180fb14dd1 update xray core 2025-03-07 00:29:14 +09:00
wyx2685
44db7512d7 use submodule 2025-03-07 00:28:11 +09:00
wyx2685
95263fea99 update sing-box core 2025-03-06 23:54:00 +09:00
wyx2685
0b155bbf89 test: Singbox内核定期释放TCP会话列表内存 2025-03-06 15:57:56 +09:00
wyx2685
1c8c17b067 test: Singbox内核删除用户时尝试关闭该用户所有TCP会话 2025-03-05 19:54:06 +09:00
wyx2685
61606646b9 fix docker build 2025-03-04 21:53:05 +09:00
wyx2685
e2904ad126 fix workflow 2025-03-04 21:28:24 +09:00
wyx2685
903aef1fb5 Update Dockerfile 2025-03-04 19:08:18 +08:00
wyx2685
2c43704090 update xray-core v25.3.3 2025-03-04 05:05:02 +09:00
wyx2685
d71df3a0df hy2内核支持sniff设置 2025-03-02 23:19:27 +09:00
wyx2685
96baa0a99c fix docker workflows 2025-02-20 21:02:37 +09:00
wyx2685
e502624fe4 读取ignore_client_bandwidth参数 2025-02-15 22:09:56 +09:00
wyx2685
484faaf0c3 update sing-box core v1.12.0 2025-02-08 13:38:05 +09:00
wyx2685
ec5dcc3c8a update cores 2025-01-15 14:31:12 +09:00
wyx2685
2f1362067b sing内核支持面板rule规则 2025-01-10 16:33:05 +09:00
wyx2685
c755e9800b fix: sing内核删除用户逻辑不正确BUG 2025-01-05 20:57:18 +09:00
wyx2685
29e0d7e56e fix hy2 core logger nil pointer 2024-12-20 16:46:05 +09:00
wyx2685
08ebbed9fb update xray-core 2024-12-15 10:13:39 +09:00
wyx2685
792841d073 Fix reality 2024-12-13 20:31:06 +09:00
wyx2685
981e59b836 update sing-box core v1.11 2024-12-13 06:22:44 +09:00
wyx2685
7dbe5fda85 fix import 2024-12-02 08:54:47 +09:00
wyx2685
4f9ccdf8db update xray-core&hy2-core 2024-12-02 08:38:23 +09:00
wyx2685
c2d5861d7d splithttp传输方式更名为xhttp 2024-11-26 10:29:12 +09:00
wyx2685
0e29c19f0e fix compile 2024-10-31 17:57:06 +09:00
wyx2685
a1c40bb1c8 update cores 2024-10-31 17:46:31 +09:00
wyx2685
a0de94efff Merge pull request #30 from KorenKrita/dev_new
add sing mux config support
2024-10-31 16:21:22 +08:00
KorenKrita
29928a1135 add sing mux config support 2024-10-31 14:21:02 +08:00
38 changed files with 1489 additions and 1655 deletions

View File

@@ -1,4 +1,5 @@
name: Publish Docker image
on:
workflow_dispatch:
release:
@@ -6,6 +7,7 @@ on:
pull_request:
branches:
- 'dev_new'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository_owner }}/v2bx
@@ -20,40 +22,51 @@ jobs:
- linux/amd64
- linux/arm64
steps:
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
echo "${digest#sha256:}" > "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: digests
name: digests-${{ env.PLATFORM_PAIR }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
@@ -64,31 +77,37 @@ jobs:
- build
steps:
- name: Download digests
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: digests
path: /tmp/digests
pattern: digests-*
merge-multiple: true
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
ls -al
echo docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
cd /tmp/digests
tags=$(echo '${{ steps.meta.outputs.json }}' | jq -cr '.tags | map("-t " + .) | join(" ")')
images=$(printf "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s " $(find . -type f -exec cat {} \;))
echo "Creating manifest with tags: $tags"
echo "Using images: $images"
docker buildx imagetools create $tags $images
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}

View File

@@ -10,14 +10,14 @@ on:
- "**/*.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yml"
- ".github/workflows/release.yml"
pull_request:
types: [opened, synchronize, reopened]
paths:
- "**/*.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yml"
- ".github/workflows/release.yml"
release:
types: [published]
@@ -109,10 +109,11 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23.2'
go-version: '1.24.5'
- name: Get project dependencies
run: go mod download
run: |
go mod download
- name: Get release version
if: ${{ github.event_name == 'release' }}
run: |
@@ -125,13 +126,13 @@ jobs:
run: |
echo "version: $version"
mkdir -p build_assets
go build -v -o build_assets/V2bX -tags "sing xray hysteria2 with_reality_server with_quic with_grpc with_utls with_wireguard with_acme" -trimpath -ldflags "-X 'github.com/InazumaV/V2bX/cmd.version=$version' -s -w -buildid="
go build -v -o build_assets/V2bX -tags "sing xray hysteria2 with_quic with_grpc with_utls with_wireguard with_acme with_gvisor" -trimpath -ldflags "-X 'github.com/InazumaV/V2bX/cmd.version=$version' -s -w -buildid="
- name: Build Mips softfloat V2bX
if: matrix.goarch == 'mips' || matrix.goarch == 'mipsle'
run: |
echo "version: $version"
GOMIPS=softfloat go build -v -o build_assets/V2bX_softfloat -tags "sing xray hysteria2 with_reality_server with_quic with_grpc with_utls with_wireguard with_acme" -trimpath -ldflags "-X 'github.com/InazumaV/V2bX/cmd.version=$version' -s -w -buildid="
GOMIPS=softfloat go build -v -o build_assets/V2bX_softfloat -tags "sing xray hysteria2 with_quic with_grpc with_utls with_wireguard with_acme with_gvisor" -trimpath -ldflags "-X 'github.com/InazumaV/V2bX/cmd.version=$version' -s -w -buildid="
- name: Rename Windows V2bX
if: matrix.goos == 'windows'
run: |

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "sing-box_mod"]
path = sing-box_mod
url = https://github.com/wyx2685/sing-box_mod.git

View File

@@ -1,10 +1,10 @@
# Build go
FROM golang:1.23.2-alpine AS builder
FROM golang:1.24.5-alpine AS builder
WORKDIR /app
COPY . .
ENV CGO_ENABLED=0
RUN go mod download
RUN go build -v -o V2bX -tags "sing xray hysteria2 with_reality_server with_quic with_grpc with_utls with_wireguard with_acme"
RUN go build -v -o V2bX -tags "sing xray hysteria2 with_quic with_grpc with_utls with_wireguard with_acme with_gvisor"
# Release
FROM alpine

View File

@@ -58,7 +58,7 @@ wget -N https://raw.githubusercontent.com/wyx2685/V2bX-script/master/install.sh
## 构建
``` bash
# 通过-tags选项指定要编译的内核 可选 xray sing, hysteria2
go build -v -o ./V2bX -tags "xray sing hysteria2 with_reality_server with_quic with_grpc with_utls with_wireguard with_acme" -trimpath -ldflags "-s -w -buildid="
go build -v -o ./V2bX -tags "xray sing hysteria2 with_quic with_grpc with_utls with_wireguard with_acme" -trimpath -ldflags "-s -w -buildid="
```
## 配置文件及详细使用教程

View File

@@ -32,6 +32,8 @@ type NodeInfo struct {
VAllss *VAllssNode
Shadowsocks *ShadowsocksNode
Trojan *TrojanNode
Tuic *TuicNode
AnyTls *AnyTlsNode
Hysteria *HysteriaNode
Hysteria2 *Hysteria2Node
Common *CommonNode
@@ -100,6 +102,17 @@ type TrojanNode struct {
NetworkSettings json.RawMessage `json:"networkSettings"`
}
type TuicNode struct {
CommonNode
CongestionControl string `json:"congestion_control"`
ZeroRTTHandshake bool `json:"zero_rtt_handshake"`
}
type AnyTlsNode struct {
CommonNode
PaddingScheme []string `json:"padding_scheme,omitempty"`
}
type HysteriaNode struct {
CommonNode
UpMbps int `json:"up_mbps"`
@@ -109,10 +122,11 @@ type HysteriaNode struct {
type Hysteria2Node struct {
CommonNode
UpMbps int `json:"up_mbps"`
DownMbps int `json:"down_mbps"`
ObfsType string `json:"obfs"`
ObfsPassword string `json:"obfs-password"`
Ignore_Client_Bandwidth bool `json:"ignore_client_bandwidth"`
UpMbps int `json:"up_mbps"`
DownMbps int `json:"down_mbps"`
ObfsType string `json:"obfs"`
ObfsPassword string `json:"obfs-password"`
}
type RawDNS struct {
@@ -202,6 +216,24 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
cm = &rsp.CommonNode
node.Trojan = rsp
node.Security = Tls
case "tuic":
rsp := &TuicNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode tuic params error: %s", err)
}
cm = &rsp.CommonNode
node.Tuic = rsp
node.Security = Tls
case "anytls":
rsp := &AnyTlsNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode anytls params error: %s", err)
}
cm = &rsp.CommonNode
node.AnyTls = rsp
node.Security = Tls
case "hysteria":
rsp := &HysteriaNode{}
err = json.Unmarshal(r.Body(), rsp)

View File

@@ -56,6 +56,8 @@ func New(c *conf.ApiConfig) (*Client, error) {
"shadowsocks",
"hysteria",
"hysteria2",
"tuic",
"anytls",
"vless":
default:
return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType)

View File

@@ -2,8 +2,11 @@ package panel
import (
"fmt"
"io"
"strings"
"github.com/goccy/go-json"
"github.com/vmihailenco/msgpack/v5"
)
type OnlineUser struct {
@@ -12,15 +15,14 @@ type OnlineUser struct {
}
type UserInfo struct {
Id int `json:"id"`
Uuid string `json:"uuid"`
SpeedLimit int `json:"speed_limit"`
DeviceLimit int `json:"device_limit"`
Id int `json:"id" msgpack:"id"`
Uuid string `json:"uuid" msgpack:"uuid"`
SpeedLimit int `json:"speed_limit" msgpack:"speed_limit"`
DeviceLimit int `json:"device_limit" msgpack:"device_limit"`
}
type UserListBody struct {
//Msg string `json:"msg"`
Users []UserInfo `json:"users"`
Users []UserInfo `json:"users" msgpack:"users"`
}
type AliveMap struct {
@@ -32,7 +34,8 @@ func (c *Client) GetUserList() ([]UserInfo, error) {
const path = "/api/v1/server/UniProxy/user"
r, err := c.client.R().
SetHeader("If-None-Match", c.userEtag).
ForceContentType("application/json").
SetHeader("X-Response-Format", "msgpack").
SetDoNotParseResponse(true).
Get(path)
if r == nil || r.RawResponse == nil {
return nil, fmt.Errorf("received nil response or raw response")
@@ -47,8 +50,19 @@ func (c *Client) GetUserList() ([]UserInfo, error) {
return nil, err
}
userlist := &UserListBody{}
if err := json.Unmarshal(r.Body(), userlist); err != nil {
return nil, fmt.Errorf("unmarshal user list error: %w", err)
if strings.Contains(r.Header().Get("Content-Type"), "application/x-msgpack") {
decoder := msgpack.NewDecoder(r.RawResponse.Body)
if err := decoder.Decode(userlist); err != nil {
return nil, fmt.Errorf("decode user list error: %w", err)
}
} else {
bodyBytes, err := io.ReadAll(r.RawResponse.Body)
if err != nil {
return nil, fmt.Errorf("read response body error: %w", err)
}
if err := json.Unmarshal(bodyBytes, userlist); err != nil {
return nil, fmt.Errorf("unmarshal user list error: %w", err)
}
}
c.userEtag = r.Header().Get("ETag")
return userlist.Users, nil
@@ -56,23 +70,24 @@ func (c *Client) GetUserList() ([]UserInfo, error) {
// GetUserAlive will fetch the alive_ip count for users
func (c *Client) GetUserAlive() (map[int]int, error) {
c.AliveMap = &AliveMap{}
const path = "/api/v1/server/UniProxy/alivelist"
r, err := c.client.R().
ForceContentType("application/json").
Get(path)
if r == nil || r.RawResponse == nil {
return nil, fmt.Errorf("received nil response or raw response")
}
defer r.RawResponse.Body.Close()
c.AliveMap = &AliveMap{}
if err != nil || r.StatusCode() >= 399 {
c.AliveMap.Alive = make(map[int]int)
return c.AliveMap.Alive, nil
}
if r == nil || r.RawResponse == nil {
fmt.Printf("received nil response or raw response")
c.AliveMap.Alive = make(map[int]int)
return c.AliveMap.Alive, nil
}
defer r.RawResponse.Body.Close()
if err := json.Unmarshal(r.Body(), c.AliveMap); err != nil {
return nil, fmt.Errorf("unmarshal user alive list error: %s", err)
fmt.Printf("unmarshal user alive list error: %s", err)
c.AliveMap.Alive = make(map[int]int)
}
return c.AliveMap.Alive, nil

View File

@@ -5,10 +5,9 @@ import (
)
type SingConfig struct {
LogConfig SingLogConfig `json:"Log"`
NtpConfig SingNtpConfig `json:"NTP"`
EnableConnClear bool `json:"EnableConnClear"`
OriginalPath string `json:"OriginalPath"`
LogConfig SingLogConfig `json:"Log"`
NtpConfig SingNtpConfig `json:"NTP"`
OriginalPath string `json:"OriginalPath"`
}
type SingLogConfig struct {
@@ -33,13 +32,13 @@ func NewSingConfig() *SingConfig {
}
type SingOptions struct {
EnableProxyProtocol bool `json:"EnableProxyProtocol"`
TCPFastOpen bool `json:"EnableTFO"`
SniffEnabled bool `json:"EnableSniff"`
SniffOverrideDestination bool `json:"SniffOverrideDestination"`
EnableDNS bool `json:"EnableDNS"`
DomainStrategy option.DomainStrategy `json:"DomainStrategy"`
SniffOverrideDestination bool `json:"SniffOverrideDestination"`
FallBackConfigs *FallBackConfigForSing `json:"FallBackConfigs"`
Multiplex *MultiplexConfig `json:"MultiplexConfig"`
}
type SingNtpConfig struct {
@@ -59,13 +58,25 @@ type FallBack struct {
ServerPort string `json:"ServerPort"`
}
type MultiplexConfig struct {
Enabled bool `json:"Enable"`
Padding bool `json:"Padding"`
Brutal BrutalOptions `json:"Brutal"`
}
type BrutalOptions struct {
Enabled bool `json:"Enable"`
UpMbps int `json:"UpMbps"`
DownMbps int `json:"DownMbps"`
}
func NewSingOptions() *SingOptions {
return &SingOptions{
EnableDNS: false,
EnableProxyProtocol: false,
TCPFastOpen: false,
SniffEnabled: true,
SniffOverrideDestination: true,
FallBackConfigs: &FallBackConfigForSing{},
Multiplex: &MultiplexConfig{},
}
}

View File

@@ -18,6 +18,8 @@ import (
"github.com/apernet/hysteria/extras/v2/masq"
"github.com/apernet/hysteria/extras/v2/obfs"
"github.com/apernet/hysteria/extras/v2/outbounds"
"github.com/apernet/hysteria/extras/v2/sniff"
eUtils "github.com/apernet/hysteria/extras/v2/utils"
"go.uber.org/zap"
)
@@ -161,6 +163,29 @@ func (n *Hysteria2node) getBandwidthConfig(info *panel.NodeInfo) *server.Bandwid
return band
}
func (n *Hysteria2node) getRequestHook(c *serverConfig) (server.RequestHook, error) {
if c.Sniff.Enable {
s := &sniff.Sniffer{
Timeout: c.Sniff.Timeout,
RewriteDomain: c.Sniff.RewriteDomain,
}
if c.Sniff.TCPPorts != "" {
s.TCPPorts = eUtils.ParsePortUnion(c.Sniff.TCPPorts)
if s.TCPPorts == nil {
return nil, fmt.Errorf("sniff.tcpPorts: invalid port union")
}
}
if c.Sniff.UDPPorts != "" {
s.UDPPorts = eUtils.ParsePortUnion(c.Sniff.UDPPorts)
if s.UDPPorts == nil {
return nil, fmt.Errorf("sniff.udpPorts: invalid port union")
}
}
return s, nil
}
return nil, nil
}
func (n *Hysteria2node) getOutboundConfig(c *serverConfig) (server.Outbound, error) {
// Resolver, ACL, actual outbound are all implemented through the Outbound interface.
// Depending on the config, we build a chain like this:
@@ -299,7 +324,7 @@ func (n *Hysteria2node) getMasqHandler(tlsconfig *server.TLSConfig, conn net.Pac
req.Header.Set("X-Forwarded-For", clientIP)
}
if !c.Masquerade.Proxy.RewriteHost {
if c.Masquerade.Proxy.RewriteHost {
req.Host = req.URL.Host
}
},
@@ -342,7 +367,7 @@ func (n *Hysteria2node) getMasqHandler(tlsconfig *server.TLSConfig, conn net.Pac
s := masq.MasqTCPServer{
QUICPort: extractPortFromAddr(conn.LocalAddr().String()),
HTTPSPort: extractPortFromAddr(c.Masquerade.ListenHTTPS),
Handler: &masqHandlerLogWrapper{H: handler, QUIC: false},
Handler: &masqHandlerLogWrapper{H: handler, QUIC: false, Logger: n.Logger},
TLSConfig: &tls.Config{
Certificates: tlsconfig.Certificates,
GetCertificate: tlsconfig.GetCertificate,
@@ -368,6 +393,10 @@ func (n *Hysteria2node) getHyConfig(info *panel.NodeInfo, config *conf.Options,
if err != nil {
return nil, err
}
sniff, err := n.getRequestHook(c)
if err != nil {
return nil, err
}
Outbound, err := n.getOutboundConfig(c)
if err != nil {
return nil, err
@@ -380,9 +409,10 @@ func (n *Hysteria2node) getHyConfig(info *panel.NodeInfo, config *conf.Options,
TLSConfig: *tls,
QUICConfig: *quic,
Conn: conn,
RequestHook: sniff,
Outbound: Outbound,
BandwidthConfig: *n.getBandwidthConfig(info),
IgnoreClientBandwidth: c.IgnoreClientBandwidth,
IgnoreClientBandwidth: info.Hysteria2.Ignore_Client_Bandwidth,
DisableUDP: c.DisableUDP,
UDPIdleTimeout: c.UDPIdleTimeout,
EventLogger: n.EventLogger,

View File

@@ -6,15 +6,25 @@ import (
"github.com/InazumaV/V2bX/common/counter"
"github.com/InazumaV/V2bX/common/format"
"github.com/InazumaV/V2bX/limiter"
"github.com/apernet/hysteria/core/v2/server"
quic "github.com/apernet/quic-go"
"go.uber.org/zap"
)
var _ server.TrafficLogger = (*HookServer)(nil)
type HookServer struct {
Tag string
logger *zap.Logger
Counter sync.Map
}
func (h *HookServer) TraceStream(stream quic.Stream, stats *server.StreamStats) {
}
func (h *HookServer) UntraceStream(stream quic.Stream) {
}
func (h *HookServer) LogTraffic(id string, tx, rx uint64) (ok bool) {
var c interface{}
var exists bool

View File

@@ -120,11 +120,11 @@ func (l *serverLogger) UDPError(addr net.Addr, uuid string, sessionId uint32, er
func initLogger(logLevel string, logFormat string) (*zap.Logger, error) {
level, ok := logLevelMap[strings.ToLower(logLevel)]
if !ok {
return nil, fmt.Errorf(fmt.Sprintf("unsupported log level: %s\n", logLevel))
return nil, fmt.Errorf("unsupported log level: %s", logLevel)
}
enc, ok := logFormatMap[strings.ToLower(logFormat)]
if !ok {
return nil, fmt.Errorf(fmt.Sprintf("unsupported log format: %s\n", logFormat))
return nil, fmt.Errorf("unsupported log format: %s", logFormat)
}
c := zap.Config{
Level: zap.NewAtomicLevelAt(level),
@@ -137,7 +137,7 @@ func initLogger(logLevel string, logFormat string) (*zap.Logger, error) {
}
logger, err := c.Build()
if err != nil {
return nil, fmt.Errorf(fmt.Sprintf("failed to initialize logger: %s\n", err))
return nil, fmt.Errorf("failed to initialize logger: %s", err)
}
return logger, nil
}

View File

@@ -17,10 +17,12 @@ type serverConfig struct {
QUIC serverConfigQUIC `mapstructure:"quic"`
Bandwidth serverConfigBandwidth `mapstructure:"bandwidth"`
IgnoreClientBandwidth bool `mapstructure:"ignoreClientBandwidth"`
SpeedTest bool `mapstructure:"speedTest"`
DisableUDP bool `mapstructure:"disableUDP"`
UDPIdleTimeout time.Duration `mapstructure:"udpIdleTimeout"`
Auth serverConfigAuth `mapstructure:"auth"`
Resolver serverConfigResolver `mapstructure:"resolver"`
Sniff serverConfigSniff `mapstructure:"sniff"`
ACL serverConfigACL `mapstructure:"acl"`
Outbounds []serverConfigOutboundEntry `mapstructure:"outbounds"`
TrafficStats serverConfigTrafficStats `mapstructure:"trafficStats"`
@@ -112,6 +114,14 @@ type serverConfigResolver struct {
HTTPS serverConfigResolverHTTPS `mapstructure:"https"`
}
type serverConfigSniff struct {
Enable bool `mapstructure:"enable"`
Timeout time.Duration `mapstructure:"timeout"`
RewriteDomain bool `mapstructure:"rewriteDomain"`
TCPPorts string `mapstructure:"tcpPorts"`
UDPPorts string `mapstructure:"udpPorts"`
}
type serverConfigACL struct {
File string `mapstructure:"file"`
Inline []string `mapstructure:"inline"`

View File

@@ -41,7 +41,7 @@ func (h *Hysteria2) AddUsers(p *vCore.AddUsersParams) (added int, err error) {
return len(p.Users), nil
}
func (h *Hysteria2) DelUsers(users []panel.UserInfo, tag string) error {
func (h *Hysteria2) DelUsers(users []panel.UserInfo, tag string, _ *panel.NodeInfo) error {
var wg sync.WaitGroup
for _, user := range users {
wg.Add(1)

View File

@@ -18,7 +18,7 @@ type Core interface {
DelNode(tag string) error
AddUsers(p *AddUsersParams) (added int, err error)
GetUserTraffic(tag, uuid string, reset bool) (up int64, down int64)
DelUsers(users []panel.UserInfo, tag string) error
DelUsers(users []panel.UserInfo, tag string, info *panel.NodeInfo) error
Protocols() []string
Type() string
}

View File

@@ -3,10 +3,11 @@ package core
import (
"errors"
"fmt"
"github.com/hashicorp/go-multierror"
"strings"
"sync"
"github.com/hashicorp/go-multierror"
"github.com/InazumaV/V2bX/api/panel"
"github.com/InazumaV/V2bX/conf"
)
@@ -135,12 +136,12 @@ func (s *Selector) GetUserTraffic(tag, uuid string, reset bool) (up int64, down
return t.(Core).GetUserTraffic(tag, uuid, reset)
}
func (s *Selector) DelUsers(users []panel.UserInfo, tag string) error {
func (s *Selector) DelUsers(users []panel.UserInfo, tag string, info *panel.NodeInfo) error {
t, e := s.nodes.Load(tag)
if !e {
return errors.New("the node is not have")
}
return t.(Core).DelUsers(users, tag)
return t.(Core).DelUsers(users, tag, info)
}
func (s *Selector) Protocols() []string {

View File

@@ -1,85 +0,0 @@
package sing
import (
"bytes"
"github.com/InazumaV/V2bX/api/panel"
"github.com/goccy/go-json"
log "github.com/sirupsen/logrus"
"os"
"strings"
)
func updateDNSConfig(node *panel.NodeInfo) (err error) {
dnsPath := os.Getenv("SING_DNS_PATH")
if len(node.RawDNS.DNSJson) != 0 {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, node.RawDNS.DNSJson, "", " "); err != nil {
return err
}
err = saveDnsConfig(prettyJSON.Bytes(), dnsPath)
} else if len(node.RawDNS.DNSMap) != 0 {
dnsConfig := DNSConfig{
Servers: []map[string]interface{}{
{
"tag": "default",
"address": "https://8.8.8.8/dns-query",
"detour": "direct",
},
},
}
for id, value := range node.RawDNS.DNSMap {
dnsConfig.Servers = append(dnsConfig.Servers,
map[string]interface{}{
"tag": id,
"address": value["address"],
"address_resolver": "default",
"detour": "direct",
},
)
rule := map[string]interface{}{
"server": id,
"disable_cache": true,
}
for _, ruleType := range []string{"domain_suffix", "domain_keyword", "domain_regex", "geosite"} {
var domains []string
for _, v := range value["domains"].([]string) {
split := strings.SplitN(v, ":", 2)
prefix := strings.ToLower(split[0])
if prefix == ruleType || (prefix == "domain" && ruleType == "domain_suffix") {
if len(split) > 1 {
domains = append(domains, split[1])
}
if len(domains) > 0 {
rule[ruleType] = domains
}
}
}
}
dnsConfig.Rules = append(dnsConfig.Rules, rule)
}
dnsConfigJSON, err := json.MarshalIndent(dnsConfig, "", " ")
if err != nil {
log.WithField("err", err).Error("Error marshaling dnsConfig to JSON")
return err
}
err = saveDnsConfig(dnsConfigJSON, dnsPath)
}
return err
}
func saveDnsConfig(dns []byte, dnsPath string) (err error) {
currentData, err := os.ReadFile(dnsPath)
if err != nil {
log.WithField("err", err).Error("Failed to read SING_DNS_PATH")
return err
}
if !bytes.Equal(currentData, dns) {
if err = os.Truncate(dnsPath, 0); err != nil {
log.WithField("err", err).Error("Failed to clear SING DNS PATH file")
}
if err = os.WriteFile(dnsPath, dns, 0644); err != nil {
log.WithField("err", err).Error("Failed to write DNS to SING DNS PATH file")
}
}
return err
}

View File

@@ -2,12 +2,10 @@ package sing
import (
"context"
"io"
"fmt"
"net"
"sync"
"github.com/sagernet/sing-box/common/urltest"
"github.com/InazumaV/V2bX/common/format"
"github.com/InazumaV/V2bX/common/rate"
@@ -17,218 +15,117 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"
)
var _ adapter.ClashServer = (*HookServer)(nil)
var _ adapter.ConnectionTracker = (*HookServer)(nil)
type HookServer struct {
ctx context.Context
urlTestHistory *urltest.HistoryStorage
EnableConnClear bool
counter sync.Map
connClears sync.Map
}
type ConnClear struct {
lock sync.RWMutex
conns map[int]io.Closer
}
func (c *ConnClear) AddConn(cn io.Closer) (key int) {
c.lock.Lock()
defer c.lock.Unlock()
key = len(c.conns)
c.conns[key] = cn
return
}
func (c *ConnClear) DelConn(key int) {
c.lock.Lock()
defer c.lock.Unlock()
delete(c.conns, key)
}
func (c *ConnClear) ClearConn() {
c.lock.Lock()
defer c.lock.Unlock()
for _, c := range c.conns {
c.Close()
}
counter sync.Map //map[string]*counter.TrafficCounter
}
func (h *HookServer) ModeList() []string {
return nil
}
func NewHookServer(ctx context.Context, enableClear bool) *HookServer {
func NewHookServer() *HookServer {
server := &HookServer{
ctx: ctx,
EnableConnClear: enableClear,
counter: sync.Map{},
connClears: sync.Map{},
}
server.urlTestHistory = service.PtrFromContext[urltest.HistoryStorage](ctx)
if server.urlTestHistory == nil {
server.urlTestHistory = urltest.NewHistoryStorage()
counter: sync.Map{},
}
return server
}
func (h *HookServer) Start() error {
return nil
}
func (h *HookServer) Close() error {
h.urlTestHistory.Close()
return nil
}
func (h *HookServer) PreStart() error {
return nil
}
func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapter.InboundContext, _ adapter.Rule) (net.Conn, adapter.Tracker) {
t := &Tracker{}
func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapter.InboundContext, _ adapter.Rule, _ adapter.Outbound) net.Conn {
l, err := limiter.GetLimiter(m.Inbound)
if err != nil {
log.Warn("get limiter for ", m.Inbound, " error: ", err)
return conn, t
}
if l.CheckDomainRule(m.Domain) {
conn.Close()
log.Error("[", m.Inbound, "] ",
"Limited ", m.User, " access to ", m.Domain, " by domain rule")
return conn, t
}
if l.CheckProtocolRule(m.Protocol) {
conn.Close()
log.Error("[", m.Inbound, "] ",
"Limited ", m.User, " use ", m.Domain, " by protocol rule")
return conn, t
return conn
}
taguuid := format.UserTag(m.Inbound, m.User)
ip := m.Source.Addr.String()
if b, r := l.CheckLimit(format.UserTag(m.Inbound, m.User), ip, true, true); r {
if b, r := l.CheckLimit(taguuid, ip, true, true); r {
conn.Close()
log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
return conn, t
return conn
} else if b != nil {
conn = rate.NewConnRateLimiter(conn, b)
}
t.AddLeave(func() {
l.ConnLimiter.DelConnCount(m.User, ip)
})
if h.EnableConnClear {
var key int
cc := &ConnClear{
conns: map[int]io.Closer{
0: conn,
},
if l != nil {
destStr := m.Destination.AddrString()
protocol := m.Protocol
if l.CheckDomainRule(destStr) {
log.Error(fmt.Sprintf(
"User %s access domain %s reject by rule",
m.User,
destStr))
conn.Close()
return conn
}
if v, ok := h.connClears.LoadOrStore(m.Inbound+m.User, cc); ok {
cc = v.(*ConnClear)
key = cc.AddConn(conn)
if len(protocol) != 0 {
if l.CheckProtocolRule(protocol) {
log.Error(fmt.Sprintf(
"User %s access protocol %s reject by rule",
m.User,
protocol))
conn.Close()
return conn
}
}
t.AddLeave(func() {
cc.DelConn(key)
})
}
if c, ok := h.counter.Load(m.Inbound); ok {
return counter.NewConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User)), t
var t *counter.TrafficCounter
if c, ok := h.counter.Load(m.Inbound); !ok {
t = counter.NewTrafficCounter()
h.counter.Store(m.Inbound, t)
} else {
c := counter.NewTrafficCounter()
h.counter.Store(m.Inbound, c)
return counter.NewConnCounter(conn, c.GetCounter(m.User)), t
t = c.(*counter.TrafficCounter)
}
conn = counter.NewConnCounter(conn, t.GetCounter(m.User))
return conn
}
func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn, m adapter.InboundContext, _ adapter.Rule) (N.PacketConn, adapter.Tracker) {
t := &Tracker{}
func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn, m adapter.InboundContext, _ adapter.Rule, _ adapter.Outbound) N.PacketConn {
l, err := limiter.GetLimiter(m.Inbound)
if err != nil {
log.Warn("get limiter for ", m.Inbound, " error: ", err)
return conn, t
}
if l.CheckDomainRule(m.Domain) {
conn.Close()
log.Error("[", m.Inbound, "] ",
"Limited ", m.User, " access to ", m.Domain, " by domain rule")
return conn, t
}
if l.CheckProtocolRule(m.Protocol) {
conn.Close()
log.Error("[", m.Inbound, "] ",
"Limited ", m.User, " use ", m.Domain, " by protocol rule")
return conn, t
return conn
}
ip := m.Source.Addr.String()
if b, r := l.CheckLimit(format.UserTag(m.Inbound, m.User), ip, false, false); r {
taguuid := format.UserTag(m.Inbound, m.User)
if b, r := l.CheckLimit(taguuid, ip, false, false); r {
conn.Close()
log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
return conn, t
return conn
} else if b != nil {
//conn = rate.NewPacketConnCounter(conn, b)
}
if h.EnableConnClear {
var key int
cc := &ConnClear{
conns: map[int]io.Closer{
0: conn,
},
if l != nil {
destStr := m.Destination.AddrString()
protocol := m.Destination.Network()
if l.CheckDomainRule(destStr) {
log.Error(fmt.Sprintf(
"User %s access domain %s reject by rule",
m.User,
destStr))
conn.Close()
return conn
}
if v, ok := h.connClears.LoadOrStore(m.Inbound+m.User, cc); ok {
cc = v.(*ConnClear)
key = cc.AddConn(conn)
if len(protocol) != 0 {
if l.CheckProtocolRule(protocol) {
log.Error(fmt.Sprintf(
"User %s access protocol %s reject by rule",
m.User,
protocol))
conn.Close()
return conn
}
}
t.AddLeave(func() {
cc.DelConn(key)
})
}
if c, ok := h.counter.Load(m.Inbound); ok {
return counter.NewPacketConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User)), t
var t *counter.TrafficCounter
if c, ok := h.counter.Load(m.Inbound); !ok {
t = counter.NewTrafficCounter()
h.counter.Store(m.Inbound, t)
} else {
c := counter.NewTrafficCounter()
h.counter.Store(m.Inbound, c)
return counter.NewPacketConnCounter(conn, c.GetCounter(m.User)), t
}
}
// not need
func (h *HookServer) Mode() string {
return ""
}
func (h *HookServer) StoreSelected() bool {
return false
}
func (h *HookServer) CacheFile() adapter.CacheFile {
return nil
}
func (h *HookServer) HistoryStorage() *urltest.HistoryStorage {
return h.urlTestHistory
}
func (h *HookServer) StoreFakeIP() bool {
return false
}
func (h *HookServer) ClearConn(inbound string, user string) {
if v, ok := h.connClears.Load(inbound + user); ok {
v.(*ConnClear).ClearConn()
h.connClears.Delete(inbound + user)
}
}
type Tracker struct {
l []func()
}
func (t *Tracker) AddLeave(f func()) {
t.l = append(t.l, f)
}
func (t *Tracker) Leave() {
for i := range t.l {
t.l[i]()
t = c.(*counter.TrafficCounter)
}
conn = counter.NewPacketConnCounter(conn, t.GetCounter(m.User))
return conn
}

View File

@@ -13,9 +13,9 @@ import (
"github.com/InazumaV/V2bX/api/panel"
"github.com/InazumaV/V2bX/conf"
"github.com/goccy/go-json"
"github.com/sagernet/sing-box/inbound"
"github.com/sagernet/sing-box/option"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/json/badoption"
)
type HttpNetworkConfig struct {
@@ -54,20 +54,23 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
if err != nil {
return option.Inbound{}, fmt.Errorf("the listen ip not vail")
}
var domainStrategy option.DomainStrategy
if c.SingOptions.EnableDNS {
domainStrategy = c.SingOptions.DomainStrategy
}
listen := option.ListenOptions{
Listen: (*option.ListenAddress)(&addr),
ListenPort: uint16(info.Common.ServerPort),
ProxyProtocol: c.SingOptions.EnableProxyProtocol,
TCPFastOpen: c.SingOptions.TCPFastOpen,
InboundOptions: option.InboundOptions{
SniffEnabled: c.SingOptions.SniffEnabled,
SniffOverrideDestination: c.SingOptions.SniffOverrideDestination,
DomainStrategy: domainStrategy,
},
Listen: (*badoption.Addr)(&addr),
ListenPort: uint16(info.Common.ServerPort),
TCPFastOpen: c.SingOptions.TCPFastOpen,
}
var multiplex *option.InboundMultiplexOptions
if c.SingOptions.Multiplex != nil {
multiplexOption := option.InboundMultiplexOptions{
Enabled: c.SingOptions.Multiplex.Enabled,
Padding: c.SingOptions.Multiplex.Padding,
Brutal: &option.BrutalOptions{
Enabled: c.SingOptions.Multiplex.Brutal.Enabled,
UpMbps: c.SingOptions.Multiplex.Brutal.UpMbps,
DownMbps: c.SingOptions.Multiplex.Brutal.DownMbps,
},
}
multiplex = &multiplexOption
}
var tls option.InboundTLSOptions
switch info.Security {
@@ -107,7 +110,7 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
ServerPort: uint16(port),
},
},
MaxTimeDifference: option.Duration(mtd),
MaxTimeDifference: badoption.Duration(mtd),
}
}
in := option.Inbound{
@@ -150,7 +153,7 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
var (
path string
ed int
headers map[string]option.Listable[string]
headers map[string]badoption.Listable[string]
)
if len(n.NetworkSettings) != 0 {
network := WsNetworkConfig{}
@@ -165,9 +168,9 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
}
path = u.Path
ed, _ = strconv.Atoi(u.Query().Get("ed"))
headers = make(map[string]option.Listable[string], len(network.Headers))
headers = make(map[string]badoption.Listable[string], len(network.Headers))
for k, v := range network.Headers {
headers[k] = option.Listable[string]{
headers[k] = badoption.Listable[string]{
v,
}
}
@@ -204,21 +207,23 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
}
if info.Type == "vless" {
in.Type = "vless"
in.VLESSOptions = option.VLESSInboundOptions{
in.Options = &option.VLESSInboundOptions{
ListenOptions: listen,
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &tls,
},
Transport: &t,
Multiplex: multiplex,
}
} else {
in.Type = "vmess"
in.VMessOptions = option.VMessInboundOptions{
in.Options = &option.VMessInboundOptions{
ListenOptions: listen,
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &tls,
},
Transport: &t,
Multiplex: multiplex,
}
}
case "shadowsocks":
@@ -233,20 +238,22 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
default:
keyLength = 16
}
in.ShadowsocksOptions = option.ShadowsocksInboundOptions{
ssoption := &option.ShadowsocksInboundOptions{
ListenOptions: listen,
Method: n.Cipher,
Multiplex: multiplex,
}
p := make([]byte, keyLength)
_, _ = rand.Read(p)
randomPasswd := string(p)
if strings.Contains(n.Cipher, "2022") {
in.ShadowsocksOptions.Password = n.ServerKey
ssoption.Password = n.ServerKey
randomPasswd = base64.StdEncoding.EncodeToString([]byte(randomPasswd))
}
in.ShadowsocksOptions.Users = []option.ShadowsocksUser{{
ssoption.Users = []option.ShadowsocksUser{{
Password: randomPasswd,
}}
in.Options = ssoption
case "trojan":
n := info.Trojan
t := option.V2RayTransportOptions{
@@ -259,7 +266,7 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
var (
path string
ed int
headers map[string]option.Listable[string]
headers map[string]badoption.Listable[string]
)
if len(n.NetworkSettings) != 0 {
network := WsNetworkConfig{}
@@ -274,9 +281,9 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
}
path = u.Path
ed, _ = strconv.Atoi(u.Query().Get("ed"))
headers = make(map[string]option.Listable[string], len(network.Headers))
headers = make(map[string]badoption.Listable[string], len(network.Headers))
for k, v := range network.Headers {
headers[k] = option.Listable[string]{
headers[k] = badoption.Listable[string]{
v,
}
}
@@ -302,19 +309,20 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
t.Type = ""
}
in.Type = "trojan"
in.TrojanOptions = option.TrojanInboundOptions{
trojanoption := &option.TrojanInboundOptions{
ListenOptions: listen,
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &tls,
},
Transport: &t,
Multiplex: multiplex,
}
if c.SingOptions.FallBackConfigs != nil {
// fallback handling
fallback := c.SingOptions.FallBackConfigs.FallBack
fallbackPort, err := strconv.Atoi(fallback.ServerPort)
if err == nil {
in.TrojanOptions.Fallback = &option.ServerOptions{
trojanoption.Fallback = &option.ServerOptions{
Server: fallback.Server,
ServerPort: uint16(fallbackPort),
}
@@ -322,12 +330,33 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
fallbackForALPNMap := c.SingOptions.FallBackConfigs.FallBackForALPN
fallbackForALPN := make(map[string]*option.ServerOptions, len(fallbackForALPNMap))
if err := processFallback(c, fallbackForALPN); err == nil {
in.TrojanOptions.FallbackForALPN = fallbackForALPN
trojanoption.FallbackForALPN = fallbackForALPN
}
}
in.Options = trojanoption
case "tuic":
in.Type = "tuic"
tls.ALPN = append(tls.ALPN, "h3")
in.Options = &option.TUICInboundOptions{
ListenOptions: listen,
CongestionControl: info.Tuic.CongestionControl,
ZeroRTTHandshake: info.Tuic.ZeroRTTHandshake,
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &tls,
},
}
case "anytls":
in.Type = "anytls"
in.Options = &option.AnyTLSInboundOptions{
ListenOptions: listen,
PaddingScheme: info.AnyTls.PaddingScheme,
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &tls,
},
}
case "hysteria":
in.Type = "hysteria"
in.HysteriaOptions = option.HysteriaInboundOptions{
in.Options = &option.HysteriaInboundOptions{
ListenOptions: listen,
UpMbps: info.Hysteria.UpMbps,
DownMbps: info.Hysteria.DownMbps,
@@ -350,11 +379,12 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
Password: info.Hysteria2.ObfsType,
}
}
in.Hysteria2Options = option.Hysteria2InboundOptions{
ListenOptions: listen,
UpMbps: info.Hysteria2.UpMbps,
DownMbps: info.Hysteria2.DownMbps,
Obfs: obfs,
in.Options = &option.Hysteria2InboundOptions{
ListenOptions: listen,
UpMbps: info.Hysteria2.UpMbps,
DownMbps: info.Hysteria2.DownMbps,
IgnoreClientBandwidth: info.Hysteria2.Ignore_Client_Bandwidth,
Obfs: obfs,
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &tls,
},
@@ -364,32 +394,20 @@ func getInboundOptions(tag string, info *panel.NodeInfo, c *conf.Options) (optio
}
func (b *Sing) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error {
err := updateDNSConfig(info)
if err != nil {
return fmt.Errorf("build dns error: %s", err)
}
c, err := getInboundOptions(tag, info, config)
if err != nil {
return err
}
in, err := inbound.New(
in := b.box.Inbound()
err = in.Create(
b.ctx,
b.box.Router(),
b.logFactory.NewLogger(F.ToString("inbound/", c.Type, "[", tag, "]")),
tag,
c,
nil,
c.Type,
c.Options,
)
if err != nil {
return fmt.Errorf("init inbound error %s", err)
}
err = in.Start()
if err != nil {
return fmt.Errorf("start inbound error: %s", err)
}
b.inbounds[tag] = in
err = b.router.AddInbound(in)
if err != nil {
return fmt.Errorf("add inbound error: %s", err)
}
@@ -397,11 +415,8 @@ func (b *Sing) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) e
}
func (b *Sing) DelNode(tag string) error {
err := b.inbounds[tag].Close()
if err != nil {
return fmt.Errorf("close inbound error: %s", err)
}
err = b.router.DelInbound(tag)
in := b.box.Inbound()
err := in.Remove(tag)
if err != nil {
return fmt.Errorf("delete inbound error: %s", err)
}

View File

@@ -5,14 +5,15 @@ import (
"fmt"
"os"
"github.com/sagernet/sing-box/include"
"github.com/sagernet/sing-box/log"
"github.com/InazumaV/V2bX/conf"
vCore "github.com/InazumaV/V2bX/core"
"github.com/goccy/go-json"
box "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/json"
)
var _ vCore.Core = (*Sing)(nil)
@@ -28,7 +29,6 @@ type Sing struct {
hookServer *HookServer
router adapter.Router
logFactory log.Factory
inbounds map[string]adapter.Inbound
}
func init() {
@@ -36,13 +36,15 @@ func init() {
}
func New(c *conf.CoreConfig) (vCore.Core, error) {
ctx := context.Background()
ctx = box.Context(ctx, include.InboundRegistry(), include.OutboundRegistry(), include.EndpointRegistry(), include.DNSTransportRegistry(), include.ServiceRegistry())
options := option.Options{}
if len(c.SingConfig.OriginalPath) != 0 {
data, err := os.ReadFile(c.SingConfig.OriginalPath)
if err != nil {
return nil, fmt.Errorf("read original config error: %s", err)
}
err = json.Unmarshal(data, &options)
options, err = json.UnmarshalExtendedContext[option.Options](ctx, data)
if err != nil {
return nil, fmt.Errorf("unmarshal original config error: %s", err)
}
@@ -63,21 +65,20 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
}
os.Setenv("SING_DNS_PATH", "")
b, err := box.New(box.Options{
Context: context.Background(),
Context: ctx,
Options: options,
})
if err != nil {
return nil, err
}
hs := NewHookServer(b.Router().GetCtx(), c.SingConfig.EnableConnClear)
b.Router().SetClashServer(hs)
hs := NewHookServer()
b.Router().AppendTracker(hs)
return &Sing{
ctx: b.Router().GetCtx(),
box: b,
hookServer: hs,
router: b.Router(),
logFactory: b.LogFactory(),
inbounds: make(map[string]adapter.Inbound),
}, nil
}
@@ -95,6 +96,8 @@ func (b *Sing) Protocols() []string {
"vless",
"shadowsocks",
"trojan",
"tuic",
"anytls",
"hysteria",
"hysteria2",
}

View File

@@ -7,33 +7,42 @@ import (
"github.com/InazumaV/V2bX/api/panel"
"github.com/InazumaV/V2bX/common/counter"
"github.com/InazumaV/V2bX/core"
"github.com/sagernet/sing-box/inbound"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/protocol/anytls"
"github.com/sagernet/sing-box/protocol/hysteria"
"github.com/sagernet/sing-box/protocol/hysteria2"
"github.com/sagernet/sing-box/protocol/shadowsocks"
"github.com/sagernet/sing-box/protocol/trojan"
"github.com/sagernet/sing-box/protocol/tuic"
"github.com/sagernet/sing-box/protocol/vless"
"github.com/sagernet/sing-box/protocol/vmess"
)
func (b *Sing) AddUsers(p *core.AddUsersParams) (added int, err error) {
in, found := b.box.Inbound().Get(p.Tag)
if !found {
return 0, errors.New("the inbound not found")
}
switch p.NodeInfo.Type {
case "vmess", "vless":
if p.NodeInfo.Type == "vless" {
us := make([]option.VLESSUser, len(p.Users))
for i := range p.Users {
us[i] = option.VLESSUser{
Name: p.Users[i].Uuid,
Flow: p.VAllss.Flow,
UUID: p.Users[i].Uuid,
}
case "vless":
us := make([]option.VLESSUser, len(p.Users))
for i := range p.Users {
us[i] = option.VLESSUser{
Name: p.Users[i].Uuid,
Flow: p.VAllss.Flow,
UUID: p.Users[i].Uuid,
}
err = b.inbounds[p.Tag].(*inbound.VLESS).AddUsers(us)
} else {
us := make([]option.VMessUser, len(p.Users))
for i := range p.Users {
us[i] = option.VMessUser{
Name: p.Users[i].Uuid,
UUID: p.Users[i].Uuid,
}
}
err = b.inbounds[p.Tag].(*inbound.VMess).AddUsers(us)
}
err = in.(*vless.Inbound).AddUsers(us)
case "vmess":
us := make([]option.VMessUser, len(p.Users))
for i := range p.Users {
us[i] = option.VMessUser{
Name: p.Users[i].Uuid,
UUID: p.Users[i].Uuid,
}
}
err = in.(*vmess.Inbound).AddUsers(us)
case "shadowsocks":
us := make([]option.ShadowsocksUser, len(p.Users))
for i := range p.Users {
@@ -49,7 +58,7 @@ func (b *Sing) AddUsers(p *core.AddUsersParams) (added int, err error) {
Password: password,
}
}
err = b.inbounds[p.Tag].(*inbound.ShadowsocksMulti).AddUsers(us)
err = in.(*shadowsocks.MultiInbound).AddUsers(us)
case "trojan":
us := make([]option.TrojanUser, len(p.Users))
for i := range p.Users {
@@ -58,7 +67,19 @@ func (b *Sing) AddUsers(p *core.AddUsersParams) (added int, err error) {
Password: p.Users[i].Uuid,
}
}
err = b.inbounds[p.Tag].(*inbound.Trojan).AddUsers(us)
err = in.(*trojan.Inbound).AddUsers(us)
case "tuic":
us := make([]option.TUICUser, len(p.Users))
id := make([]int, len(p.Users))
for i := range p.Users {
us[i] = option.TUICUser{
Name: p.Users[i].Uuid,
UUID: p.Users[i].Uuid,
Password: p.Users[i].Uuid,
}
id[i] = p.Users[i].Id
}
err = in.(*tuic.Inbound).AddUsers(us, id)
case "hysteria":
us := make([]option.HysteriaUser, len(p.Users))
for i := range p.Users {
@@ -67,7 +88,7 @@ func (b *Sing) AddUsers(p *core.AddUsersParams) (added int, err error) {
AuthString: p.Users[i].Uuid,
}
}
err = b.inbounds[p.Tag].(*inbound.Hysteria).AddUsers(us)
err = in.(*hysteria.Inbound).AddUsers(us)
case "hysteria2":
us := make([]option.Hysteria2User, len(p.Users))
id := make([]int, len(p.Users))
@@ -78,7 +99,16 @@ func (b *Sing) AddUsers(p *core.AddUsersParams) (added int, err error) {
}
id[i] = p.Users[i].Id
}
err = b.inbounds[p.Tag].(*inbound.Hysteria2).AddUsers(us, id)
err = in.(*hysteria2.Inbound).AddUsers(us, id)
case "anytls":
us := make([]option.AnyTLSUser, len(p.Users))
for i := range p.Users {
us[i] = option.AnyTLSUser{
Name: p.Users[i].Uuid,
Password: p.Users[i].Uuid,
}
}
err = in.(*anytls.Inbound).AddUsers(us)
}
if err != nil {
return 0, err
@@ -103,29 +133,32 @@ type UserDeleter interface {
DelUsers(uuid []string) error
}
func (b *Sing) DelUsers(users []panel.UserInfo, tag string) error {
func (b *Sing) DelUsers(users []panel.UserInfo, tag string, info *panel.NodeInfo) error {
var del UserDeleter
if i, ok := b.inbounds[tag]; ok {
switch i.Type() {
if i, found := b.box.Inbound().Get(tag); found {
switch info.Type {
case "vmess":
del = i.(*inbound.VMess)
del = i.(*vmess.Inbound)
case "vless":
del = i.(*inbound.VLESS)
del = i.(*vless.Inbound)
case "shadowsocks":
del = i.(*inbound.ShadowsocksMulti)
del = i.(*shadowsocks.MultiInbound)
case "trojan":
del = i.(*inbound.Trojan)
del = i.(*trojan.Inbound)
case "tuic":
del = i.(*tuic.Inbound)
case "hysteria":
del = i.(*inbound.Hysteria)
del = i.(*hysteria.Inbound)
case "hysteria2":
del = i.(*inbound.Hysteria2)
del = i.(*hysteria2.Inbound)
case "anytls":
del = i.(*anytls.Inbound)
}
} else {
return errors.New("the inbound not found")
}
uuids := make([]string, len(users))
for i := range users {
b.hookServer.ClearConn(tag, users[i].Uuid)
uuids[i] = users[i].Uuid
}
err := del.DelUsers(uuids)

View File

@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.31.0
// protoc v4.23.4
// protoc-gen-go v1.35.1
// protoc v3.21.12
// source: config.proto
package dispatcher
@@ -28,11 +28,9 @@ type SessionConfig struct {
func (x *SessionConfig) Reset() {
*x = SessionConfig{}
if protoimpl.UnsafeEnabled {
mi := &file_config_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
mi := &file_config_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *SessionConfig) String() string {
@@ -43,7 +41,7 @@ func (*SessionConfig) ProtoMessage() {}
func (x *SessionConfig) ProtoReflect() protoreflect.Message {
mi := &file_config_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
@@ -68,11 +66,9 @@ type Config struct {
func (x *Config) Reset() {
*x = Config{}
if protoimpl.UnsafeEnabled {
mi := &file_config_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
mi := &file_config_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *Config) String() string {
@@ -83,7 +79,7 @@ func (*Config) ProtoMessage() {}
func (x *Config) ProtoReflect() protoreflect.Message {
mi := &file_config_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
@@ -140,7 +136,7 @@ func file_config_proto_rawDescGZIP() []byte {
}
var file_config_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_config_proto_goTypes = []interface{}{
var file_config_proto_goTypes = []any{
(*SessionConfig)(nil), // 0: v2bx.core.app.dispatcher.SessionConfig
(*Config)(nil), // 1: v2bx.core.app.dispatcher.Config
}
@@ -158,32 +154,6 @@ func file_config_proto_init() {
if File_config_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_config_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SessionConfig); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_config_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Config); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{

View File

@@ -39,17 +39,21 @@ type cachedReader struct {
cache buf.MultiBuffer
}
func (r *cachedReader) Cache(b *buf.Buffer) {
mb, _ := r.reader.ReadMultiBufferTimeout(time.Millisecond * 100)
func (r *cachedReader) Cache(b *buf.Buffer, deadline time.Duration) error {
mb, err := r.reader.ReadMultiBufferTimeout(deadline)
if err != nil {
return err
}
r.Lock()
if !mb.IsEmpty() {
r.cache, _ = buf.MergeMulti(r.cache, mb)
}
b.Clear()
rawBytes := b.Extend(buf.Size)
rawBytes := b.Extend(min(r.cache.Len(), b.Cap()))
n := r.cache.Copy(rawBytes)
b.Resize(0, int32(n))
r.Unlock()
return nil
}
func (r *cachedReader) readInternal() buf.MultiBuffer {
@@ -98,18 +102,18 @@ type DefaultDispatcher struct {
router routing.Router
policy policy.Manager
stats stats.Manager
dns dns.Client
fdns dns.FakeDNSEngine
Wm *WriterManager
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
d := new(DefaultDispatcher)
if err := core.RequireFeatures(ctx, func(om outbound.Manager, router routing.Router, pm policy.Manager, sm stats.Manager, dc dns.Client) error {
core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) {
core.OptionalFeatures(ctx, func(fdns dns.FakeDNSEngine) {
d.fdns = fdns
})
return d.Init(config.(*Config), om, router, pm, sm, dc)
return d.Init(config.(*Config), om, router, pm, sm)
}); err != nil {
return nil, err
}
@@ -118,12 +122,14 @@ func init() {
}
// Init initializes DefaultDispatcher.
func (d *DefaultDispatcher) Init(config *Config, om outbound.Manager, router routing.Router, pm policy.Manager, sm stats.Manager, dns dns.Client) error {
func (d *DefaultDispatcher) Init(config *Config, om outbound.Manager, router routing.Router, pm policy.Manager, sm stats.Manager) error {
d.ohm = om
d.router = router
d.policy = pm
d.stats = sm
d.dns = dns
d.Wm = &WriterManager{
writers: make(map[string]map[*ManagedWriter]struct{}),
}
return nil
}
@@ -187,6 +193,13 @@ func (d *DefaultDispatcher) getLink(ctx context.Context, network net.Network) (*
common.Interrupt(inboundLink.Reader)
return nil, nil, nil, errors.New("Limited ", user.Email, " by conn or ip")
}
managedWriter := &ManagedWriter{
writer: uplinkWriter,
email: user.Email,
manager: d.Wm,
}
d.Wm.AddWriter(managedWriter)
inboundLink.Writer = managedWriter
if w != nil {
inboundLink.Writer = rate.NewRateLimitWriter(inboundLink.Writer, w)
outboundLink.Writer = rate.NewRateLimitWriter(outboundLink.Writer, w)
@@ -358,7 +371,7 @@ func (d *DefaultDispatcher) DispatchLink(ctx context.Context, destination net.De
protocol = resComp.ProtocolForDomainResult()
}
isFakeIP := false
if fkr0, ok := d.fdns.(dns.FakeDNSEngineRev0); ok && ob.Target.Address.Family().IsIP() && fkr0.IsIPInIPPool(ob.Target.Address) {
if fkr0, ok := d.fdns.(dns.FakeDNSEngineRev0); ok && fkr0.IsIPInIPPool(ob.Target.Address) {
isFakeIP = true
}
if sniffingRequest.RouteOnly && protocol != "fakedns" && protocol != "fakedns+others" && !isFakeIP {
@@ -366,7 +379,6 @@ func (d *DefaultDispatcher) DispatchLink(ctx context.Context, destination net.De
} else {
ob.Target = destination
}
destination.Address.Family()
}
d.routedDispatch(ctx, outbound, destination, nil, content.Protocol)
}
@@ -375,7 +387,7 @@ func (d *DefaultDispatcher) DispatchLink(ctx context.Context, destination net.De
}
func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, network net.Network) (SniffResult, error) {
payload := buf.New()
payload := buf.NewWithSize(32767)
defer payload.Release()
sniffer := NewSniffer(ctx)
@@ -387,26 +399,36 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, netw
}
contentResult, contentErr := func() (SniffResult, error) {
cacheDeadline := 200 * time.Millisecond
totalAttempt := 0
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
totalAttempt++
if totalAttempt > 2 {
return nil, errSniffingTimeout
cachingStartingTimeStamp := time.Now()
err := cReader.Cache(payload, cacheDeadline)
if err != nil {
return nil, err
}
cachingTimeElapsed := time.Since(cachingStartingTimeStamp)
cacheDeadline -= cachingTimeElapsed
cReader.Cache(payload)
if !payload.IsEmpty() {
result, err := sniffer.Sniff(ctx, payload.Bytes(), network)
if err != common.ErrNoClue {
switch err {
case common.ErrNoClue: // No Clue: protocol not matches, and sniffer cannot determine whether there will be a match or not
totalAttempt++
case protocol.ErrProtoNeedMoreData: // Protocol Need More Data: protocol matches, but need more data to complete sniffing
// in this case, do not add totalAttempt(allow to read until timeout)
default:
return result, err
}
} else {
totalAttempt++
}
if payload.IsFull() {
return nil, errUnknownContent
if totalAttempt >= 2 || cacheDeadline <= 0 {
return nil, errSniffingTimeout
}
}
}
@@ -423,29 +445,10 @@ func sniffer(ctx context.Context, cReader *cachedReader, metadataOnly bool, netw
func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.Link, destination net.Destination, l *limiter.Limiter, protocol string) {
outbounds := session.OutboundsFromContext(ctx)
ob := outbounds[len(outbounds)-1]
if hosts, ok := d.dns.(dns.HostsLookup); ok && destination.Address.Family().IsDomain() {
proxied := hosts.LookupHosts(ob.Target.String())
if proxied != nil {
ro := ob.RouteTarget == destination
destination.Address = *proxied
if ro {
ob.RouteTarget = destination
} else {
ob.Target = destination
}
}
}
sessionInbound := session.InboundFromContext(ctx)
if sessionInbound.User != nil {
if l != nil {
// del connect count
if destination.Network == net.Network_TCP {
defer func() {
l.ConnLimiter.DelConnCount(sessionInbound.User.Email, sessionInbound.Source.Address.IP().String())
}()
}
} else {
if l == nil {
var err error
l, err = limiter.GetLimiter(sessionInbound.Tag)
if err != nil {
@@ -504,7 +507,11 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
outTag := route.GetOutboundTag()
if h := d.ohm.GetHandler(outTag); h != nil {
isPickRoute = 2
errors.LogInfo(ctx, "taking detour [", outTag, "] for [", destination, "]")
if route.GetRuleTag() == "" {
errors.LogInfo(ctx, "taking detour [", outTag, "] for [", destination, "]")
} else {
errors.LogInfo(ctx, "Hit route rule: [", route.GetRuleTag(), "] so taking detour [", outTag, "] for [", destination, "]")
}
handler = h
} else {
errors.LogWarning(ctx, "non existing outTag: ", outTag)
@@ -514,10 +521,6 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
}
}
if handler == nil {
handler = d.ohm.GetHandler(inTag)
}
if handler == nil {
handler = d.ohm.GetDefaultHandler()
}
@@ -529,6 +532,7 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport.
return
}
ob.Tag = handler.Tag()
if accessMessage := log.AccessMessageFromContext(ctx); accessMessage != nil {
if tag := handler.Tag(); tag != "" {
if inTag == "" {

View File

@@ -0,0 +1,61 @@
package dispatcher
import (
sync "sync"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
)
type WriterManager struct {
writers map[string]map[*ManagedWriter]struct{}
mu sync.Mutex
}
func (m *WriterManager) AddWriter(writer *ManagedWriter) {
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.writers[writer.email]; !exists {
m.writers[writer.email] = make(map[*ManagedWriter]struct{})
}
m.writers[writer.email][writer] = struct{}{}
}
func (m *WriterManager) RemoveWriter(writer *ManagedWriter) {
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.writers[writer.email]; !exists {
return
}
delete(m.writers[writer.email], writer)
}
func (m *WriterManager) RemoveWritersForUser(email string) {
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.writers[email]; !exists {
return
}
for writer := range m.writers[email] {
delete(m.writers[email], writer)
common.Close(writer.writer)
}
delete(m.writers, email)
}
type ManagedWriter struct {
writer buf.Writer
email string
manager *WriterManager
}
func (w *ManagedWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
return w.writer.WriteMultiBuffer(mb)
}
func (w *ManagedWriter) Close() error {
w.manager.RemoveWriter(w)
return common.Close(w.writer)
}

View File

@@ -41,6 +41,7 @@ import (
_ "github.com/xtls/xray-core/proxy/http"
_ "github.com/xtls/xray-core/proxy/loopback"
_ "github.com/xtls/xray-core/proxy/shadowsocks"
_ "github.com/xtls/xray-core/proxy/shadowsocks_2022"
_ "github.com/xtls/xray-core/proxy/socks"
_ "github.com/xtls/xray-core/proxy/trojan"
_ "github.com/xtls/xray-core/proxy/vless/inbound"
@@ -51,13 +52,10 @@ import (
//_ "github.com/xtls/xray-core/proxy/wireguard"
// Transports
//_ "github.com/xtls/xray-core/transport/internet/domainsocket"
_ "github.com/xtls/xray-core/transport/internet/grpc"
_ "github.com/xtls/xray-core/transport/internet/http"
//_ "github.com/xtls/xray-core/transport/internet/kcp"
//_ "github.com/xtls/xray-core/transport/internet/quic"
_ "github.com/xtls/xray-core/transport/internet/kcp"
_ "github.com/xtls/xray-core/transport/internet/reality"
_ "github.com/xtls/xray-core/transport/internet/splithttp"
_ "github.com/xtls/xray-core/transport/internet/tcp"
_ "github.com/xtls/xray-core/transport/internet/tls"
_ "github.com/xtls/xray-core/transport/internet/udp"
@@ -70,5 +68,5 @@ import (
_ "github.com/xtls/xray-core/transport/internet/headers/tls"
_ "github.com/xtls/xray-core/transport/internet/headers/utp"
_ "github.com/xtls/xray-core/transport/internet/headers/wechat"
//_ "github.com/xtls/xray-core/transport/internet/headers/wireguard"
_ "github.com/xtls/xray-core/transport/internet/headers/wireguard"
)

View File

@@ -204,12 +204,7 @@ func buildV2ray(config *conf.Options, nodeInfo *panel.NodeInfo, inbound *coreCon
return fmt.Errorf("unmarshal ws settings error: %s", err)
}
case "grpc":
err := json.Unmarshal(v.NetworkSettings, &inbound.StreamSetting.GRPCConfig)
if err != nil {
return fmt.Errorf("unmarshal grpc settings error: %s", err)
}
case "http":
err := json.Unmarshal(v.NetworkSettings, &inbound.StreamSetting.HTTPSettings)
err := json.Unmarshal(v.NetworkSettings, &inbound.StreamSetting.GRPCSettings)
if err != nil {
return fmt.Errorf("unmarshal grpc settings error: %s", err)
}
@@ -218,10 +213,10 @@ func buildV2ray(config *conf.Options, nodeInfo *panel.NodeInfo, inbound *coreCon
if err != nil {
return fmt.Errorf("unmarshal httpupgrade settings error: %s", err)
}
case "splithttp":
case "splithttp", "xhttp":
err := json.Unmarshal(v.NetworkSettings, &inbound.StreamSetting.SplitHTTPSettings)
if err != nil {
return fmt.Errorf("unmarshal splithttp settings error: %s", err)
return fmt.Errorf("unmarshal xhttp settings error: %s", err)
}
default:
return errors.New("the network type is not vail")
@@ -267,7 +262,7 @@ func buildTrojan(config *conf.Options, nodeInfo *panel.NodeInfo, inbound *coreCo
return fmt.Errorf("unmarshal ws settings error: %s", err)
}
case "grpc":
err := json.Unmarshal(v.NetworkSettings, &inbound.StreamSetting.GRPCConfig)
err := json.Unmarshal(v.NetworkSettings, &inbound.StreamSetting.GRPCSettings)
if err != nil {
return fmt.Errorf("unmarshal grpc settings error: %s", err)
}

View File

@@ -41,9 +41,7 @@ func buildSSUser(tag string, userInfo *panel.UserInfo, cypher string, serverKey
case "2022-blake3-chacha20-poly1305":
keyLength = 32
}
ssAccount := &shadowsocks_2022.User{
Level: 0,
Email: format.UserTag(tag, userInfo.Uuid),
ssAccount := &shadowsocks_2022.Account{
Key: base64.StdEncoding.EncodeToString([]byte(userInfo.Uuid[:keyLength])),
}
return &protocol.User{

View File

@@ -27,7 +27,7 @@ func (c *Xray) GetUserManager(tag string) (proxy.UserManager, error) {
return userManager, nil
}
func (c *Xray) DelUsers(users []panel.UserInfo, tag string) error {
func (c *Xray) DelUsers(users []panel.UserInfo, tag string, _ *panel.NodeInfo) error {
userManager, err := c.GetUserManager(tag)
if err != nil {
return fmt.Errorf("get user manager error: %s", err)
@@ -43,6 +43,7 @@ func (c *Xray) DelUsers(users []panel.UserInfo, tag string) error {
down = "user>>>" + user + ">>>traffic>>>downlink"
c.shm.UnregisterCounter(up)
c.shm.UnregisterCounter(down)
c.dispatcher.Wm.RemoveWritersForUser(user)
}
return nil
}

View File

@@ -7,14 +7,15 @@
{
"Type": "sing",
"Log": {
"Level": "error",
"Level": "info",
"Timestamp": true
},
"NTP": {
"Enable": true,
"Enable": false,
"Server": "time.apple.com",
"ServerPort": 0
}
},
"OriginalPath": "/etc/V2bX/sing_origin.json"
}
],
"Nodes": [
@@ -27,20 +28,18 @@
"Timeout": 30,
"ListenIP": "0.0.0.0",
"SendIP": "0.0.0.0",
"EnableProxyProtocol": false,
"EnableDNS": true,
"DomainStrategy": "ipv4_only",
"LimitConfig": {
"EnableRealtime": false,
"SpeedLimit": 0,
"IPLimit": 0,
"ConnLimit": 0,
"EnableDynamicSpeedLimit": false,
"DynamicSpeedLimitConfig": {
"Periodic": 60,
"Traffic": 1000,
"SpeedLimit": 100,
"ExpireTime": 60
"DeviceOnlineMinTraffic": 200,
"TCPFastOpen": false,
"SniffEnabled": true,
"CertConfig": {
"CertMode": "self",
"RejectUnknownSni": false,
"CertDomain": "example.com",
"CertFile": "/etc/V2bX/fullchain.cer",
"KeyFile": "/etc/V2bX/cert.key",
"Provider": "cloudflare",
"DNSEnv": {
"EnvName": "env1"
}
}
}

View File

@@ -1,261 +0,0 @@
{
"Log": {
// V2bX 的日志配置,独立于各 Core 的 log 配置
// 日志等级info, warn, error, none
"Level": "error",
// 日志输出路径,默认输出到标准输出
"Output": ""
},
"Cores": [
{
// Core类型
"Type": "sing",
// Core标识名可选如果需要启动多个同类型内核则必填
"Name": "sing1",
"Log": {
// 同 SingBox log 部分配置
"Level": "error",
"Timestamp": true
},
"NTP": {
// 同 SingBox ntp 部分配置
// VMess VLESS 建议开启
"Enable": true,
"Server": "time.apple.com",
"ServerPort": 0
},
"DnsConfigPath": "/etc/V2bX/dns.json",
// SingBox源配置文件目录用于引用标准SingBox配置文件
"OriginalPath": "/etc/V2bX/sing_origin.json",
// 在删除用户时清理已建立的连接
"EnableConnClear": false,
},
{
"Type": "sing",
"Name": "sing2",
"Log": {
"Level": "info",
"Timestamp": false
}
},
{
"Type": "xray",
"Log": {
// 同 Xray-core log 部分配置
"Level": "error"
},
// 静态资源文件目录
"AssetPath": "",
// DNS配置文件目录
"DnsConfigPath": "",
// 路由配置文件目录
"RouteConfigPath": "",
// 本地策略相关配置
"ConnectionConfig": {
// 详见 https://xtls.github.io/config/policy.html#levelpolicyobject
"handshake": 4,
"connIdle": 300,
"uplinkOnly": 2,
"downlinkOnly": 5,
"statsUserUplink": false,
"statsUserDownlink": false,
"bufferSize": 4
},
// Inbound配置文件目录
"InboundConfigPath": "",
// Outbound配置文件目录
"OutboundConfigPath": ""
}
],
"Nodes": [
// Node配置有两种写法
{
// 写法1
// sing内核
// Node标识名便于查看日志不填将通过下发的节点配置自动生成
// 务必注意不要重复,否则会出现问题
"Name": "sing_node1",
// 要使用的Core的类型
// 如果填写了CoreName可不填但单内核务必填写
// 建议视情况填写Core和CoreName其中一个如果均没有填写将随机选择支持的内核
"Core": "sing",
// 要使用的Core的标识名如果没有定义多个同类型内核可不填
"CoreName": "sing1",
// API接口地址
"ApiHost": "http://127.0.0.1",
// API密钥即Token
"ApiKey": "test",
// 节点ID
"NodeID": 33,
// 节点类型
"NodeType": "shadowsocks",
// 请求超时时间
"Timeout": 30,
// 监听IP
"ListenIP": "0.0.0.0",
// 发送IP
"SendIP": "0.0.0.0",
// 开启 Proxy Protocol参见 https://github.com/haproxy/haproxy/blob/master/doc/proxy-protocol.txt
"EnableProxyProtocol": false,
// 开启 TCP Fast Open
"EnableTFO": true,
// 开启 DNS
"EnableDNS" : true,
// 设置 Domain Strategy 需要开启 DNS ,默认 AsIS
// 可选 prefer_ipv4 / prefer_ipv6 / ipv4_only / ipv6_only
"DomainStrategy": "ipv4_only",
// 限制器相关配置
"LimitConfig": {
// 开启实时连接数及IP数限制
"EnableRealtime": false,
// 用户速度限制
"SpeedLimit": 0,
// 用户IP限制
"IPLimit": 0,
// 用户连接数限制
"ConnLimit": 0,
// 开启动态限速
"EnableDynamicSpeedLimit": false,
// 动态限速相关配置
"DynamicSpeedLimitConfig": {
// 检查周期
"Periodic": 60,
// 检查周期内触发限制的流量数
"Traffic": 1000,
// 触发限制后的速度限制
"SpeedLimit": 100,
// 速度限制过期时间
"ExpireTime": 60
}
},
// 证书相关配置
"CertConfig": {
// 证书申请模式none、http、dns、self
"CertMode": "none",
"RejectUnknownSni": false,
// 证书域名
"CertDomain": "test.com",
// 证书文件目录
"CertFile": "/etc/V2bX/cert/1.pem",
// 密钥文件目录
"KeyFile": "/etc/V2bX/cert/1.key",
// 申请证书时使用的用户邮箱
"Email": "1@test.com",
// DNS解析提供者
"Provider": "cloudflare",
// DNS解析提供者的环境变量详见 https://go-acme.github.io/lego/dns/
"DNSEnv": {
"EnvName": "env1"
}
}
},
{
// xray内核
"Name": "xray_node1",
"Core": "xray",
"CoreName": "",
"ApiHost": "http://127.0.0.1",
"ApiKey": "test",
"NodeID": 33,
"NodeType": "shadowsocks",
"Timeout": 30,
"ListenIP": "0.0.0.0",
"SendIP": "0.0.0.0",
"EnableProxyProtocol": true,
"EnableTFO": true,
// 以上同 sing
// 开启自定义DNS
"EnableDNS": false,
// DNS解析类型AsIs、UseIP、UseIPv4、UseIPv6
"DNSType": "AsIs",
// 开启udp over tcp
"EnableUot": false,
// 禁用IVCheck
"DisableIVCheck": false,
// 禁用嗅探
"DisableSniffing": false,
// 开启回落
"EnableFallback": false,
// 回落相关配置
"FallBackConfigs":{
// 详见 https://xtls.github.io/config/features/fallback.html#fallbackobject
"SNI": "",
"Alpn": "",
"Path": "",
"Dest": "",
"ProxyProtocolVer": 0
}
},
{
// 写法2
// 类似旧配置文件 ApiConfig 部分
"ApiConfig": {
"ApiHost": "http://127.0.0.1",
"ApiKey": "test",
"NodeID": 33,
"Timeout": 30
},
// 类似旧配置文件 ControllerConfig 部分
"Options": {
"Core": "sing",
"EnableProxyProtocol": true,
"EnableTFO": true,
"DomainStrategy": "ipv4_only"
// More
}
},
{
// 引用本地其他配置文件
"Include": "../example/config_full_node1.json"
},
{
// 通过Http引用远端配置文件
"Include": "http://127.0.0.1:11451/config_full_node1.json"
}
]
}

View File

@@ -1,13 +0,0 @@
{
"Core": "xray",
"ApiHost": "https://127.0.0.1",
"ApiKey": "key",
"NodeID": 1,
"NodeType": "vmess",
"Timeout": 30,
"ListenIP": "0.0.0.0",
"SendIP": "0.0.0.0",
"EnableProxyProtocol": false,
"EnableTFO": true,
"DNSType": "ipv4_only"
}

355
go.mod
View File

@@ -1,42 +1,46 @@
module github.com/InazumaV/V2bX
go 1.23
go 1.24
toolchain go1.23.2
toolchain go1.24.5
require (
github.com/apernet/hysteria/core/v2 v2.5.2
github.com/apernet/hysteria/extras/v2 v2.5.2
github.com/beevik/ntp v1.2.0
github.com/fsnotify/fsnotify v1.7.0
github.com/go-acme/lego/v4 v4.17.4
github.com/go-resty/resty/v2 v2.13.1
github.com/goccy/go-json v0.10.3
github.com/hashicorp/go-multierror v1.1.1
github.com/apernet/hysteria/core/v2 v2.6.2
github.com/apernet/hysteria/extras/v2 v2.6.2
github.com/apernet/quic-go v0.52.1-0.20250607183305-9320c9d14431
github.com/beevik/ntp v1.4.4-0.20240716062501-06ef196b89ec
github.com/fsnotify/fsnotify v1.8.0
github.com/go-acme/lego/v4 v4.21.1-0.20241220151055-ee7a9e4fa04f
github.com/go-resty/resty/v2 v2.16.2
github.com/goccy/go-json v0.10.4
github.com/hashicorp/go-multierror v1.1.2-0.20241119060415-613124da9385
github.com/juju/ratelimit v1.0.2
github.com/sagernet/sing v0.5.0-beta.2
github.com/sagernet/sing-box v1.10.0-beta.11
github.com/sagernet/sing v0.7.0-beta.1.0.20250722151551-64142925accb
github.com/sagernet/sing-box v1.12.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.15.0
github.com/xtls/xray-core v1.8.25-0.20241005021528-c30f5d47964b
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.19.0
github.com/vmihailenco/msgpack/v5 v5.4.1
github.com/xtls/xray-core v1.250608.1-0.20250723105259-dbd912568602
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.27.0
golang.org/x/sys v0.25.0
google.golang.org/protobuf v1.34.2
golang.org/x/crypto v0.40.0
golang.org/x/sys v0.34.0
google.golang.org/protobuf v1.36.6
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)
require (
berty.tech/go-libtor v1.0.385 // indirect
cloud.google.com/go/compute/metadata v0.5.0 // indirect
cloud.google.com/go/auth v0.13.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.6 // indirect
cloud.google.com/go/compute/metadata v0.7.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/AdamSLevy/jsonrpc2/v14 v14.1.0 // indirect
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.12.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.6.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.9.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dns/armdns v1.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns v1.2.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.9.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.29 // indirect
@@ -49,227 +53,280 @@ require (
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/OpenDNS/vegadns2client v0.0.0-20180418235048-a3fa4a771d87 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/akamai/AkamaiOPEN-edgegrid-golang v1.2.2 // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.62.712 // indirect
github.com/akutz/memconn v0.1.0 // indirect
github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.63.72 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/apernet/quic-go v0.47.1-0.20241004180137-a80d14e2080d // indirect
github.com/aws/aws-sdk-go-v2 v1.27.2 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.18 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.18 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.11 // indirect
github.com/aws/aws-sdk-go-v2/service/lightsail v1.38.3 // indirect
github.com/aws/aws-sdk-go-v2/service/route53 v1.40.10 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.11 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.12 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/anytls/sing-anytls v0.0.9-0.20250508103614-8bc6dd599731 // indirect
github.com/aws/aws-sdk-go-v2 v1.32.7 // indirect
github.com/aws/aws-sdk-go-v2/config v1.28.7 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.48 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.22 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.26 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.26 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.7 // indirect
github.com/aws/aws-sdk-go-v2/service/lightsail v1.42.8 // indirect
github.com/aws/aws-sdk-go-v2/service/route53 v1.46.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.8 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.7 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.33.3 // indirect
github.com/aws/smithy-go v1.22.1 // indirect
github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/caddyserver/certmagic v0.20.0 // indirect
github.com/caddyserver/certmagic v0.23.0 // indirect
github.com/caddyserver/zerossl v0.1.3 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/civo/civogo v0.3.11 // indirect
github.com/cloudflare/circl v1.4.0 // indirect
github.com/cloudflare/cloudflare-go v0.97.0 // indirect
github.com/cloudflare/circl v1.6.1 // indirect
github.com/cloudflare/cloudflare-go v0.112.0 // indirect
github.com/coder/websocket v1.8.13 // indirect
github.com/coreos/go-iptables v0.7.1-0.20240112124308-65c67c9f46e6 // indirect
github.com/cpu/goacmedns v0.1.1 // indirect
github.com/cretz/bine v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deepmap/oapi-codegen v1.9.1 // indirect
github.com/database64128/netx-go v0.0.0-20240905055117-62795b8b054a // indirect
github.com/database64128/tfo-go/v2 v2.2.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dblohm7/wingoes v0.0.0-20240119213807-a09d6be7affa // indirect
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 // indirect
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/dnsimple/dnsimple-go v1.7.0 // indirect
github.com/exoscale/egoscale v0.102.3 // indirect
github.com/exoscale/egoscale/v3 v3.1.7 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gaissmai/bart v0.11.1 // indirect
github.com/ghodss/yaml v1.0.1-0.20220118164431-d8423dcdf344 // indirect
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/go-chi/chi/v5 v5.2.2 // indirect
github.com/go-chi/render v1.0.3 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-jose/go-jose/v4 v4.0.2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-jose/go-jose/v4 v4.0.5 // indirect
github.com/go-json-experiment/json v0.0.0-20250103232110-6a9a0fde9288 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/gofrs/uuid/v5 v5.2.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/godbus/dbus/v5 v5.1.1-0.20230522191255-76236955d466 // indirect
github.com/gofrs/flock v0.12.1 // indirect
github.com/gofrs/uuid/v5 v5.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/nftables v0.2.1-0.20240414091927-5e242ec57806 // indirect
github.com/google/pprof v0.0.0-20240528025155-186aa0362fba // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/gophercloud/gophercloud v1.12.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.14.0 // indirect
github.com/gophercloud/gophercloud v1.14.1 // indirect
github.com/gophercloud/utils v0.0.0-20231010081019-80377eca5d56 // indirect
github.com/gorilla/csrf v1.7.3-0.20250123201450-9dd6af1f6d30 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.2 // indirect
github.com/hdevalence/ed25519consensus v0.2.0 // indirect
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.128 // indirect
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df // indirect
github.com/illarion/gonotify/v2 v2.0.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/infobloxopen/infoblox-go-client v1.1.1 // indirect
github.com/insomniacslk/dhcp v0.0.0-20250417080101-5f8cf70e8c5f // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/jsimonetti/rtnetlink v1.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/kolo/xmlrpc v0.0.0-20220921171641-a4b6fa1dd06b // indirect
github.com/kortschak/wol v0.0.0-20200729010619-da482cc4850a // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/labbsr0x/bindman-dns-webhook v1.0.2 // indirect
github.com/labbsr0x/goh v1.0.1 // indirect
github.com/libdns/alidns v1.0.3 // indirect
github.com/libdns/cloudflare v0.1.1 // indirect
github.com/libdns/libdns v0.2.2 // indirect
github.com/linode/linodego v1.28.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/libdns/alidns v1.0.5-libdns.v1.beta1 // indirect
github.com/libdns/cloudflare v0.2.2-0.20250708034226-c574dccb31a6 // indirect
github.com/libdns/libdns v1.1.0 // indirect
github.com/linode/linodego v1.44.0 // indirect
github.com/liquidweb/liquidweb-cli v0.6.9 // indirect
github.com/liquidweb/liquidweb-go v1.6.4 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/metacubex/tfo-go v0.0.0-20240821025650-e9be0afd5e7d // indirect
github.com/mholt/acmez v1.2.0 // indirect
github.com/miekg/dns v1.1.62 // indirect
github.com/mdlayher/genetlink v1.3.2 // indirect
github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42 // indirect
github.com/mdlayher/sdnotify v1.0.0 // indirect
github.com/mdlayher/socket v0.5.1 // indirect
github.com/metacubex/tfo-go v0.0.0-20241231083714-66613d49c422 // indirect
github.com/metacubex/utls v1.8.0 // indirect
github.com/mholt/acmez/v3 v3.1.2 // indirect
github.com/miekg/dns v1.1.67 // indirect
github.com/mimuret/golang-iij-dpf v0.9.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/namedotcom/go v0.0.0-20180403034216-08470befbe04 // indirect
github.com/nrdcg/auroradns v1.1.0 // indirect
github.com/nrdcg/bunny-go v0.0.0-20240207213615-dde5bf4577a3 // indirect
github.com/nrdcg/desec v0.8.0 // indirect
github.com/nrdcg/desec v0.10.0 // indirect
github.com/nrdcg/dnspod-go v0.4.0 // indirect
github.com/nrdcg/freemyip v0.2.0 // indirect
github.com/nrdcg/freemyip v0.3.0 // indirect
github.com/nrdcg/goinwx v0.10.0 // indirect
github.com/nrdcg/mailinabox v0.2.0 // indirect
github.com/nrdcg/namesilo v0.2.1 // indirect
github.com/nrdcg/nodion v0.1.0 // indirect
github.com/nrdcg/porkbun v0.3.0 // indirect
github.com/nrdcg/porkbun v0.4.0 // indirect
github.com/nzdjb/go-metaname v1.0.0 // indirect
github.com/onsi/ginkgo/v2 v2.19.0 // indirect
github.com/ooni/go-libtor v1.1.8 // indirect
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
github.com/oracle/oci-go-sdk/v65 v65.63.1 // indirect
github.com/oschwald/maxminddb-golang v1.12.0 // indirect
github.com/ovh/go-ovh v1.5.1 // indirect
github.com/oracle/oci-go-sdk/v65 v65.81.1 // indirect
github.com/ovh/go-ovh v1.6.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pires/go-proxyproto v0.7.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/peterhellberg/link v1.2.0 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pires/go-proxyproto v0.8.1 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pquerna/otp v1.4.0 // indirect
github.com/prometheus-community/pro-bing v0.4.0 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
github.com/quic-go/quic-go v0.46.0 // indirect
github.com/refraction-networking/utls v1.6.7 // indirect
github.com/quic-go/quic-go v0.54.0 // indirect
github.com/refraction-networking/utls v1.8.0 // indirect
github.com/regfish/regfish-dnsapi-go v0.1.1 // indirect
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 // indirect
github.com/sacloud/api-client-go v0.2.10 // indirect
github.com/sacloud/go-http v0.1.8 // indirect
github.com/sacloud/iaas-api-go v1.12.0 // indirect
github.com/sacloud/iaas-api-go v1.14.0 // indirect
github.com/sacloud/packages-go v0.0.10 // indirect
github.com/safchain/ethtool v0.3.0 // indirect
github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a // indirect
github.com/sagernet/cloudflare-tls v0.0.0-20231208171750-a4483c1b7cd1 // indirect
github.com/sagernet/cors v1.2.1 // indirect
github.com/sagernet/fswatch v0.1.1 // indirect
github.com/sagernet/gvisor v0.0.0-20240428053021-e691de28565f // indirect
github.com/sagernet/gvisor v0.0.0-20250325023245-7a9c0f5725fb // indirect
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a // indirect
github.com/sagernet/nftables v0.3.0-beta.4 // indirect
github.com/sagernet/quic-go v0.47.0-beta.2 // indirect
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 // indirect
github.com/sagernet/sing-dns v0.3.0-beta.14 // indirect
github.com/sagernet/sing-mux v0.2.0 // indirect
github.com/sagernet/sing-quic v0.3.0-beta.3 // indirect
github.com/sagernet/sing-shadowsocks v0.2.7 // indirect
github.com/sagernet/sing-shadowsocks2 v0.2.0 // indirect
github.com/sagernet/sing-shadowtls v0.1.4 // indirect
github.com/sagernet/sing-tun v0.4.0-beta.16 // indirect
github.com/sagernet/sing-vmess v0.1.12 // indirect
github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7 // indirect
github.com/sagernet/utls v1.6.7 // indirect
github.com/sagernet/wireguard-go v0.0.0-20231215174105-89dec3b2f3e8 // indirect
github.com/sagernet/quic-go v0.52.0-beta.1 // indirect
github.com/sagernet/sing-mux v0.3.2 // indirect
github.com/sagernet/sing-quic v0.5.0-beta.3 // indirect
github.com/sagernet/sing-shadowsocks v0.2.8 // indirect
github.com/sagernet/sing-shadowsocks2 v0.2.1 // indirect
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11 // indirect
github.com/sagernet/sing-tun v0.6.10-0.20250721014417-ebbe32588cfb // indirect
github.com/sagernet/sing-vmess v0.2.4 // indirect
github.com/sagernet/smux v1.5.34-mod.2 // indirect
github.com/sagernet/tailscale v1.80.3-mod.5 // indirect
github.com/sagernet/wireguard-go v0.0.1-beta.7 // indirect
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30 // indirect
github.com/seiflotfy/cuckoofilter v0.0.0-20240715131351-a2f2c23f1771 // indirect
github.com/selectel/domains-go v1.1.0 // indirect
github.com/selectel/go-selvpcclient/v3 v3.1.1 // indirect
github.com/selectel/go-selvpcclient/v3 v3.2.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9 // indirect
github.com/softlayer/softlayer-go v1.1.5 // indirect
github.com/softlayer/softlayer-go v1.1.7 // indirect
github.com/softlayer/xmlrpc v0.0.0-20200409220501-5f089df7cb7e // indirect
github.com/sony/gobreaker v0.5.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.898 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.898 // indirect
github.com/transip/gotransip/v6 v6.23.0 // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tailscale/certstore v0.1.1-0.20231202035212-d3fa0460f47e // indirect
github.com/tailscale/go-winio v0.0.0-20231025203758-c4f33415bf55 // indirect
github.com/tailscale/golang-x-crypto v0.0.0-20240604161659-3fde5e568aa4 // indirect
github.com/tailscale/goupnp v1.0.1-0.20210804011211-c64d0f06ea05 // indirect
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a // indirect
github.com/tailscale/netlink v1.1.1-0.20240822203006-4d49adab4de7 // indirect
github.com/tailscale/peercred v0.0.0-20250107143737-35a0c7bd7edc // indirect
github.com/tailscale/web-client-prebuilt v0.0.0-20250124233751-d4cd19a26976 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1065 // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.1065 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/transip/gotransip/v6 v6.26.0 // indirect
github.com/txthinking/runnergroup v0.0.0-20210608031112-152c7c4432bf // indirect
github.com/txthinking/socks5 v0.0.0-20230325130024-4230056ae301 // indirect
github.com/ultradns/ultradns-go-sdk v1.6.1-20231103022937-8589b6a // indirect
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 // indirect
github.com/ultradns/ultradns-go-sdk v1.8.0-20241010134910-243eeec // indirect
github.com/v2fly/ss-bloomring v0.0.0-20210312155135-28617310f63e // indirect
github.com/vinyldns/go-vinyldns v0.9.16 // indirect
github.com/vishvananda/netlink v1.3.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/vultr/govultr/v2 v2.17.2 // indirect
github.com/xtls/reality v0.0.0-20240909153216-e26ae2305463 // indirect
github.com/yandex-cloud/go-genproto v0.0.0-20240318083951-4fe6125f286e // indirect
github.com/yandex-cloud/go-sdk v0.0.0-20240318084659-dfa50323a0b4 // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/mock v0.4.0 // indirect
github.com/vishvananda/netlink v1.3.1 // indirect
github.com/vishvananda/netns v0.0.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/volcengine/volc-sdk-golang v1.0.189 // indirect
github.com/vultr/govultr/v3 v3.9.1 // indirect
github.com/wyx2685/sing-vmess v0.0.0-20250723121437-95d5ab59ff92 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xtls/reality v0.0.0-20250723115123-fadd8146daab // indirect
github.com/yandex-cloud/go-genproto v0.0.0-20241220122821-aeb3b05efd1c // indirect
github.com/yandex-cloud/go-sdk v0.0.0-20241220131134-2393e243c134 // indirect
github.com/zeebo/blake3 v0.2.4 // indirect
go.mongodb.org/mongo-driver v1.12.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel v1.36.0 // indirect
go.opentelemetry.io/otel/metric v1.36.0 // indirect
go.opentelemetry.io/otel/trace v1.36.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/mock v0.5.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/ratelimit v0.3.0 // indirect
go.uber.org/zap/exp v0.3.0 // indirect
go4.org/mem v0.0.0-20240501181205-ae6ca9944745 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
golang.org/x/mod v0.26.0 // indirect
golang.org/x/net v0.42.0 // indirect
golang.org/x/oauth2 v0.30.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/term v0.33.0 // indirect
golang.org/x/text v0.27.0 // indirect
golang.org/x/time v0.9.0 // indirect
golang.org/x/tools v0.34.0 // indirect
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173 // indirect
google.golang.org/api v0.172.0 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.67.1 // indirect
golang.zx2c4.com/wireguard/windows v0.5.3 // indirect
google.golang.org/api v0.214.0 // indirect
google.golang.org/genproto v0.0.0-20241021214115-324edc3d5d38 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250528174236-200df99c418a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a // indirect
google.golang.org/grpc v1.74.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/ns1/ns1-go.v2 v2.7.13 // indirect
gopkg.in/ns1/ns1-go.v2 v2.13.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gvisor.dev/gvisor v0.0.0-20231202080848-1f7806d17489 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
gvisor.dev/gvisor v0.0.0-20250428193742-2d800c3129d5 // indirect
lukechampine.com/blake3 v1.4.1 // indirect
)
// replace github.com/sagernet/sing-box v1.10.0-alpha.22 => /root/sing-box_mod
replace github.com/sagernet/sing-box v1.10.0-beta.11 => github.com/wyx2685/sing-box_mod v1.10.0-beta.11
//replace github.com/sagernet/sing-box v1.12.0 => ../sing-box_mod
replace github.com/sagernet/sing-box v1.12.0 => github.com/wyx2685/sing-box_mod v1.12.0-rc.2

1111
go.sum

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +0,0 @@
package limiter
import log "github.com/sirupsen/logrus"
func ClearOnlineIP() error {
log.WithField("Type", "Limiter").
Debug("Clear online ip...")
limitLock.RLock()
for _, l := range limiter {
l.ConnLimiter.ClearOnlineIP()
}
limitLock.RUnlock()
log.WithField("Type", "Limiter").
Debug("Clear online ip done")
return nil
}

View File

@@ -1,165 +0,0 @@
package limiter
import (
"sync"
"time"
)
type ConnLimiter struct {
realtime bool
ipLimit int
connLimit int
count sync.Map // map[string]int
ip sync.Map // map[string]map[string]int
}
func NewConnLimiter(conn int, ip int, realtime bool) *ConnLimiter {
return &ConnLimiter{
realtime: realtime,
connLimit: conn,
ipLimit: ip,
count: sync.Map{},
ip: sync.Map{},
}
}
func (c *ConnLimiter) AddConnCount(user string, ip string, isTcp bool) (limit bool) {
if c.connLimit != 0 {
if v, ok := c.count.Load(user); ok {
if v.(int) >= c.connLimit {
// over connection limit
return true
} else if isTcp {
// tcp protocol
// connection count add
c.count.Store(user, v.(int)+1)
}
} else if isTcp {
// tcp protocol
// store connection count
c.count.Store(user, 1)
}
}
if c.ipLimit == 0 {
return false
}
// first user map
ipMap := new(sync.Map)
if c.realtime {
if isTcp {
ipMap.Store(ip, 2)
} else {
ipMap.Store(ip, 1)
}
} else {
ipMap.Store(ip, time.Now())
}
// check user online ip
if v, ok := c.ip.LoadOrStore(user, ipMap); ok {
// have user
ips := v.(*sync.Map)
cn := 0
if online, ok := ips.Load(ip); ok {
// online ip
if c.realtime {
if isTcp {
// tcp count add
ips.Store(ip, online.(int)+2)
}
} else {
// update connect time for not realtime
ips.Store(ip, time.Now())
}
} else {
// not online ip
ips.Range(func(_, _ interface{}) bool {
cn++
if cn >= c.ipLimit {
limit = true
return false
}
return true
})
if limit {
// over ip limit
return
}
if c.realtime {
if isTcp {
ips.Store(ip, 2)
} else {
ips.Store(ip, 1)
}
} else {
ips.Store(ip, time.Now())
}
}
}
return
}
// DelConnCount Delete tcp connection count, no tcp do not use
func (c *ConnLimiter) DelConnCount(user string, ip string) {
if !c.realtime {
return
}
if c.connLimit != 0 {
if v, ok := c.count.Load(user); ok {
if v.(int) == 1 {
c.count.Delete(user)
} else {
c.count.Store(user, v.(int)-1)
}
}
}
if c.ipLimit == 0 {
return
}
if i, ok := c.ip.Load(user); ok {
is := i.(*sync.Map)
if i, ok := is.Load(ip); ok {
if i.(int) == 2 {
is.Delete(ip)
} else {
is.Store(user, i.(int)-2)
}
notDel := false
c.ip.Range(func(_, _ any) bool {
notDel = true
return false
})
if !notDel {
c.ip.Delete(user)
}
}
}
}
// ClearOnlineIP Clear udp,icmp and other packet protocol online ip
func (c *ConnLimiter) ClearOnlineIP() {
c.ip.Range(func(u, v any) bool {
userIp := v.(*sync.Map)
notDel := false
userIp.Range(func(ip, v any) bool {
notDel = true
if _, ok := v.(int); ok {
if v.(int) == 1 {
// clear packet ip for realtime
userIp.Delete(ip)
}
return true
} else {
// clear ip for not realtime
if v.(time.Time).Before(time.Now().Add(time.Minute)) {
// 1 minute no active
userIp.Delete(ip)
}
}
return true
})
if !notDel {
c.ip.Delete(u)
}
return true
})
}

View File

@@ -1,56 +0,0 @@
package limiter
import (
"sync"
"testing"
"time"
)
var c *ConnLimiter
func init() {
c = NewConnLimiter(1, 1, true)
}
func TestConnLimiter_AddConnCount(t *testing.T) {
t.Log(c.AddConnCount("1", "1", true))
t.Log(c.AddConnCount("1", "2", true))
}
func TestConnLimiter_DelConnCount(t *testing.T) {
t.Log(c.AddConnCount("1", "1", true))
t.Log(c.AddConnCount("1", "2", true))
c.DelConnCount("1", "1")
t.Log(c.AddConnCount("1", "2", true))
}
func TestConnLimiter_ClearOnlineIP(t *testing.T) {
t.Log(c.AddConnCount("1", "1", false))
t.Log(c.AddConnCount("1", "2", false))
c.ClearOnlineIP()
t.Log(c.AddConnCount("1", "2", true))
c.DelConnCount("1", "2")
t.Log(c.AddConnCount("1", "1", false))
// not realtime
c.realtime = false
t.Log(c.AddConnCount("3", "2", true))
c.ClearOnlineIP()
t.Log(c.ip.Load("3"))
time.Sleep(time.Minute)
c.ClearOnlineIP()
t.Log(c.ip.Load("3"))
}
func BenchmarkConnLimiter(b *testing.B) {
wg := sync.WaitGroup{}
for i := 0; i < b.N; i++ {
wg.Add(1)
go func() {
c.AddConnCount("1", "2", true)
c.DelConnCount("1", "2")
wg.Done()
}()
}
wg.Wait()
}

View File

@@ -11,8 +11,6 @@ import (
"github.com/InazumaV/V2bX/common/format"
"github.com/InazumaV/V2bX/conf"
"github.com/juju/ratelimit"
log "github.com/sirupsen/logrus"
"github.com/xtls/xray-core/common/task"
)
var limitLock sync.RWMutex
@@ -20,28 +18,17 @@ var limiter map[string]*Limiter
func Init() {
limiter = map[string]*Limiter{}
c := task.Periodic{
Interval: time.Minute * 3,
Execute: ClearOnlineIP,
}
go func() {
log.WithField("Type", "Limiter").
Debug("ClearOnlineIP started")
time.Sleep(time.Minute * 3)
_ = c.Start()
}()
}
type Limiter struct {
DomainRules []*regexp.Regexp
ProtocolRules []string
SpeedLimit int
UserOnlineIP *sync.Map // Key: Name, value: {Key: Ip, value: Uid}
OldUserOnline map[string]int // Key: Ip, value: Uid
UserOnlineIP *sync.Map // Key: TagUUID, value: {Key: Ip, value: Uid}
OldUserOnline *sync.Map // Key: Ip, value: Uid
UUIDtoUID map[string]int // Key: UUID, value: Uid
UserLimitInfo *sync.Map // Key: Uid value: UserLimitInfo
ConnLimiter *ConnLimiter // Key: Uid value: ConnLimiter
SpeedLimiter *sync.Map // key: Uid, value: *ratelimit.Bucket
UserLimitInfo *sync.Map // Key: TagUUID value: UserLimitInfo
SpeedLimiter *sync.Map // key: TagUUID, value: *ratelimit.Bucket
AliveList map[int]int // Key: Uid, value: alive_ip
}
@@ -59,10 +46,9 @@ func AddLimiter(tag string, l *conf.LimitConfig, users []panel.UserInfo, aliveLi
SpeedLimit: l.SpeedLimit,
UserOnlineIP: new(sync.Map),
UserLimitInfo: new(sync.Map),
ConnLimiter: NewConnLimiter(l.ConnLimit, l.IPLimit, l.EnableRealtime),
SpeedLimiter: new(sync.Map),
AliveList: aliveList,
OldUserOnline: make(map[string]int),
OldUserOnline: new(sync.Map),
}
uuidmap := make(map[string]int)
for i := range users {
@@ -105,6 +91,7 @@ func (l *Limiter) UpdateUser(tag string, added []panel.UserInfo, deleted []panel
for i := range deleted {
l.UserLimitInfo.Delete(format.UserTag(tag, deleted[i].Uuid))
l.UserOnlineIP.Delete(format.UserTag(tag, deleted[i].Uuid))
l.SpeedLimiter.Delete(format.UserTag(tag, deleted[i].Uuid))
delete(l.UUIDtoUID, deleted[i].Uuid)
delete(l.AliveList, deleted[i].Id)
}
@@ -140,10 +127,6 @@ func (l *Limiter) CheckLimit(taguuid string, ip string, isTcp bool, noSSUDP bool
// check if ipv4 mapped ipv6
ip = strings.TrimPrefix(ip, "::ffff:")
// ip and conn limiter
if l.ConnLimiter.AddConnCount(taguuid, ip, isTcp) {
return nil, true
}
// check and gen speed limit Bucket
nodeLimit := l.SpeedLimit
userLimit := 0
@@ -164,26 +147,34 @@ func (l *Limiter) CheckLimit(taguuid string, ip string, isTcp bool, noSSUDP bool
} else {
userLimit = determineSpeedLimit(u.SpeedLimit, u.DynamicSpeedLimit)
}
} else {
return nil, true
}
if noSSUDP {
// Store online user for device limit
ipMap := new(sync.Map)
ipMap.Store(ip, uid)
newipMap := new(sync.Map)
newipMap.Store(ip, uid)
aliveIp := l.AliveList[uid]
// If any device is online
if v, ok := l.UserOnlineIP.LoadOrStore(taguuid, ipMap); ok {
ipMap := v.(*sync.Map)
if v, loaded := l.UserOnlineIP.LoadOrStore(taguuid, newipMap); loaded {
oldipMap := v.(*sync.Map)
// If this is a new ip
if _, ok := ipMap.LoadOrStore(ip, uid); !ok {
if deviceLimit > 0 {
if _, loaded := oldipMap.LoadOrStore(ip, uid); !loaded {
if v, loaded := l.OldUserOnline.Load(ip); loaded {
if v.(int) == uid {
l.OldUserOnline.Delete(ip)
}
} else if deviceLimit > 0 {
if deviceLimit <= aliveIp {
ipMap.Delete(ip)
oldipMap.Delete(ip)
return nil, true
}
}
}
} else if l.OldUserOnline[ip] == uid {
delete(l.OldUserOnline, ip)
} else if v, ok := l.OldUserOnline.Load(ip); ok {
if v.(int) == uid {
l.OldUserOnline.Delete(ip)
}
} else {
if deviceLimit > 0 {
if deviceLimit <= aliveIp {
@@ -210,13 +201,14 @@ func (l *Limiter) CheckLimit(taguuid string, ip string, isTcp bool, noSSUDP bool
func (l *Limiter) GetOnlineDevice() (*[]panel.OnlineUser, error) {
var onlineUser []panel.OnlineUser
l.OldUserOnline = new(sync.Map)
l.UserOnlineIP.Range(func(key, value interface{}) bool {
taguuid := key.(string)
ipMap := value.(*sync.Map)
ipMap.Range(func(key, value interface{}) bool {
uid := value.(int)
ip := key.(string)
l.OldUserOnline[ip] = uid
l.OldUserOnline.Store(ip, uid)
onlineUser = append(onlineUser, panel.OnlineUser{UID: uid, IP: ip})
return true
})

View File

@@ -71,7 +71,11 @@ func (c *Controller) nodeInfoMonitor() (err error) {
// get user alive
newA, err := c.apiClient.GetUserAlive()
if err != nil {
return err
log.WithFields(log.Fields{
"tag": c.tag,
"err": err,
}).Error("Get alive list failed")
return nil
}
if newN != nil {
c.info = newN
@@ -87,7 +91,7 @@ func (c *Controller) nodeInfoMonitor() (err error) {
log.WithFields(log.Fields{
"tag": c.tag,
"err": err,
}).Error("Delete node failed")
}).Panic("Delete node failed")
return nil
}
@@ -100,6 +104,10 @@ func (c *Controller) nodeInfoMonitor() (err error) {
l := limiter.AddLimiter(c.tag, &c.LimitConfig, c.userList, newA)
c.limiter = l
}
// update alive list
if newA != nil {
c.limiter.AliveList = newA
}
// Update rule
err = c.limiter.UpdateRule(&newN.Rules)
if err != nil {
@@ -127,7 +135,7 @@ func (c *Controller) nodeInfoMonitor() (err error) {
log.WithFields(log.Fields{
"tag": c.tag,
"err": err,
}).Error("Add node failed")
}).Panic("Add node failed")
return nil
}
_, err = c.server.AddUsers(&vCore.AddUsersParams{
@@ -170,7 +178,7 @@ func (c *Controller) nodeInfoMonitor() (err error) {
deleted, added := compareUserList(c.userList, newU)
if len(deleted) > 0 {
// have deleted users
err = c.server.DelUsers(deleted, c.tag)
err = c.server.DelUsers(deleted, c.tag, c.info)
if err != nil {
log.WithFields(log.Fields{
"tag": c.tag,