-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.go
More file actions
208 lines (163 loc) · 4.95 KB
/
connection.go
File metadata and controls
208 lines (163 loc) · 4.95 KB
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
package httpcord
import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"io/ioutil"
"mime/multipart"
"net/http"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
type HttpConnection int
const (
DefaultHttpConnection HttpConnection = iota + 1
FastHttpConnection
)
type ConnectionContext struct {
SendRes func(res *InteractionResponse) bool
Interaction Interaction
clientToken string
}
type ConnectionOptions struct {
// Use github.com/valyala/fasthttp instead net/http
HttpConnection HttpConnection
// Discord public key
PublicKey string
// Discord token (Necessary for external requests)
Token string
}
type Connection struct {
FastHandler fasthttp.RequestHandler
DefaultHandler http.HandlerFunc
}
var InteractionHandlers = make([]func(ctx ConnectionContext), 0, 10)
func parsePublicKey(key string) (ed25519.PublicKey, error) {
return hex.DecodeString(key)
}
func verifyKey(body []byte, signature string, publicKey ed25519.PublicKey) bool {
sig, err := hex.DecodeString(signature)
if err != nil {
return false
}
return ed25519.Verify(publicKey, body, sig)
}
func NewConnection(options ConnectionOptions) Connection {
publicKey, err := parsePublicKey(options.PublicKey)
if err != nil {
panic(err)
}
handler := httpHandler(publicKey, options.Token)
if options.HttpConnection == DefaultHttpConnection {
return Connection{
DefaultHandler: handler,
}
} else if options.HttpConnection == FastHttpConnection {
return Connection{
FastHandler: fasthttpadaptor.NewFastHTTPHandler(handler),
}
}
return Connection{
DefaultHandler: handler,
}
}
func (c Connection) Connect(address string) error {
if c.FastHandler != nil {
return fasthttp.ListenAndServe(address, c.FastHandler)
}
return http.ListenAndServe(address, c.DefaultHandler)
}
func httpHandler(publicKey ed25519.PublicKey, token string) http.HandlerFunc {
var res InteractionResponse
return func(w http.ResponseWriter, r *http.Request) {
je := json.NewEncoder(w)
signature := r.Header.Get("X-Signature-Ed25519")
timestamp := r.Header.Get("X-Signature-Timestamp")
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
body := append([]byte(timestamp), bodyBytes...)
if !verifyKey(body, signature, publicKey) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
return
}
var rawInteraction APIInteraction
err = json.Unmarshal(bodyBytes, &rawInteraction)
if err != nil {
panic("Error on get interaction: " + err.Error())
}
interaction := ResolveInteraction(&rawInteraction)
if interaction.Type == PingInteraction {
w.Header().Set("Content-Type", "application/json")
err := je.Encode(InteractionResponse{
Type: PongResponse,
})
if err != nil {
panic("Error writing response")
}
return
}
if (res.Type == ChannelMessageWithSourceResponse || res.Type == UpdateMessageResponse) && len(res.Data.Files) > 0 {
m := multipart.NewWriter(w)
w.Header().Set("Content-Type", m.FormDataContentType())
for id, file := range res.Data.Files {
attach, err := file.MakeAttach(Snowflake(rune(id+1)), m)
if err != nil {
panic("Error creating attachment: " + err.Error())
}
res.Data.Attachments = append(res.Data.Attachments, attach)
}
if field, err := m.CreateFormField("payload_json"); err != nil {
panic("Error creating payload_json form field")
} else if err := json.NewEncoder(field).Encode(res); err != nil {
panic("Error encoding payload_json")
}
if err := m.Close(); err != nil {
panic("Error on close multipart writer")
}
return
}
ctx := ConnectionContext{
Interaction: interaction,
SendRes: func(r *InteractionResponse) bool {
w.Header().Set("Content-Type", "application/json")
err = je.Encode(r)
return err != nil
},
clientToken: token,
}
for _, h := range InteractionHandlers {
h(ctx)
}
}
}
func (c Connection) AddInteractionHandler(handler func(ctx ConnectionContext)) {
InteractionHandlers = append(InteractionHandlers, handler)
}
func (ctx *ConnectionContext) ReplyInteraction(data *InteractionCallbackData) {
ctx.SendRes(&InteractionResponse{
Type: ChannelMessageWithSourceResponse,
Data: data,
})
}
func (ctx *ConnectionContext) DeferReplyInteraction() {
ctx.SendRes(&InteractionResponse{
Type: DeferredChannelMessageWithSourceResponse,
})
}
func (ctx *ConnectionContext) DeferUpdateInteraction() {
ctx.SendRes(&InteractionResponse{
Type: DeferredUpdateResponse,
})
}
func (ctx *ConnectionContext) EditReply(data *WebhookEdit) {
EditOriginalInteractionResponse(ctx.Interaction.ApplicationID.String(), ctx.Interaction.Token, data)
}
func (ctx *ConnectionContext) DeleteReply() {
DeleteOriginalInteractionResponse(ctx.Interaction.ApplicationID.String(), ctx.Interaction.Token)
}
func (ctx *ConnectionContext) FollowUp(data *WebhookEdit) {
FollowUpInteractionResponse(ctx.Interaction.ApplicationID.String(), ctx.Interaction.Token, data)
}