Skip to content

Commit 3fdd61a

Browse files
authored
Merge pull request #118 from yokowu/feat-data-report
feat: 安装数据上报
2 parents 867b490 + 8f9041b commit 3fdd61a

File tree

15 files changed

+383
-1
lines changed

15 files changed

+383
-1
lines changed

.github/workflows/backend-ci-cd.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ jobs:
8787
VERSION=${GITHUB_REF#refs/tags/}
8888
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
8989
90+
- name: Get build time
91+
id: get_build_time
92+
run: |
93+
BUILD_TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
94+
echo "BUILD_TIME=${BUILD_TIME}" >> $GITHUB_OUTPUT
95+
96+
- name: Get git commit
97+
id: get_git_commit
98+
run: |
99+
GIT_COMMIT=$(git rev-parse HEAD)
100+
echo "GIT_COMMIT=${GIT_COMMIT}" >> $GITHUB_OUTPUT
101+
90102
- name: Set up QEMU
91103
uses: docker/setup-qemu-action@v3
92104

@@ -114,5 +126,8 @@ jobs:
114126
GOCACHE=/tmp/go-build
115127
GOMODCACHE=/tmp/go-mod
116128
REPO_COMMIT=${{ github.sha }}
129+
VERSION=${{ steps.get_version.outputs.VERSION }}
130+
BUILD_TIME=${{ steps.get_build_time.outputs.BUILD_TIME }}
131+
GIT_COMMIT=${{ steps.get_git_commit.outputs.GIT_COMMIT }}
117132
cache-from: type=gha
118133
cache-to: type=gha,mode=max

backend/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ OUTPUT=type=docker,dest=${HOME}/tmp/monkeycode_server.tar
44
GOCACHE=${HOME}/.cache/go-build
55
GOMODCACHE=${HOME}/go/pkg/mod
66
REGISTRY=monkeycode.chaitin.cn/monkeycode
7+
VERSION=dev-${shell git rev-parse HEAD}
8+
BUILD_TIME=${shell date -u +"%Y-%m-%dT%H:%M:%SZ"}
9+
GIT_COMMIT=${shell git rev-parse HEAD}
710

811
# make build PLATFORM= TAG= OUTPUT= GOCACHE=
912
image:
@@ -12,6 +15,9 @@ image:
1215
--build-arg GOCACHE=${GOCACHE} \
1316
--build-arg GOMODCACHE=${GOMODCACHE} \
1417
--build-arg REPO_COMMIT=$(shell git rev-parse HEAD) \
18+
--build-arg VERSION=${VERSION} \
19+
--build-arg BUILD_TIME=${BUILD_TIME} \
20+
--build-arg GIT_COMMIT=${GIT_COMMIT} \
1521
--platform ${PLATFORM} \
1622
--tag ${REGISTRY}/backend:${TAG} \
1723
--output ${OUTPUT} \

backend/build/Dockerfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ RUN --mount=type=cache,target=${GOMODCACHE} \
99
go mod download
1010

1111
ARG TARGETOS TARGETARCH GOCACHE
12+
ARG VERSION
13+
ARG BUILD_TIME
14+
ARG GIT_COMMIT
1215
RUN --mount=type=bind,target=. \
1316
--mount=type=cache,target=${GOMODCACHE} \
1417
--mount=type=cache,target=${GOCACHE} \
15-
GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/main cmd/server/main.go cmd/server/wire_gen.go
18+
GOOS=$TARGETOS GOARCH=$TARGETARCH \
19+
go build \
20+
-ldflags "-w -s -X 'github.com/chaitin/MonkeyCode/backend/pkg/version.Version=${VERSION}' -X 'github.com/chaitin/MonkeyCode/backend/pkg/version.BuildTime=${BUILD_TIME}' -X 'github.com/chaitin/MonkeyCode/backend/pkg/version.GitCommit=${GIT_COMMIT}'" \
21+
-o /out/main \
22+
cmd/server/main.go cmd/server/wire_gen.go
1623

1724
FROM alpine:3.21 as binary
1825

backend/cmd/server/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func main() {
2424
panic(err)
2525
}
2626

27+
s.version.Print()
2728
s.logger.With("config", s.config).Debug("config")
2829

2930
if s.config.Debug {
@@ -40,6 +41,10 @@ func main() {
4041
panic(err)
4142
}
4243

44+
if err := s.report.ReportInstallation(); err != nil {
45+
panic(err)
46+
}
47+
4348
svc := service.NewService(service.WithPprof())
4449
svc.Add(s)
4550
if err := svc.Run(); err != nil {

backend/cmd/server/wire.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
v1 "github.com/chaitin/MonkeyCode/backend/internal/model/handler/http/v1"
1818
openaiV1 "github.com/chaitin/MonkeyCode/backend/internal/openai/handler/v1"
1919
userV1 "github.com/chaitin/MonkeyCode/backend/internal/user/handler/v1"
20+
"github.com/chaitin/MonkeyCode/backend/pkg/report"
21+
"github.com/chaitin/MonkeyCode/backend/pkg/version"
2022
)
2123

2224
type Server struct {
@@ -29,6 +31,8 @@ type Server struct {
2931
userV1 *userV1.UserHandler
3032
dashboardV1 *dashv1.DashboardHandler
3133
billingV1 *billingv1.BillingHandler
34+
version *version.VersionInfo
35+
report *report.Reporter
3236
}
3337

3438
func newServer() (*Server, error) {

backend/cmd/server/wire_gen.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ type Config struct {
6767
LimitSecond int `mapstructure:"limit_second"`
6868
Limit int `mapstructure:"limit"`
6969
} `mapstructure:"extension"`
70+
71+
DataReport struct {
72+
Key string `mapstructure:"key"`
73+
} `mapstructure:"data_report"`
7074
}
7175

7276
func Init() (*Config, error) {
@@ -103,6 +107,7 @@ func Init() (*Config, error) {
103107
v.SetDefault("extension.baseurl", "https://release.baizhi.cloud")
104108
v.SetDefault("extension.limit", 1)
105109
v.SetDefault("extension.limit_second", 10)
110+
v.SetDefault("data_report.key", "")
106111

107112
c := Config{}
108113
if err := v.Unmarshal(&c); err != nil {

backend/internal/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
userV1 "github.com/chaitin/MonkeyCode/backend/internal/user/handler/v1"
2525
userrepo "github.com/chaitin/MonkeyCode/backend/internal/user/repo"
2626
userusecase "github.com/chaitin/MonkeyCode/backend/internal/user/usecase"
27+
"github.com/chaitin/MonkeyCode/backend/pkg/version"
2728
)
2829

2930
var Provider = wire.NewSet(
@@ -50,4 +51,5 @@ var Provider = wire.NewSet(
5051
billingusecase.NewBillingUsecase,
5152
erepo.NewExtensionRepo,
5253
eusecase.NewExtensionUsecase,
54+
version.NewVersionInfo,
5355
)

backend/pkg/aes/aes.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package aes
2+
3+
import (
4+
"crypto/aes"
5+
"crypto/cipher"
6+
"crypto/rand"
7+
"encoding/base64"
8+
"errors"
9+
)
10+
11+
func Encrypt(key []byte, plaintext string) (string, error) {
12+
block, err := aes.NewCipher(key)
13+
if err != nil {
14+
return "", err
15+
}
16+
17+
gcm, err := cipher.NewGCM(block)
18+
if err != nil {
19+
return "", err
20+
}
21+
22+
nonce := make([]byte, gcm.NonceSize())
23+
if _, err := rand.Read(nonce); err != nil {
24+
return "", err
25+
}
26+
27+
ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
28+
29+
return base64.StdEncoding.EncodeToString(ciphertext), nil
30+
}
31+
32+
func Decrypt(key []byte, ciphertext string) (string, error) {
33+
data, err := base64.StdEncoding.DecodeString(ciphertext)
34+
if err != nil {
35+
return "", err
36+
}
37+
38+
block, err := aes.NewCipher(key)
39+
if err != nil {
40+
return "", err
41+
}
42+
43+
gcm, err := cipher.NewGCM(block)
44+
if err != nil {
45+
return "", err
46+
}
47+
48+
nonceSize := gcm.NonceSize()
49+
if len(data) < nonceSize {
50+
return "", errors.New("ciphertext too short")
51+
}
52+
53+
nonce, text := data[:nonceSize], data[nonceSize:]
54+
plaintext, err := gcm.Open(nil, nonce, text, nil)
55+
if err != nil {
56+
return "", err
57+
}
58+
59+
return string(plaintext), nil
60+
}

backend/pkg/machine/machine.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package machine
2+
3+
import (
4+
"crypto/md5"
5+
"crypto/sha256"
6+
"fmt"
7+
"net"
8+
"os"
9+
"runtime"
10+
"sort"
11+
"strings"
12+
)
13+
14+
type MachineInfo struct {
15+
Hostname string `json:"hostname"`
16+
MACAddresses []string `json:"mac_addresses"`
17+
OSType string `json:"os_type"`
18+
OSRelease string `json:"os_release"`
19+
CPUInfo string `json:"cpu_info"`
20+
}
21+
22+
func GetMachineInfo() (*MachineInfo, error) {
23+
hostname, err := os.Hostname()
24+
if err != nil {
25+
return nil, fmt.Errorf("failed to get hostname: %w", err)
26+
}
27+
28+
macAddresses, err := getMACAddresses()
29+
if err != nil {
30+
return nil, fmt.Errorf("failed to get MAC addresses: %w", err)
31+
}
32+
33+
return &MachineInfo{
34+
Hostname: hostname,
35+
MACAddresses: macAddresses,
36+
OSType: runtime.GOOS,
37+
OSRelease: getOSRelease(),
38+
CPUInfo: getCPUInfo(),
39+
}, nil
40+
}
41+
42+
func GenerateMachineID() (string, error) {
43+
machineInfo, err := GetMachineInfo()
44+
if err != nil {
45+
return "", err
46+
}
47+
48+
var parts []string
49+
parts = append(parts, machineInfo.Hostname)
50+
parts = append(parts, strings.Join(machineInfo.MACAddresses, ","))
51+
parts = append(parts, machineInfo.OSType)
52+
parts = append(parts, machineInfo.OSRelease)
53+
parts = append(parts, machineInfo.CPUInfo)
54+
55+
combined := strings.Join(parts, "|")
56+
hash := sha256.Sum256([]byte(combined))
57+
return fmt.Sprintf("%x", hash), nil
58+
}
59+
60+
func GenerateShortMachineID() (string, error) {
61+
machineInfo, err := GetMachineInfo()
62+
if err != nil {
63+
return "", err
64+
}
65+
66+
var parts []string
67+
parts = append(parts, machineInfo.Hostname)
68+
if len(machineInfo.MACAddresses) > 0 {
69+
parts = append(parts, machineInfo.MACAddresses[0])
70+
}
71+
parts = append(parts, machineInfo.OSType)
72+
73+
combined := strings.Join(parts, "|")
74+
hash := md5.Sum([]byte(combined))
75+
return fmt.Sprintf("%x", hash), nil
76+
}
77+
78+
func getMACAddresses() ([]string, error) {
79+
interfaces, err := net.Interfaces()
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
var macAddresses []string
85+
for _, iface := range interfaces {
86+
if iface.Flags&net.FlagLoopback != 0 || len(iface.HardwareAddr) == 0 {
87+
continue
88+
}
89+
macAddresses = append(macAddresses, iface.HardwareAddr.String())
90+
}
91+
92+
sort.Strings(macAddresses)
93+
return macAddresses, nil
94+
}
95+
96+
func getOSRelease() string {
97+
return runtime.GOARCH
98+
}
99+
100+
func getCPUInfo() string {
101+
return fmt.Sprintf("%s_%d", runtime.GOARCH, runtime.NumCPU())
102+
}

0 commit comments

Comments
 (0)