-
Notifications
You must be signed in to change notification settings - Fork 7
/
notify.go
113 lines (96 loc) · 2.92 KB
/
notify.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
package upload
import (
"crypto/x509"
"net/http"
"net/url"
"os"
"time"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func (u Upload) SendNotify(requuid string) error {
url, urlError := url.Parse(u.NotifyURL)
if urlError != nil {
u.logger.Error("Read caCert error",
zap.String("requuid", requuid),
zap.Error(urlError),
)
return errors.Wrapf(urlError, "URL Parsing error")
}
// https://www.loginradius.com/blog/engineering/http-security-headers/
// https://www.loginradius.com/blog/engineering/tune-the-go-http-client-for-high-performance/
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 50
t.MaxConnsPerHost = 50
t.MaxIdleConnsPerHost = 50
t.DisableKeepAlives = true
t.IdleConnTimeout = 30 * time.Second
if u.MyTlsSetting.InsecureSkipVerify {
if url.Scheme != "https" {
u.logger.Error("check Schema insecure",
zap.String("requuid", requuid),
zap.Bool("insecure", u.MyTlsSetting.InsecureSkipVerify),
)
return errors.New("Parameter 'insecure' makes no sense without Scheme https")
}
t.TLSClientConfig.InsecureSkipVerify = true
}
if u.MyTlsSetting.CAPath != "" {
if url.Scheme != "https" {
u.logger.Error("check Schema capath",
zap.String("requuid", requuid),
zap.String("capath", u.MyTlsSetting.CAPath),
)
return errors.New("Parameter 'capath' makes no sense without Scheme https")
}
// caCert, err := ioutil.ReadFile(u.MyTlsSetting.CAPath)
caCert, err := os.ReadFile(u.MyTlsSetting.CAPath)
if err != nil {
u.logger.Error("Read caCert error",
zap.String("requuid", requuid),
zap.Any("capath", u.MyTlsSetting.CAPath),
zap.Error(err),
)
return errors.Wrapf(err, "failed to read capath %q", u.MyTlsSetting.CAPath)
}
caCertPool := x509.NewCertPool()
successful := caCertPool.AppendCertsFromPEM(caCert)
if !successful {
u.logger.Error("caCertPool.AppendCertsFromPEM error",
zap.String("requuid", requuid),
)
return errors.New("failed to parse ca certificate as PEM encoded content")
}
t.TLSClientConfig.RootCAs = caCertPool
}
httpClient := &http.Client{
Timeout: 5 * time.Second,
Transport: t,
}
// TODO: Handle notify Body
myRequest, reqerror := http.NewRequestWithContext(u.ctx, u.NotifyMethod, url.String(), nil)
if reqerror != nil {
u.logger.Error("httpClient build Request error",
zap.String("requuid", requuid),
zap.Any("Request", myRequest),
zap.Error(reqerror),
)
return errors.Wrapf(reqerror, "httpClient build Request error")
}
myRequest.Header.Set("User-Agent", "MyUpload-Handler_v"+Version)
myResp, error := httpClient.Do(myRequest)
if error != nil {
u.logger.Error("httpClient Request error",
zap.String("requuid", requuid),
zap.Any("Request", myRequest),
zap.Any("Response", myResp),
zap.Error(error),
)
return errors.Wrapf(error, "httpClient Request error")
}
u.logger.Debug("Notify Info",
zap.Any("Request", myRequest),
zap.Any("Response", myResp),
)
return nil
}