Compare commits

...

4 Commits

Author SHA1 Message Date
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
6 changed files with 99 additions and 31 deletions

View File

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

View File

@@ -10,14 +10,14 @@ on:
- "**/*.go" - "**/*.go"
- "go.mod" - "go.mod"
- "go.sum" - "go.sum"
- ".github/workflows/*.yml" - ".github/workflows/release.yml"
pull_request: pull_request:
types: [opened, synchronize, reopened] types: [opened, synchronize, reopened]
paths: paths:
- "**/*.go" - "**/*.go"
- "go.mod" - "go.mod"
- "go.sum" - "go.sum"
- ".github/workflows/*.yml" - ".github/workflows/release.yml"
release: release:
types: [published] types: [published]

View File

@@ -1,5 +1,5 @@
# Build go # Build go
FROM golang:1.23.2-alpine AS builder FROM golang:1.24.0-alpine AS builder
WORKDIR /app WORKDIR /app
COPY . . COPY . .
ENV CGO_ENABLED=0 ENV CGO_ENABLED=0

View File

@@ -20,7 +20,8 @@ import (
var _ adapter.ConnectionTracker = (*HookServer)(nil) var _ adapter.ConnectionTracker = (*HookServer)(nil)
type HookServer struct { type HookServer struct {
counter sync.Map counter sync.Map //map[string]*counter.TrafficCounter
userconn sync.Map //map[string][]net.Conn
} }
func (h *HookServer) ModeList() []string { func (h *HookServer) ModeList() []string {
@@ -30,6 +31,7 @@ func (h *HookServer) ModeList() []string {
func NewHookServer() *HookServer { func NewHookServer() *HookServer {
server := &HookServer{ server := &HookServer{
counter: sync.Map{}, counter: sync.Map{},
userconn: sync.Map{},
} }
return server return server
} }
@@ -40,8 +42,9 @@ func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapte
log.Warn("get limiter for ", m.Inbound, " error: ", err) log.Warn("get limiter for ", m.Inbound, " error: ", err)
return conn return conn
} }
taguuid := format.UserTag(m.Inbound, m.User)
ip := m.Source.Addr.String() 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() conn.Close()
log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn") log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
return conn return conn
@@ -70,13 +73,26 @@ func (h *HookServer) RoutedConnection(_ context.Context, conn net.Conn, m adapte
} }
} }
} }
if c, ok := h.counter.Load(m.Inbound); ok { var t *counter.TrafficCounter
return counter.NewConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User)) if c, ok := h.counter.Load(m.Inbound); !ok {
t = counter.NewTrafficCounter()
h.counter.Store(m.Inbound, t)
} else { } else {
c := counter.NewTrafficCounter() t = c.(*counter.TrafficCounter)
h.counter.Store(m.Inbound, c)
return counter.NewConnCounter(conn, c.GetCounter(m.User))
} }
conn = counter.NewConnCounter(conn, t.GetCounter(m.User))
if conns, exist := h.userconn.Load(taguuid); exist {
if connList, ok := conns.([]net.Conn); ok {
h.userconn.Store(taguuid, append(connList, conn))
} else {
h.userconn.Store(taguuid, []net.Conn{conn})
}
} else {
h.userconn.Store(taguuid, []net.Conn{conn})
}
return conn
} }
func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn, m adapter.InboundContext, _ adapter.Rule, _ adapter.Outbound) N.PacketConn { func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn, m adapter.InboundContext, _ adapter.Rule, _ adapter.Outbound) N.PacketConn {
@@ -86,7 +102,8 @@ func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn
return conn return conn
} }
ip := m.Source.Addr.String() 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() conn.Close()
log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn") log.Error("[", m.Inbound, "] ", "Limited ", m.User, " by ip or conn")
return conn return conn
@@ -115,11 +132,37 @@ func (h *HookServer) RoutedPacketConnection(_ context.Context, conn N.PacketConn
} }
} }
} }
if c, ok := h.counter.Load(m.Inbound); ok { var t *counter.TrafficCounter
return counter.NewPacketConnCounter(conn, c.(*counter.TrafficCounter).GetCounter(m.User)) if c, ok := h.counter.Load(m.Inbound); !ok {
t = counter.NewTrafficCounter()
h.counter.Store(m.Inbound, t)
} else { } else {
c := counter.NewTrafficCounter() t = c.(*counter.TrafficCounter)
h.counter.Store(m.Inbound, c) }
return counter.NewPacketConnCounter(conn, c.GetCounter(m.User)) conn = counter.NewPacketConnCounter(conn, t.GetCounter(m.User))
return conn
}
func (h *HookServer) CloseConnections(tag string, uuids []string) error {
for _, uuid := range uuids {
taguuid := format.UserTag(tag, uuid)
v, ok := h.userconn.Load(taguuid)
if !ok {
continue
}
connList, ok := v.([]net.Conn)
if !ok {
h.userconn.Delete(taguuid)
continue
}
for _, conn := range connList {
err := conn.Close()
if err != nil {
log.Error("close conn error: ", err)
} }
} }
h.userconn.Delete(taguuid)
}
return nil
}

View File

@@ -138,5 +138,9 @@ func (b *Sing) DelUsers(users []panel.UserInfo, tag string, info *panel.NodeInfo
if err != nil { if err != nil {
return err return err
} }
err = b.hookServer.CloseConnections(tag, uuids)
if err != nil {
return err
}
return nil return nil
} }

View File

@@ -164,6 +164,8 @@ func (l *Limiter) CheckLimit(taguuid string, ip string, isTcp bool, noSSUDP bool
} else { } else {
userLimit = determineSpeedLimit(u.SpeedLimit, u.DynamicSpeedLimit) userLimit = determineSpeedLimit(u.SpeedLimit, u.DynamicSpeedLimit)
} }
} else {
return nil, true
} }
if noSSUDP { if noSSUDP {
// Store online user for device limit // Store online user for device limit