-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
73 lines (61 loc) · 2.04 KB
/
example.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
var minimaLT = require('./index.js')
var client_key = minimaLT.generate_clientkey()
var server_cert = minimaLT.generate_servercert('a.localhost')
var server = minimaLT.listen('127.0.0.1', 21398, server_cert, function(con, servicename, auth, cb){
if (servicename == 'my_thing' && memcmp(client_key.public, auth)) {
cb(null, {
ping: function(x) {
console.log('OMG PING:', x)
con.call('pong', x)
}
})
}
})
//var connection = client.connectECert(server.ephemeral_certificate, "my_thing", null, {
// pong: function(x) {
// console.log("OMG PONG:", x)
// }
//})
//connection.call('ping', 42)
var known_certs = []
var name_cert = minimaLT.generate_servercert('name.localhost')
var nameservice = minimaLT.nameservice('127.0.0.1', 21397, name_cert, function(cert, eCert, cb) {
console.log('got certificate from client for', cert.hostname)
known_certs.push([cert, eCert])
cb(null)
})
known_certs.push([name_cert.cert, nameservice.ephemeral_certificate])
//known_certs.push([server_cert.cert, server.ephemeral_certificate])
var domain_cert = minimaLT.generate_servercert('domain.localhost')
var domainservice = minimaLT.domainservice('127.0.0.1', 21396, domain_cert, function(ident, cb) {
console.log("got domain service request for", ident.hostname)
known_certs.some(function(certpair) {
if (ident.matches(certpair[0])) {
cb(null, certpair[0].toBuffer(), certpair[1].toBuffer())
}
})
// if (ident.matches(name_cert.cert)) {
})
server.setDomainService('127.0.0.1', 21396, domain_cert.cert)
server.advertise(name_cert.cert.toIdentity(), function() {
var client = minimaLT.client('127.0.0.1', 21396, domain_cert.cert)
client.connect(server_cert.cert.toIdentity(), "my_thing", client_key, function(connection) {
connection.call('ping', 42)
var x = 0
setInterval(function() {
connection.call('ping', x++)
}, 3000)
return {
pong: function(x) {
console.log("OMG PONG:", x)
}
}
})
})
function memcmp(a, b) {
if (a.length != b.length) return false
for (var i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false
}
return true
}