-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathapp.js
165 lines (147 loc) · 4.02 KB
/
app.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
'use strict';
var DataCenter = require('./core');
var config = require('./config');
var accesslog = require('./core/accesslog');
var graceful = require('graceful');
var app = require('koa')();
var router = require('koa-router')();
var koaBody = require('koa-body')();
var crypto = require('crypto');
var pkgJSON = require('./package.json');
app.on('error', function (err) {
console.error(err)
console.error(err.stack)
});
app.use(accesslog());
app.use(function *(next) {
if (this.request.header.origin && this.request.header.origin.match(/datav.aliyun.com(:\d+)?/)) {
this.set('Access-Control-Allow-Origin', '*')
this.set('Content-Type', 'application/json; charset=utf-8');
}
yield next;
});
app.use(function *exceptionHandler(next){
try {
yield next;
} catch(err) {
if (err.redirect) {
this.redirect(err.redirect);
} else if(err*1 == err){
this.status = err;
} else if(typeof err == 'string') {
var data = {
isError: true,
message: err
}
this.body = JSON.stringify(data);
} else {
console.error(err.stack);
this.status = 500;
this.body = err.message || 'Server Error';
}
}
});
router.get('/status', function *() {
this.body = "hi there."
});
router.get('/version', function *() {
this.body = pkgJSON.version;
});
router.get('/database', function *() {
var key = this.query._datav_id;
var time = parseInt(this.query._datav_time);
var data = decrypt(key, time);
if (!data) return this.status = 401;
delete this.query._datav_id;
delete this.query._datav_time;
var db = findDB(data.db)
if (!db) throw "数据库不存在:" + data.db;
this.body = yield DataCenter.get({
type: 'database',
config: {
sql: data.sql,
storage: db
}
}, this.query);
})
router.get('/get/databases', function *() {
var key = this.query._datav_id;
var time = parseInt(this.query._datav_time);
try {
var data = decrypt(key, time);
} catch(e) {
return this.body={isError: true, message:"签名错误"};
}
var dbs = [];
if (data.action != 'getDatabases') return this.body={isError: true, message:"配置错误"};
this.body = {data: dbs};
config.databases.forEach(function(db){
dbs.push(db.id);
});
})
router.get('/test/connection', function *() {
var key = this.query._datav_id;
var time = parseInt(this.query._datav_time);
try {
var data = decrypt(key, time);
} catch(e) {
return this.body={isError: true, message:"签名错误"};
}
if (data.action != 'connectTest') return this.body={isError: true, message:"配置错误"};
var db = findDB(data.db)
if (!db) return this.body={isError: true, message: "数据库不存在:" + data.db};
this.body = {
data: yield DataCenter.testConnection({
type: 'database',
config: {
storage: db
}
})
}
})
app.use(router.routes());
/**
* Page not found handler
*/
app.use(function *() {
this.status = 404;
this.body = 'Not Found';
});
app.listen(config.port, config.bindingHost);
graceful({
server: app,
error: function (err, throwErrorCount) {
if (err.message) {
err.message += ' (uncaughtException throw ' + throwErrorCount + ' times on pid:' + process.pid + ')';
}
console.error(err);
}
});
function decrypt(data, time) {
if (new Date().getTime()/1000 > time + config.expired) {
return false;
}
var key = time + config.key.substr(time.toString().length)
var cipherChunks, decipher;
cipherChunks = [];
data = data.replace(/ /g, '+');
decipher = crypto.createDecipheriv('aes-256-cbc', new Buffer(key, 'binary'), new Buffer(config.secret, 'binary'));
decipher.setAutoPadding(true);
cipherChunks.push(decipher.update(data, 'base64', 'utf8'));
try {
cipherChunks.push(decipher.final('utf8'));
} catch (e) {
return false;
}
data = cipherChunks.join('');
try {
return JSON.parse(data);
} catch(e) {
return false;
}
}
function findDB(id) {
return config.databases.filter(function(db){
return db.id == id;
}).shift();
}