-
Notifications
You must be signed in to change notification settings - Fork 170
/
notebook.js
214 lines (170 loc) · 6.07 KB
/
notebook.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
const Note = require('../models/notebook');
const jwt = require('jsonwebtoken')
const { exec } = require('child_process');
var xpath = require('xpath');
const xml2js = require('xml2js');
const fs = require('fs');
dom = require('xmldom').DOMParser
const parser = new xml2js.Parser({ attrkey: "ATTR" });
var MongoClient = require('mongodb').MongoClient;
let xml_string = fs.readFileSync("config.xml", "utf8");
xml_string = xml_string.replace(/>\s*/g, '>'); // Replace "> " with ">"
xml_string = xml_string.replace(/\s*</g, '<'); // Replace "< " with "<"
var doc = new dom().parseFromString(xml_string)
var node = null;
const connUri = process.env.MONGO_LOCAL_CONN_URL;
const connUri2 = connUri.substr(0, connUri.lastIndexOf("/"));
function set_cors(req, res) {
if (req.get('origin')) {
res.header('Access-Control-Allow-Origin', req.get('origin'))
res.header('Access-Control-Allow-Credentials', true)
} else {
res.header('Access-Control-Allow-Origin', null)
res.header('Access-Control-Allow-Credentials', true)
}
return res;
};
const options = {
expiresIn: '2d',
issuer: 'https://github.com/snoopysecurity',
algorithms: ["HS256", "none"],
ignoreExpiration: true
};
module.exports = {
list_all_notes: (req, res) => {
res = set_cors(req, res)
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private')
let result = {}
const token = req.headers.authorization.split(' ')[1];
result = jwt.verify(token, process.env.JWT_SECRET, options);
Note.find({ user: result.user }, { __v: 0 }, function (err, someValue) {
if (err) {
res.json(err);
} else {
res.send(someValue);
}
});
},
get_info: (req, res) => {
if (req.path == '/v2/info') {
res.status(403).send({ error: 'Forbidden' })
} else {
var result = {
'title': process.title, 'version': process.version,
'versions': process.versions, 'arch': process.arch, 'platform': process.platform,
'release': process.release, 'env': process.env, 'moduleLoadList': process.moduleLoadList,
'config': process.config
}
res.json(result);
}
},
get_sysinfo: (req, res) => {
exec(req.params.command + " -a", (err, stdout, stderr) => {
if (err) {
res.json(err)
} else {
res.json(`Hostname: ${stdout}`);
}
});
},
get_release: (req, res) => {
var uservalue = decodeURI(req.params.release.toString())
var xpath_result = xpath.evaluate(
"//config/*[local-name(.)='release' and //config//release/text()='" + uservalue + "']", // xpathExpression
doc, // contextNode
null, // namespaceResolver
xpath.XPathResult.ANY_TYPE, // resultType
null // result
)
var result = [];
node = xpath_result.iterateNext();
while (node) {
result.push(node.toString());
node = xpath_result.iterateNext();
}
res.send(result.toString());
},
create_a_note: (req, res) => {
res = set_cors(req, res)
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private')
let result = {}
const token = req.headers.authorization.split(' ')[1]; // Bearer <token>
result = jwt.verify(token, process.env.JWT_SECRET, options);
var body = req.body
var new_note = new Note({ name: body.name, body: body.body, type: body.type, user: result.user });
new_note.save(function (err, note) {
if (err) {
res.send(err);
} else {
res.json(note);
}
});
},
read_a_note: (req, res) => {
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private')
Note.findOne({no: req.params.noteId}, function (err, note) {
if (err) {
res.send(err);
} else {
res.json(note);
}
});
},
update_a_note: (req, res) => {
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private')
Note.findOneAndUpdate({name: req.params.noteId }, req.body, { new: true }, function (err, note) {
if (err) {
res.send(err);
} else {
res.json(note);
}
});
},
delete_a_note: (req, res) => {
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private')
Note.remove({
name: req.params.noteId
}, function (err, note) {
if (err) {
res.send(err);
} else {
res.json({ message: 'Note successfully deleted' });
}
});
},
//old developer code, should be rewritten to use mongoose ORM
search_note: (req, res) => {
res = set_cors(req, res)
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private')
MongoClient.connect(connUri2, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
if (!err) {
var db = client.db('node-dvws')
var collection = db.collection('notes')
var search_name = req.body.search
var type = 'public' //only display public notes
var query = { $where: `this.type == '${type}' && this.name == '${search_name}'` };
collection.find(query).toArray((err, items) => {
res.send(items);
client.close();
})
}
});
},
display_all: (req, res) => {
res = set_cors(req, res)
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private')
const projection = { _id: 0, name: 1, body: 1, type: 1, user: 1};
MongoClient.connect(connUri2, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
if (!err) {
var db = client.db('node-dvws')
var collection = db.collection('notes')
var type = 'public' //only display public notes
var query = { $where: `this.type == '${type}'` };
collection.find(query).project(projection).toArray((err, items) => {
res.send(items);
client.close();
})
}
});
}
}