summaryrefslogtreecommitdiff
path: root/app.js
blob: 0c1872f3f2273c3d7e3fa277c4586d6c26b2d5fa (plain)
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
const express = require('express')
const app = express()
const limit = require('express-limit').limit
const helmet = require('helmet')
const bp = require('body-parser')
const cookieParser = require('cookie-parser')
const createDOMPurify = require('dompurify')
const { JSDOM } = require('jsdom')
const Controller = require('./source/controller')
const readfile = require('./source/readfile')
const session = require('./source/session')
const logger = require('./source/logger')()
const mime = {
    html: 'text/html',
    txt: 'text/plain',
    css: 'text/css',
    gif: 'image/gif',
    jpg: 'image/jpeg',
    png: 'image/png',
    webp: 'image/webp',
    avif: 'image/avif',
    ico: 'image/ico',
    svg: 'image/svg+xml',
    js: 'application/javascript'
}
require('dotenv').config()

// Handles the routes
class App {
    constructor() {
        this.readfile = new readfile({baseUrl: process.env.baseUrl, mime: mime})
        this.controller = new Controller()
    }

    App() {
        app.use(bp.json())
        app.use(cookieParser())
        if (process.env.NODE_ENV === 'prod') {
            app.use(helmet())
            app.use(limit({
                max: 100, // 100 requests
                period: 300 * 1000 // per minute (five minutes)
            }))
        }

        app.get('/js/*', this.#Logger, (req, res) => {
            const data = this.readfile.GetFile(req.path)
            data.mime.then(output => this.#FileOpen({data: data, mime: output, res: res}))
        })
        app.get('/css/*', this.#Logger, (req, res) => {
            const data = this.readfile.GetFile(req.path)
            data.mime.then(output => this.#FileOpen({data: data, mime: output, res: res}))
        })
        app.get('/img/*', this.#Logger, (req, res) => {
            const data = this.readfile.GetFile(req.path)
            data.mime.then(output => this.#FileOpen({data: data, mime: output, res: res}))
        })

        app.route('*')
            .get(this.#ValidateCookie, this.#Logger, (req, res) => {
                this.controller.Main().then(output => {
                    this.path = {
                        split: req.path.split('/'),
                        string: req.path
                    }

                    switch (this.path.split[1]) {
                        case 'favicon.ico':
                            this.data = this.readfile.GetFavicon()
                            this.data.mime.then(output => this.#FileOpen({data: this.data, mime: output, res: res}))
                            break
                        case 'robots.txt':
                            this.data = this.readfile.GetRobots()
                            this.data.mime.then(output => this.#FileOpen({data: this.data, mime: output, res: res}))
                            break
                        default:
                            if (this.path.string.endsWith('/') && this.path.string.length > 1) this.path.string = this.path.string.substring(0, this.path.string.length - 1)
                            if (output.has(this.path.string)) {res.send(this.readfile.GetMain())} else {res.sendStatus(404)}
                            break

                    }
                })
            })
            .post(this.#Logger, (req, res) => {
                this.path = {
                    split: req.path.split('/'),
                    string: this.#RemoveBaseUrl(req.body.url)
                }

                if (this.path.string.endsWith('/') && this.path.string.length > 1) this.path.string = this.path.string.substring(0, this.path.string.length - 1)
                this.controller.Main(req.body.value).then(output => {
                    if (output.has(this.path.string)) {
                        const data = this.readfile.Create(output.get(this.path.string))
                        data.then(output => {
                            try {
                                const window = new JSDOM('').window
                                const DOMPurify = createDOMPurify(window)
                                const clean = DOMPurify.sanitize(output.layouts.header + output.layouts.data + output.layouts.footer)
                                res.send(clean)
                            } catch (err) {
                                logger.Error(err)
                                res.send('Not Found')
                            }
                        })
                    } else {
                        res.send('Not Found')
                    }
                })
            })

        app.listen(3000)
    }

    #Logger(req, res, next) {
        const { cookies } = req
        try {
            logger.Info(`${req.method} ${req.path} from ${req.ip}:${cookies.session_id.replace('session_id', '')}`)
        } catch (err) {
            logger.Info(`${req.method} ${req.path} from ${req.ip}:n/a`)
        }
        next()
    }
    #FileOpen(x) {
        x.res.set('Content-Type', x.mime)
        x.data.file.on('open', () => x.data.file.pipe(x.res)).on('error', () => x.res.sendStatus(404))
    }
    #ValidateCookie(req, res, next) {
        const { cookies } = req
        session().Expired()
        if ('session_id' in cookies) {
            if (session().Exists(cookies.session_id.replace('session_id', ''))) {
                res.clearCookie('session_id')
                res.cookie('session_id', session().Create())
            }
        } else res.cookie('session_id', session().Create())
        next()
    }
    #RemoveBaseUrl(x) {
        const object = {
            baseUrlPattern: /^https?:\/\/[a-z\:0-9.]+/,
            result: '',
            url: ''
        }
        const match = object.baseUrlPattern.exec(x)
        if (match !== null) object.result = match[0]
        if (object.result.length > 0) object.result = match[0]
        if (object.result.length > 0) object.url = x.replace(object.result, '')
        return object.url
    }
}
new App().App()