-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
66 lines (57 loc) · 1.43 KB
/
utils.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
package browser
import (
"bytes"
"io"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zutil"
)
var cacheDir = "browser"
func init() {
launcher.DefaultBrowserDir = filepath.Join(map[string]string{
"windows": os.Getenv("APPDATA"),
"darwin": filepath.Join(os.Getenv("HOME"), ".cache"),
"linux": filepath.Join(os.Getenv("HOME"), ".cache"),
}[runtime.GOOS], cacheDir, "browser")
}
func isDebian() bool {
if !zutil.IsLinux() {
return false
}
resp, _ := zfile.ReadFile("/etc/os-release")
if len(resp) == 0 {
return false
}
return bytes.Contains(resp, []byte("debian"))
}
func copyBody(b io.ReadCloser) (io.ReadCloser, io.ReadCloser, error) {
if b == nil || b == http.NoBody {
return http.NoBody, http.NoBody, nil
}
var buf bytes.Buffer
if _, err := buf.ReadFrom(b); err != nil {
return nil, b, err
}
if err := b.Close(); err != nil {
return nil, b, err
}
return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil
}
func transformHeaders(h []*proto.FetchHeaderEntry) http.Header {
newHeader := http.Header{}
for _, data := range h {
newHeader.Add(data.Name, data.Value)
}
return newHeader
}
// RandomSleep 随机暂停指定时间范围,单位毫秒
func RandomSleep(ms, maxMS int) {
time.Sleep(time.Millisecond * time.Duration(ms+rand.Intn(maxMS)))
}