Skip to content

Commit a45cd98

Browse files
committed
fix: 添加权限校验
1 parent 8820ee1 commit a45cd98

5 files changed

Lines changed: 37 additions & 3 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
run: |
2525
git config --global user.name 'github-actions'
2626
git config --global user.email 'github-actions@github.com'
27-
TAG="v0.0.9-$(date +'%Y%m%d%H%M%S')"
27+
TAG="v0.0.10-$(date +'%Y%m%d%H%M%S')"
2828
git tag $TAG
2929
git push origin $TAG
3030
echo "TAG=$TAG" >> $GITHUB_ENV

model/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package model
22

3-
const SpeedTestVersion = "v0.0.9"
3+
const SpeedTestVersion = "v0.0.10"
44

55
var EnableLoger = false
66
var (

sp/perm.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package sp
5+
6+
import (
7+
"os"
8+
)
9+
10+
// hasRootPermission 检测是否有root权限
11+
func hasRootPermission() bool {
12+
return os.Getuid() == 0
13+
}

sp/perm_windows.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package sp
5+
6+
import (
7+
"golang.org/x/sys/windows"
8+
)
9+
10+
func hasRootPermission() bool {
11+
sid, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
12+
if err != nil {
13+
return false
14+
}
15+
token := windows.Token(0)
16+
member, err := token.IsMember(sid)
17+
if err != nil {
18+
return false
19+
}
20+
return member
21+
}

sp/sp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func isSudoAvailable() bool {
2222

2323
// 如果sudo可用,则使用sudo执行命令
2424
func execCommand(name string, arg ...string) *exec.Cmd {
25-
if isSudoAvailable() {
25+
if hasRootPermission() && isSudoAvailable() {
2626
return exec.Command("sudo", append([]string{name}, arg...)...)
2727
}
2828
return exec.Command(name, arg...)

0 commit comments

Comments
 (0)