-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
259 lines (246 loc) · 7 KB
/
server.js
File metadata and controls
259 lines (246 loc) · 7 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
let express = require("express");
let mysql = require("mysql");
let cors = require("cors");
let fs = require("fs");
let path = require("path");
let crypto = require("crypto");
let concat = require("concat-stream");
let twilio = require("twilio")("ACa04b84d457ce0ec858957de2e7eae4b9", "577b20694c0ab5ecb15a26e694a24b30");
let dbconfig = require("./database.js");
let connection = mysql.createConnection( dbconfig );
let app = express();
let corsOptions = {
origin: "*",
optionsSuccessStatus: 200
};
let id = "";
let pw = "";
let email = "";
let phone = "";
let rank = "";
let dir = path.join(__dirname, "/");
let security = {
"whether" : false,
securityCode : 0
};
let key = process.env.ENCRYPTION_KEY || 'abcdefghijklmnop'.repeat(2);
function encrypt(text) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
const encrypted = cipher.update(text);
return iv.toString('hex') + ':' + Buffer.concat([encrypted, cipher.final()]).toString('hex');
};
function decrypt(text) {
const textParts = text.split(':');
const iv = Buffer.from(textParts.shift(), 'hex');
const encryptedText = Buffer.from(textParts.join(':'), 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
const decrypted = decipher.update(encryptedText);
return Buffer.concat([decrypted, decipher.final()]).toString();
};
function securityPassword() {
security.securityCode = Math.round(Math.random()*100000+1);
return security.securityCode;
};
// let getToken = () => "pco";
// let encryption = (password) => {
// let arr = [...password];
// let arr2 = [...getToken()];
// let arr3 = [];
// for(let i = 0; i < arr2.length; i++) {
// arr[i] += arr2[i];
// }
// for(let j = 0; j < arr.length; j++) {
// arr[0] += arr[j];
// }
// arr3.push(arr[0].slice(2, arr[0].length));
// let encryption_pw = arr3[0] + 1110;
// return encryption_pw;
// };
app.use(express.static(dir));
app.use(cors({
origin: "*",
optionsSuccessStatus: 200
}));
app.post("/user/checkDuplicate", function(req, res, next) {
req.pipe(concat(function(data) {
req.body = JSON.parse(data);
next();
}));
}, function(req, res) {
connection.query(`select ID from people where ID='${req.body.id}';`, function(err, rows) {
if(err) res.send({"message" : err});
if(rows && rows == "" || null || undefined || 0) {
res.send({"message" : "not overlap"})
}else{
res.send({"message" : "overlap!"});
};
})
});
app.post("/user/register", function(req, res, next) {
req.pipe(concat(function(data){
req.body = JSON.parse(data);
next();
}));
}, function(req, res) {
pw = encrypt(req.body.pw);
connection.query(`insert into people(ID,Password,Email,Phone,Update_Time)values('${req.body.id}','${pw}','${req.body.email}','${req.body.phone}',NOW());`, (err, rows) => {
if(err) {
res.send({"message" : "Duplicate error!"});
}else {
id = req.body.id;
email = req.body.email;
phone = req.body.phone;
rank = "Nomal";
res.send({"message" : "register success"});
};
});
});
app.post("/user/login", function(req, res, next) {
req.pipe(concat(function(data){
req.body = JSON.parse(data);
next();
}));
}, function(req, res) {
connection.query(`select * from people where ID='${req.body.id}';`, function(err, rows) {
if(err) res.send({"error message" : err});;
if(rows) {
if(decrypt(rows[0].Password) == req.body.pw) {
id = rows[0].ID;
email = rows[0].Email;
phone = rows[0].Phone;
rank = rows[0].Rank;
res.send({"message" : "success"});
}else {
res.send({"message" : "fail"});
};
};
});
});
app.post("/user/logout", function(req, res) {
id = "";
email = "";
phone = "";
rank = "";
});
app.post("/user/searchId", function(req, res, next) {
req.pipe(concat(function(data){
req.body = JSON.parse(data);
next();
}));
}, function(req, res) {
connection.query(`select * from people where Email='${req.body.email}' and Phone='${req.body.phone}';`, (err, rows) => {
if(err) res.send(err);
if(rows) {
try{
if(rows[0].ID == undefined) throw "undefined";
twilio.messages.create({
to: `+82${req.body.phone}`,
from: "+14698047083",
body: `아이디는 ${rows[0].ID}입니다.`
});
res.end();
}catch(err) {
res.send({"message" : err});
}
};
});
});
app.post("/user/searchPassword", function(req, res, next) {
req.pipe(concat(function(data) {
req.body = JSON.parse(data);
next();
}));
}, function(req, res) {
connection.query(`select * from people where ID='${req.body.id}' and Email='${req.body.email}' and Phone='${req.body.phone}';`, (err, rows) => {
if(err) res.send(err);
if(rows) {
try{
if(rows[0].Password == undefined) throw "undefined";
twilio.messages.create({
to: `+82${req.body.phone}`,
from: "+14698047083",
body: `비밀번호는 ${decrypt(rows[0].Password)}입니다.`
});
res.end();
}catch(err) {
res.send({"message" : err});
}
};
})
})
app.post("/write/write", function(req, res, next) {
req.pipe(concat(function(data){
req.body = JSON.parse(data);
next();
}));
}, function(req, res) {
connection.query(`insert into contents values('${req.body.id}','${req.body.title}','${req.body.content}',NOW(), 1);`, function(err, rows) {
if(rows) {
res.send({"add data" : "success"});
}else {
res.send({"err" : err})
};
});
});
app.post("/security/getCode", function(req, res, next) {
req.pipe(concat(function(data){
req.body = JSON.parse(data);
next();
}));
}, function(req, res) {
securityPassword();
twilio.messages.create({
to: `+82${req.body.phone}`,
from: "+14698047083",
body: `보안문자는 ${security.securityCode}입니다.`
});
res.send({"code" : security.securityCode});
});
app.get("/main", function(req, res) {
res.sendFile(path.join(__dirname, "../main.html"));
});
app.get("/userData", function(req, res) {
res.send({
"status" : "200",
"id" : id,
"email" : email,
"phone" : phone,
"rank" : rank
});
});
app.get("/login", function(req, res) {
res.sendFile(path.join(__dirname, "../login.html"));
});
app.get("/register", function(req, res) {
res.sendFile(path.join(__dirname, "../register.html"));
});
app.get("/user", function(req, res) {
res.sendFile(path.join(__dirname, "../user.html"));
});
app.get("/write/write-content", function(req, res) {
res.sendFile(path.join(__dirname, "../writeContent.html"));
});
app.get("/write/list", function(req, res) {
res.sendFile(path.join(__dirname, "../writeList.html"));
});
app.get('/write/ListData', function(req, res) {
connection.query(`select * from contents where Whether=1`, function(err, rows) {
if(rows) {
res.send({
"success" : "success",
"data" : rows
});
}else {
res.send({"err" : err});
};
});
});
app.get("/user/searchId", function(req, res) {
res.sendFile(path.join(__dirname, "../searchId.html"));
});
app.get("/user/changeInfo", function(req, res) {
res.sendFile(path.join(__dirname, "../changeInfo.html"));
});
app.set("port", process.env.PORT || 3000 );
app.listen( app.get("port"), console.log("Express 서버를 시작했습니다. 포트 : " + app.get('port')));