forked from URLooker/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
145 lines (116 loc) · 2.76 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
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
package utils
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
"github.com/toolkits/str"
"github.com/urlooker/web/g"
)
func Getkey(idc string, sid int) string {
keys := g.Config.MonitorMap[idc]
count := len(keys)
return keys[sid%count]
}
func IsIP(ip string) bool {
if ip != "" {
isOk, _ := regexp.MatchString(`^(\d{1,3}\.){3}\d{1,3}$`, ip)
if isOk {
return isOk
}
}
return false
}
func ParseUrl(target string) (schema, host, port, path string) {
targetArr := strings.Split(target, "//")
schema = targetArr[0]
url := strings.Split(targetArr[1], "/")
addrArr := strings.Split(url[0], ":")
if len(addrArr) == 2 {
host = addrArr[0]
port = addrArr[1]
} else {
host = url[0]
}
for _, seg := range url[1:] {
path += ("/" + seg)
}
return
}
func KeysOfMap(m map[string]string) []string {
keys := make([]string, len(m))
i := 0
for key, _ := range m {
keys[i] = key
i++
}
return keys
}
func EncryptPassword(raw string) string {
return str.Md5Encode(g.Config.Salt + raw)
}
func CheckUrl(url string) error {
if !strings.Contains(url, "https://") && !strings.Contains(url, "http://") {
return fmt.Errorf("http or https is necessary")
}
if len(url) > 1024 {
return fmt.Errorf("url is too long over 1024")
}
return nil
}
func TimeFormat(ts int64) string {
t := time.Unix(ts, 0).Format("2006-01-02 15:04:05")
arr := strings.Split(t, " ")
t = arr[1]
arr = strings.Split(t, ":")
return fmt.Sprintf("%s:%s", arr[0], arr[1])
}
func ReadLastLine(filename string) (string, error) {
var previousOffset int64 = 0
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
reader := bufio.NewReader(f)
// we need to calculate the size of the last line for file.ReadAt(offset) to work
// NOTE : not a very effective solution as we need to read
// the entire file at least for 1 pass :(
lastLineSize := 0
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
lastLineSize = len(line)
}
fileInfo, err := os.Stat(filename)
if err != nil {
return "", err
}
// make a buffer size according to the lastLineSize
// +1 to compensate for the initial 0 byte of the line
// otherwise, the initial character of the line will be missing
// instead of reading the whole file into memory, we just read from certain offset
l := int64(lastLineSize) * 30
if fileInfo.Size() < l {
l = fileInfo.Size() - 1
}
buffer := make([]byte, l)
offset := fileInfo.Size() - int64(l+1)
numRead, err := f.ReadAt(buffer, offset)
if err != nil && err != io.EOF {
return "", err
}
if previousOffset != offset {
// print out last line content
buffer = buffer[:numRead]
fmt.Printf("%s \n", buffer)
previousOffset = offset
}
//res := strings.Split(string(buffer), "\n")
return string(buffer), nil
}