-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
5,311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE IF NOT EXISTS `message` ( | ||
[MessageID] INTEGER NOT NULL, | ||
[WebhookID] INTEGER NOT NULL, | ||
[Content] TEXT | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE IF NOT EXISTS `user` ( | ||
[UserID] INTEGER NOT NULL, | ||
[Email] VARCHAR(100) NOT NULL, | ||
[Password] VARCHAR(120) NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE IF NOT EXISTS `webhook` ( | ||
[WebhookID] INTEGER NOT NULL, | ||
[UserID] INTEGER NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as sqlite from 'sqlite3'; | ||
const db = new sqlite.Database('test.sqlite'); | ||
import * as fs from 'fs'; | ||
|
||
new Promise((resolve, reject) => { | ||
return fs.readdir(__dirname + '/Tables', (err: any, filenames: string[]) => err != null ? reject(err) : resolve(filenames)) | ||
}).then((filenames: any) => { | ||
filenames.forEach((data: string) => { | ||
fs.readFile(__dirname + '/Tables/'+data,'utf-8',(err:any, datax: string) => { | ||
db.serialize(() => { | ||
db.run(datax); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
export default db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import express,{Router,Request,Response,NextFunction} from 'express'; | ||
const router : Router = express(); | ||
import * as crypt from 'bcrypt'; | ||
import jwt, { sign, SignOptions } from 'jsonwebtoken'; | ||
import db from '../Database/database'; | ||
import SnowFlake from '../Utils/Snowflake'; | ||
|
||
router.post('/login',async(req: Request<{username: string, password: string}>, res:Response) => { | ||
db.serialize(() => { | ||
db.all(`SELECT * FROM user WHERE email= '${req.body.username}'`,async (err,row) =>{ | ||
if(row[0] == undefined)return res.send(JSON.stringify({code: '403', message: 'Access denied.'})); | ||
console.log(req.body.password, row[0].Password); | ||
if(await crypt.compare(req.body.password, row[0].Password)){ | ||
const sigInOpt: SignOptions = { | ||
algorithm: 'HS256', | ||
expiresIn: '24h' | ||
} | ||
var token = sign({user: {id: row[0].UserID}},"N05EUnVpdDQ4dTYmNFI2cCEyRHJOZXVSSTYxTSFAZXl5OU1zejEmeWdOJUBwOFJMJWs",sigInOpt) | ||
return res.send(JSON.stringify({id:row[0].UserID, username: row[0].Email, token: token})); | ||
}else{ | ||
return res.send(JSON.stringify({code: '403', message: 'Access denied.'})); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
router.post('/webhook/:id/messages',async(req: Request<{content: string, id: number}>, res:Response,next: NextFunction) => { | ||
if(req.body.content == undefined)return res.send({code: 4003, message: 'Invalid Form Body.'}); | ||
if(!Number(req.params.id)){return res.send({code: 4005, message: 'Invalid Webhook Id.'})} | ||
db.serialize(() => { | ||
db.all(`SELECT * FROM webhook WHERE WebhookID=${req.params.id}`,(err,row) => { | ||
if(!row[0])return res.send({code: 4005, message: 'Webhook invalid.'}) | ||
db.exec(`INSERT INTO message (MessageID,WebhookID,Content) VALUES(${SnowFlake()}, ${req.params.id}, '${req.body.content}')`); | ||
return res.send({code: 200, message: 'OK'}); | ||
}); | ||
}); | ||
}); | ||
|
||
router.use((req: Request, res:Response,next: NextFunction) => { | ||
let token = req.headers.authorization; | ||
if(token == undefined)return res.send(JSON.stringify({code: '403', message: 'Access denied.'})); | ||
const decoded = jwt.decode(token, {complete: true}); | ||
if (decoded === null) {return res.status(401).send(JSON.stringify({code: '403', message: 'Access denied.'}));} | ||
if(decoded !== null) next(); | ||
}); | ||
|
||
|
||
router.post('/webhook/',async(req: Request, res:Response,next: NextFunction) => { | ||
let token = req.headers.authorization; | ||
if(token == undefined)return res.send(JSON.stringify({code: '403', message: 'Access denied.'})); | ||
const decoded = jwt.decode(token, {complete: true}); | ||
if (decoded === null) {return res.status(401).send(JSON.stringify({code: '403', message: 'Access denied.'}));} | ||
|
||
let payload : any = decoded.payload; let id = payload.user.id; | ||
|
||
try { | ||
db.exec(`INSERT INTO webhook (WebhookID,UserID) VALUES(${SnowFlake()},${id})`); | ||
return res.send({code: 200, message: 'OK'}); | ||
} catch (error) { | ||
if(error)console.log(error); | ||
} | ||
}); | ||
|
||
|
||
router.delete('/webhook/:id/',async(req: Request, res:Response,next: NextFunction) => { | ||
let token = req.headers.authorization; | ||
if(token == undefined)return res.send(JSON.stringify({code: '403', message: 'Access denied.'})); | ||
const decoded = jwt.decode(token, {complete: true}); | ||
if (decoded === null) {return res.status(401).send(JSON.stringify({code: '403', message: 'Access denied.'}));} | ||
if(!Number(req.params.id)){return res.send({code: 4005, message: 'Invalid Webhook Id.'})} | ||
|
||
let payload : any = decoded.payload; let id = payload.user.id; | ||
|
||
db.serialize(() => { | ||
db.all(`SELECT * FROM webhook WHERE WebhookID=${req.params.id}`,(err,row) => { | ||
if(!row[0])return res.send({code: 4005, message: 'Webhook invalid.'}) | ||
if(id == row[0].UserID){ | ||
db.exec(`DELETE FROM webhook WHERE WebhookID=${req.params.id}`) | ||
res.send({code: 200, message:'OK'}); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
router.get('/webhook/:id/messages',async(req: Request<{id: number}>, res:Response,next: NextFunction) => { | ||
let token = req.headers.authorization; | ||
if(token == undefined)return res.send(JSON.stringify({code: '403', message: 'Access denied.'})); | ||
const decoded = jwt.decode(token, {complete: true}); | ||
if (decoded === null) {return res.status(401).send(JSON.stringify({code: '403', message: 'Access denied.'}));} | ||
if(!Number(req.params.id)){return res.send({code: 4005, message: 'Invalid Webhook Id.'})} | ||
|
||
let payload : any = decoded.payload; let id = payload.user.id; | ||
|
||
db.serialize(() => { | ||
db.all(`SELECT * FROM webhook WHERE WebhookID=${req.params.id}`,(err,row) => { | ||
if(!row[0])return res.send({code: 4005, message: 'Webhook invalid.'}) | ||
if(id == row[0].UserID){ | ||
db.all(`SELECT * FROM message WHERE WebhookID=${req.params.id}`,async(err,row) => { | ||
var messages: any[] = []; | ||
row.forEach(message => {messages.push(message)}); | ||
await res.send({messages:messages}); | ||
}); | ||
}else{ | ||
return res.send(JSON.stringify({code: '403', message: 'Access denied.'})); | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
router.get('/',(req: Request, res:Response,next: NextFunction) => { | ||
res.send('Welcome!'); | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default function SnowFlake(){ | ||
var date = Date.now(); | ||
var id = Math.floor(Math.random() * (999999999999999999 - 0 + 100000000000000000)) + 100000000000000000; | ||
return id+date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import express,{Express,Request,Response,NextFunction} from 'express'; | ||
import http from "http"; | ||
import router from './Router/mainRoute'; | ||
import * as cons from 'consolidate'; | ||
import * as path from 'path'; | ||
import './Database/database'; | ||
|
||
const app : Express = express(); | ||
var server = http.createServer(app); | ||
|
||
server.listen(3000,() => { | ||
console.log('[Server] Server started with 3000 port.'); | ||
}); | ||
|
||
app.engine('html', cons.swig) | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'html'); | ||
app.enable('trust proxy'); | ||
|
||
app.use(express.json()); | ||
app.use(express.urlencoded({extended: true})); | ||
|
||
app.use((req: Request, res:Response,next: NextFunction) => { | ||
res.setHeader('content-type','application/json'); | ||
res.setHeader('charset','utf8'); | ||
res.setHeader('X-Powered-By','643077ed7b14af1f') | ||
next(); | ||
}) | ||
|
||
app.use('/',router) | ||
|
||
app.use((req: Request, res:Response,next: NextFunction) => { | ||
res.render('404'); | ||
}); | ||
|
Oops, something went wrong.