diff --git a/client/src/Entrypoint.ts b/client/src/Entrypoint.ts index aee6a1c..7d92147 100644 --- a/client/src/Entrypoint.ts +++ b/client/src/Entrypoint.ts @@ -1,3 +1,23 @@ +class SockContext { + private static didInit: boolean = false; + + private static _masterSock: Connection; + public static get masterSock(): Connection { + return this._masterSock; + } + + private static _slaveSock: Connection; + public static get slaveSock(): Connection { + return this._slaveSock; + } + + public static init(): void { + if(this.didInit) return; + + + } +} + class Entrypoint { private static initStatus = { fileCache: false @@ -5,8 +25,8 @@ class Entrypoint { private static initCheck(): void { var done = true; - for(var i in Entrypoint.initStatus) - done = done && Entrypoint.initStatus[i]; + for(var i in this.initStatus) + done = done && this.initStatus[i]; if(done) Entrypoint.ready(); @@ -18,7 +38,7 @@ class Entrypoint { FileCache.initCache( // SUCCESS () => { - Entrypoint.initStatus.fileCache = true; + this.initStatus.fileCache = true; this.initCheck(); }, diff --git a/client/src/FileCache.ts b/client/src/FileCache.ts index 6543138..0382740 100644 --- a/client/src/FileCache.ts +++ b/client/src/FileCache.ts @@ -25,13 +25,13 @@ class FileCache { }; request.onsuccess = (event: any) => { - FileCache.dbHandle = request.result; + this.dbHandle = request.result; success(); }; } public static getMeta(fileName: string, success: (meta: FileMeta)=>void, error: (error: string)=>void): void { - var query = FileCache.dbHandle.transaction("metadata"); + var query = this.dbHandle.transaction("metadata"); var store = query.objectStore("metadata"); var request = store.get(fileName); @@ -45,13 +45,13 @@ class FileCache { } public static setMeta(meta: FileMeta) { - var query = FileCache.dbHandle.transaction("metadata", "readwrite"); + var query = this.dbHandle.transaction("metadata", "readwrite"); var store = query.objectStore("metadata"); store.put(meta); } public static getFile(fileName: string, success: (name: string, data: Uint8Array)=>void, error: (error: string)=>void): void { - var query = FileCache.dbHandle.transaction("files"); + var query = this.dbHandle.transaction("files"); var store = query.objectStore("files"); var request = store.get(fileName); @@ -65,13 +65,17 @@ class FileCache { } public static setFile(fileName: string, data: Uint8Array) { - var query = FileCache.dbHandle.transaction("files", "readwrite"); + var query = this.dbHandle.transaction("files", "readwrite"); var store = query.objectStore("files"); store.put({name: fileName, data: data}); } public static deleteFile(fileName: string) { - + var query = this.dbHandle.transaction("files", "readwrite"); + var store = query.objectStore("files"); + store.delete(fileName); + store = query.objectStore("metadata"); + store.delete(fileName); } } diff --git a/client/src/MasterProtocol.ts b/client/src/MasterProtocol.ts new file mode 100644 index 0000000..9db44e2 --- /dev/null +++ b/client/src/MasterProtocol.ts @@ -0,0 +1,20 @@ +class MasterProtocol { + public static get packetHandlers(): PacketHandle[] { + return [ + { id: 1, event: this.keyExchange } + ]; + } + + private static keyExchange(data: Packet, conn: Connection): void { + var response = Key.generateResponsePacket(data); + if(Key.succeeded) { + Cipher.init(Key.privateKey); + conn.send(response); + } else + CriticalStop.redirect("Could not establish an encrypted connection with the server."); + } + + public static loginAttempt(username: string, password: string): void { + + } +} \ No newline at end of file diff --git a/client/src/SlaveProtocol.ts b/client/src/SlaveProtocol.ts new file mode 100644 index 0000000..8b19cf1 --- /dev/null +++ b/client/src/SlaveProtocol.ts @@ -0,0 +1,11 @@ +class SlaveProtocol { + public static get packetHandlers(): PacketHandle[] { + return [ + { id: 1, event: this.userLoginResponse } + ]; + } + + private static userLoginResponse(data: Packet, conn: Connection): void { + console.log("mario has logged in"); + } +} \ No newline at end of file diff --git a/client/src/sock/Connection.ts b/client/src/sock/Connection.ts index 126e7d5..b303ec9 100644 --- a/client/src/sock/Connection.ts +++ b/client/src/sock/Connection.ts @@ -1,23 +1,50 @@ +type ConnEvent = (conn: Connection) => void; +type ConnErrorEvent = (event: any, conn: Connection) => void; +type PacketEvent = (data: Packet, conn: Connection) => void; +type PacketHandle = { id: number, event: PacketEvent }; + class Connection { private sock: WebSocket = null; private _isOpen: boolean = false; - private onOpenFunc: () => void = null; - private onCloseFunc: () => void = null; + + private address: string; + private useCipher: boolean; + private handles: PacketEvent[] = []; + + private onOpenFunc: ConnEvent = null; + private onCloseFunc: ConnEvent = null; + private onErrorFunc: ConnErrorEvent = null; + public get isOpen(): boolean { return this._isOpen; } - public open(onOpen: () => void = null): void { + public constructor(address: string, handles: PacketHandle[], useCipher: boolean = false, + onOpen: ConnEvent = null, onClose: ConnEvent = null, onError: ConnErrorEvent = null) + { + this.address = address; + this.useCipher = useCipher; + + this.onOpenFunc = onOpen; + this.onCloseFunc = onClose; + this.onErrorFunc = onError; + + handles.forEach(element => { + this.handles[element.id] = element.event; + }); + } + + public open(): void { if(this._isOpen) return; // FLAG replace hard coded url with one loaded from a config file - this.sock = new WebSocket("ws://localhost:6770"); + this.sock = new WebSocket(this.address); this.sock.binaryType = "arraybuffer"; - this.onOpenFunc = onOpen; this.sock.onopen = this.onOpen; this.sock.onmessage = this.onMessage; + this.sock.onerror = this.onError; this.sock.onclose = this.onClose; } @@ -29,37 +56,41 @@ class Connection { this._isOpen = true; if(this.onOpenFunc) - this.onOpenFunc(); + this.onOpenFunc(this); } private onMessage(event: any): void { var raw = new Uint8Array(event.data); var msg: Packet; try { - msg = !Cipher.ready ? Packet.fromBytes(raw) - : Packet.fromBytes(Cipher.parse(raw)); + msg = !this.useCipher || !Cipher.ready ? Packet.fromBytes(raw) + : Packet.fromBytes(Cipher.parse(raw)); } catch(e) { close(); return; } console.log(msg); + if(msg.id < this.handles.length && this.handles[msg.id] !== undefined) + this.handles[msg.id](msg, this); + + /* switch(msg.id) { case kMasterId.KeyExchange: - var response = Key.generateResponsePacket(msg); - if(Key.succeeded) { - Cipher.init(Key.privateKey); - this.send(response); - } else - CriticalStop.redirect("Could not establish an encrypted connection with the server."); break; case kMasterId.LoginAttempt: - + break; case kMasterId.RegistrationAttempt: break; } + */ + } + + private onError(event: any): void { + if(this.onErrorFunc) + this.onErrorFunc(event, this); } private onClose(event: any): void { @@ -67,14 +98,13 @@ class Connection { Cipher.close(); if(this.onCloseFunc) - this.onCloseFunc(); + this.onCloseFunc(this); } - public close(onClose: () => void = null): void { + public close(): void { if(!this._isOpen) return; - this.onCloseFunc = onClose; this.sock.close(); } } \ No newline at end of file diff --git a/client/src/util/Extensions.ts b/client/src/util/Extensions.ts index 6510c5f..eb1c663 100644 --- a/client/src/util/Extensions.ts +++ b/client/src/util/Extensions.ts @@ -209,7 +209,7 @@ Uint8Array.prototype.toHexString = function(): string { for(var i = 0; i < this.byteLength; ++i) { var byte = this[i].toString(16); if(byte.length < 2) - byte = "0"+ byte; + byte = "0"+ byte; ret += byte +" "; } diff --git a/server/Socks/MasterIntraClient.cs b/server/Socks/MasterIntraClient.cs index c980d42..2f37805 100644 --- a/server/Socks/MasterIntraClient.cs +++ b/server/Socks/MasterIntraClient.cs @@ -36,7 +36,7 @@ namespace SockScape { // TODO figure out what this has to do with ICMP (in server too) uint IOC_IN = 0x80000000, IOC_VENDOR = 0x18000000, - SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12; + SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 0xC; Sock.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] {0}, null); Key = new Key(); diff --git a/server/Socks/Protocols/Packet.cs b/server/Socks/Protocols/Packet.cs index 320bd99..08bec9b 100644 --- a/server/Socks/Protocols/Packet.cs +++ b/server/Socks/Protocols/Packet.cs @@ -202,7 +202,7 @@ namespace SockScape { if(Buffer.Count > MaxSize) Buffer = Buffer.Where(x => x.Key >= id - MaxSize) - .ToDictionary(x => x.Key, x => x.Value); + .ToDictionary(x => x.Key, x => x.Value); } public byte[] this[uint i]