-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode.js
More file actions
147 lines (123 loc) · 3.6 KB
/
node.js
File metadata and controls
147 lines (123 loc) · 3.6 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
var SecureWebSocket = require('../index.js');
var SecureWebSocketServer = SecureWebSocket.Server;
var Uint8ToBuffer = SecureWebSocket.Uint8ToBuffer;
var nacl = require('tweetnacl/nacl-fast');
var msgpack = require('msgpack-js');
var skp = nacl.box.keyPair();
var wss = new SecureWebSocketServer(
{port: 6668, host: 'localhost', path: '/wspp'},
{
myPublicKey: skp.publicKey,
mySecretKey: skp.secretKey
});
wss.on('connection', function(ws){
ws.on('message', function(message, flags){
///console.log('srv msg:'+JSON.stringify(message));
if (flags.binary) {
console.log('server message:'+msgpack.decode(message));
ws.send(message);
} else
console.error('Not support String message');
});
});
var ckp = nacl.box.keyPair();
var ws = new SecureWebSocket(
'ws://localhost:6668/wspp',
{
myPublicKey: ckp.publicKey,
mySecretKey: ckp.secretKey
});
ws.on('open', function(){
console.log('secure ws connected');
ws.on('message', function(message, flags){
///console.log('cln msg:'+JSON.stringify(message));
if (flags.binary) {
console.log('client message:'+msgpack.decode(message));
} else {
console.log('Not support String:'+JSON.stringify(message))
}
});
setInterval(function(){
ws.send(msgpack.encode('Hello,Am tom@'+Date.now()));
}, 2000);
});
ws.on('warn', function(warn){
console.log('Warning: '+JSON.stringify(warn));
});
ws.on('error', function(err){
console.log('Error: '+JSON.stringify(err));
});
// V2 with NaclCert
var naclcert = require('nacl-cert');
var rootCA = naclcert.generateCA({name: '51dese.com', tte: new Date('3030-01-01').getTime()});
var srvkp = nacl.box.keyPair();
var srvReqDesc = {
version: '1.0',
type: 'ca',
tte: new Date('2026-01-01').getTime(),
publickey: naclcert.Uint8ToArray(srvkp.publicKey),
names: ['localhost', '51dese.com', 'ruyier.com', 'localhost'],
ips: ['127.0.0.1']
};
var srvcert = naclcert.generate(srvReqDesc, rootCA.secretkey, rootCA.cert);
var clnkp = nacl.box.keyPair();
var clnReqDesc = {
version: '1.0',
type: 'ca',
tte: new Date('2026-01-01').getTime(),
publickey: naclcert.Uint8ToArray(clnkp.publicKey),
names: ['localhost'],
ips: ['127.0.0.1']
};
var clncert = naclcert.generate(clnReqDesc, rootCA.secretkey, rootCA.cert);
var cwss = new SecureWebSocketServer(
{port: 6688, host: 'localhost', path: '/wspp'},
{
version: 2,
cert: srvcert,
ca: rootCA.cert,
requireCert: true,
myPublicKey: srvkp.publicKey,
mySecretKey: srvkp.secretKey,
});
cwss.on('connection', function(ws){
ws.on('message', function(message, flags){
///console.log('srv msg:'+JSON.stringify(message));
if (flags.binary) {
console.log('V2 server message:'+msgpack.decode(message));
ws.send(message);
} else
console.error('V2 Not support String message');
});
});
var cws = new SecureWebSocket(
'ws://localhost:6688/wspp',
{
naclinfo: {
version: 2,
cert: clncert,
ca: rootCA.cert,
myPublicKey: clnkp.publicKey,
mySecretKey: clnkp.secretKey
}
});
cws.on('open', function(){
console.log('V2 secure ws connected');
cws.on('message', function(message, flags){
///console.log('cln msg:'+JSON.stringify(message));
if (flags.binary) {
console.log('V2 client message:'+msgpack.decode(message));
} else {
console.log('V2 Not support String:'+JSON.stringify(message))
}
});
setInterval(function(){
ws.send(msgpack.encode('V2 Hello,Am tom@'+Date.now()));
}, 2000);
});
cws.on('warn', function(warn){
console.log('Warning: '+JSON.stringify(warn));
});
cws.on('error', function(err){
console.log('Error: '+JSON.stringify(err));
});