-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper-net.go
243 lines (202 loc) · 6.47 KB
/
helper-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
package helper
/*
* Copyright 2020-2023 Aldelo, LP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"bytes"
"encoding/json"
"fmt"
"github.com/aldelo/common/rest"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
)
// GetNetListener triggers the specified port to listen via tcp
func GetNetListener(port uint) (net.Listener, error) {
if l, e := net.Listen("tcp", fmt.Sprintf(":%d", port)); e != nil {
return nil, fmt.Errorf("Listen Tcp on Port %d Failed: %v", port, e)
} else {
return l, nil
}
}
// IsHttpsEndpoint returns true if url is https, false if otherwise
func IsHttpsEndpoint(url string) bool {
return strings.ToLower(Left(url, 8)) == "https://"
}
// GetLocalIP returns the first non loopback ip
func GetLocalIP() string {
if addrs, err := net.InterfaceAddrs(); err != nil {
return ""
} else {
for _, a := range addrs {
if ip, ok := a.(*net.IPNet); ok && !ip.IP.IsLoopback() && !ip.IP.IsInterfaceLocalMulticast() && !ip.IP.IsLinkLocalMulticast() && !ip.IP.IsLinkLocalUnicast() && !ip.IP.IsMulticast() && !ip.IP.IsUnspecified() {
if ip.IP.To4() != nil {
return ip.IP.String()
}
}
}
return ""
}
}
// DnsLookupIps returns list of IPs for the given host
// if host is private on aws route 53, then lookup ip will work only when within given aws vpc that host was registered with
func DnsLookupIps(host string) (ipList []net.IP) {
if ips, err := net.LookupIP(host); err != nil {
return []net.IP{}
} else {
for _, ip := range ips {
ipList = append(ipList, ip)
}
return ipList
}
}
// DnsLookupSrvs returns list of IP and port addresses based on host
// if host is private on aws route 53, then lookup ip will work only when within given aws vpc that host was registered with
func DnsLookupSrvs(host string) (ipList []string) {
if _, addrs, err := net.LookupSRV("", "", host); err != nil {
return []string{}
} else {
for _, v := range addrs {
ipList = append(ipList, fmt.Sprintf("%s:%d", v.Target, v.Port))
}
return ipList
}
}
// ParseHostFromURL will parse out the host name from url
func ParseHostFromURL(url string) string {
parts := strings.Split(strings.ReplaceAll(strings.ReplaceAll(strings.ToLower(url), "https://", ""), "http://", ""), "/")
if len(parts) >= 0 {
return parts[0]
} else {
return ""
}
}
// VerifyGoogleReCAPTCHAv2 will verify recaptcha v2 response data against given secret and obtain a response from google server
func VerifyGoogleReCAPTCHAv2(response string, secret string) (success bool, challengeTs time.Time, hostName string, err error) {
if LenTrim(response) == 0 {
return false, time.Time{}, "", fmt.Errorf("ReCAPTCHA Response From CLient is Required")
}
if LenTrim(secret) == 0 {
return false, time.Time{}, "", fmt.Errorf("ReCAPTCHA Secret Key is Required")
}
u := fmt.Sprintf("https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s", url.PathEscape(secret), url.PathEscape(response))
if statusCode, responseBody, e := rest.POST(u, []*rest.HeaderKeyValue{}, ""); e != nil {
return false, time.Time{}, "", fmt.Errorf("ReCAPTCHA Service Failed: %s", e)
} else {
if statusCode != 200 {
return false, time.Time{}, "", fmt.Errorf("ReCAPTCHA Service Failed: Status Code %d", statusCode)
} else {
m := make(map[string]json.RawMessage)
if err = json.Unmarshal([]byte(responseBody), &m); err != nil {
return false, time.Time{}, "", fmt.Errorf("ReCAPTCHA Service Response Failed: (Parse Json Response Error) %s", err)
} else {
if m == nil {
return false, time.Time{}, "", fmt.Errorf("ReCAPTCHA Service Response Failed: %s", "Json Response Map Nil")
} else {
// response json from google is valid
if strings.ToLower(string(m["success"])) == "true" {
success = true
}
challengeTs = ParseDateTime(string(m["challenge_ts"]))
hostName = string(m["hostname"])
if !success {
errs := string(m["error-codes"])
s := []string{}
if err = json.Unmarshal([]byte(errs), &s); err != nil {
err = fmt.Errorf("Parse ReCAPTCHA Verify Errors Failed: %s", err)
} else {
buf := ""
for _, v := range s {
if LenTrim(v) > 0 {
if LenTrim(buf) > 0 {
buf += ", "
}
buf += v
}
}
err = fmt.Errorf("ReCAPTCHA Verify Errors: %s", buf)
}
}
return success, challengeTs, hostName, err
}
}
}
}
}
// ReadHttpRequestBody reads raw body from http request body object,
// and then sets the read body back to the request (once reading will remove the body content if not restored)
func ReadHttpRequestBody(req *http.Request) ([]byte, error) {
if req == nil {
return []byte{}, fmt.Errorf("Http Request Nil")
}
if body, err := ioutil.ReadAll(req.Body); err != nil {
return []byte{}, err
} else {
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return body, nil
}
}
// ReadHttpRequestHeaders parses headers into map
func ReadHttpRequestHeaders(req *http.Request) (map[string]string, error) {
if req == nil {
return nil, fmt.Errorf("Http Request Nil")
}
m := make(map[string]string)
for k, v := range req.Header {
buf := ""
for _, d := range v {
if LenTrim(buf) > 0 {
buf += "; "
}
buf += d
}
m[k] = buf
}
return m, nil
}
// ParseHttpHeader parses headers into map[string]string,
// where slice of header values for a key is delimited with semi-colon (;)
func ParseHttpHeader(respHeader http.Header) (map[string]string, error) {
if respHeader == nil {
return nil, fmt.Errorf("Http Response Header Nil")
}
m := make(map[string]string)
for k, v := range respHeader {
buf := ""
for _, d := range v {
if LenTrim(buf) > 0 {
buf += "; "
}
buf += d
}
m[k] = buf
}
return m, nil
}
// EncodeHttpHeaderMapToString converts header map[string]string to string representation
func EncodeHttpHeaderMapToString(headerMap map[string]string) string {
if headerMap == nil {
return ""
} else {
buf := ""
for k, v := range headerMap {
buf += fmt.Sprintf("%s: %s\n", k, v)
}
return buf
}
}