1
0

common.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env bash
  2. set -e -o pipefail
  3. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  4. PROJECT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
  5. BINARY_NAME="sing-box"
  6. INSTALL_BIN_PATH="/usr/local/bin"
  7. INSTALL_CONFIG_PATH="/usr/local/etc/sing-box"
  8. INSTALL_DATA_PATH="/var/lib/sing-box"
  9. SYSTEMD_SERVICE_PATH="/etc/systemd/system"
  10. DEFAULT_BUILD_TAGS="with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_acme,with_clash_api,with_tailscale,with_ccm,badlinkname,tfogo_checklinkname0"
  11. setup_environment() {
  12. if [ -d /usr/local/go ]; then
  13. export PATH="$PATH:/usr/local/go/bin"
  14. fi
  15. if ! command -v go &> /dev/null; then
  16. echo "Error: Go is not installed or not in PATH"
  17. echo "Run install_go.sh to install Go"
  18. exit 1
  19. fi
  20. }
  21. get_build_tags() {
  22. local extra_tags="$1"
  23. if [ -n "$extra_tags" ]; then
  24. echo "${DEFAULT_BUILD_TAGS},${extra_tags}"
  25. else
  26. echo "${DEFAULT_BUILD_TAGS}"
  27. fi
  28. }
  29. get_version() {
  30. cd "$PROJECT_DIR"
  31. GOHOSTOS=$(go env GOHOSTOS)
  32. GOHOSTARCH=$(go env GOHOSTARCH)
  33. CGO_ENABLED=0 GOOS=$GOHOSTOS GOARCH=$GOHOSTARCH go run github.com/sagernet/sing-box/cmd/internal/read_tag@latest
  34. }
  35. get_ldflags() {
  36. local version
  37. version=$(get_version)
  38. echo "-X 'github.com/sagernet/sing-box/constant.Version=${version}' -s -w -buildid= -checklinkname=0"
  39. }
  40. build_sing_box() {
  41. local tags="$1"
  42. local ldflags
  43. ldflags=$(get_ldflags)
  44. echo "Building sing-box with tags: $tags"
  45. cd "$PROJECT_DIR"
  46. export GOTOOLCHAIN=local
  47. go install -v -trimpath -ldflags "$ldflags" -tags "$tags" ./cmd/sing-box
  48. }
  49. install_binary() {
  50. local gopath
  51. gopath=$(go env GOPATH)
  52. echo "Installing binary to $INSTALL_BIN_PATH/$BINARY_NAME"
  53. sudo cp "${gopath}/bin/${BINARY_NAME}" "${INSTALL_BIN_PATH}/"
  54. }
  55. setup_config() {
  56. echo "Setting up configuration"
  57. sudo mkdir -p "$INSTALL_CONFIG_PATH"
  58. if [ ! -f "$INSTALL_CONFIG_PATH/config.json" ]; then
  59. sudo cp "$PROJECT_DIR/release/config/config.json" "$INSTALL_CONFIG_PATH/config.json"
  60. echo "Default config installed to $INSTALL_CONFIG_PATH/config.json"
  61. else
  62. echo "Config already exists at $INSTALL_CONFIG_PATH/config.json (not overwriting)"
  63. fi
  64. }
  65. setup_systemd() {
  66. echo "Setting up systemd service"
  67. sudo cp "$SCRIPT_DIR/sing-box.service" "$SYSTEMD_SERVICE_PATH/"
  68. sudo systemctl daemon-reload
  69. }
  70. stop_service() {
  71. if systemctl is-active --quiet sing-box; then
  72. echo "Stopping sing-box service"
  73. sudo systemctl stop sing-box
  74. fi
  75. }
  76. start_service() {
  77. echo "Starting sing-box service"
  78. sudo systemctl start sing-box
  79. }
  80. restart_service() {
  81. echo "Restarting sing-box service"
  82. sudo systemctl restart sing-box
  83. }