forked from ycrash/yc-360-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.go
More file actions
197 lines (180 loc) · 4.22 KB
/
post.go
File metadata and controls
197 lines (180 loc) · 4.22 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package shell
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"shell/config"
)
func GetOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return net.IPv4(127, 0, 0, 1)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
func PostData(endpoint, dt string, file *os.File) (msg string, ok bool) {
return PostCustomData(endpoint, "dt="+dt, file)
}
func PositionZero(file *os.File) (err error) {
_, err = file.Seek(0, io.SeekStart)
return
}
func PositionLast5000Lines(file *os.File) (err error) {
return PositionLastLines(file, 5000)
}
func PositionLastLines(file *os.File, n uint) (err error) {
var cursor int64 = 0
stat, err := file.Stat()
if err != nil {
return
}
size := stat.Size()
char := make([]byte, 1)
lines := n
for {
cursor -= 1
_, err = file.Seek(cursor, io.SeekEnd)
if err != nil {
return
}
_, err = file.Read(char)
if err != nil {
return
}
switch char[0] {
case '\r':
case '\n':
lines--
}
if lines == 0 {
return
}
if cursor == -size {
_, err = file.Seek(0, io.SeekStart)
return
}
}
}
func PostCustomData(endpoint, params string, file *os.File) (msg string, ok bool) {
return PostCustomDataWithPositionFunc(endpoint, params, file, PositionZero)
}
func PostCustomDataWithPositionFunc(endpoint, params string, file *os.File, position func(file *os.File) error) (msg string, ok bool) {
if config.GlobalConfig.OnlyCapture {
msg = "in only capture mode"
return
}
if file == nil {
msg = "file is not captured"
return
}
stat, err := file.Stat()
if err != nil {
msg = fmt.Sprintf("file stat err %s", err.Error())
return
}
fileName := stat.Name()
if stat.Size() < 1 {
msg = fmt.Sprintf("skipped empty file %s", fileName)
return
}
url := fmt.Sprintf("%s&%s", endpoint, params)
transport := http.DefaultTransport.(*http.Transport)
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: !config.GlobalConfig.VerifySSL,
}
path := config.GlobalConfig.CACertPath
if len(path) > 0 {
pool := x509.NewCertPool()
ca, err := ioutil.ReadFile(path)
if err != nil {
msg = err.Error()
return
}
pool.AppendCertsFromPEM(ca)
transport.TLSClientConfig.RootCAs = pool
}
httpClient := &http.Client{
Transport: transport,
}
err = position(file)
if err != nil {
msg = fmt.Sprintf("PostData position err %s", err.Error())
return
}
req, err := http.NewRequest("POST", url, file)
if err != nil {
msg = fmt.Sprintf("PostData new req err %s", err.Error())
return
}
req.Header.Set("Content-Type", "text")
req.Header.Set("ApiKey", config.GlobalConfig.ApiKey)
resp, err := httpClient.Do(req)
if err != nil {
msg = fmt.Sprintf("PostData post err %s", err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
msg = fmt.Sprintf("PostData get resp err %s", err.Error())
return
}
msg = fmt.Sprintf("%s\nstatus code %d\n%s", url, resp.StatusCode, body)
if resp.StatusCode == http.StatusOK {
ok = true
}
return
}
func GetData(endpoint string) (msg string, ok bool) {
if config.GlobalConfig.OnlyCapture {
msg = "in only capture mode"
return
}
transport := http.DefaultTransport.(*http.Transport)
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: !config.GlobalConfig.VerifySSL,
}
path := config.GlobalConfig.CACertPath
if len(path) > 0 {
pool := x509.NewCertPool()
ca, err := ioutil.ReadFile(path)
if err != nil {
msg = err.Error()
return
}
pool.AppendCertsFromPEM(ca)
transport.TLSClientConfig.RootCAs = pool
}
httpClient := &http.Client{
Transport: transport,
}
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
msg = fmt.Sprintf("GetData new req err %s", err.Error())
return
}
req.Header.Set("ApiKey", config.GlobalConfig.ApiKey)
resp, err := httpClient.Do(req)
if err != nil {
msg = fmt.Sprintf("GetData get err %s", err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
msg = fmt.Sprintf("GetData get resp err %s", err.Error())
return
}
msg = fmt.Sprintf("%s\nstatus code %d\n%s", endpoint, resp.StatusCode, body)
if resp.StatusCode == http.StatusOK {
ok = true
}
return
}