-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
167 lines (136 loc) · 3.59 KB
/
util.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
166
167
package common
import (
"compress/gzip"
"crypto/tls"
"io"
"net/http"
"net/url"
"path/filepath"
"strconv"
"strings"
"github.com/gabriel-vasile/mimetype"
_ "unsafe"
)
var tlsCipherSuites = []uint16{
// AEADs w/ ECDHE
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
// CBC w/ ECDHE
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
// AEADs w/o ECDHE
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
// CBC w/o ECDHE
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
// 3DES
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
}
var (
httpClient = &http.Client{
Transport: &http.Transport{
ForceAttemptHTTP2: true,
MaxConnsPerHost: 0,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 256,
TLSClientConfig: &tls.Config{
CipherSuites: tlsCipherSuites,
MinVersion: tls.VersionTLS10,
InsecureSkipVerify: true,
},
},
}
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66"
)
func Itoa(i int64) string {
return strconv.FormatInt(i, 10)
}
func Atoi(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}
func EscapeText(parseMode string, text string) string {
var replacer *strings.Replacer
if parseMode == "Markdown" {
replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[")
} else if parseMode == "MarkdownV2" {
replacer = strings.NewReplacer(
"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(",
"\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>",
"#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|",
"\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!",
)
} else {
return ""
}
return replacer.Replace(text)
}
//go:linkname Uint32 runtime.fastrand
func Uint32() uint32
func NextRandom() string {
return strconv.Itoa(int(Uint32()))
}
func Download(path string) (*BlobData, error) {
data, err := GetBytes(path)
if err != nil {
return nil, err
}
blobData := &BlobData{
Mime: mimetype.Detect(data).String(),
Binary: data,
}
if u, err := url.Parse(path); err == nil {
if p, err := url.QueryUnescape(u.EscapedPath()); err == nil {
blobData.Name = filepath.Base(p)
}
}
return blobData, nil
}
func GetBytes(url string) ([]byte, error) {
reader, err := HTTPGetReadCloser(url)
if err != nil {
return nil, err
}
defer func() {
_ = reader.Close()
}()
return io.ReadAll(reader)
}
type gzipCloser struct {
f io.Closer
r *gzip.Reader
}
func NewGzipReadCloser(reader io.ReadCloser) (io.ReadCloser, error) {
gzipReader, err := gzip.NewReader(reader)
if err != nil {
return nil, err
}
return &gzipCloser{
f: reader,
r: gzipReader,
}, nil
}
func (g *gzipCloser) Read(p []byte) (n int, err error) {
return g.r.Read(p)
}
func (g *gzipCloser) Close() error {
_ = g.f.Close()
return g.r.Close()
}
func HTTPGetReadCloser(url string) (io.ReadCloser, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header["User-Agent"] = []string{UserAgent}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
return NewGzipReadCloser(resp.Body)
}
return resp.Body, err
}