-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
146 lines (127 loc) · 4.18 KB
/
webhook.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
package archive
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"time"
)
type WebhookReportUploaded struct {
ID string `json:"id"`
Type string `json:"type"`
Timestamp time.Time `json:"timestamp"`
RecordingID string `json:"recording_id"`
ChannelID string `json:"channel_id"`
Filename string `json:"filename"`
FileURL string `json:"file_url"`
RecordingMetadata json.RawMessage `json:"recording_metadata,omitempty"`
}
type WebhookArchiveUploaded struct {
ID string `json:"id"`
Type string `json:"type"`
Timestamp time.Time `json:"timestamp"`
RecordingID string `json:"recording_id"`
SessionID string `json:"session_id"`
ClientID string `json:"client_id"`
ChannelID string `json:"channel_id"`
ConnectionID string `json:"connection_id"`
Filename string `json:"filename"`
FileURL string `json:"file_url"`
MetadataFilename string `json:"metadata_filename"`
MetadataFileURL string `json:"metadata_file_url"`
}
type WebhookArchiveEndUploaded struct {
ID string `json:"id"`
Type string `json:"type"`
Timestamp time.Time `json:"timestamp"`
RecordingID string `json:"recording_id"`
SessionID string `json:"session_id"`
ClientID string `json:"client_id"`
ChannelID string `json:"channel_id"`
ConnectionID string `json:"connection_id"`
Filename string `json:"filename"`
FileURL string `json:"file_url"`
}
// mTLS を組み込んだ http.Client を構築する
func createHTTPClient(config *Config) (*http.Client, error) {
e, err := url.Parse(config.WebhookEndpointURL)
if err != nil {
return nil, err
}
// http または VerifyCacertPath 指定していない場合はそのまま投げる
if e.Scheme != "https" || config.WebhookTLSVerifyCacertPath == "" {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: time.Duration(config.WebhookRequestTimeoutS) * time.Second,
}
return client, nil
}
CaCert, err := os.ReadFile(config.WebhookTLSVerifyCacertPath)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(CaCert)
var certificates []tls.Certificate
if config.WebhookTLSFullchainPath != "" && config.WebhookTLSPrivkeyPath != "" {
pair, err := tls.LoadX509KeyPair(config.WebhookTLSFullchainPath, config.WebhookTLSPrivkeyPath)
if err != nil {
return nil, err
}
certificates = append(certificates, pair)
}
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: time.Duration(config.WebhookRequestTimeoutS) * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
// hostname はチェックする
ServerName: e.Hostname(),
RootCAs: caCertPool,
Certificates: certificates,
},
// TODO: config へ
// ForceAttemptHTTP2: true,
},
}
return client, nil
}
func (u Uploader) httpClientDo(client *http.Client, webhookType string, buf []byte) error {
req, err := http.NewRequest("POST", u.config.WebhookEndpointURL, bytes.NewBuffer(buf))
if err != nil {
return err
}
// 固有ヘッダーを追加する
req.Header.Set("Content-Type", "application/json")
req.Header.Add(u.config.WebhookTypeHeaderName, webhookType)
// 設定があれば Basic 認証に対応する
if u.config.WebhookBasicAuthUsername != "" && u.config.WebhookBasicAuthPassword != "" {
req.SetBasicAuth(u.config.WebhookBasicAuthUsername, u.config.WebhookBasicAuthPassword)
}
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status_code: %d", resp.StatusCode)
}
return nil
}
func (u Uploader) postWebhook(webhookType string, buf []byte) error {
client, err := createHTTPClient(u.config)
if err != nil {
return err
}
if err := u.httpClientDo(client, webhookType, buf); err != nil {
return err
}
return nil
}