-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
341 lines (324 loc) · 9.52 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"bytes"
cryptoRand "crypto/rand"
"crypto/rsa"
"crypto/sha256"
_ "embed"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"github.com/1f349/mjwt"
"github.com/discord-plays/bigben.discord-plays.xyz/utils"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/oauth2"
"github.com/disgoorg/snowflake/v2"
"github.com/gorilla/mux"
"gopkg.in/yaml.v3"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
var (
//go:embed www/home.go.html
rawHomePage string
//go:embed www/leaderboard.go.html
rawLeaderboardPage string
//go:embed www/assets/bigben.png
rawLogo []byte
//go:embed www/assets/style.css
rawStyle []byte
homePage = func() *template.Template {
parse, err := template.New("home").Parse(rawHomePage)
if err != nil {
log.Fatal(err)
}
return parse
}()
leaderboardPage = func() *template.Template {
parse, err := template.New("leaderboard").Funcs(template.FuncMap{
"renderTime": func(a float64) string {
return time.Duration(a * float64(time.Second)).Truncate(time.Millisecond).String()
},
"renderUser": func(users map[snowflake.ID]string, id snowflake.ID) string {
u := users[id]
if u == "" {
return "Unknown User"
}
if len(u) > 5 && u[len(u)-5] == '#' {
return u[:len(u)-5]
}
if len(u) > 2 && u[len(u)-2] == '#' {
return u[:len(u)-2]
}
return u
},
}).Parse(rawLeaderboardPage)
if err != nil {
log.Fatal(err)
}
return parse
}()
)
func main() {
var configPath string
flag.StringVar(&configPath, "conf", "config.yml", "Config file")
flag.Parse()
wd := filepath.Dir(configPath)
signer, err := mjwt.NewMJwtSignerFromFileOrCreate("bigben.discord-plays.xyz", filepath.Join(wd, "session-key.private.pem"), cryptoRand.Reader, 4096)
if err != nil {
log.Fatal("NewMJwtSignerFromFileOrCreate(): ", err)
}
configFile, err := os.Open(configPath)
if err != nil {
log.Fatal("os.Open(): ", err)
}
var config Config
decoder := yaml.NewDecoder(configFile)
err = decoder.Decode(&config)
if err != nil {
log.Fatal("yaml.Decode(): ", err)
}
err = os.MkdirAll(config.Cache, os.ModePerm)
if err != nil {
log.Fatal("os.MkdirAll(): ", err)
}
clientIdParse, err := snowflake.Parse(config.ClientId)
if err != nil {
log.Fatal("Parse ClientId snowflake: ", err)
}
oauthClient := oauth2.New(clientIdParse, config.ClientSecret)
log.Println("Building data cache")
yearMap := make(map[string]*yearItem)
for k, v := range config.Year {
yp := filepath.Join(config.Cache, k)
yearMap[k] = &yearItem{
lock: &sync.RWMutex{},
downloadUrl: v,
dir: yp,
Guilds: make(map[snowflake.ID]*utils.AsyncRead),
}
if _, err := os.Stat(yp); errors.Is(err, os.ErrNotExist) {
log.Printf("Cache missing for %s, downloading...\n", k)
err = utils.DownloadData(yp, v)
if err != nil {
log.Fatal("Failed to download and process data: ", err)
}
}
dir, err := os.ReadDir(yp)
if err != nil {
log.Fatalf("Failed to read '%s' directory", yp)
}
for _, i := range dir {
v := i.Name()
if strings.HasSuffix(v, ".json") {
v2 := v[:len(v)-5]
v3, err := snowflake.Parse(v2)
if err != nil {
log.Fatalf("Failed to parse snowflake '%s'", v2)
return
}
yearMap[k].Guilds[v3] = utils.NewAsyncRead(filepath.Join(yp, v))
}
}
}
router := mux.NewRouter()
router.HandleFunc("/", checkLogin(signer, oauthClient, func(rw http.ResponseWriter, req *http.Request, session *oauth2.Session, data loginData) {
if session == nil || !data.LoggedIn {
err := homePage.Execute(rw, struct{ Login loginData }{Login: data})
if err != nil {
log.Println("homePage.Execute(no login): ", err)
}
return
}
guilds, err := oauthClient.GetGuilds(*session)
if err != nil {
http.Error(rw, "500 Failed to get guild data", http.StatusInternalServerError)
return
}
yearGuilds := make(map[string][]guildItem)
for k, v := range yearMap {
yearGuilds[k] = []guildItem{}
v.lock.RLock()
for _, i := range guilds {
if _, ok := v.Guilds[i.ID]; ok {
guildIcon := ""
if i.Icon != nil {
iconRaw := *i.Icon
conf := discord.DefaultCDNConfig()
if strings.HasPrefix(iconRaw, "a_") && !conf.Format.Animated() {
conf.Format = discord.FileFormatGIF
}
conf.Values["size"] = 512
guildIcon = discord.GuildIcon.URL(conf.Format, conf.Values, i.ID.String(), iconRaw)
}
discord.User{}.EffectiveAvatarURL()
yearGuilds[k] = append(yearGuilds[k], guildItem{
Id: i.ID.String(),
Name: i.Name,
Icon: guildIcon,
})
}
}
v.lock.RUnlock()
}
err = homePage.Execute(rw, struct {
Login loginData
Guilds map[string][]guildItem
}{Login: data, Guilds: yearGuilds})
if err != nil {
log.Println("homePage.Execute(with login): ", err)
}
}))
router.HandleFunc("/bigben.png", func(rw http.ResponseWriter, req *http.Request) {
http.ServeContent(rw, req, "bigben.png", time.Now(), bytes.NewReader(rawLogo))
})
router.HandleFunc("/style.css", func(rw http.ResponseWriter, req *http.Request) {
http.ServeContent(rw, req, "style.css", time.Now(), bytes.NewReader(rawStyle))
})
router.HandleFunc("/login", func(rw http.ResponseWriter, req *http.Request) {
var (
query = req.URL.Query()
code = query.Get("code")
state = query.Get("state")
)
if code != "" && state != "" {
oaSess, _, err := oauthClient.StartSession(code, state)
if err != nil {
http.Error(rw, "500 Error starting session", http.StatusInternalServerError)
return
}
marshal, err := json.Marshal(oaSess)
if err != nil {
http.Error(rw, "500 Error saving session", http.StatusInternalServerError)
return
}
encrypt, err := rsa.EncryptOAEP(sha256.New(), cryptoRand.Reader, signer.PublicKey(), marshal, []byte("bigben-session"))
if err != nil {
http.Error(rw, "500 Error encrypting session", http.StatusInternalServerError)
return
}
encryptB64 := base64.RawURLEncoding.EncodeToString(encrypt)
http.SetCookie(rw, &http.Cookie{Name: "bigben-session", Value: encryptB64, Path: "/", Expires: time.Now().Add(time.Hour * 24 * 7)})
http.Redirect(rw, req, "/", http.StatusFound)
return
}
http.Redirect(rw, req, oauthClient.GenerateAuthorizationURL(config.RedirectUrl, discord.PermissionsNone, 0, true, discord.OAuth2ScopeIdentify, discord.OAuth2ScopeGuilds), http.StatusFound)
})
router.HandleFunc("/logout", func(rw http.ResponseWriter, req *http.Request) {
http.SetCookie(rw, &http.Cookie{Name: "bigben-session", Value: "", Path: "/", Expires: time.Now().Add(-time.Second)})
})
router.HandleFunc("/{year}/{guild}", checkLogin(signer, oauthClient, func(rw http.ResponseWriter, req *http.Request, session *oauth2.Session, data loginData) {
if session == nil || !data.LoggedIn {
http.Redirect(rw, req, "/", http.StatusFound)
return
}
vars := mux.Vars(req)
yearNum := vars["year"]
guildId, err := snowflake.Parse(vars["guild"])
if err != nil {
http.Error(rw, "400 Invalid guild", http.StatusBadRequest)
return
}
if y, ok := yearMap[yearNum]; ok {
if y.lock.TryRLock() {
y2 := y.Guilds[guildId]
y.lock.RUnlock()
if y2 == nil {
y.lock.Lock()
y.Guilds[guildId] = utils.NewAsyncRead(y.dir)
y.lock.Unlock()
}
c := y2.Get()
err = leaderboardPage.Execute(rw, struct {
Login loginData
HasC bool
Year string
C *utils.CacheData
}{
Login: data,
HasC: c != nil,
Year: yearNum,
C: c,
})
if err != nil {
log.Println("leaderboardPage.Execute(with login): ", err)
}
return
}
http.Error(rw, "404 Not Found", http.StatusNotFound)
return
}
http.Error(rw, "404 Not Found", http.StatusNotFound)
})).Methods(http.MethodGet)
log.Printf("Serving HTTP on '%s'\n", config.Listen)
srv := &http.Server{
Addr: config.Listen,
Handler: router,
}
log.Fatal("ListenAndServe(): ", srv.ListenAndServe())
}
func checkLogin(signer mjwt.Signer, oauthClient oauth2.Client, f func(rw http.ResponseWriter, req *http.Request, session *oauth2.Session, data loginData)) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
cookie, err := req.Cookie("bigben-session")
if err == nil {
decryptDecode, err := base64.RawURLEncoding.DecodeString(cookie.Value)
if err != nil {
http.Error(rw, "500 Failed to decode cookie value", http.StatusInternalServerError)
return
}
decrypt, err := rsa.DecryptOAEP(sha256.New(), nil, signer.PrivateKey(), decryptDecode, []byte("bigben-session"))
if err != nil {
http.Error(rw, "500 Failed to read cookie value", http.StatusInternalServerError)
return
}
var oaSess oauth2.Session
if json.Unmarshal(decrypt, &oaSess) != nil {
http.Error(rw, "500 Error loading session", http.StatusInternalServerError)
return
}
user, err := oauthClient.GetUser(oaSess)
if err != nil {
http.Error(rw, "500 Failed to get user data", http.StatusInternalServerError)
return
}
f(rw, req, &oaSess, loginData{
LoggedIn: true,
UserId: user.ID.String(),
UserTag: user.Tag(),
UserIcon: user.EffectiveAvatarURL(),
})
return
}
f(rw, req, nil, loginData{
LoggedIn: false,
UserId: "",
UserTag: "",
UserIcon: "",
})
}
}
type yearItem struct {
lock *sync.RWMutex
downloadUrl string
cache *utils.CacheData
dir string
Guilds map[snowflake.ID]*utils.AsyncRead
}
type guildItem struct {
Id string
Name string
Icon string
}
type loginData struct {
LoggedIn bool
UserId string
UserTag string
UserIcon string
}