client happenings

This commit is contained in:
malloc 2018-03-28 16:58:23 -05:00
parent 13aa6b184f
commit 8c3469c4c1
12 changed files with 68 additions and 30 deletions

View file

@ -1,2 +0,0 @@
<?php
echo $_SERVER["REMOTE_ADDR"];

View file

@ -34,7 +34,10 @@ var Entrypoint = (function () {
};
Entrypoint.start = function () {
var _this = this;
Key.init();
Key.init(function () {
_this.initStatus.keyInit = true;
_this.initCheck();
});
FileCache.initCache(
// SUCCESS
function () {
@ -47,10 +50,12 @@ var Entrypoint = (function () {
});
};
Entrypoint.ready = function () {
alert("ready");
};
return Entrypoint;
}());
Entrypoint.initStatus = {
keyInit: false,
fileCache: false
};
var FileCache = (function () {
@ -71,7 +76,8 @@ var FileCache = (function () {
db.createObjectStore("metadata", { keyPath: "name", autoIncrement: false });
};
request.onerror = function (event) {
error("Could not upgrade the client database to the most recent version.");
error("Could not upgrade the client database "
+ "to the most recent version.");
};
request.onsuccess = function (event) {
_this.dbHandle = request.result;
@ -204,7 +210,6 @@ var Connection = (function () {
Connection.prototype.open = function () {
if (this._isOpen)
return;
// FLAG replace hard coded url with one loaded from a config file
this.sock = new WebSocket(this.address);
this.sock.binaryType = "arraybuffer";
this.sock.onopen = this.onOpen;
@ -224,7 +229,8 @@ var Connection = (function () {
var raw = new Uint8Array(event.data);
var msg;
try {
msg = !this.useCipher || !Cipher.ready ? Packet.fromBytes(raw)
msg = (!this.useCipher || !Cipher.ready)
? Packet.fromBytes(raw)
: Packet.fromBytes(Cipher.parse(raw));
}
catch (e) {
@ -281,8 +287,11 @@ var Key = (function () {
enumerable: true,
configurable: true
});
Key.init = function () {
Key.secret = Random.generatePrime(512);
Key.init = function (onsuccess) {
setTimeout(function () {
Key.secret = Random.generatePrime(512);
onsuccess();
}, 0);
};
Key.generateResponsePacket = function (request) {
var generator = new bigInt(request[0].toString(), 16);
@ -458,7 +467,7 @@ var Packet = (function () {
};
return Packet;
}());
Packet.magicNumber = new Uint8Array([0xF0, 0x9F, 0xA6, 0x91]);
Packet.magicNumber = new Uint8Array([0xB0, 0x0B]);
// ** STRING EXTENSIONS ** \\
String.prototype.replaceAll = function (needle, replace, ignoreCase) {
if (ignoreCase === void 0) { ignoreCase = false; }

View file

@ -20,6 +20,7 @@ class SockContext {
class Entrypoint {
private static initStatus = {
keyInit: false,
fileCache: false
}
@ -33,7 +34,10 @@ class Entrypoint {
}
public static start(): void {
Key.init();
Key.init(() => {
this.initStatus.keyInit = true;
this.initCheck();
});
FileCache.initCache(
// SUCCESS
@ -50,6 +54,6 @@ class Entrypoint {
}
private static ready(): void {
alert("ready");
}
}

View file

@ -1,7 +1,9 @@
class FileCache {
private static dbHandle: IDBDatabase = null;
public static initCache(success: ()=>void, error: (error: string)=>void): void {
public static initCache(success: ()=>void,
error: (error: string)=>void): void
{
var request = window.indexedDB.open("fileCache", 3);
request.onupgradeneeded = (event: any) => {
@ -16,12 +18,15 @@ class FileCache {
if(db.objectStoreNames.contains("hashes"))
db.deleteObjectStore("hashes");
db.createObjectStore("files", {keyPath: "name", autoIncrement: false});
db.createObjectStore("metadata", {keyPath: "name", autoIncrement: false});
db.createObjectStore("files",
{keyPath: "name", autoIncrement: false});
db.createObjectStore("metadata",
{keyPath: "name", autoIncrement: false});
};
request.onerror = (event: any) => {
error("Could not upgrade the client database to the most recent version.");
error("Could not upgrade the client database "
+"to the most recent version.");
};
request.onsuccess = (event: any) => {
@ -30,7 +35,9 @@ class FileCache {
};
}
public static getMeta(fileName: string, success: (meta: FileMeta)=>void, error: (error: string)=>void): void {
public static getMeta(fileName: string, success: (meta: FileMeta)=>void,
error: (error: string)=>void): void
{
var query = this.dbHandle.transaction("metadata");
var store = query.objectStore("metadata");
var request = store.get(fileName);
@ -50,7 +57,10 @@ class FileCache {
store.put(meta);
}
public static getFile(fileName: string, success: (name: string, data: Uint8Array)=>void, error: (error: string)=>void): void {
public static getFile(fileName: string,
success: (name: string, data: Uint8Array)=>void,
error: (error: string)=>void): void
{
var query = this.dbHandle.transaction("files");
var store = query.objectStore("files");
var request = store.get(fileName);

View file

@ -39,7 +39,6 @@ class Connection {
if(this._isOpen)
return;
// FLAG replace hard coded url with one loaded from a config file
this.sock = new WebSocket(this.address);
this.sock.binaryType = "arraybuffer";
@ -63,7 +62,7 @@ class Connection {
private onMessage(event: any): void {
var raw = new Uint8Array(event.data);
var msg: Packet;
try {
try {
msg = (!this.useCipher || !Cipher.ready)
? Packet.fromBytes(raw)
: Packet.fromBytes(Cipher.parse(raw));

View file

@ -9,8 +9,11 @@ class Key {
return !Key._privateKey.eq(new bigInt(0));
}
public static init(): void {
Key.secret = Random.generatePrime(512);
public static init(onsuccess: ()=>void): void {
setTimeout(() => {
Key.secret = Random.generatePrime(512);
onsuccess();
}, 0);
}
public static generateResponsePacket(request: Packet): Packet {

View file

@ -10,7 +10,7 @@ const enum kSlaveId {
}
class Packet {
private static magicNumber: Uint8Array = new Uint8Array([0xF0, 0x9F, 0xA6, 0x91]);
private static magicNumber: Uint8Array = new Uint8Array([0xB0, 0x0B]);
private _id: number;
public get id(): number {

View file

@ -49,7 +49,8 @@ body {
top:0;
bottom:0;
margin: auto;
background: url('img/sock.png') center no-repeat;
/* NOTE uncomment this when not at work */
/* background: url('img/sock.png') center no-repeat; */
background-size: contain;
/* FLEX BOX - USED TO CENTER CHILD ELEMENTS DYNAMICALLY*/

Binary file not shown.

View file

@ -1,18 +1,19 @@
#include "keyex.hpp"
sosc::BigUInt sosc::cgc::KeyExchange::secret;
sosc::cgc::KeyExchange::KeyExchange() {
if(KeyExchange::secret.IsZero())
KeyExchange::secret =
BigUInt::GenerateRandomPrime(this->key_size_bytes);
KeyExchange::secret = FastRandomPrime();
this->modulus = BigUInt::GenerateRandomPrime(this->key_size_bytes);
this->modulus = FastRandomPrime();
}
sosc::Packet sosc::cgc::KeyExchange::GenerateRequest() const {
return Packet(1, {
this->generator.ToString(),
this->modulus.ToString(),
BigUInt::ModPow(this->generator, this->secret, this->modulus);
BigUInt::ModPow(this->generator, this->secret, this->modulus)
});
}
@ -50,3 +51,12 @@ bool sosc::cgc::KeyExchange::ParseResponse(const Packet& response) {
return true;
}
sosc::BigUInt sosc::cgc::KeyExchange::FastRandomPrime() {
BigUInt prime;
for(int i = 0; i < this->key_size_bytes; i += 16)
prime += BigUInt::GenerateRandomPrime(16) << (i * 8);
return prime;
}

View file

@ -25,6 +25,8 @@ public:
const int key_size = 512;
const int key_size_bytes = key_size / 8;
private:
BigUInt FastRandomPrime();
const BigUInt generator = BigUInt(2u);
static BigUInt secret;
BigUInt modulus;

View file

@ -81,9 +81,11 @@ int main(int argc, char **argv) {
//<< c.ToString() << std::endl << std::endl
//<< sosc::BigUInt::ModPow(a, b, c).ToString() << std::endl;
<< sosc::BigUInt::GenerateRandomPrime(32).ToString() << std::endl
<< sosc::BigUInt::GenerateRandomPrime(32).ToString() << std::endl
<< sosc::BigUInt::GenerateRandomPrime(32).ToString() << std::endl;
<< sosc::BigUInt::GenerateRandomPrime(16).ToString() << std::endl
<< sosc::BigUInt::GenerateRandomPrime(16).ToString() << std::endl
<< sosc::BigUInt::GenerateRandomPrime(16).ToString() << std::endl
<< sosc::BigUInt::GenerateRandomPrime(16).ToString() << std::endl;
std::cout << (time(NULL) - start) << std::endl;