Bläddra i källkod

Improve build

世界 3 år sedan
förälder
incheckning
44fcfab9aa

+ 4 - 11
.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*

+ 2 - 1
.gitignore

@@ -3,4 +3,5 @@
 /*.json
 /*.db
 /site/
-/bin/
+/bin/
+/dist/

+ 1 - 1
.golangci.yml

@@ -17,4 +17,4 @@ linters-settings:
       - prefix(github.com/sagernet/)
       - default
   staticcheck:
-    go: '1.18'
+    go: '1.19'

+ 82 - 0
.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 <[email protected]>
+    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/[email protected]
+        dst: /etc/systemd/system/[email protected]
+      - 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

+ 53 - 0
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/[email protected]
+
+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

+ 31 - 5
cmd/sing-box/cmd_version.go

@@ -17,11 +17,37 @@ var commandVersion = &cobra.Command{
 	Args:  cobra.NoArgs,
 }
 
+var nameOnly bool
+
+func init() {
+	commandVersion.Flags().BoolVarP(&nameOnly, "name", "n", false, "print version name only")
+}
+
 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 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)
 }

+ 2 - 2
constant/version.go

@@ -1,6 +1,6 @@
 package constant
 
 var (
-	Version   = "nightly"
-	BuildTime = "unknown"
+	Version = "0.1.0"
+	Commit  = ""
 )

+ 0 - 7
format.go

@@ -1,7 +0,0 @@
-package box
-
-//go:generate go install -v mvdan.cc/gofumpt@latest
-//go:generate go install -v github.com/daixiang0/[email protected]
-//go:generate gofumpt -l -w .
-//go:generate gofmt -s -w .
-//go:generate gci write -s "standard,prefix(github.com/sagernet/),default" .

+ 0 - 0
release/local/config.json → release/config/config.json


+ 15 - 0
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

+ 15 - 0
release/config/[email protected]

@@ -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

+ 1 - 1
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

+ 2 - 0
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