Got encrypted, stable communication between the server and the client
This commit is contained in:
parent
04893b2ac5
commit
727643aa79
|
@ -6,5 +6,7 @@ An open source chat client
|
|||
|
||||
# Required libraries
|
||||
- net
|
||||
- electron
|
||||
- fs
|
||||
- electron
|
||||
- node-rsa
|
||||
- buffer-split
|
||||
|
|
177
index.js
177
index.js
|
@ -1,31 +1,190 @@
|
|||
|
||||
// Get some libraries
|
||||
const net = require("net");
|
||||
const node_rsa = require("node-rsa");
|
||||
const bsplit = require("buffer-split");
|
||||
|
||||
// Load the settings
|
||||
var settings = require("./settings.json");
|
||||
|
||||
// Encryption varibles
|
||||
var encryption;
|
||||
var rsa_key;
|
||||
|
||||
// Create a client
|
||||
var client = new net.Socket();
|
||||
|
||||
function send_data(client, data)
|
||||
function string_decrypt(key, string)
|
||||
{
|
||||
// Send the data
|
||||
client.write(JSON.stringify(data));
|
||||
// Convert the string to a buffer
|
||||
var buff = new Buffer.from(string);
|
||||
|
||||
// Create some new data
|
||||
var decrypted = new Buffer.alloc(buff.length);
|
||||
|
||||
// Iterate over the string
|
||||
for(var i=0;i<string.length;i++)
|
||||
{
|
||||
// Convert the string item to a number
|
||||
var d = buff[i]-key.str[key.at.rx];
|
||||
while(d < 0) d += 256;
|
||||
decrypted[i] = d;
|
||||
|
||||
// Add 1 to the key counter
|
||||
key.at.rx += 1;
|
||||
|
||||
// Is the key counter out of range
|
||||
if(key.at.rx >= key.str.length)
|
||||
{
|
||||
// Set to zero
|
||||
key.at.rx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the encrypted data
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
function string_encrypt(key, string)
|
||||
{
|
||||
// Convert the string to a buffer
|
||||
var buff = new Buffer.from(string);
|
||||
|
||||
// Create some new data
|
||||
var encrypted = new Buffer.alloc(buff.length);
|
||||
|
||||
// Iterate over the string
|
||||
for(var i=0;i<string.length;i++)
|
||||
{
|
||||
// Convert the string item to a number
|
||||
var e = buff[i]+key.str[key.at.tx];
|
||||
while(e > 255) e -= 256;
|
||||
encrypted[i] = e;
|
||||
|
||||
// Add 1 to the key counter
|
||||
key.at.tx += 1;
|
||||
|
||||
// Is the key counter out of range
|
||||
if(key.at.tx >= key.str.length)
|
||||
{
|
||||
// Set to zero
|
||||
key.at.tx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the encrypted data
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
function make_encryption_key(string)
|
||||
{
|
||||
// Make a new key
|
||||
var key = new Object();
|
||||
|
||||
// Set the varibles
|
||||
key.str = string;
|
||||
key.at = new Object();
|
||||
key.at.rx = 0;
|
||||
key.at.tx = 0;
|
||||
|
||||
// Return the key
|
||||
return key;
|
||||
}
|
||||
|
||||
function socket_write(socket, encryption, data)
|
||||
{
|
||||
// Send the data encrypted with JSON
|
||||
socket.write(string_encrypt(encryption, JSON.stringify(data)+"\n"));
|
||||
}
|
||||
|
||||
function socket_ondata(socket, callback)
|
||||
{
|
||||
// Wait for a response
|
||||
socket.on('data', function(data)
|
||||
{
|
||||
// Decode the data
|
||||
data = JSON.parse(data);
|
||||
|
||||
// Call the callback
|
||||
callback(data);
|
||||
});
|
||||
}
|
||||
|
||||
// Connect to the server
|
||||
client.connect(22068, '127.0.0.1', function()
|
||||
{
|
||||
// Send some data to the server
|
||||
send_data(client, {
|
||||
status: "connected"
|
||||
});
|
||||
// Create an rsa key
|
||||
rsa_key = new node_rsa({b:settings.rsa_size});
|
||||
|
||||
// Send the public key to the server
|
||||
client.write(rsa_key.exportKey("public"));
|
||||
});
|
||||
|
||||
// Set new
|
||||
var sock_new = 2;
|
||||
|
||||
// Set a temporary encryption varible
|
||||
var raw_encryption_data = "";
|
||||
var raw_encryption_data_upto = 0;
|
||||
var raw_encryption_data_size = 0;
|
||||
|
||||
// Wait for data
|
||||
client.on('data', function(data)
|
||||
{
|
||||
document.getElementById('server-response').innerHTML = "Response: " + data;
|
||||
if(sock_new == 2)
|
||||
{
|
||||
console.log("Recieved key.");
|
||||
|
||||
// Set the size of the key
|
||||
raw_encryption_data_size = parseInt(data);
|
||||
|
||||
// Set sock new to 1
|
||||
sock_new = 1;
|
||||
}
|
||||
|
||||
// Is the socket recieving the key
|
||||
else if(sock_new == 1)
|
||||
{
|
||||
console.log(data);
|
||||
|
||||
// Decode the data
|
||||
raw_encryption_data += data;
|
||||
raw_encryption_data_upto += data.length;
|
||||
console.log(data.length);
|
||||
|
||||
// Is this the end of the stream
|
||||
if(raw_encryption_data_upto == raw_encryption_data_size)
|
||||
{
|
||||
// Set sock new to 0
|
||||
sock_new = 0;
|
||||
|
||||
// Set the key
|
||||
encryption = make_encryption_key(rsa_key.decrypt(raw_encryption_data));
|
||||
|
||||
console.log("Done.");
|
||||
|
||||
socket_write(client, encryption, {message: "hello, world!", number: 53});
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
// Decrypt the data
|
||||
data = string_decrypt(encryption, data);
|
||||
|
||||
// Split the data by newlines
|
||||
data = bsplit(data, Buffer.from("\n"));
|
||||
|
||||
// Iterate over them
|
||||
for(var i=0;i<data.length-1;i++)
|
||||
{
|
||||
console.log(JSON.parse(data[i]));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
client.on('close', function(data)
|
||||
{
|
||||
document.getElementById('server-response').innerHTML = "Connection closed";
|
||||
// Close the window
|
||||
//window.close();
|
||||
});
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- 0.6
|
||||
- 0.8
|
||||
- "0.10"
|
||||
- "0.11"
|
|
@ -0,0 +1,20 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Ryan Day
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,29 @@
|
|||
[![Build Status](https://secure.travis-ci.org/soldair/node-buffer-indexof.png)](http://travis-ci.org/soldair/node-buffer-indexof)
|
||||
|
||||
|
||||
buffer-indexof
|
||||
===================
|
||||
|
||||
find the index of a buffer in a buffer. should behave like String.indexOf etc.
|
||||
|
||||
```js
|
||||
|
||||
var bindexOf = require('buffer-indexof');
|
||||
|
||||
var newLineBuffer = new Buffer("\n");
|
||||
|
||||
var b = new Buffer("hi\nho\nsilver");
|
||||
|
||||
|
||||
bindexOf(b,newLineBuffer) === 2
|
||||
|
||||
// you can also start from index
|
||||
|
||||
bindexOf(b,newLineBuffer,3) === 5
|
||||
|
||||
// no match === -1
|
||||
|
||||
bindexOf(b,newLineBuffer,6) === -1
|
||||
|
||||
|
||||
```
|
|
@ -0,0 +1,19 @@
|
|||
module.exports = function bufferIndexOf(buf,search,offset){
|
||||
offset = offset||0
|
||||
|
||||
var m = 0;
|
||||
var s = -1;
|
||||
for(var i=offset;i<buf.length;++i){
|
||||
if(buf[i] == search[m]) {
|
||||
if(s == -1) s = i;
|
||||
++m;
|
||||
if(m == search.length) break;
|
||||
} else {
|
||||
s = -1;
|
||||
m = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (s > -1 && buf.length - s < search.length) return -1;
|
||||
return s;
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
{
|
||||
"_args": [
|
||||
[
|
||||
"buffer-indexof@~0.0.0",
|
||||
"/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-client/node_modules/buffer-split"
|
||||
]
|
||||
],
|
||||
"_from": "buffer-indexof@>=0.0.0 <0.1.0",
|
||||
"_id": "buffer-indexof@0.0.2",
|
||||
"_inCache": true,
|
||||
"_installable": true,
|
||||
"_location": "/buffer-indexof",
|
||||
"_npmUser": {
|
||||
"email": "soldair@gmail.com",
|
||||
"name": "soldair"
|
||||
},
|
||||
"_npmVersion": "1.4.20",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"name": "buffer-indexof",
|
||||
"raw": "buffer-indexof@~0.0.0",
|
||||
"rawSpec": "~0.0.0",
|
||||
"scope": null,
|
||||
"spec": ">=0.0.0 <0.1.0",
|
||||
"type": "range"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/buffer-split"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/buffer-indexof/-/buffer-indexof-0.0.2.tgz",
|
||||
"_shasum": "ed0f36b7ae166a66a7cd174c0467ae8dedf008f5",
|
||||
"_shrinkwrap": null,
|
||||
"_spec": "buffer-indexof@~0.0.0",
|
||||
"_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-client/node_modules/buffer-split",
|
||||
"author": {
|
||||
"name": "Ryan Day"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/soldair/node-buffer-indexof/issues"
|
||||
},
|
||||
"dependencies": {},
|
||||
"description": "find the index of a buffer in a buffe",
|
||||
"devDependencies": {
|
||||
"tape": "~1.1.0"
|
||||
},
|
||||
"directories": {},
|
||||
"dist": {
|
||||
"shasum": "ed0f36b7ae166a66a7cd174c0467ae8dedf008f5",
|
||||
"tarball": "https://registry.npmjs.org/buffer-indexof/-/buffer-indexof-0.0.2.tgz"
|
||||
},
|
||||
"gitHead": "277ab0cedaf14f6d6ae18571e9dd4d0dea1981a0",
|
||||
"homepage": "https://github.com/soldair/node-buffer-indexof",
|
||||
"main": "index.js",
|
||||
"maintainers": [
|
||||
{
|
||||
"name": "soldair",
|
||||
"email": "soldair@gmail.com"
|
||||
}
|
||||
],
|
||||
"name": "buffer-indexof",
|
||||
"optionalDependencies": {},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"repository": {
|
||||
"url": "git://github.com/soldair/node-buffer-indexof.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tape test/*.js"
|
||||
},
|
||||
"version": "0.0.2"
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
var test = require('tape');
|
||||
var bindexOf = require('../');
|
||||
|
||||
test("can haz working",function(t){
|
||||
|
||||
|
||||
var newLineBuffer = new Buffer("\n");
|
||||
|
||||
var b = new Buffer("hi\nho\nsilver");
|
||||
|
||||
t.equals(bindexOf(new Buffer('a'), new Buffer('abc')), -1, 'should not match')
|
||||
|
||||
t.equals(bindexOf(new Buffer('aaa'), new Buffer('aa'), 2), -1, 'should not match')
|
||||
|
||||
t.equals(bindexOf(b,newLineBuffer),2,'should find newlines');
|
||||
|
||||
// you can also start from index
|
||||
|
||||
t.equals(bindexOf(b,newLineBuffer,3),5,"should find newlines after offset");
|
||||
|
||||
// no match === -1
|
||||
|
||||
t.equals(bindexOf(b,newLineBuffer,6),-1,"should not find newlines where none are.");
|
||||
|
||||
|
||||
t.end();
|
||||
})
|
|
@ -0,0 +1,4 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- "0.8"
|
||||
- "0.10"
|
|
@ -0,0 +1,20 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Ryan Day
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,40 @@
|
|||
|
||||
[![Build Status](https://secure.travis-ci.org/soldair/node-buffer-split.png)](http://travis-ci.org/soldair/node-buffer-split)
|
||||
|
||||
buffer-split
|
||||
============
|
||||
|
||||
split a buffer by another buffer. think String.split()
|
||||
|
||||
```js
|
||||
|
||||
var bsplit = require('buffer-split')
|
||||
, b = new Buffer("this is a buffer i like to split")
|
||||
, delim = new Buffer('buffer')
|
||||
, result = bsplit(b,delim)
|
||||
;
|
||||
|
||||
result.length === 2
|
||||
|
||||
result[0].toString() === "this is a "
|
||||
|
||||
result[1].toString() === " i like to split"
|
||||
|
||||
|
||||
```
|
||||
|
||||
you may include the delimiter in the result by passing a thrthy value as the third arg. its more efficient if you need it.
|
||||
|
||||
```js
|
||||
var bsplit = require('buffer-split')
|
||||
, b = new Buffer("this is a buffer i like to split")
|
||||
, delim = new Buffer('buffer')
|
||||
, result = bsplit(b,delim,true)
|
||||
;
|
||||
|
||||
result[0].toString() === "this is a buffer"
|
||||
|
||||
result[1].toString() === " i like to split"
|
||||
|
||||
|
||||
```
|
|
@ -0,0 +1,22 @@
|
|||
var bufferIndexOf = require('buffer-indexof');
|
||||
|
||||
module.exports = function(buf,splitBuf,includeDelim){
|
||||
|
||||
var search = -1
|
||||
, lines = []
|
||||
, move = includeDelim?splitBuf.length:0
|
||||
;
|
||||
|
||||
while((search = bufferIndexOf(buf,splitBuf)) > -1){
|
||||
lines.push(buf.slice(0,search+move));
|
||||
buf = buf.slice(search+splitBuf.length,buf.length);
|
||||
}
|
||||
|
||||
lines.push(buf);
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
{
|
||||
"_args": [
|
||||
[
|
||||
"buffer-split",
|
||||
"/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-client"
|
||||
]
|
||||
],
|
||||
"_from": "buffer-split@latest",
|
||||
"_id": "buffer-split@1.0.0",
|
||||
"_inCache": true,
|
||||
"_installable": true,
|
||||
"_location": "/buffer-split",
|
||||
"_nodeVersion": "4.4.4",
|
||||
"_npmOperationalInternal": {
|
||||
"host": "packages-12-west.internal.npmjs.com",
|
||||
"tmp": "tmp/buffer-split-1.0.0.tgz_1465569032148_0.7547089036088437"
|
||||
},
|
||||
"_npmUser": {
|
||||
"email": "soldair@gmail.com",
|
||||
"name": "soldair"
|
||||
},
|
||||
"_npmVersion": "3.9.0",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"name": "buffer-split",
|
||||
"raw": "buffer-split",
|
||||
"rawSpec": "",
|
||||
"scope": null,
|
||||
"spec": "latest",
|
||||
"type": "tag"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#USER"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/buffer-split/-/buffer-split-1.0.0.tgz",
|
||||
"_shasum": "4427dbff53731b61d7a71aba47f503396613784a",
|
||||
"_shrinkwrap": null,
|
||||
"_spec": "buffer-split",
|
||||
"_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-client",
|
||||
"author": {
|
||||
"name": "Ryan Day"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/soldair/node-buffer-split/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"buffer-indexof": "~0.0.0"
|
||||
},
|
||||
"description": "split a buffer by another buffer. think String.split()",
|
||||
"devDependencies": {
|
||||
"tape": "~1.1.0"
|
||||
},
|
||||
"directories": {},
|
||||
"dist": {
|
||||
"shasum": "4427dbff53731b61d7a71aba47f503396613784a",
|
||||
"tarball": "https://registry.npmjs.org/buffer-split/-/buffer-split-1.0.0.tgz"
|
||||
},
|
||||
"gitHead": "6d86666a2f7e4dc1f650f254bb11ba90b2d82567",
|
||||
"homepage": "https://github.com/soldair/node-buffer-split#readme",
|
||||
"keywords": [
|
||||
"binary",
|
||||
"buffer",
|
||||
"chunks",
|
||||
"split"
|
||||
],
|
||||
"main": "index.js",
|
||||
"maintainers": [
|
||||
{
|
||||
"name": "soldair",
|
||||
"email": "soldair@gmail.com"
|
||||
}
|
||||
],
|
||||
"name": "buffer-split",
|
||||
"optionalDependencies": {},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"repository": {
|
||||
"url": "git://github.com/soldair/node-buffer-split.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "node test/*.js"
|
||||
},
|
||||
"version": "1.0.0"
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
var test = require('tape')
|
||||
, bsplit = require('../')
|
||||
;
|
||||
|
||||
test("can split",function(t){
|
||||
|
||||
var b = new Buffer("this is a buffer i like to split")
|
||||
, delim = new Buffer('buffer')
|
||||
, result = bsplit(b,delim)
|
||||
;
|
||||
|
||||
t.equals(result.length,2,'should have found chunks');
|
||||
|
||||
t.equals(result[0].toString(),"this is a ","first chunk should not include delim");
|
||||
|
||||
t.equals(result[1].toString()," i like to split","should have all results");
|
||||
|
||||
|
||||
result = bsplit(b,delim,true);
|
||||
|
||||
t.equals(result[0].toString(),"this is a buffer",'should include delim')
|
||||
|
||||
result = bsplit(new Buffer('foo,'),new Buffer(','));
|
||||
|
||||
t.equals(result.length,2,"should have all results");
|
||||
|
||||
result = bsplit(new Buffer('foo'),new Buffer(','));
|
||||
|
||||
t.equals(result.length,1,"should have all results");
|
||||
|
||||
t.end();
|
||||
|
||||
})
|
|
@ -32,7 +32,8 @@
|
|||
"type": "tag"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#USER"
|
||||
"#USER",
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/electron/-/electron-4.1.1.tgz",
|
||||
"_shasum": "dc9343ba829cabbf01364761e72796f766704184",
|
||||
|
|
|
@ -26,7 +26,8 @@
|
|||
"type": "tag"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#USER"
|
||||
"#USER",
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
|
||||
"_shasum": "8a7bd37186b6dddf3813f23858b57ecaaf5e41d4",
|
||||
|
|
|
@ -25,7 +25,8 @@
|
|||
"type": "tag"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#USER"
|
||||
"#USER",
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/net/-/net-1.0.2.tgz",
|
||||
"_shasum": "d1757ec9a7fb2371d83cf4755ce3e27e10829388",
|
||||
|
|
|
@ -31,7 +31,8 @@
|
|||
"type": "tag"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#USER"
|
||||
"#USER",
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/node-rsa/-/node-rsa-1.0.5.tgz",
|
||||
"_shasum": "854dc1b275729d69bc25883f83ca80705db9262e",
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"rsa_size": 512
|
||||
}
|
Loading…
Reference in New Issue