-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.go
87 lines (78 loc) · 1.66 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"math/rand"
"os/exec"
"strconv"
"strings"
"syscall"
"gopkg.in/yaml.v2"
)
func processExists(pid int) bool {
if err := syscall.Kill(pid, 0); err == nil {
return true
}
return false
}
func runXray(args []string) (pid int, err error) {
cmd := exec.Command(xrayBin, args...)
cmd.Dir = conf.Xray.Path
if err = cmd.Start(); err != nil {
return 0, err
}
return cmd.Process.Pid, nil
}
func portInUse(port int) bool {
output, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("lsof -i:%d", port)).CombinedOutput()
return err == nil && len(output) > 0
}
func randomPort() (p int) {
for {
p = 30000 + rand.Intn(10000)
if !portInUse(p) {
break
}
}
return
}
func writeXrayConfg(obj Project) (err error) {
xrayConf, err := getDefaultXrayConfig()
if err != nil {
logger.Errorln(err)
return
}
domains := strings.Split(obj.Domain, ",")
xrayConf.Mitm.Restriction.Includes = domains
// TODO: BasicCrawler
// xrayConf.BasicCrawler.Restriction.Includes = domains
data, err := yaml.Marshal(&xrayConf)
if err != nil {
logger.Errorln(err)
return
}
err = ioutil.WriteFile(obj.Config, data, 0666)
if err != nil {
logger.Errorln(err)
return
}
return nil
}
// pagination - 分页生成
// @return limit,offset
func pagination(page, pageSize string) (int, int) {
pageSizeInt, _ := strconv.Atoi(pageSize)
pageInt, _ := strconv.Atoi(page)
if pageSizeInt < 20 || pageSizeInt > 100 {
pageSizeInt = 20
}
return pageSizeInt, (pageInt - 1) * pageSizeInt
}
// MD5 -
func MD5(text string) string {
ctx := md5.New()
_, _ = ctx.Write([]byte(text))
return hex.EncodeToString(ctx.Sum(nil))
}