-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
259 lines (221 loc) · 7.17 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
package main
import (
"context"
"encoding/json"
"log"
"os"
"strings"
"time"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
"zeroshare-backend/config"
controller "zeroshare-backend/controllers"
"zeroshare-backend/middlewares"
"zeroshare-backend/structs"
pb "zeroshare-backend/proto"
)
var DB *gorm.DB
var redisStore *redis.Client
func shoudSkipPath(c *fiber.Ctx) bool {
// Skip authentication for login and callback routes
path := c.Path()
// Handle dynamic routes manually (e.g., /login/qr/:token)
if path == "/oauth/google" || path == "/auth/google/callback" ||
strings.HasPrefix(path, "/login/") ||
strings.HasPrefix(path, "/refresh") ||
strings.HasPrefix(path, "/stream") ||
strings.HasPrefix(path, "/notification-tone") ||
strings.HasPrefix(path, "/sse/") {
return true
}
return false
}
func main() {
app := fiber.New()
app.Use(logger.New())
app.Static("/assets", "./assets")
// Load environment variables from .env file if not running in a containerized environment
if os.Getenv("APP_ENV") != "production" {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Dayum, Error loading .env file:", err)
}
}
DB = controller.InitDatabase()
go pb.StartGRPCServer(DB)
controller.InitNebula(context.Background())
app.Use(cors.New(cors.Config{
AllowOrigins: "*", // Adjust this to allow only specific origins if needed
AllowMethods: "GET,POST,PUT,DELETE,OPTIONS",
AllowHeaders: "Authorization, Content-Type, Accept",
}))
// JWT Middleware
app.Use(func(c *fiber.Ctx) error {
if shoudSkipPath(c) {
// Skip JWT authentication for these paths
return c.Next()
}
// Otherwise, apply JWT middleware
return middlewares.NewAuthMiddleware(config.Secret)(c)
})
app.Use("/stream", func(c *fiber.Ctx) error {
log.Println("Incoming request:", c.Method(), c.Path())
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
if websocket.IsWebSocketUpgrade(c) {
c.Locals("allowed", true)
return c.Next()
}
return fiber.ErrUpgradeRequired
})
oauthConf := controller.SetUpOAuth()
redisStore = controller.SetupRedis()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Get("/sse/:sessionToken", func(c *fiber.Ctx) error {
sessionToken := c.Params("sessionToken")
return controller.SSE(c, redisStore, sessionToken)
})
app.Get("/login/:token", func(c *fiber.Ctx) error {
sessionToken := c.Params("token")
url := oauthConf.AuthCodeURL(sessionToken)
return c.Redirect(url)
})
app.Get("auth/google/callback", func(c *fiber.Ctx) error {
//get code from query params for generating token
return controller.GetAuthData(c, oauthConf, DB, redisStore)
})
app.Post("/node", func(c *fiber.Ctx) error {
response := new(structs.NodeResponse)
json.Unmarshal(c.Body(), response)
userId, _ := controller.GetFromToken(c, "ID")
uid, _ := uuid.Parse(userId.(string))
peer := structs.Peer{
MachineName: response.MachineName,
NetworkId: response.NetworkId,
NodeId: response.NodeId,
UserId: uid,
Platform: response.Platform,
}
controller.AddPeerAndAuthorize(context.Background(), peer, DB)
return c.SendStatus(fiber.StatusOK)
})
app.Post("/device", func(c *fiber.Ctx) error {
response := new(structs.Device)
json.Unmarshal(c.Body(), response)
userId, _ := controller.GetFromToken(c, "ID")
uid, _ := uuid.Parse(userId.(string))
response.UserId = uid
DB.Where("device_id = ?", response.DeviceId).FirstOrCreate(&response)
return c.SendStatus(fiber.StatusOK)
})
app.Get("/devices", func(c *fiber.Ctx) error {
userId, _ := controller.GetFromToken(c, "ID")
devices := []*structs.Device{}
DB.Where("user_id = ?", userId).Find(&devices)
return c.JSON(devices)
})
app.Post("/login/verify-google", func(c *fiber.Ctx) error {
response := new(structs.GoogleTokenResponse)
json.Unmarshal(c.Body(), response)
tokenResponse, err := controller.GetAuthDataFromGooglePayload(response.Token, DB)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.JSON(tokenResponse)
})
app.Post("/refresh", func(c *fiber.Ctx) error {
refreshToken := new(structs.RefreshTokenRequest)
json.Unmarshal(c.Body(), refreshToken)
return controller.RefreshToken(c, DB, refreshToken.RefreshToken)
})
app.Get("/peers", func(c *fiber.Ctx) error {
userId, _ := controller.GetFromToken(c, "ID")
members, err := controller.FetchAllMembers(context.Background(), c.Query("networkId"), userId.(string), DB)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
return c.JSON(members)
})
app.Post("/nebula/sign-public-key", func(c *fiber.Ctx) error {
body := struct {
PublicKey string `json:"public_key"`
DeviceId string `json:"device_id"`
}{}
if err := c.BodyParser(&body); err != nil {
return c.Status(400).JSON(fiber.Map{
"error": "Invalid request body",
})
}
// TODO, get last public ip
signedKey, caCert, incomingSite, err := controller.SignPublicKey(body.PublicKey, body.DeviceId, DB)
if err != nil {
log.Println(err)
return c.Status(500).JSON(fiber.Map{
"error": "Failed to sign public key",
})
}
return c.JSON(fiber.Map{
"signed_key": signedKey,
"ca_cert": caCert,
"incoming_site": incomingSite,
})
})
app.Post("/device/send/:id", func(c *fiber.Ctx) error {
deviceId := c.Params("id")
userId, _ := controller.GetFromToken(c, "ID")
SSERequest := new(structs.SSERequest)
json.Unmarshal(c.Body(), SSERequest)
device := new(structs.Device)
DB.Where("device_id = ? AND user_id = ?", SSERequest.UniqueID, userId).First(&device)
SSEResponse := new(structs.SSEResponse)
SSEResponse.Type = SSERequest.Type
SSEResponse.Data = SSERequest.Data
SSEResponse.Device = *device
jsonData, _ := json.Marshal(SSEResponse)
log.Println("Device ID: ", deviceId, "User Id:", userId)
redisStore.Publish(context.Background(), deviceId, jsonData)
return c.SendStatus(fiber.StatusOK)
})
app.Get("/device/receive/:id", func(c *fiber.Ctx) error {
deviceId := c.Params("id")
log.Println("Device ID: ", deviceId)
return controller.DeviceSSE(c, redisStore, deviceId)
})
cfg := websocket.Config{
RecoverHandler: func(conn *websocket.Conn) {
if err := recover(); err != nil {
conn.WriteJSON(fiber.Map{"customError": "error occurred"})
}
},
}
app.Get("/stream", websocket.New(func(c *websocket.Conn) {
// Ensure the connection is closed properly
defer func() {
err := c.Close()
if err != nil {
log.Println("Error closing connection:", err)
}
}()
// Start a ping-pong loop to keep the connection alive
go func() {
ticker := time.NewTicker(15 * time.Second) // Adjust interval as needed
defer ticker.Stop()
for range ticker.C {
if err := c.WriteMessage(websocket.PingMessage, nil); err != nil {
log.Println("Error sending ping:", err)
return
}
}
}()
controller.Stream(c, redisStore)
}, cfg))
log.Fatal(app.Listen(":" + os.Getenv("PORT")))
}