-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
238 lines (199 loc) · 6.69 KB
/
index.js
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
// Αρχηκοποιηση και εναρξη της express εφαρμογης μας
// Οπως και ολα τα πακετα που εγκαταστησαμε
const User = require('./models/User')
const fs = require('fs')
const jwt = require("jsonwebtoken")
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const helmet = require('helmet')
const morgan = require('morgan')
const multer = require("multer")
var cors = require('cors')
const cookieParser = require("cookie-parser")
const userRoute = require('./routes/users')
const authRoute = require('./routes/auth')
const messageRoute = require("./routes/messages")
const convRoute = require("./routes/conversations")
const path = require("path")
// Ανοιγμα του .env αρχειου μας
dotenv.config()
// Συνδεση στην βαση δεδομενων
mongoose.connect(process.env.MONGO_DB, {useNewUrlParser: true, useUnifiedTopology: true}, () => {
console.log("connected to mongo")
})
// cors
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
// Αναθετησει τον μεσολογισμικων στην express εφαρμογη
app.use(express.json())
app.use(helmet())
app.use(
helmet.contentSecurityPolicy({
directives: {
imgSrc: ['*', "'self'", 'data:', 'https:', 'blob:']
}
})
)
// helps control DNS prefetching and improves user privacy
app.use(
helmet.dnsPrefetchControl({
allow: true,
})
)
//remove X-Powered-By
app.use(helmet.hidePoweredBy())
// sets the X-Frame-Options in the header to prevent clickjacking attacks
app.use(
helmet.frameguard({
action: "deny",
})
)
//prefer https over http strict
app.use(
helmet.hsts({
maxAge: 123456,
includeSubDomains: false,
})
)
// prevents MIME type sniffing
app.use(helmet.noSniff())
//controls the information inside the Referer header
app.use(
helmet.referrerPolicy({
policy: ["origin", "unsafe-url"],
})
)
// prevents cross-site scripting
app.use(helmet.xssFilter())
app.use(morgan('common'))
app.use(cookieParser())
app.use("*/images/", express.static(path.join(__dirname, "PUBLIC_FOLDER/images/")))
app.use("*/background/", express.static(path.join(__dirname, "PUBLIC_FOLDER/background/")))
app.use("*/sounds/", express.static(path.join(__dirname, "PUBLIC_FOLDER/sounds/")))
const verify_token = (req, res, next) => {
const authHeader = req.headers.authorization
if (!authHeader) return res.status(401).json("You are not authorized")
const access_jwt = authHeader.split(" ")[1]
const public_key = fs.readFileSync(path.join(process.cwd(), "keys/accPublic.pem"))
jwt.verify(access_jwt, public_key, {algorithms : ['RS512']}, (err, user) => {
if (err) return res.status(403).json("Token not valid")
req.user = user
next()
})
}
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "PUBLIC_FOLDER/images")
},
filename: (req, file, cb) => {
cb(null, file.originalname)
},
})
const upload = multer({ storage: storage })
app.post("/upload/image/:id", verify_token, upload.single("image"), async (req, res) => {
try {
prev_photo = await User.findById(req.params.id, {_id : 0, profilePic : 1})
if (prev_photo.profilePic !== "default.png") {
fs.unlinkSync("PUBLIC_FOLDER/images/" + prev_photo.profilePic)
}
await User.findByIdAndUpdate(req.params.id, {
profilePic : req.file.originalname,
})
res.status(200).send({"messages" : "File uploded successfully", "filename" : req.file.originalname})
} catch (error) {
console.error(error)
}
})
const storage_back = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "PUBLIC_FOLDER/background")
},
filename: (req, file, cb) => {
cb(null, file.originalname)
},
})
const upload_back = multer({ storage: storage_back });
app.post("/upload/background/:id", verify_token, upload_back.single("Cover-image"), async (req, res) => {
try {
try {
prev_photo = await User.findById(req.params.id, {_id : 0, coverPic : 1})
if (prev_photo.coverPic !== "defaultCover.png") {
fs.unlinkSync("PUBLIC_FOLDER/images/" + prev_photo.coverPic)
}
await User.findByIdAndUpdate(req.params.id, {
coverPic : req.file.originalname,
})
res.status(200).send({"messages" : "File uploded successfully", "filename" : req.file.originalname})
} catch (error) {
console.error(error)
}
} catch (error) {
console.error(error);
}
})
app.use("/api/v1/users", userRoute)
app.use("/api/v1/auth", authRoute)
app.use("/api/v1/conversations", convRoute)
app.use("/api/v1/messages", messageRoute)
/*app.use(express.static(path.join(__dirname, "/chat-client/build")))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/chat-client/build', 'index.html'))
})*/
// Η port η οποια θα τρεχει ο backend server μας
const server = app.listen(process.env.PORT || 8800, () => {
console.log("Backend Server is running")
})
const io = require("socket.io")(server, {cors: {origin: 'http://localhost:8800',}})
// web socket middleware, checks user id and allows connection
io.use((socket, next) => {
const userId = socket.handshake.auth.userId
if (!userId) {
return next(new Error("Invalid Id"))
}
socket.userId = userId
next()
})
io.on("connection", (socket) =>{
var users = []
// παρε το id του χρηστη απο τον client
for (let [id, socket] of io.of("/").sockets) {
users.push({
socketId : id,
userId : socket.userId
})
}
// στειλε ολους τους συνδεμενους χρηστες στον client
io.emit("users", users)
io.emit("me", socket.id)
// παρε το αντικειμενο senderId, receiverId, text
// απο τον client
socket.on("sendMsg", ({data, conversationId, initMess, read, to}) => {
// στειλε σε ολους τους συνδεμενους χρηστες
// το αντικειμενο senderId και text
io.to(to).emit("getMsg", {
initMess,
conversationId,
read,
data,
from : socket.id
})
})
socket.on("disc", (User) => {
users = users.filter(us => us.userId === User.userId)
io.emit("users", users)
})
socket.on("callUser", ({userToCall, signalData, from, name}) => {
io.to(userToCall).emit("callUser", {signal : signalData, from, name})
})
socket.on("answerCall", (data) => {
io.to(data.to).emit("callAccept", data.signal)
})
socket.on("disconnectCall", () => {
socket.broadcast.emit("callEnded")
})
})