-
Notifications
You must be signed in to change notification settings - Fork 11
/
token.go
165 lines (152 loc) · 3.02 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package translator
import (
"fmt"
"io"
"math"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
var ReTkk = regexp.MustCompile(`tkk:'(.+?)'`)
type tokenAcquirer struct {
tkk string
host string
client *http.Client
}
func Token(host string, client *http.Client) *tokenAcquirer {
host = strings.ToLower(host)
if !strings.HasPrefix(host, "http") {
host = "https://" + host
}
return &tokenAcquirer{
tkk: "0",
host: host,
client: client,
}
}
func (a *tokenAcquirer) do(text string) (string, error) {
err := a.update()
if err != nil {
return "", err
}
tk := a.acquire(text)
return tk, nil
}
func (a *tokenAcquirer) update() error {
now := int(math.Floor(float64(time.Now().UnixNano()) / 1000000.00 / 3600000.00))
tkk, _ := strconv.Atoi(strings.Split(a.tkk, ".")[0])
if a.tkk != "" && tkk == now {
return nil
}
resp, err := a.client.Get(a.host)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
rawTkk := ReTkk.FindStringSubmatch(string(body))
if len(rawTkk) > 0 {
a.tkk = rawTkk[1]
return nil
}
return nil
}
func (a *tokenAcquirer) acquire(text string) string {
var textSlice []int
for _, value := range text {
val := int(value)
if value < 0x10000 {
textSlice = append(textSlice, val)
} else {
textSlice = append(textSlice, int(math.Floor(float64(val-0x10000)/0x400+0xD800)))
textSlice = append(textSlice, int(math.Floor(float64((val-0x10000)%0x400)+0xDC00)))
}
}
x := ""
if a.tkk != "0" {
x = a.tkk
}
d := strings.Split(x, ".")
b := 0
if len(d) > 1 {
b, _ = strconv.Atoi(d[0])
}
var e []int
g := 0
size := len(textSlice)
for g < size {
l := textSlice[g]
// just append if l is less than 128(ascii: DEL)
if l < 128 {
e = append(e, l)
} else {
// append calculated value if l is less than 2048
if l < 2048 {
e = append(e, l>>6|192)
} else {
if (l&64512) == 55296 && g+1 < size && textSlice[g+1]&64512 == 56320 {
g += 1
l = 65536 + ((l & 1023) << 10) + (textSlice[g] & 1023)
e = append(e, l>>18|240)
e = append(e, l>>12&63|128)
} else {
e = append(e, l>>12|224)
}
e = append(e, l>>6&63|128)
}
e = append(e, l&63|128)
}
g += 1
}
temp := b
for _, value := range e {
temp += value
temp = xr(temp, "+-a^+6")
}
temp = xr(temp, "+-3^+b+-f")
if len(d) > 1 {
t, _ := strconv.Atoi(d[1])
temp ^= t
} else {
temp ^= 0
}
if temp < 0 {
temp = (temp & 2147483647) + 2147483648
}
temp %= 1000000
return fmt.Sprintf("%d.%d", temp, temp^b)
}
func rShift(val, n int) int {
return (val % 0x100000000) >> n
}
func xr(a int, b string) int {
lenB := len(b)
c := 0
for c < lenB-2 {
stringD := string(b[c+2])
intD := int(b[c+2])
if stringD >= "a" {
intD = intD - 87
} else {
temp, _ := strconv.Atoi(stringD)
intD = temp
}
if string(b[c+1]) == "+" {
intD = rShift(a, intD)
} else {
intD = a << intD
}
if string(b[c]) == "+" {
a = (a + intD) & 4294967295
} else {
a = a ^ intD
}
c += 3
}
return a
}