66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
//go:build !windows && !darwin
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
func runClient(protocolArg string) {
|
|
identity, err := loadOrCreateIdentity()
|
|
if err != nil {
|
|
showError("DCAuth 初始化失败", err.Error())
|
|
return
|
|
}
|
|
|
|
if protocolArg == "" {
|
|
text := fmt.Sprintf(
|
|
"DCAuth v%s\n设备码:%s\n机器码:%s\n\n设备码用于识别本机,机器码用于管理员核对。\n请从后台登录页点击“验证本机”完成登录。",
|
|
clientVersion,
|
|
identity.deviceID,
|
|
identity.machineCode,
|
|
)
|
|
copyToClipboard(identity.deviceID + "\n" + identity.machineCode)
|
|
showInfo("DCAuth v"+clientVersion+" 设备信息", text+"\n\n设备码和机器码已复制到剪贴板。")
|
|
return
|
|
}
|
|
|
|
target, err := parseAuthorizeURL(protocolArg)
|
|
if err != nil {
|
|
showError("DCAuth 协议无效", err.Error())
|
|
return
|
|
}
|
|
app := appRegistry[target.App]
|
|
if !confirm(
|
|
app.DisplayName+" · 本机登录确认",
|
|
fmt.Sprintf("是否确认使用本机设备登录「%s」?\n\n设备码:%s\n机器码:%s", app.DisplayName, identity.deviceID, identity.machineCode),
|
|
) {
|
|
return
|
|
}
|
|
if err := submitAuthorization(identity, target); err != nil {
|
|
showError("本机验证失败", err.Error())
|
|
return
|
|
}
|
|
showInfo("本机验证成功", "已确认本机设备,请回到浏览器继续登录。")
|
|
}
|
|
|
|
func showInfo(title, text string) {
|
|
fmt.Printf("[%s]\n%s\n", title, text)
|
|
}
|
|
|
|
func showError(title, text string) {
|
|
fmt.Fprintf(os.Stderr, "[%s]\n%s\n", title, text)
|
|
}
|
|
|
|
func confirm(title, text string) bool {
|
|
fmt.Printf("[%s]\n%s\n[y/N] ", title, text)
|
|
var answer string
|
|
_, _ = fmt.Scanln(&answer)
|
|
return answer == "y" || answer == "Y"
|
|
}
|
|
|
|
func copyToClipboard(text string) {
|
|
_ = text
|
|
}
|