-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (127 loc) · 5.95 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
const path = require('path')
const app = require('express')()
const https = require('https')
const config = require('./libs/config')
const applib = require('./libs/app-lib.js')
const apilib = require('./libs/api-lib.js')
const is_admin = applib.running_as_admin()
// Validate if runnign with Adminstrative (Root) permissions is allowed.
if (is_admin && !config.SECURITY.RUN_AS_ADMIN) {
console.log("ERROR: Running with Adminstrative permissions is not permitted.")
console.log(" See: " + config.APP.CONF_FILE)
process.exit(1)
}
// Verify and Initialize Certificates
if (config.SECURITY.HTTPS) {} // TBD
// Verify and Initialize Credentials
if (config.SECURITY.ENFORCE_API_KEYS) {} // TBD
// DEBUG
// console.log(JSON.stringify(config, null, 2))
// ============= API Routing ============
let api_key='API-KEY' // Not currently Used
// API Queries
// app.use('/api/*', function(req, res) {
// const segments = req.params[0].split('/');
// const req_array = segments.filter(segment => segment !== ''); // Remove empty segments
// const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
// applib.logger('INFO: ACCESS GRANTED: ' + fullUrl + ' TO: '+ req.connection.remoteAddress)
// // res.writeHead(200, {'Content-Type': 'application/json'})
// // res.end(JSON.stringify(apilib.api_router(req_array)), null, 2))
// const responseData = apilib.api_router(req_array);
// res.status(200).json(responseData);
// })
app.use('/api/*', function(req, res) {
const segments = req.params[0].split('/');
const req_array = segments.filter(segment => segment !== ''); // Remove empty segments
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
applib.logger('INFO: ACCESS GRANTED: ' + fullUrl + ' TO: '+ req.connection.remoteAddress)
const responseData = apilib.api_router(req_array);
// Check for the content type in the response data
const contentType = responseData['CONTENT-TYPE'];
switch (contentType) {
case 'application/json':
res.status(200).json(responseData['PAYLOAD']);
break;
case 'application/pdf':
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'inline; filename="' + responseData['FILENAME'] + '"');
res.status(200).send(responseData['PAYLOAD']);
break;
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheetcsv':
// Handle XLSX content
// Convert your responseData['PAYLOAD'] to CSV format
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheetcsv');
res.setHeader('Content-Disposition', 'attachment; filename="' + responseData['FILENAME'] + '"');
res.status(200).send(responseData['PAYLOAD']);
break;
case 'text/csv':
// Handle CSV content
// Convert your responseData['PAYLOAD'] to CSV format
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename="' + responseData['FILENAME'] + '"');
res.status(200).send(responseData['PAYLOAD']);
break;
// Add other cases as needed for different content types
default:
// Default response, e.g., if the content type is not supported
res.status(415).send('Unsupported Media Type');
}
})
app.use('/favicon.ico', function(req, res) {
// applib.logger('INFO: ACCESS GRANTED: ' + req.originalUrl + ' TO: '+ req.connection.remoteAddress)
res.sendFile(path.join(__dirname, 'favicon.ico'))
})
// Usage
app.use('/help', function(req, res) {
// applib.logger('INFO: ACCESS GRANTED: ' + req.originalUrl + ' TO: '+ req.connection.remoteAddress)
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(apilib.get_usage(api_key), null, 2))
})
app.use('/usage', function(req, res) {
// applib.logger('INFO: ACCESS GRANTED: ' + req.originalUrl + ' TO: '+ req.connection.remoteAddress)
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(apilib.get_usage(api_key), null, 2))
})
// Config - DISABLE For Production
app.use('/config', function(req, res) {
// applib.logger('INFO: ACCESS GRANTED: ' + req.originalUrl + ' TO: '+ req.connection.remoteAddress)
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(config, null, 2))
})
app.use('/', function(req, res) {
// applib.logger('INFO: ACCESS GRANTED: ' + req.originalUrl + ' TO: '+ req.connection.remoteAddress)
// let valid_api_key = apilib.verify_key(req.query.key)
// switch (valid_api_key) {
// case true:
// api_key = req.query.key
// break;
// }
res.writeHead(200, {'Content-Type': 'application/json'})
res.end(JSON.stringify(apilib.get_usage(api_key), null, 2))
})
// 404 Errors
app.use(function (req, res, next) {
res.status(404).send("404 Page not found: " + req.url )
var remote_ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress
remote_ip = remote_ip.replace(/^\:\:ffff\:/, '') // IPv4 part only
console.log('ERR ' + res.statusCode + ' ' + req.method + ' ' + remote_ip + ' [' + req.headers['user-agent'] + '] ' + ' ' + req.url)
})
// Starting Express Web Service
// https://hackernoon.com/set-up-ssl-in-nodejs-and-express-using-openssl-f2529eab5bb
switch (config.SECURITY.HTTPS) {
case true: // HTTPS
https.createServer({
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
cert: fs.readFileSync(path.join(__dirname,'cert.pem')),
passphrase: config.CREDENTIALS.SSL.CERTIFICATE_PASSPHRASE
}, app).listen(config.APP.API_TCP_PORT)
break;
default: // Non SSL
app.listen(config.APP.API_TCP_PORT, function () {
console.log('Auth API on: http://localhost:' + config.APP.API_TCP_PORT)
})
}
applib.logger("Started " + config.PACKAGE.name)
applib.logger(" Version: " + config.PACKAGE.version)
applib.logger(" Date: " + config.PACKAGE.version_date)
applib.logger(" Description: " + config.PACKAGE.description)