From 44fcfab9aa31a332ae34df5ba78bdee05fc3cee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 12 Aug 2022 22:53:46 +0800 Subject: [PATCH] Improve build --- .github/workflows/debug.yml | 15 ++--- .gitignore | 3 +- .golangci.yml | 2 +- .goreleaser.yaml | 82 +++++++++++++++++++++++++++ Makefile | 53 +++++++++++++++++ cmd/sing-box/cmd_version.go | 40 ++++++++++--- constant/version.go | 4 +- format.go | 7 --- release/{local => config}/config.json | 0 release/config/sing-box.service | 15 +++++ release/config/sing-box@.service | 15 +++++ release/local/install.sh | 2 +- release/local/sing-box.service | 2 + 13 files changed, 210 insertions(+), 30 deletions(-) create mode 100644 .goreleaser.yaml create mode 100644 Makefile delete mode 100644 format.go rename release/{local => config}/config.json (100%) create mode 100644 release/config/sing-box.service create mode 100644 release/config/sing-box@.service diff --git a/.github/workflows/debug.yml b/.github/workflows/debug.yml index 7147a8d2..304b0b2c 100644 --- a/.github/workflows/debug.yml +++ b/.github/workflows/debug.yml @@ -47,6 +47,7 @@ jobs: go mod init build go get -v github.com/sagernet/sing-box@$version popd + continue-on-error: true - name: Run Test run: | go test -v ./... @@ -160,6 +161,7 @@ jobs: GOARM: ${{ matrix.goarm }} GOMIPS: ${{ matrix.gomips }} CGO_ENABLED: 0 + TAGS: with_clash_api,with_quic steps: - name: Checkout uses: actions/checkout@v2 @@ -181,18 +183,9 @@ jobs: key: go-${{ hashFiles('**/go.sum') }} - name: Build id: build - run: | - VERSION="$(date +%Y%m%d).$(git rev-parse --short HEAD)" - BUILDTIME="$(LANG=en_US.UTF-8 date -u)" - - go build -v -trimpath -ldflags '\ - -X "github.com/sagernet/sing-box/constant.Version=$VERSION" \ - -X "github.com/sagernet/sing-box/constant.BuildTime=$BUILDTIME" \ - -s -w -buildid=' ./cmd/sing-box - - echo "::set-output name=VERSION::$VERSION" + run: make - name: Upload artifact uses: actions/upload-artifact@v2 with: - name: sing-box-${{ matrix.name }}-${{ steps.build.outputs.VERSION }} + name: sing-box-${{ matrix.name }} path: sing-box* \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9f9a3d27..25408018 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /*.json /*.db /site/ -/bin/ \ No newline at end of file +/bin/ +/dist/ \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml index 012d08a1..88628eca 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -17,4 +17,4 @@ linters-settings: - prefix(github.com/sagernet/) - default staticcheck: - go: '1.18' + go: '1.19' diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 00000000..3d6a3987 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,82 @@ +project_name: sing-box +builds: + - main: ./cmd/sing-box + flags: + - -v + - -trimpath + asmflags: + - all=-trimpath={{.Env.GOPATH}} + gcflags: + - all=-trimpath={{.Env.GOPATH}} + ldflags: + - -X github.com/sagernet/sing-box/constant.Commit={{ .ShortCommit }} -s -w -buildid= + tags: + - with_clash_api + - with_quic + env: + - CGO_ENABLED=0 + targets: + - linux_amd64_v1 + - linux_amd64_v3 + - linux_arm64 + - linux_arm_7 + - windows_amd64_v1 + - windows_amd64_v3 + - windows_386 + - windows_arm64 + - darwin_amd64_v1 + - darwin_amd64_v3 + - darwin_arm64 + mod_timestamp: '{{ .CommitTimestamp }}' +snapshot: + name_template: "{{ .Version }}.{{ .ShortCommit }}" +archives: + - id: archive + format: tar.gz + format_overrides: + - goos: windows + format: zip + wrap_in_directory: true + files: + - LICENSE + - src: release/config/config.json + strip_parent: true + name_template: '{{ .ProjectName }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}' +nfpms: + - id: package + package_name: sing-box + file_name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}' + vendor: sagernet + homepage: https://sing-box.sagernet.org/ + maintainer: nekohasekai + description: The universal proxy platform. + license: GPLv3 or later + formats: + - deb + - rpm + priority: extra + contents: + - src: release/config/config.json + dst: /etc/sing-box/config.json + type: config + - src: release/config/sing-box.service + dst: /etc/systemd/system/sing-box.service + - src: release/config/sing-box@.service + dst: /etc/systemd/system/sing-box@.service + - src: LICENSE + dst: /usr/share/licenses/sing-box/LICENSE +source: + enabled: true + name_template: '{{ .ProjectName }}-{{ .Version }}.source' + prefix_template: '{{ .ProjectName }}-{{ .Version }}/' +checksum: + name_template: '{{ .ProjectName }}-{{ .Version }}.checksum' +signs: + - artifacts: checksum +release: + github: + owner: SagerNet + name: sing-box + name_template: '{{ if .IsSnapshot }}{{ nightly }}{{ else }}{{ .Version }}{{ end }}' + draft: true + mode: replace \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..527d4ec4 --- /dev/null +++ b/Makefile @@ -0,0 +1,53 @@ +NAME=sing-box +COMMIT=$(shell git rev-parse --short HEAD) +PARAMS=-trimpath -tags '$(TAGS)' -ldflags \ + '-X "github.com/sagernet/sing-box/constant.Commit=$(COMMIT)" \ + -w -s -buildid=' +MAIN=./cmd/sing-box + +.PHONY: test release + +build: + go build $(PARAMS) $(MAIN) + +action_version: build + echo "::set-output name=VERSION::`./sing-box version -n`" + +install: + go install $(PARAMS) $(MAIN) + +release: + goreleaser release --snapshot --rm-dist + +fmt_install: + go install -v mvdan.cc/gofumpt@latest + go install -v github.com/daixiang0/gci@v0.4.0 + +fmt: + gofumpt -l -w . + gofmt -s -w . + gci write -s "standard,prefix(github.com/sagernet/),default" . + +lint_install: + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + +lint: + GOOS=linux golangci-lint run ./... + GOOS=windows golangci-lint run ./... + GOOS=darwin golangci-lint run ./... + GOOS=freebsd golangci-lint run ./... + +test: + go test -v . && \ + pushd test && \ + go test -v . && \ + popd + +clean: + rm -rf bin dist + rm -f $(shell go env GOPATH)/sing-box + +update: + git fetch + git reset FETCH_HEAD --hard + git clean -fdx \ No newline at end of file diff --git a/cmd/sing-box/cmd_version.go b/cmd/sing-box/cmd_version.go index 6327d814..e1ecc381 100644 --- a/cmd/sing-box/cmd_version.go +++ b/cmd/sing-box/cmd_version.go @@ -17,11 +17,37 @@ var commandVersion = &cobra.Command{ Args: cobra.NoArgs, } -func printVersion(cmd *cobra.Command, args []string) { - os.Stderr.WriteString(F.ToString("sing-box version ", C.Version, " (", runtime.Version(), ", ", runtime.GOOS, "/", runtime.GOARCH, ", CGO ")) - if C.CGO_ENABLED { - os.Stderr.WriteString("enabled)\n") - } else { - os.Stderr.WriteString("disabled)\n") - } +var nameOnly bool + +func init() { + commandVersion.Flags().BoolVarP(&nameOnly, "name", "n", false, "print version name only") +} + +func printVersion(cmd *cobra.Command, args []string) { + var version string + if !nameOnly { + version = "sing-box " + } + version += F.ToString(C.Version) + if C.Commit != "" { + version += "." + C.Commit + } + if !nameOnly { + version += " (" + version += runtime.Version() + version += ", " + version += runtime.GOOS + version += ", " + version += runtime.GOARCH + version += ", " + version += "CGO " + if C.CGO_ENABLED { + version += "enabled" + } else { + version += "disabled" + } + version += ")" + } + version += "\n" + os.Stdout.WriteString(version) } diff --git a/constant/version.go b/constant/version.go index 037699f4..69cf5449 100644 --- a/constant/version.go +++ b/constant/version.go @@ -1,6 +1,6 @@ package constant var ( - Version = "nightly" - BuildTime = "unknown" + Version = "0.1.0" + Commit = "" ) diff --git a/format.go b/format.go deleted file mode 100644 index 7b30b9ac..00000000 --- a/format.go +++ /dev/null @@ -1,7 +0,0 @@ -package box - -//go:generate go install -v mvdan.cc/gofumpt@latest -//go:generate go install -v github.com/daixiang0/gci@v0.4.0 -//go:generate gofumpt -l -w . -//go:generate gofmt -s -w . -//go:generate gci write -s "standard,prefix(github.com/sagernet/),default" . diff --git a/release/local/config.json b/release/config/config.json similarity index 100% rename from release/local/config.json rename to release/config/config.json diff --git a/release/config/sing-box.service b/release/config/sing-box.service new file mode 100644 index 00000000..6369b1ba --- /dev/null +++ b/release/config/sing-box.service @@ -0,0 +1,15 @@ +[Unit] +Description=sing-box service +Documentation=https://sing-box.sagernet.org +After=network.target nss-lookup.target + +[Service] +CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE +AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE +ExecStart=/usr/bin/sing-box run -c /etc/sing-box/config.json +Restart=on-failure +RestartSec=10s +LimitNOFILE=infinity + +[Install] +WantedBy=multi-user.target diff --git a/release/config/sing-box@.service b/release/config/sing-box@.service new file mode 100644 index 00000000..e12a46e1 --- /dev/null +++ b/release/config/sing-box@.service @@ -0,0 +1,15 @@ +[Unit] +Description=sing-box service +Documentation=https://sing-box.sagernet.org +After=network.target nss-lookup.target + +[Service] +CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE +AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE +ExecStart=/usr/bin/sing-box run -c /etc/sing-box/%i.json +Restart=on-failure +RestartSec=10s +LimitNOFILE=infinity + +[Install] +WantedBy=multi-user.target diff --git a/release/local/install.sh b/release/local/install.sh index 13846e6a..17db8a9d 100755 --- a/release/local/install.sh +++ b/release/local/install.sh @@ -11,6 +11,6 @@ popd sudo cp $(go env GOPATH)/bin/sing-box /usr/local/bin/ sudo mkdir -p /usr/local/etc/sing-box -sudo cp $DIR/config.json /usr/local/etc/sing-box/config.json +sudo cp $PROJECT/release/config/config.json /usr/local/etc/sing-box/config.json sudo cp $DIR/sing-box.service /etc/systemd/system sudo systemctl daemon-reload diff --git a/release/local/sing-box.service b/release/local/sing-box.service index 0943fcef..9a234645 100644 --- a/release/local/sing-box.service +++ b/release/local/sing-box.service @@ -4,6 +4,8 @@ Documentation=https://sing-box.sagernet.org After=network.target nss-lookup.target [Service] +CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE +AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE ExecStart=/usr/local/bin/sing-box run -c /usr/local/etc/sing-box/config.json Restart=on-failure RestartSec=10s