This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (196 loc) · 7.75 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
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
var localGmail = require("./gmail.js")
/*localGmail.initGmail((url,auth)=>{
const gmail = google.gmail({version: 'v1', auth});
//listen(gmail,db,latestHistory)
})*/
var http = require('http');
var fs = require('fs');
var path = require('path');
//var qs = require('querystring');
var indexFile
var otherFile
/*fs.readFile('./public/index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
res.write(req.url);
response.write(html);
response.end();
}).listen(8000);
});*/
http.createServer(function (request, response) {
console.log('request starting...');
var filePath = './public' + request.url;
if (filePath == './public/')
filePath = './public/index.html';
var extname = path.extname(filePath);
var isCommand = false
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
case '.wav':
contentType = 'audio/wav';
break;
case '':
console.log("prolly a command")
isCommand = true
handleCommand(request,response)
}
if(!isCommand){
console.log("reading: "+filePath)
fs.readFile(filePath, function(error, content) {
if (error) {
console.log(error)
if(error.code == 'ENOENT'){
fs.readFile('./public//404.html', function(error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
response.end();
}
}
else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}
}).listen(8254);
console.log('Server running at http://localhost:8254/');
function handleCommand(request,response){
console.log(request.body)
var result = {}
switch(request.url){
case '/init':
var localGmail = require("./gmail.js")
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6)
request.connection.destroy();
});
request.on('end', function () {
// var post = qs.parse(body);
result.result = "success"
body = JSON.parse(body)
console.log(body)
if(body.code){
console.log("starting i guess")
localGmail.initGmail(body.code,(url,auth)=>{
if(auth){
result.result = "success"
response.writeHead(200, { 'Content-Type':'text/html' });
response.end(JSON.stringify(result), 'utf-8');
}
})
} else {
result.result = "error"
response.writeHead(200, { 'Content-Type':'text/html' });
response.end(JSON.stringify(result), 'utf-8');
}
// use post['blah'], etc.
});
} else{
localGmail.initGmail("",(url,auth)=>{
if(url){
console.log("hehe")
result.response = "success"
result.url = url
response.writeHead(200, { 'Content-Type':'text/html' });
response.end(JSON.stringify(result), 'utf-8');
}
})
}
break
case '/check':
var localGmail = require("./gmail.js")
localGmail.checkToken(err=>{
if(err){
result.result = "error"
response.writeHead(200, { 'Content-Type':'text/html' });
response.end(JSON.stringify(result), 'utf-8');
} else {
result.result = "success"
response.writeHead(200, { 'Content-Type':'text/html' });
response.end(JSON.stringify(result), 'utf-8');
}
})
break
case '/status':
var cMail = require("./cMail.js")
result.status = cMail.getStatus()
result.result = "success"
response.writeHead(200, { 'Content-Type':'text/html' });
response.end(JSON.stringify(result), 'utf-8');
break
case '/start':
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6)
request.connection.destroy();
});
request.on('end', function () {
// var post = qs.parse(body);
result.result = "success"
body = JSON.parse(body)
console.log(body)
var localGmail = require("./gmail.js")
localGmail.initGmail("",(url,auth)=>{
if(auth){
var cMail = require("./cMail.js")
const {google} = require('googleapis');
const gmail = google.gmail({version: 'v1', auth});
cMail.startD(gmail,{
cost:body.cost,
message:body.message
})
result.result = "success"
response.writeHead(200, { 'Content-Type':'text/html' });
response.end(JSON.stringify(result), 'utf-8');
}
})
// use post['blah'], etc.
});
}
break
case '/stop':
result.result = "success"
response.writeHead(200, { 'Content-Type':'text/html' });
response.end(JSON.stringify(result), 'utf-8');
process.kill(process.pid, 'SIGTERM')
break
default:
response.writeHead(500);
result.result = "error"
result.error = "Sorry, thsi command is invalid"
response.end(JSON.stringify(result));
response.end();
}
}