-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
142 lines (113 loc) · 3.18 KB
/
main.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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/cp-hackathon/twitch"
"github.com/cp-hackathon/websocket"
"github.com/joho/godotenv"
)
func verifySignature(signature string, id string, timestamp string, body []byte) bool {
message := id + timestamp + string(body)
h := hmac.New(sha256.New, []byte(os.Getenv("WH_SECRET")))
h.Write([]byte(message))
sha := "sha256=" + hex.EncodeToString(h.Sum(nil))
return signature == sha
}
func handleEvent(pool *websocket.Pool, event twitch.Event) {
if event.Reward.Title == "Hydrate" {
log.Println("Hydrate redeemed")
message := websocket.BroadcastMessage{Event: "hydrate"}
pool.Broadcast <- message
}
}
func handleWs(pool *websocket.Pool, w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
conn, err := websocket.Upgrade(w, r)
if err != nil {
log.Fatal("Error upgrading connection", err)
}
client := &websocket.Client{
Conn: conn,
Pool: pool,
}
pool.Register <- client
log.Println("Client connected to websocket")
client.Read()
}
func handleNotifcation(pool *websocket.Pool, w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Failed to parse Notification request body", err)
}
if !verifySignature(
r.Header.Get("Twitch-Eventsub-Message-Signature"),
r.Header.Get("Twitch-Eventsub-Message-Id"),
r.Header.Get("Twitch-Eventsub-Message-Timestamp"),
body,
) {
http.Error(w, "Forbidden", http.StatusForbidden)
log.Println("Invalid notification signature")
return
}
if r.Header.Get("Twitch-Eventsub-Message-Type") == "webhook_callback_verification" {
var challenge twitch.Challenge
if err := json.Unmarshal(body, &challenge); err != nil {
log.Println("Error parsing challenge json", err)
return
}
w.Write([]byte(challenge.Challenge))
}
if r.Header.Get("Twitch-Eventsub-Message-Type") == "notification" {
var notification twitch.Notification
if err := json.Unmarshal(body, ¬ification); err != nil {
log.Println("Error parsing challenge json", err)
return
}
w.WriteHeader(http.StatusNoContent)
handleEvent(pool, notification.Event)
}
}
func setupRoutes() {
pool := websocket.NewPool()
go pool.Start()
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
handleWs(pool, w, r)
})
http.HandleFunc("/notification", func(w http.ResponseWriter, r *http.Request) {
handleNotifcation(pool, w, r)
})
}
func setupSubscription() {
token := twitch.GetAppAccessToken(os.Getenv("TWITCH_CLIENT_ID"), os.Getenv("TWITCH_CLIENT_SECRET"))
twitch.CreateChannelPointsSubscription(
os.Getenv("TWITCH_BROADCASTER_ID"),
os.Getenv("TWITCH_CLIENT_ID"),
token,
os.Getenv("WH_SECRET"),
os.Getenv("WH_CALLBACK_URL"),
)
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
setupRoutes()
setupSubscription()
fmt.Println("Starting server at port :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}