-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.go
87 lines (83 loc) · 1.83 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"bufio"
"errors"
"fmt"
"net/url"
"os"
"strings"
)
func ParserTokenUrl(tokenUrl string) (url.Values, error) {
if !strings.HasPrefix(tokenUrl, "https") {
return nil, errors.New("地址应该以 https 开头: " + tokenUrl)
}
parse, err := url.Parse(tokenUrl)
if err != nil {
return nil, err
}
query, err := url.ParseQuery(parse.RawQuery)
if err != nil {
return nil, err
}
if !query.Has("token") {
return nil, errors.New("地址必须有 token 参数,请完整复制")
}
return query, nil
}
func InputTokenUrl() string {
var tokenUrl string
reader := bufio.NewReaderSize(os.Stdin, 65536)
for {
fmt.Println("登录信息无效,请重新输入授权地址,获取教程:https://blog.xausky.cn")
for {
tokenUrlBytes, _, _ := reader.ReadLine()
tokenUrl = strings.TrimSpace(string(tokenUrlBytes))
if tokenUrl != "" {
break
}
}
_, err := ParserTokenUrl(tokenUrl)
if err != nil {
fmt.Println("授权地址有误请严格按照教程执行。", err)
continue
} else {
break
}
}
return tokenUrl
}
func LoadToken(clean bool) url.Values {
file := os.ExpandEnv("$APPDATA\\svpc-launcher-token.txt")
if clean {
err := os.Remove(file)
if err != nil {
panic(err)
}
}
tokenUrlBytes, err := os.ReadFile(file)
var tokenUrl string
if os.IsNotExist(err) {
tokenUrl = InputTokenUrl()
err := os.WriteFile(file, []byte(tokenUrl), 0644)
if err != nil {
panic(err)
}
} else if err != nil {
panic(err)
} else {
tokenUrl = string(tokenUrlBytes)
}
tokenUrlParams, err := ParserTokenUrl(tokenUrl)
if err != nil {
fmt.Println(err)
tokenUrl = InputTokenUrl()
tokenUrlParams, _ = ParserTokenUrl(tokenUrl)
err = os.WriteFile(file, []byte(tokenUrl), 0644)
if err != nil {
panic(err)
}
}
tokenUrlParams.Del("gv")
tokenUrlParams.Del("cv")
return tokenUrlParams
}