Skip to content

Commit 9ba60da

Browse files
committed
feat: add machine-id util
1 parent 9011712 commit 9ba60da

3 files changed

Lines changed: 203 additions & 8 deletions

File tree

go.mod

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/omalloc/contrib
22

3-
go 1.23.0
4-
5-
toolchain go1.23.4
3+
go 1.24.0
64

75
require (
86
github.com/DATA-DOG/go-sqlmock v1.5.2
@@ -13,7 +11,9 @@ require (
1311
github.com/go-kratos/kratos/contrib/registry/etcd/v2 v2.0.0-20250312125852-142ea0a93a9f
1412
github.com/go-kratos/kratos/v2 v2.8.4
1513
github.com/go-resty/resty/v2 v2.16.5
16-
github.com/stretchr/testify v1.10.0
14+
github.com/samber/lo v1.53.0
15+
github.com/shirou/gopsutil/v4 v4.26.3
16+
github.com/stretchr/testify v1.11.1
1717
github.com/szyhf/go-gcache/v2 v2.0.0-20221228090800-6d7d42eb5558
1818
go.etcd.io/etcd/client/v3 v3.5.19
1919
go.opentelemetry.io/contrib v1.35.0
@@ -37,12 +37,14 @@ require (
3737
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
3838
github.com/davecgh/go-spew v1.1.1 // indirect
3939
github.com/dustin/go-humanize v1.0.1 // indirect
40+
github.com/ebitengine/purego v0.10.0 // indirect
4041
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
4142
github.com/gin-contrib/sse v1.0.0 // indirect
4243
github.com/glebarez/go-sqlite v1.22.0 // indirect
4344
github.com/go-kratos/aegis v0.2.0 // indirect
4445
github.com/go-logr/logr v1.4.2 // indirect
4546
github.com/go-logr/stdr v1.2.2 // indirect
47+
github.com/go-ole/go-ole v1.2.6 // indirect
4648
github.com/go-playground/form/v4 v4.2.1 // indirect
4749
github.com/go-playground/locales v0.14.1 // indirect
4850
github.com/go-playground/universal-translator v0.18.1 // indirect
@@ -58,15 +60,20 @@ require (
5860
github.com/json-iterator/go v1.1.12 // indirect
5961
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
6062
github.com/leodido/go-urn v1.4.0 // indirect
63+
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a // indirect
6164
github.com/mattn/go-isatty v0.0.20 // indirect
6265
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6366
github.com/modern-go/reflect2 v1.0.2 // indirect
6467
github.com/ncruces/go-strftime v0.1.9 // indirect
6568
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
6669
github.com/pmezard/go-difflib v1.0.0 // indirect
70+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
6771
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
72+
github.com/tklauser/go-sysconf v0.3.16 // indirect
73+
github.com/tklauser/numcpus v0.11.0 // indirect
6874
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
6975
github.com/ugorji/go/codec v1.2.12 // indirect
76+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
7077
go.etcd.io/etcd/api/v3 v3.5.19 // indirect
7178
go.etcd.io/etcd/client/pkg/v3 v3.5.19 // indirect
7279
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
@@ -78,7 +85,7 @@ require (
7885
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
7986
golang.org/x/net v0.37.0 // indirect
8087
golang.org/x/sync v0.12.0 // indirect
81-
golang.org/x/sys v0.31.0 // indirect
88+
golang.org/x/sys v0.41.0 // indirect
8289
golang.org/x/text v0.23.0 // indirect
8390
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
8491
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect

go.sum

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
2121
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2222
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
2323
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
24+
github.com/ebitengine/purego v0.10.0 h1:QIw4xfpWT6GWTzaW5XEKy3HXoqrJGx1ijYHzTF0/ISU=
25+
github.com/ebitengine/purego v0.10.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
2426
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
2527
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
2628
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
@@ -44,6 +46,8 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
4446
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
4547
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
4648
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
49+
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
50+
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
4751
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
4852
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
4953
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
@@ -67,6 +71,7 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
6771
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
6872
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
6973
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
74+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7075
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
7176
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
7277
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -95,6 +100,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
95100
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
96101
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
97102
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
103+
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a h1:N9zuLhTvBSRt0gWSiJswwQ2HqDmtX/ZCDJURnKUt1Ik=
104+
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a/go.mod h1:JKx41uQRwqlTZabZc+kILPrO/3jlKnQ2Z8b7YiVw5cE=
98105
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
99106
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
100107
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -108,10 +115,16 @@ github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNH
108115
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
109116
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
110117
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
118+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
119+
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
111120
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
112121
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
113122
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
114123
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
124+
github.com/samber/lo v1.53.0 h1:t975lj2py4kJPQ6haz1QMgtId2gtmfktACxIXArw3HM=
125+
github.com/samber/lo v1.53.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
126+
github.com/shirou/gopsutil/v4 v4.26.3 h1:2ESdQt90yU3oXF/CdOlRCJxrP+Am1aBYubTMTfxJ1qc=
127+
github.com/shirou/gopsutil/v4 v4.26.3/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ=
115128
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
116129
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
117130
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -123,16 +136,23 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
123136
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
124137
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
125138
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
126-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
127139
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
140+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
141+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
128142
github.com/szyhf/go-gcache/v2 v2.0.0-20221228090800-6d7d42eb5558 h1:E/lPwp59Sl/D1nG7BckwpSeT5jyTM52FaQwQtUAZqhE=
129143
github.com/szyhf/go-gcache/v2 v2.0.0-20221228090800-6d7d42eb5558/go.mod h1:dS8opzy6F15R6IuvkuOT9xQWDygItjLNTDytkd2F9wk=
144+
github.com/tklauser/go-sysconf v0.3.16 h1:frioLaCQSsF5Cy1jgRBrzr6t502KIIwQ0MArYICU0nA=
145+
github.com/tklauser/go-sysconf v0.3.16/go.mod h1:/qNL9xxDhc7tx3HSRsLWNnuzbVfh3e7gh/BmM179nYI=
146+
github.com/tklauser/numcpus v0.11.0 h1:nSTwhKH5e1dMNsCdVBukSZrURJRoHbSEQjdEbY+9RXw=
147+
github.com/tklauser/numcpus v0.11.0/go.mod h1:z+LwcLq54uWZTX0u/bGobaV34u6V7KNlTZejzM6/3MQ=
130148
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
131149
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
132150
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
133151
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
134152
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
135153
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
154+
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
155+
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
136156
go.etcd.io/etcd/api/v3 v3.5.19 h1:w3L6sQZGsWPuBxRQ4m6pPP3bVUtV8rjW033EGwlr0jw=
137157
go.etcd.io/etcd/api/v3 v3.5.19/go.mod h1:QqKGViq4KTgOG43dr/uH0vmGWIaoJY3ggFi6ZH0TH/U=
138158
go.etcd.io/etcd/client/pkg/v3 v3.5.19 h1:9VsyGhg0WQGjDWWlDI4VuaS9PZJGNbPkaHEIuLwtixk=
@@ -191,10 +211,12 @@ golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
191211
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
192212
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
193213
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
214+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
194215
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
216+
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
195217
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
196-
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
197-
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
218+
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
219+
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
198220
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
199221
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
200222
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=

machineid/machineid.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package machineid
2+
3+
import (
4+
"crypto/sha1"
5+
"fmt"
6+
"math/big"
7+
"net"
8+
"os"
9+
"path/filepath"
10+
"runtime"
11+
"strings"
12+
13+
"github.com/samber/lo"
14+
"github.com/shirou/gopsutil/v4/cpu"
15+
)
16+
17+
type MachineCPUInfo struct {
18+
VendorID string
19+
ModeName string
20+
Cores uint32
21+
CpuMHz float64
22+
}
23+
24+
type MachineInfo struct {
25+
arch string
26+
archMark string // AMD64 = 22, AARCH64 = 24, UNKNOWN = 20
27+
cores uint32 // cpu cores
28+
vendorID string
29+
cpuMHz float64
30+
modelName string
31+
productUUID string
32+
defaultGatewayIface string
33+
defaultGatewayMAC string
34+
}
35+
36+
func Load() MachineInfo {
37+
m := MachineInfo{}
38+
39+
cpus := getCPUs()
40+
m.arch = runtime.GOARCH
41+
m.archMark = archMark(m.arch)
42+
m.cores = cpus.Cores
43+
m.vendorID = cpus.VendorID
44+
m.cpuMHz = cpus.CpuMHz
45+
m.modelName = cpus.ModeName
46+
m.productUUID = getProductUUID()
47+
m.defaultGatewayIface = getDefaultRoute()
48+
m.defaultGatewayMAC = getIfaceMAC(m.defaultGatewayIface)
49+
return m
50+
}
51+
52+
func (m MachineInfo) SN() string {
53+
sb := strings.Builder{}
54+
55+
sb.WriteString(m.arch) // CPU ARCH
56+
sb.WriteString(m.modelName) // CPU名称
57+
sb.WriteRune(rune(m.cores)) // 核心数
58+
sb.WriteString(m.productUUID) // 主板序列号
59+
sb.WriteString(m.defaultGatewayMAC) // 默认路由的MAC地址
60+
61+
h := sha1.New()
62+
h.Write([]byte(sb.String()))
63+
hash := h.Sum(nil)
64+
65+
mark := m.archMark
66+
digit := hashToFixedDigits(hash, 26)
67+
return fmt.Sprintf("%s00%s", mark, digit)
68+
}
69+
70+
func getCPUs() MachineCPUInfo {
71+
m := MachineCPUInfo{}
72+
73+
infos, err := cpu.Info()
74+
if err != nil {
75+
return m
76+
}
77+
78+
cores := len(infos)
79+
m.Cores = uint32(cores)
80+
m.VendorID = infos[0].VendorID
81+
m.ModeName = infos[0].ModelName
82+
m.CpuMHz = infos[0].Mhz
83+
return m
84+
}
85+
86+
// Get /sys/class/dmi/id/product_uuid
87+
func getProductUUID() string {
88+
data, err := os.ReadFile(filepath.Clean("/sys/class/dmi/id/product_uuid"))
89+
if err != nil {
90+
// backport
91+
return getMachineID()
92+
}
93+
return string(data)
94+
}
95+
96+
// Get /etc/machine-id
97+
func getMachineID() string {
98+
data, err := os.ReadFile(filepath.Clean("/etc/machine-id"))
99+
if err != nil {
100+
return "unknown"
101+
}
102+
return strings.TrimSpace(string(data))
103+
}
104+
105+
func getDefaultRoute() string {
106+
data, err := os.ReadFile(filepath.Clean("/proc/net/route"))
107+
if err != nil {
108+
return ""
109+
}
110+
111+
routes := make([]lo.Tuple2[string, string], 0)
112+
113+
lines := strings.Split(string(data), "\n")
114+
for _, line := range lines {
115+
fields := strings.Fields(line)
116+
if len(fields) < 2 {
117+
continue
118+
}
119+
120+
routes = append(routes, lo.Tuple2[string, string]{
121+
A: fields[0],
122+
B: fields[1],
123+
})
124+
}
125+
126+
defaultRoute := ""
127+
for _, route := range routes {
128+
if route.B == "00000000" {
129+
defaultRoute = route.A
130+
break
131+
}
132+
}
133+
return defaultRoute
134+
}
135+
136+
func getIfaceMAC(ifName string) string {
137+
iface, err := net.InterfaceByName(ifName)
138+
if err != nil {
139+
return ""
140+
}
141+
142+
return iface.HardwareAddr.String()
143+
}
144+
145+
func archMark(arch string) string {
146+
switch strings.ToUpper(arch) {
147+
case "AMD64", "X86_64":
148+
return "22"
149+
case "ARM64", "AARCH64":
150+
return "24"
151+
default:
152+
return "20"
153+
}
154+
}
155+
156+
func hashToFixedDigits(data []byte, digits int) string {
157+
h := sha1.Sum(data)
158+
159+
n := new(big.Int).SetBytes(h[:])
160+
161+
mod := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(digits)), nil)
162+
163+
n.Mod(n, mod)
164+
165+
return fmt.Sprintf("%0*d", digits, n)
166+
}

0 commit comments

Comments
 (0)