Skip to content

Commit

Permalink
Merge pull request #10 from StrayDragon/l8ng/fix
Browse files Browse the repository at this point in the history
fix: 修复windows bat脚本乱码, 弃用提权逻辑避免杀毒误报&强制管理员运行
  • Loading branch information
StrayDragon authored Nov 9, 2024
2 parents a8cdb5d + 2bbb1c9 commit a407583
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
7 changes: 4 additions & 3 deletions artifact/windows/install.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@echo off
echo 正在安装必要配置...
powershell -Command "Start-Process -Verb RunAs cmd -ArgumentList '/c cd /d %~dp0 && bookxnote-local-ocr.exe install'"
@echo off
echo 正在安装必要配置(请以管理员身份运行)...
cd /d %~dp0
bookxnote-local-ocr.exe install
pause
7 changes: 4 additions & 3 deletions artifact/windows/start.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@echo off
echo 正在启动本地OCR服务...
powershell -Command "Start-Process -Verb RunAs cmd -ArgumentList '/c cd /d %~dp0 && bookxnote-local-ocr.exe server'"
@echo off
echo 正在启动本地OCR服务(请以管理员身份运行)...
cd /d %~dp0
bookxnote-local-ocr.exe server
pause
7 changes: 4 additions & 3 deletions artifact/windows/uninstall.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@echo off
echo 正在卸载配置...
powershell -Command "Start-Process -Verb RunAs cmd -ArgumentList '/c cd /d %~dp0 && bookxnote-local-ocr.exe uninstall'"
@echo off
echo 正在卸载配置(请以管理员身份运行)...
cd /d %~dp0
bookxnote-local-ocr.exe uninstall
pause
19 changes: 19 additions & 0 deletions cmd/bookxnote-local-ocr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/areYouLazy/libhosty"
Expand Down Expand Up @@ -253,7 +254,25 @@ func (app *App) runUninstall() error {
return nil
}

func checkAdminPrivileges() error {
if runtime.GOOS == "windows" {
cmd := exec.Command("net", "session")
if err := cmd.Run(); err != nil {
return fmt.Errorf("需要管理员权限运行! 请右键点击程序,选择「以管理员身份运行」")
}
} else if runtime.GOOS == "linux" {
if os.Geteuid() != 0 {
return fmt.Errorf("需要root权限运行! 请使用 sudo 运行此程序")
}
}
return nil
}

func (app *App) Run() error {
if err := checkAdminPrivileges(); err != nil {
return err
}

if len(os.Args) < 2 {
app.printUsage()
return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/smallstep/truststore v0.13.0 // indirect
github.com/smallstep/truststore v0.13.0
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down

0 comments on commit a407583

Please sign in to comment.