forked from dan-v/awslambdaproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
47 lines (40 loc) · 802 Bytes
/
util.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
package awslambdaproxy
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"sync"
"github.com/pkg/errors"
)
const (
getIPUrl = "http://checkip.amazonaws.com/"
)
func bidirectionalCopy(src io.ReadWriteCloser, dst io.ReadWriteCloser) {
defer dst.Close()
defer src.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
io.Copy(dst, src)
wg.Done()
}()
wg.Add(1)
go func() {
io.Copy(src, dst)
wg.Done()
}()
wg.Wait()
}
func getPublicIP() (string, error) {
resp, err := http.Get(getIPUrl)
if err != nil {
return "", errors.Wrap(err, "Failed to get IP address from "+getIPUrl)
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "Failed to read IP address from "+getIPUrl)
}
return string(bytes.TrimSpace(buf)), nil
}