forked from sggdv/wechat-token-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (52 loc) · 1.2 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
var express = require('express');
var path = require('path');
var morgan = require('morgan');
var shipper = require('./shipper');
var schedule = require('./schedule');
var access = require('./conf/access');
var app = express();
app.use(morgan('dev'));
app.use(express.static(path.join(__dirname, 'public')));
// 安全机制
app.use(function(req, res, next) {
var hostname = access.hostname;
var valid = false;
if (hostname) {
for (var i = 0; i < hostname.length; i++) {
if (hostname[i] == req.hostname) {
valid = true;
break;
}
};
}
if(!valid) {
var ip = access.ip;
if (ip) {
for (var i = 0; i < ip.length; i++) {
if(ip[i] == req.ip) {
valid = true;
break;
}
};
}
}
if (valid) {
next();
} else {
res.send("permission deny !");
}
});
app.get('/', function(req, res) {
shipper.get(function(err, data) {
res.json({ access_token: data });
});
});
app.get('/ticket', function(req, res) {
shipper.ticket(function(err, data) {
res.json({ ticket: data});
});
});
schedule.start();
var server = app.listen(3000, function() {
console.log('listen on port %d', server.address().port);
});