-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
executable file
·173 lines (145 loc) · 5.29 KB
/
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
package gweb
import (
"github.com/nbvghost/glog"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
//Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1
//Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Mobile Safari/537.36
//Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36
//Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1
func GetDeviceName(UserAgent string) string {
UserAgent = strings.ToLower(UserAgent)
//fmt.Println(UserAgent)
if strings.Contains(UserAgent, "iphone") {
//FrameworkHttp.OutHtmlFileWithPath(context,"game/web/ssc.html")
return "iphone"
} else if strings.Contains(UserAgent, "android") {
//FrameworkHttp.OutHtmlFileWithPath(context,"game/web/ssc.html")
return "android"
} else if strings.Contains(UserAgent, "ipad") {
//FrameworkHttp.OutHtmlFileWithPath(context,"game/web/ssc.html")
return "ipad"
} else if strings.Contains(UserAgent, "windows") {
//FrameworkHttp.OutHtmlFileWithPath(context,"game/web/ssc.html")
return "windows"
} else {
return "other"
}
}
func GetIP(request *http.Request) string {
//fmt.Println(context.Request)
//fmt.Println(context.Request.Header.Get("X-Forwarded-For"))
//fmt.Println(context.Request.RemoteAddr)
//Ali-Cdn-Real-Ip
//_IP := context.Request.Header.Get("X-Forwarded-For")
IP := strings.Split(request.Header.Get("X-Forwarded-For"), ",")[0]
if strings.EqualFold(IP, "") {
text := request.RemoteAddr
if strings.Contains(text, "::") {
IP = "0.0.0.0"
} else {
IP = strings.Split(text, ":")[0]
}
} else {
IP = strings.Split(IP, ":")[0]
}
return IP
}
func QueryParams(m url.Values) map[string]string {
data := make(map[string]string)
for key, value := range m {
//util.Trace(key)
//util.Trace(value)
data[key] = value[0]
}
return data
}
/*func RequestByHeader(url string, UserAgent string, Referer string) ([]byte,error) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil,err
}
//req.Header.Add("User-Agent","Mozilla/5.0 (Linux; Android 7.0; SLA-AL00 Build/HUAWEISLA-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN")
if !strings.EqualFold(UserAgent, "") {
req.Header.Add("User-Agent", UserAgent)
}
if !strings.EqualFold(Referer, "") {
req.Header.Add("Referer", Referer)
}
resp, err := client.Do(req)
if err != nil {
return nil,err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
return b,err
}*/
func RequestByHeader(url string, UserAgent string, Referer string) (error, *http.Response, []byte) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err, nil, nil
}
//req.Header.Add("User-Agent","Mozilla/5.0 (Linux; Android 7.0; SLA-AL00 Build/HUAWEISLA-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN")
if !strings.EqualFold(UserAgent, "") {
req.Header.Add("User-Agent", UserAgent)
}
if !strings.EqualFold(Referer, "") {
req.Header.Add("Referer", Referer)
}
resp, err := client.Do(req)
if err != nil {
return err, nil, nil
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
return err, resp, b
}
func DownloadInternetImageTemp(url string, UserAgent string, Referer string) string {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return ""
}
//req.Header.Add("User-Agent","Mozilla/5.0 (Linux; Android 7.0; SLA-AL00 Build/HUAWEISLA-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN")
if !strings.EqualFold(UserAgent, "") {
req.Header.Add("User-Agent", UserAgent)
}
if !strings.EqualFold(Referer, "") {
req.Header.Add("Referer", Referer)
}
resp, err := client.Do(req)
if err != nil {
return ""
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
glog.Error(err)
return WriteTempFile(b, resp.Header.Get("Content-Type"))
}
func DownloadInternetImage(url string, UserAgent string, Referer string, dynamicDirName string) string {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return ""
}
//req.Header.Add("User-Agent","Mozilla/5.0 (Linux; Android 7.0; SLA-AL00 Build/HUAWEISLA-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044109 Mobile Safari/537.36 MicroMessenger/6.6.7.1321(0x26060739) NetType/WIFI Language/zh_CN")
if !strings.EqualFold(UserAgent, "") {
req.Header.Add("User-Agent", UserAgent)
}
if !strings.EqualFold(Referer, "") {
req.Header.Add("Referer", Referer)
}
resp, err := client.Do(req)
if err != nil {
return ""
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
glog.Error(err)
return WriteFile(b, resp.Header.Get("Content-Type"), dynamicDirName, "net")
}