-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpairings.js
148 lines (133 loc) · 5.22 KB
/
pairings.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
var http = require('http')
, https = require('https')
, speakeasy = require('speakeasy')
, params = require('./steward').params
, url = require('url')
;
http.createServer(function(request, response) {
var content= '';
request.setEncoding('utf8');
request.on('data', function(chunk) {
content += chunk.toString();
}).on('close', function() {
console.log('http error: premature close');
}).on('end', function() {
if (request.method !== 'GET') {
response.writeHead(405, { Allow: 'GET, POST' });
return response.end();
}
// at present, don't care about the pathname...
roundtrip(function(err, code, data) {
if (!!err) {
response.writeHead(500);
return response.end(err.message);
}
response.writeHead(code);
return response.end(data);
});
});
}).listen(8894, '127.0.0.1', function() {
console.log('listening on http://127.0.0.1:8894');
});
var roundtrip = function(callback) {
var options, u;
u = url.parse(params.issuer);
options = { host : params.server.hostname
, port : params.server.port
, method : 'GET'
, path : '/pairings/' + params.labels[0]
, headers : { authorization : 'TOTP '
+ 'username="' + params.uuid[0] + '", '
+ 'response="' + speakeasy.totp({ key : params.base32
, length : 6
, encoding : 'base32'
, step : params.step }) + '"'
, host : u.hostname + ':' + params.server.port
}
, agent : false
, ca : [ new Buffer(params.server.ca) ]
};
https.request(options, function(response) {
var content = '';
response.setEncoding('utf8');
response.on('data', function(chunk) {
content += chunk.toString();
}).on('end', function() {
var data, entry, i, message;
try {
if (response.statusCode !== 200) throw new Error();
message = JSON.parse(content);
message.reverse();
} catch(ex) { return callback(null, response.statusCode, content); }
data = '<table><tr><td style="text-align: right;">when </td>'
+ '<td style="text-align: center;">responder</td>'
+ '<td style="text-align: center;">initiate</td>'
+ '<td>pathname</td></tr>';
for (i = 0; i < message.length; i++) {
entry = message[i];
data += '<tr><td style="text-align: right;">' + time_ago(entry.timestamp, true)
+ '  </td><td>' + entry.responder
+ ' </td><td>' + entry.initiator
+ ' </td><td>' + entry.pathname
+ '</td></tr>';
}
data += '</table>';
callback(null, response.statusCode, data);
}).on('close', function() {
callback(new Error('premature eof'));
});
}).on('error', function(err) {
callback(err);
}).end();
};
// http://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site
var time_ago = function(time, agoP) {
switch (typeof time) {
case 'number':
break;
case 'string':
time = +new Date(time);
break;
case 'object':
if (time.constructor === Date) time = time.getTime();
break;
default:
time = +new Date();
break;
}
var time_formats = [
[ 60, 's' , 1], // 60
[ 120, '1m', '1m from now'], // 60*2
[ 3600, 'm', 60], // 60*60, 60
[ 7200, '1h', '1h from now'], // 60*60*2
[ 86400, 'h', 3600], // 60*60*24, 60*60
[ 172800, 'yesterday', 'tomorrow'], // 60*60*24*2
[ 604800, 'd', 86400], // 60*60*24*7, 60*60*24
[ 1209600, 'last week', 'next week'], // 60*60*24*7*4*2
[ 2419200, 'w', 604800], // 60*60*24*7*4, 60*60*24*7
[ 4838400, 'last month', 'next month'], // 60*60*24*7*4*2
[ 29030400, 'months', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4
[ 58060800, 'last year', 'next year'], // 60*60*24*7*4*12*2
[ 2903040000, 'years', 29030400], // 60*60*24*7*4*12*100, 60*60*24*7*4*12
[ 5806080000, 'last century', 'next century'], // 60*60*24*7*4*12*100*2
[58060800000, 'centuries', 2903040000] // 60*60*24*7*4*12*100*20, 60*60*24*7*4*12*100
];
var seconds = (+new Date() - time) / 1000
, token = agoP ? 'ago' : ''
, list_choice = 1;
if (seconds < 0) {
seconds = Math.abs(seconds);
token = 'from now';
list_choice = 2;
} else if (seconds < 1) {
return 'now';
}
var i = 0
, format;
while (!!(format = time_formats[i++]))
if (seconds < format[0]) {
if (typeof format[2] == 'string') return format[list_choice];
return Math.floor(seconds / format[2]) + format[1] + ' ' + token;
}
return time;
};