Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .build.config.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# AntiHook 构建配置文件
# 复制此文件为 .build.config 并修改配置

# Kiro 服务器地址
# 默认: http://localhost:8045
SERVER_URL="http://localhost:8045"

# 后端服务器地址
# 默认: http://localhost:8008
BACKEND_URL="http://localhost:8008"

# 生产环境示例:
# SERVER_URL="https://your-kiro-server.com"
# BACKEND_URL="https://your-backend-server.com"
109 changes: 93 additions & 16 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ env:
BACKEND_URL: ${{ secrets.BACKEND_URL || 'http://localhost:8008' }}

jobs:
build-and-release:
name: Build and Release
runs-on: windows-latest
build:
name: Build ${{ matrix.os }}
runs-on: ${{ matrix.os }}
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
permissions:
contents: write
strategy:
matrix:
include:
- os: windows-latest
goos: windows
goarch: amd64
output: antihook.exe
- os: macos-latest
goos: darwin
goarch: amd64
output: antihook-darwin-amd64
- os: macos-latest
goos: darwin
goarch: arm64
output: antihook-darwin-arm64

steps:
- name: Checkout code
Expand All @@ -34,19 +47,64 @@ jobs:

- name: Generate version
id: version
shell: bash
run: |
$short_sha = "${{ github.sha }}".Substring(0, 7)
$date = Get-Date -Format "yyyyMMdd-HHmmss"
$version = "dev-$date-$short_sha"
echo "VERSION=$version" >> $env:GITHUB_OUTPUT
short_sha=$(echo "${{ github.sha }}" | cut -c1-7)
date=$(date -u +"%Y%m%d-%H%M%S")
version="dev-$date-$short_sha"
echo "VERSION=$version" >> $GITHUB_OUTPUT
echo "Generated version: $version"
shell: pwsh

- name: Build
run: go build -v -ldflags="-s -w -X main.DefaultServerURL=${{ env.KIRO_SERVER_URL }} -X main.DefaultBackendURL=${{ env.BACKEND_URL }}" -o antihook.exe .
env:
KIRO_SERVER_URL: ${{ secrets.KIRO_SERVER_URL || 'http://localhost:8045' }}
BACKEND_URL: ${{ secrets.BACKEND_URL || 'http://localhost:8008' }}
- name: Build for ${{ matrix.goos }}-${{ matrix.goarch }}
shell: bash
run: |
LDFLAGS="-s -w"
LDFLAGS="$LDFLAGS -X 'main.DefaultServerURL=${{ secrets.KIRO_SERVER_URL || 'http://localhost:8045' }}'"
LDFLAGS="$LDFLAGS -X 'main.DefaultBackendURL=${{ secrets.BACKEND_URL || 'http://localhost:8008' }}'"
LDFLAGS="$LDFLAGS -X 'main.BuildVersion=${{ steps.version.outputs.VERSION }}'"
LDFLAGS="$LDFLAGS -X 'main.BuildTime=$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"

GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} \
go build -v -ldflags="$LDFLAGS" -o ${{ matrix.output }} .

echo "Built: ${{ matrix.output }}"
ls -lh ${{ matrix.output }}

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.output }}
path: ${{ matrix.output }}
retention-days: 7

release:
name: Create Release
needs: build
runs-on: ubuntu-latest
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
permissions:
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Generate version
id: version
run: |
short_sha=$(echo "${{ github.sha }}" | cut -c1-7)
date=$(date -u +"%Y%m%d-%H%M%S")
version="dev-$date-$short_sha"
echo "VERSION=$version" >> $GITHUB_OUTPUT
echo "Generated version: $version"

- name: Download all artifacts
uses: actions/download-artifact@v3
with:
path: artifacts

- name: Display structure of downloaded files
run: ls -R artifacts

- name: Create and push tag
run: |
Expand All @@ -62,9 +120,28 @@ jobs:
with:
tag_name: ${{ steps.version.outputs.VERSION }}
name: Release ${{ steps.version.outputs.VERSION }}
files: antihook.exe
files: |
artifacts/antihook.exe/antihook.exe
artifacts/antihook-darwin-amd64/antihook-darwin-amd64
artifacts/antihook-darwin-arm64/antihook-darwin-arm64
draft: false
prerelease: false
generate_release_notes: true
body: |
## 📦 构建产物

- `antihook.exe` - Windows (amd64)
- `antihook-darwin-amd64` - macOS Intel
- `antihook-darwin-arm64` - macOS Apple Silicon

## 🔧 配置信息

- Kiro 服务器: ${{ secrets.KIRO_SERVER_URL || 'http://localhost:8045' }}
- 后端服务器: ${{ secrets.BACKEND_URL || 'http://localhost:8008' }}
- 构建版本: ${{ steps.version.outputs.VERSION }}

## 📖 使用说明

请参考 [README.md](https://github.com/${{ github.repository }}/blob/main/README.md) 了解详细的安装和使用方法。
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

/bin/
/dist/
/build/

# 构建产物
antihook
antihook-*
*.app

logs/
*.log
Expand All @@ -21,4 +27,7 @@ go.work
*~

.DS_Store
Thumbs.db
Thumbs.db

# 构建配置(包含敏感信息)
.build.config
178 changes: 178 additions & 0 deletions BUILD_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# AntiHook 构建指南

本指南说明如何配置和构建 AntiHook,使打包后的程序自动使用您的后端地址。

## 🎯 快速开始

### 1. 创建配置文件

```bash
# 复制配置模板
cp .build.config.example .build.config

# 编辑配置文件
vim .build.config # 或使用您喜欢的编辑器
```

### 2. 配置您的后端地址

在 `.build.config` 文件中设置:

```bash
# Kiro 服务器地址
SERVER_URL="https://your-kiro-server.com"

# 后端服务器地址
BACKEND_URL="https://your-backend-server.com"
```

### 3. 构建程序

```bash
# 构建所有平台
./build.sh all

# 或构建特定平台
./build.sh darwin # macOS
./build.sh windows # Windows
./build.sh linux # Linux
```

构建完成后,生成的二进制文件会自动包含您配置的后端地址!

## 📋 配置优先级

AntiHook 支持三种配置方式,优先级从高到低:

1. **运行时环境变量**(最高优先级)
```bash
export KIRO_SERVER_URL="https://runtime-server.com"
export BACKEND_URL="https://runtime-backend.com"
./antihook
```

2. **构建时配置文件**(推荐用于发布)
```bash
# .build.config 文件
SERVER_URL="https://build-server.com"
BACKEND_URL="https://build-backend.com"
```

3. **构建时环境变量**
```bash
SERVER_URL="https://env-server.com" ./build.sh all
```

4. **默认值**(开发环境)
- SERVER_URL: `http://localhost:8045`
- BACKEND_URL: `http://localhost:8008`

## 🔧 使用场景

### 场景一:本地开发

不需要任何配置,直接构建即可使用 localhost:

```bash
go build -o antihook .
./antihook
```

### 场景二:生产环境发布

1. 创建 `.build.config` 配置生产环境地址
2. 构建发布版本
3. 分发给用户

```bash
# .build.config
SERVER_URL="https://prod-kiro.example.com"
BACKEND_URL="https://prod-api.example.com"

# 构建
./build.sh all

# 构建产物在 build/ 目录
ls -lh build/
```

### 场景三:多环境构建

为不同环境构建不同版本:

```bash
# 测试环境
SERVER_URL="https://test-api.com" \
BACKEND_URL="https://test-backend.com" \
./build.sh darwin
mv build/antihook-darwin-amd64 build/antihook-darwin-amd64-test

# 生产环境
SERVER_URL="https://prod-api.com" \
BACKEND_URL="https://prod-backend.com" \
./build.sh darwin
mv build/antihook-darwin-amd64 build/antihook-darwin-amd64-prod
```

## 🔍 验证构建配置

构建完成后,可以验证配置是否正确注入:

```bash
# macOS/Linux
strings build/antihook-darwin-amd64 | grep -E "https?://"

# Windows (使用 PowerShell)
Select-String -Path build/antihook-windows-amd64.exe -Pattern "https?://" -AllMatches
```

您应该能看到配置的 URL 地址。

## 📝 注意事项

1. **`.build.config` 文件已添加到 `.gitignore`**
- 不会被提交到版本控制
- 可以安全地包含敏感信息

2. **配置文件格式**
- 使用 Bash 变量格式
- URL 必须包含协议(http:// 或 https://)
- 不要在末尾添加斜杠

3. **构建信息**
- 构建时会自动注入构建时间和版本号
- 可用于追踪和调试

## 🚀 CI/CD 集成

在 CI/CD 流水线中使用环境变量:

```yaml
# GitHub Actions 示例
- name: Build AntiHook
env:
SERVER_URL: ${{ secrets.PROD_SERVER_URL }}
BACKEND_URL: ${{ secrets.PROD_BACKEND_URL }}
run: ./build.sh all
```

## 💡 最佳实践

1. **开发环境**:不使用配置文件,使用默认 localhost
2. **测试环境**:使用 `.build.config` 配置测试服务器
3. **生产环境**:使用 CI/CD 环境变量或专门的配置文件
4. **多环境**:为每个环境维护单独的配置文件

## ❓ 常见问题

**Q: 构建后的程序还能通过环境变量修改地址吗?**
A: 可以!运行时环境变量的优先级最高,会覆盖构建时的配置。

**Q: 如何验证程序使用的是哪个地址?**
A: 可以通过抓包工具(如 Charles、Wireshark)查看实际请求的地址。

**Q: 可以不同平台使用不同配置吗?**
A: 可以,在构建特定平台前临时修改 `.build.config` 或使用环境变量。

**Q: 配置文件支持注释吗?**
A: 支持!使用 `#` 开头的行会被忽略。
Loading