repl.it
@AdCharity/

repl-mail-server-old

Nodejs

old server for repl mail

fork
loading
Files
  • index.js
  • data
  • config.js
  • config.json
  • methods.js
  • reports.txt
  • Packager files
  • package-lock.json
  • package.json
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
// Files

console.log("Importing files...")
const config = require("./config.js").config
const methods = require("./methods.js")

// Startup

methods.log("Starting repl mail server...")

// Modules

methods.log("Importing modules...")
const http = require("http")

// HTTP server

methods.log("Creating HTTP server...")

const server = http.createServer(function(req, res) {
    // Access-Control-Allow-Origin

    if (req.headers.origin) {
        res.setHeader("Access-Control-Allow-Origin", req.headers.origin)
    } else {
        res.setHeader("Access-Control-Allow-Origin", "*")
    }

    // Handle requests

    if (req.method == "POST") {
        // Check origin

        if (req.headers.origin && config.origins.includes(req.headers.origin)) {
            // Collect data

            let data = ""
            req.on("data", function(chunk) {
                data += chunk.toString()
                if (data.length > config.reqLimit) {
                    // Request too large

                    res.writeHead(400)
                    res.write("ERROR: REQUEST TOO LARGE")
                    res.end()
                }
            })

            // Data collected

            req.on("end", function() {
                // Parse data

                const type = data.substr(0, data.indexOf("|"))
                const content = data.substr(data.indexOf("|") + 1)

                // Check data

                if (type && content) {
                    // Process data

                    methods.process_req(type, content).then(function(response) {
                        // Send response
                        
                        res.writeHead(200)
                        res.write(response)
                        res.end()
                    }).catch(function(error) {
                        // Internal error

                        res.writeHead(500)
                        res.write("ERROR: Internal server error")
                        res.end()

                        // Log error

                        methods.log(`ERROR: ${error}`, true)
                    })
                } else {
                    // Invalid data

                    res.writeHead(200)
                    res.write("ERROR: Invalid request data")
                    res.end()
                }
            })
        } else {
            // Invalid origin

            res.writeHead(403)
            res.write("REJECTED: INVALID ORIGIN")
            res.end()
        }
    } else {
        // Generic response

        res.writeHead(200)
        res.write("repl mail server\nserver code is accessible here - https://filebin.net/lfsxzchai17287bb")
        res.end()
    }

}).listen(config.port)

server.on("listening", function() {
    methods.log(`HTTP server listening on port ${config.port}`)
})

/*
// Sessions update interval

setInterval(function() {
    methods.update_sessions()
}, 60000)
*/
Fetching token
?