-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
177 lines (142 loc) · 4.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
// Vencord Cloud, the API for the Vencord client mod
// Copyright (C) 2023 Vendicated and contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"encoding/base64"
"os"
"strconv"
"strings"
"github.com/ansrivas/fiberprometheus/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/redis/go-redis/v9"
g "github.com/vencord/backend/globals"
"github.com/vencord/backend/routes"
"github.com/vencord/backend/util"
)
func requireAuth(c *fiber.Ctx) error {
authToken := c.Get("Authorization")
if authToken == "" {
return c.Status(401).JSON(&fiber.Map{
"error": "Missing authorization",
})
}
// decode base64 token and split by:
// token[0] = secret
// token[1] = user id
token, err := base64.StdEncoding.DecodeString(authToken)
if err != nil {
return c.Status(401).JSON(&fiber.Map{
"error": "Invalid authorization",
})
}
tokenStr := string(token)
tokenSplit := strings.Split(tokenStr, ":")
if len(tokenSplit) != 2 {
return c.Status(401).JSON(&fiber.Map{
"error": "Invalid authorization",
})
}
secret := tokenSplit[0]
userId := tokenSplit[1]
if g.ALLOWED_USERS != nil && c.Path() != "/v1" && c.Method() != "DELETE" && !g.ALLOWED_USERS[userId] {
return c.Status(403).JSON(&fiber.Map{
"error": "User is not whitelisted",
})
}
storedSecret, err := g.RDB.Get(c.Context(), "secrets:"+util.Hash(g.PEPPER_SECRETS+userId)).Result()
if err == redis.Nil {
return c.Status(401).JSON(&fiber.Map{
"error": "Invalid authorization",
})
} else if err != nil {
panic(err)
}
if storedSecret != secret {
return c.Status(401).JSON(&fiber.Map{
"error": "Invalid authorization",
})
}
c.Context().SetUserValue("userId", userId)
return c.Next()
}
func main() {
// environment
slRaw, _ := strconv.ParseInt(os.Getenv("SIZE_LIMIT"), 10, 0)
g.SIZE_LIMIT = int(slRaw)
auRaw := os.Getenv("ALLOWED_USERS")
if auRaw != "" {
g.ALLOWED_USERS = make(map[string]bool)
for _, userId := range strings.Split(auRaw, ",") {
g.ALLOWED_USERS[userId] = true
}
}
app := fiber.New(fiber.Config{
ProxyHeader: os.Getenv("PROXY_HEADER"),
})
g.RDB = redis.NewClient(&redis.Options{
Addr: g.REDIS_URI,
})
if os.Getenv("PROMETHEUS") == "true" {
promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "vencord_accounts_registered",
Help: "The total number of accounts registered",
}, func() float64 {
iter := g.RDB.Scan(context.Background(), 0, "secrets:*", 0).Iterator()
var count int64
for iter.Next(context.Background()) {
count++
}
if err := iter.Err(); err != nil {
panic(err)
}
return float64(count)
})
prometheus := fiberprometheus.New("vencord")
prometheus.RegisterAt(app, "/metrics")
app.Use(prometheus.Middleware)
}
app.Use(cors.New(cors.Config{
ExposeHeaders: "ETag",
AllowOrigins: "https://discord.com,https://ptb.discord.com,https://canary.discord.com,https://discordapp.com,https://ptb.discordapp.com,https://canary.discordapp.com",
}))
// Add the docker health endpoint before the logger middleware, such that
// it doesn't spam the logs full with it
app.Get("/v1", routes.GET)
app.Use(logger.New())
// #region settings
app.All("/v1/settings", requireAuth)
app.Head("/v1/settings", routes.HEADSettings)
app.Get("/v1/settings", routes.GETSettings)
app.Put("/v1/settings", routes.PUTSettings)
app.Delete("/v1/settings", routes.DELETESettings)
// #endregion
// #region discord oauth
app.Get("/v1/oauth/callback", routes.GETOAuthCallback)
app.Get("/v1/oauth/settings", routes.GETOAuthSettings)
// #endregion
// #region erase all
app.Delete("/v1", requireAuth, routes.DELETE)
// #endregion
app.Get("/", func(c *fiber.Ctx) error {
return c.Redirect(g.ROOT_REDIRECT, 303)
})
app.Listen(g.HOST + ":" + g.PORT)
}