Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/189pc/help.go
1987 views
1
package _189pc
2
3
import (
4
"bytes"
5
"crypto/aes"
6
"crypto/hmac"
7
"crypto/rand"
8
"crypto/rsa"
9
"crypto/sha1"
10
"crypto/x509"
11
"encoding/hex"
12
"encoding/pem"
13
"encoding/xml"
14
"fmt"
15
"math"
16
"net/http"
17
"regexp"
18
"strings"
19
"time"
20
21
"github.com/alist-org/alist/v3/internal/model"
22
"github.com/alist-org/alist/v3/pkg/utils/random"
23
)
24
25
func clientSuffix() map[string]string {
26
rand := random.Rand
27
return map[string]string{
28
"clientType": PC,
29
"version": VERSION,
30
"channelId": CHANNEL_ID,
31
"rand": fmt.Sprintf("%d_%d", rand.Int63n(1e5), rand.Int63n(1e10)),
32
}
33
}
34
35
// 带params的SignatureOfHmac HMAC签名
36
func signatureOfHmac(sessionSecret, sessionKey, operate, fullUrl, dateOfGmt, param string) string {
37
urlpath := regexp.MustCompile(`://[^/]+((/[^/\s?#]+)*)`).FindStringSubmatch(fullUrl)[1]
38
mac := hmac.New(sha1.New, []byte(sessionSecret))
39
data := fmt.Sprintf("SessionKey=%s&Operate=%s&RequestURI=%s&Date=%s", sessionKey, operate, urlpath, dateOfGmt)
40
if param != "" {
41
data += fmt.Sprintf("&params=%s", param)
42
}
43
mac.Write([]byte(data))
44
return strings.ToUpper(hex.EncodeToString(mac.Sum(nil)))
45
}
46
47
// RAS 加密用户名密码
48
func RsaEncrypt(publicKey, origData string) string {
49
block, _ := pem.Decode([]byte(publicKey))
50
pubInterface, _ := x509.ParsePKIXPublicKey(block.Bytes)
51
data, _ := rsa.EncryptPKCS1v15(rand.Reader, pubInterface.(*rsa.PublicKey), []byte(origData))
52
return strings.ToUpper(hex.EncodeToString(data))
53
}
54
55
// aes 加密params
56
func AesECBEncrypt(data, key string) string {
57
block, _ := aes.NewCipher([]byte(key))
58
paddingData := PKCS7Padding([]byte(data), block.BlockSize())
59
decrypted := make([]byte, len(paddingData))
60
size := block.BlockSize()
61
for src, dst := paddingData, decrypted; len(src) > 0; src, dst = src[size:], dst[size:] {
62
block.Encrypt(dst[:size], src[:size])
63
}
64
return strings.ToUpper(hex.EncodeToString(decrypted))
65
}
66
67
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
68
padding := blockSize - len(ciphertext)%blockSize
69
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
70
return append(ciphertext, padtext...)
71
}
72
73
// 获取http规范的时间
74
func getHttpDateStr() string {
75
return time.Now().UTC().Format(http.TimeFormat)
76
}
77
78
// 时间戳
79
func timestamp() int64 {
80
return time.Now().UTC().UnixNano() / 1e6
81
}
82
83
func MustParseTime(str string) *time.Time {
84
lastOpTime, _ := time.ParseInLocation("2006-01-02 15:04:05 -07", str+" +08", time.Local)
85
return &lastOpTime
86
}
87
88
type Time time.Time
89
90
func (t *Time) UnmarshalJSON(b []byte) error { return t.Unmarshal(b) }
91
func (t *Time) UnmarshalXML(e *xml.Decoder, ee xml.StartElement) error {
92
b, err := e.Token()
93
if err != nil {
94
return err
95
}
96
if b, ok := b.(xml.CharData); ok {
97
if err = t.Unmarshal(b); err != nil {
98
return err
99
}
100
}
101
return e.Skip()
102
}
103
func (t *Time) Unmarshal(b []byte) error {
104
bs := strings.Trim(string(b), "\"")
105
var v time.Time
106
var err error
107
for _, f := range []string{"2006-01-02 15:04:05 -07", "Jan 2, 2006 15:04:05 PM -07"} {
108
v, err = time.ParseInLocation(f, bs+" +08", time.Local)
109
if err == nil {
110
break
111
}
112
}
113
*t = Time(v)
114
return err
115
}
116
117
type String string
118
119
func (t *String) UnmarshalJSON(b []byte) error { return t.Unmarshal(b) }
120
func (t *String) UnmarshalXML(e *xml.Decoder, ee xml.StartElement) error {
121
b, err := e.Token()
122
if err != nil {
123
return err
124
}
125
if b, ok := b.(xml.CharData); ok {
126
if err = t.Unmarshal(b); err != nil {
127
return err
128
}
129
}
130
return e.Skip()
131
}
132
func (s *String) Unmarshal(b []byte) error {
133
*s = String(bytes.Trim(b, "\""))
134
return nil
135
}
136
137
func toFamilyOrderBy(o string) string {
138
switch o {
139
case "filename":
140
return "1"
141
case "filesize":
142
return "2"
143
case "lastOpTime":
144
return "3"
145
default:
146
return "1"
147
}
148
}
149
150
func toDesc(o string) string {
151
switch o {
152
case "desc":
153
return "true"
154
case "asc":
155
fallthrough
156
default:
157
return "false"
158
}
159
}
160
161
func ParseHttpHeader(str string) map[string]string {
162
header := make(map[string]string)
163
for _, value := range strings.Split(str, "&") {
164
if k, v, found := strings.Cut(value, "="); found {
165
header[k] = v
166
}
167
}
168
return header
169
}
170
171
func MustString(str string, err error) string {
172
return str
173
}
174
175
func BoolToNumber(b bool) int {
176
if b {
177
return 1
178
}
179
return 0
180
}
181
182
// 计算分片大小
183
// 对分片数量有限制
184
// 10MIB 20 MIB 999片
185
// 50MIB 60MIB 70MIB 80MIB ∞MIB 1999片
186
func partSize(size int64) int64 {
187
const DEFAULT = 1024 * 1024 * 10 // 10MIB
188
if size > DEFAULT*2*999 {
189
return int64(math.Max(math.Ceil((float64(size)/1999) /*=单个切片大小*/ /float64(DEFAULT)) /*=倍率*/, 5) * DEFAULT)
190
}
191
if size > DEFAULT*999 {
192
return DEFAULT * 2 // 20MIB
193
}
194
return DEFAULT
195
}
196
197
func isBool(bs ...bool) bool {
198
for _, b := range bs {
199
if b {
200
return true
201
}
202
}
203
return false
204
}
205
206
func IF[V any](o bool, t V, f V) V {
207
if o {
208
return t
209
}
210
return f
211
}
212
213
type WrapFileStreamer struct {
214
model.FileStreamer
215
Name string
216
}
217
218
func (w *WrapFileStreamer) GetName() string {
219
return w.Name
220
}
221
222