-
Notifications
You must be signed in to change notification settings - Fork 114
/
func_net.go
248 lines (222 loc) · 5.05 KB
/
func_net.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package antnet
import (
"bytes"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"net/smtp"
"os"
"strings"
)
func Send(msg *Message, fun func(msgque IMsgQue) bool) {
if msg == nil {
return
}
c := make(chan struct{})
gmsgMapSync.Lock()
gmsg := gmsgArray[gmsgId]
gmsgArray[gmsgId+1] = &gMsg{c: c}
gmsgId++
gmsgMapSync.Unlock()
gmsg.msg = msg
gmsg.fun = fun
close(gmsg.c)
}
func SendGroup(group string, msg *Message) {
Send(msg, func(msgque IMsgQue) bool {
return msgque.IsInGroup(group)
})
}
func HttpGetWithBasicAuth(url, name, passwd string) (string, error, *http.Response) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", ErrHttpRequest, nil
}
req.SetBasicAuth(name, passwd)
resp, err := client.Do(req)
if err != nil {
return "", ErrHttpRequest, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", ErrHttpRequest, nil
}
return string(body), nil, resp
}
func HttpGet(url string) (string, error, *http.Response) {
resp, err := http.Get(url)
if err != nil {
return "", ErrHttpRequest, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", ErrHttpRequest, resp
}
return string(body), nil, resp
}
func HttpPost(url, form string) (string, error, *http.Response) {
resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(form))
if err != nil {
return "", ErrHttpRequest, nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", ErrHttpRequest, resp
}
return string(body), nil, resp
}
func HttpPostJson(url string, body string) (string, error, *http.Response) {
res, err := http.Post(url, "application/json", strings.NewReader(body))
if err != nil {
return "", ErrHttpRequest, nil
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", ErrHttpRequest, res
}
return string(resBody), nil, res
}
func HttpUpload(url, field, file string) (*http.Response, error) {
buf := new(bytes.Buffer)
writer := multipart.NewWriter(buf)
formFile, err := writer.CreateFormFile(field, file)
if err != nil {
LogError("create form file failed:%s\n", err)
return nil, err
}
srcFile, err := os.Open(file)
if err != nil {
LogError("%open source file failed:%s\n", err)
return nil, err
}
defer srcFile.Close()
_, err = io.Copy(formFile, srcFile)
if err != nil {
LogError("write to form file falied:%s\n", err)
return nil, err
}
contentType := writer.FormDataContentType()
writer.Close()
resp, err := http.Post(url, contentType, buf)
if err != nil {
LogError("post failed:%s\n", err)
}
resp.Body.Close()
return resp, err
}
func SendMail(user, password, host, to, subject, body, mailtype string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
var content_type string
if mailtype == "html" {
content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
} else {
content_type = "Content-Type: text/plain" + "; charset=UTF-8"
}
msg := []byte("To: " + to + "\r\nFrom: " + user + ">\r\nSubject: " + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
return err
}
var allIp []string
func GetSelfIp(ifnames ...string) []string {
if allIp != nil {
return allIp
}
inters, _ := net.Interfaces()
if len(ifnames) == 0 {
ifnames = []string{"eth", "lo", "eno", "无线网络连接", "以太网", "WLAN", "本地连接"}
}
filterFunc := func(name string) bool {
for _, v := range ifnames {
if strings.Index(name, v) != -1 {
return true
}
}
return false
}
for _, inter := range inters {
if !filterFunc(inter.Name) {
continue
}
addrs, _ := inter.Addrs()
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok {
if ipnet.IP.To4() != nil {
allIp = append(allIp, ipnet.IP.String())
}
}
}
}
return allIp
}
func IsIpAddress(ip string) bool {
address := net.ParseIP(ip)
if address != nil {
return true
}
return false
}
func IsIntraIp(ip string) bool {
if ip == "127.0.0.1" {
return true
}
ips := strings.Split(ip, ".")
ipA := ips[0]
if ipA == "10" {
return true
}
ipB := ips[1]
if ipA == "192" {
if ipB == "168" {
return true
}
}
if ipA == "172" {
ipb := Atoi(ipB)
if ipb >= 16 && ipb <= 31 {
return true
}
}
return false
}
func GetSelfIntraIp(ifnames ...string) (ips []string) {
all := GetSelfIp(ifnames...)
for _, v := range all {
if IsIntraIp(v) {
ips = append(ips, v)
}
}
return
}
func GetSelfExtraIp(ifnames ...string) (ips []string) {
all := GetSelfIp(ifnames...)
for _, v := range all {
if IsIntraIp(v) {
continue
}
ips = append(ips, v)
}
return
}
func IPCanUse(ip string) bool {
var err error
for port := 1024; port < 65535; port++ {
addr := Sprintf("%v:%v", ip, port)
listen, err := net.Listen("tcp", addr)
if err == nil {
listen.Close()
break
} else if StrFind(err.Error(), "address is not valid") != -1 {
return false
}
}
return err == nil
}