From 3d263d500f25ead2d496b49430753a349961fc3e Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Thu, 18 Apr 2024 22:34:38 +0200 Subject: [PATCH] ultimate docker solution --- .github/workflows/docker-core.yml | 57 +++++++++++++++++++++++++++++ .github/workflows/docker.yml | 4 +- .github/workflows/release.yml | 12 ++++-- Dockerfile | 6 +-- backend/config/config.go | 4 ++ backend/service/server.go | 6 +-- backend/service/sinxbox.go | 5 ++- core/Dockerfile | 28 ++++++++++++++ runSingbox.sh => core/runSingbox.sh | 0 docker-compose.yml | 47 ++++++++++++++++++++++++ 10 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/docker-core.yml create mode 100644 core/Dockerfile rename runSingbox.sh => core/runSingbox.sh (100%) create mode 100644 docker-compose.yml diff --git a/.github/workflows/docker-core.yml b/.github/workflows/docker-core.yml new file mode 100644 index 0000000..2cab5da --- /dev/null +++ b/.github/workflows/docker-core.yml @@ -0,0 +1,57 @@ +name: Sing-box Docker Image CI +on: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Get latest release + id: get_release + run: | + latest_release=$(curl -Ls "https://api.github.com/repos/sagernet/sing-box/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') + echo "latest_release: $latest_release" + echo "latest_release=$latest_release" >> $GITHUB_OUTPUT + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + alireza7/s-ui-singbox + ghcr.io/alireza0/s-ui-singbox + tags: | + type=sha + type=pep440,pattern=${{ steps.get_release.outputs.latest_release }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_TOKEN }} + + - name: Login to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: core/ + push: true + build-args: SINGBOX_VER=${{ steps.get_release.outputs.latest_release }} + platforms: linux/amd64,linux/arm64/v8,linux/arm/v7,linux/386 + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 7365bf9..2040e9d 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -11,8 +11,6 @@ jobs: steps: - uses: actions/checkout@v4 - with: - submodules: true - name: Docker meta id: meta @@ -50,6 +48,6 @@ jobs: with: context: . push: true - platforms: linux/amd64,linux/arm64/v8 + platforms: linux/amd64,linux/arm64/v8,linux/arm/v7,linux/386 tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5d121c9..8d90c0b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -31,6 +31,7 @@ jobs: with: node-version: '20' registry-url: 'https://registry.npmjs.org' + - name: Install dependencies run: | sudo apt-get update && sudo apt-get install upx -yq @@ -70,13 +71,18 @@ jobs: #### Build Sing-Box git clone -b v1.8.7 https://github.com/SagerNet/sing-box cd sing-box - go build -v -gcflags=all="-l -B -C" -mod=mod -trimpath -ldflags "-s -w -buildid= -extldflags '-static'" -a -tags='netgo osusergo static_build with_v2ray_api with_clash_api with_grpc with_quic with_ech' -o sing-box ./cmd/sing-box + go build -v -gcflags=all="-l -B -C" -mod=mod -trimpath \ + -ldflags "-s -w -buildid= -extldflags '-static'" -a \ + -tags='netgo osusergo static_build with_quic with_grpc with_wireguard with_ech with_utls with_reality_server with_acme with_v2ray_api with_clash_api with_gvisor' \ + -o sing-box ./cmd/sing-box upx --ultra-brute -9 -v --lzma --best --force sing-box cd .. ### Build s-ui cd backend - go build -v -gcflags=all="-l -B -C" -mod=mod -trimpath -ldflags "-s -w -buildid= -extldflags '-static'" -a -tags='netgo osusergo static_build sqlite_omit_load_extension' -o ../sui main.go + go build -v -gcflags=all="-l -B -C" -mod=mod -trimpath \ + -ldflags "-s -w -buildid= -extldflags '-static'" -a -tags='netgo osusergo static_build sqlite_omit_load_extension' \ + -o ../sui main.go cd .. upx --ultra-brute -9 -v --lzma --best --force sui @@ -86,7 +92,7 @@ jobs: cp sing-box.service s-ui/ mkdir s-ui/bin cp sing-box/sing-box s-ui/bin/ - cp runSingbox.sh s-ui/bin/ + cp core/runSingbox.sh s-ui/bin/ - name: Package run: tar -zcvf s-ui-linux-${{ matrix.platform }}.tar.gz s-ui diff --git a/Dockerfile b/Dockerfile index 74cd274..ac7d60a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ -FROM node:alpine as front-builder +FROM --platform=$BUILDPLATFORM node:alpine as front-builder WORKDIR /app COPY frontend/ ./ RUN npm install && npm run build -FROM golang:1.22-alpine AS backend-builder +FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS backend-builder WORKDIR /app ARG TARGETARCH ENV CGO_CFLAGS="-D_LARGEFILE64_SOURCE" @@ -13,7 +13,7 @@ COPY backend/ ./ COPY --from=front-builder /app/dist/ /app/web/html/ RUN go build -o sui main.go -FROM alpine +FROM --platform=$BUILDPLATFORM alpine LABEL org.opencontainers.image.authors="alireza7@gmail.com" ENV TZ=Asia/Tehran WORKDIR /app diff --git a/backend/config/config.go b/backend/config/config.go index 7b826c5..188699c 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -69,6 +69,10 @@ func GetDBPath() string { } func GetDefaultConfig() string { + apiEnv := GetEnvApi() + if len(apiEnv) > 0 { + return strings.Replace(defaultConfig, "127.0.0.1:1080", apiEnv, 1) + } return defaultConfig } diff --git a/backend/service/server.go b/backend/service/server.go index 123fbad..73074c7 100644 --- a/backend/service/server.go +++ b/backend/service/server.go @@ -89,12 +89,12 @@ func (s *ServerService) GetNetInfo() map[string]interface{} { func (s *ServerService) GetSingboxInfo() map[string]interface{} { info := make(map[string]interface{}, 0) - if s.SingBoxService.IsRunning() { + sysStats, err := s.SingBoxService.GetSysStats() + if err == nil { info["running"] = true - sysStats, _ := s.SingBoxService.GetSysStats() info["stats"] = sysStats } else { - info["running"] = false + info["running"] = s.SingBoxService.IsRunning() } return info } diff --git a/backend/service/sinxbox.go b/backend/service/sinxbox.go index e7c1f5b..ce64a4f 100644 --- a/backend/service/sinxbox.go +++ b/backend/service/sinxbox.go @@ -26,7 +26,10 @@ func (s *SingBoxService) GetStats() error { } func (s *SingBoxService) GetSysStats() (*map[string]interface{}, error) { - s.V2rayAPI.Init(ApiAddr) + err := s.V2rayAPI.Init(ApiAddr) + if err != nil { + return nil, err + } defer s.V2rayAPI.Close() resp, err := s.V2rayAPI.GetSysStats() if err != nil { diff --git a/core/Dockerfile b/core/Dockerfile new file mode 100644 index 0000000..9c0c7ad --- /dev/null +++ b/core/Dockerfile @@ -0,0 +1,28 @@ +FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS singbox-builder +LABEL maintainer="Alireza " +WORKDIR /app +ARG TARGETOS TARGETARCH +ARG SINGBOX_VER=v1.8.10 +ARG SINGBOX_TAGS="with_quic,with_grpc,with_wireguard,with_ech,with_utls,with_reality_server,with_acme,with_v2ray_api,with_clash_api,with_gvisor" +ARG GOPROXY="" +ENV GOPROXY ${GOPROXY} +ENV CGO_ENABLED=0 +ENV GOOS=$TARGETOS +ENV GOARCH=$TARGETARCH +RUN apk --no-cache --update add build-base gcc wget unzip git +RUN set -ex \ + && git clone --depth 1 --branch $SINGBOX_VER https://github.com/SagerNet/sing-box.git \ + && cd sing-box \ + && go build -v -trimpath -tags \ + $SINGBOX_TAGS \ + -ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$SINGBOX_VER\" -s -w -buildid=" \ + ./cmd/sing-box + +FROM --platform=$BUILDPLATFORM alpine +LABEL maintainer="Alireza " +ENV TZ=Asia/Tehran +WORKDIR /app +RUN apk add --no-cache --update ca-certificates tzdata bash +COPY --from=singbox-builder /app/sing-box/sing-box . +COPY runSingbox.sh . +ENTRYPOINT [ "./runSingbox.sh" ] \ No newline at end of file diff --git a/runSingbox.sh b/core/runSingbox.sh similarity index 100% rename from runSingbox.sh rename to core/runSingbox.sh diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6f29138 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,47 @@ +--- +version: "3" + +services: + s-ui: + image: alireza7/s-ui + container_name: s-ui + hostname: "S-UI docker" + volumes: + - "singbox:/app/bin" + - "$PWD/db:/app/db" + - "$PWD/cert:/app/cert" + environment: + SINGBOX_API: "sing-box:1080" + SUI_DB_FOLDER: "db" + tty: true + restart: unless-stopped + ports: + - "2095:2095" + - "2096:2096" + networks: + - s-ui + entrypoint: "./sui" + + sing-box: + image: alireza7/s-ui-singbox + container_name: sing-box + volumes: + - "singbox:/app/" + - "$PWD/cert:/cert" + networks: + - s-ui + ports: + - "443:443" + - "1443:1443" + - "2443:2443" + - "3443:3443" + restart: unless-stopped + depends_on: + - s-ui + +networks: + s-ui: + driver: bridge + +volumes: + singbox: \ No newline at end of file