85d2225e0c
* Fix loading within the CLI * Remove app * Remove promise handle * Add initial travis file * Add libxkbfile dependency * Add libxkbfile-dev * Add build script * Fix malformed bash statement * Remove yarn from script * Improve build script * Extract upx before usage * Only run upx if on linux * Ensure resource directory exists * Pack runnable binary * Export binary with platform * Improve build process * Install upx before running install script * Update typescript version before running nexe * Add os.release() function for multi-platform support * Update travis.yml to improve deployment * Add on CI * Update to v1.31.0 * Add libsecret * Update build target * Skip cleanup * Fix built-in extensions * Add basics for apps * Create custom DNS server * Fix forking within CLI. Fixes TS language features * Fix filename resolve * Fix default extensions path * Add custom dialog * Store workspace path * Remove outfiles * Cleanup * Always authed outside of CLI * Use location.host for client * Remove useless app interface * Remove debug file for building wordlist * Use chromes tcp host * Update patch * Build browser app before packaging * Replace all css containing file:// URLs, fix webviews * Fix save * Fix mkdir
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
//@ts-ignore
|
|
import { TcpHost, TcpServer, TcpConnection } from "@coder/app/common/src/app";
|
|
import { Event, Emitter } from "@coder/events/src";
|
|
|
|
export const tcpHost: TcpHost = {
|
|
listen(host: string, port: number): Promise<TcpServer> {
|
|
const socketApi: {
|
|
readonly tcpServer: {
|
|
create(props: {}, cb: (createInfo: { readonly socketId: number }) => void): void;
|
|
listen(socketId: number, address: string, port: number, callback: (result: number) => void): void;
|
|
disconnect(socketId: number, callback: () => void): void;
|
|
|
|
readonly onAccept: {
|
|
addListener(callback: (info: { readonly socketId: number; readonly clientSocketId: number }) => void): void;
|
|
};
|
|
};
|
|
readonly tcp: {
|
|
readonly onReceive: {
|
|
addListener(callback: (info: { readonly socketId: number; readonly data: ArrayBuffer; }) => void): void;
|
|
};
|
|
close(socketId: number, callback?: () => void): void;
|
|
send(socketId: number, data: ArrayBuffer, callback?: () => void): void;
|
|
setPaused(socketId: number, value: boolean): void;
|
|
};
|
|
// tslint:disable-next-line:no-any
|
|
} = (<any>chrome).sockets;
|
|
|
|
return new Promise((resolve, reject): void => {
|
|
socketApi.tcpServer.create({}, (createInfo) => {
|
|
const serverSocketId = createInfo.socketId;
|
|
socketApi.tcpServer.listen(serverSocketId, host, port, (result) => {
|
|
if (result < 0) {
|
|
return reject("Failed to listen: " + chrome.runtime.lastError);
|
|
}
|
|
|
|
const connectionEmitter = new Emitter<TcpConnection>();
|
|
|
|
socketApi.tcpServer.onAccept.addListener((info) => {
|
|
if (info.socketId !== serverSocketId) {
|
|
return;
|
|
}
|
|
|
|
const dataEmitter = new Emitter<ArrayBuffer>();
|
|
|
|
socketApi.tcp.onReceive.addListener((recvInfo) => {
|
|
if (recvInfo.socketId !== info.clientSocketId) {
|
|
return;
|
|
}
|
|
|
|
dataEmitter.emit(recvInfo.data);
|
|
});
|
|
|
|
socketApi.tcp.setPaused(info.clientSocketId, false);
|
|
|
|
connectionEmitter.emit({
|
|
send: (data): Promise<void> => {
|
|
return new Promise<void>((res): void => {
|
|
socketApi.tcp.send(info.clientSocketId, data, () => {
|
|
res();
|
|
});
|
|
});
|
|
},
|
|
close: (): Promise<void> => {
|
|
return new Promise((res): void => {
|
|
socketApi.tcp.close(info.clientSocketId, () => {
|
|
res();
|
|
});
|
|
});
|
|
},
|
|
get onData(): Event<ArrayBuffer> {
|
|
return dataEmitter.event;
|
|
},
|
|
});
|
|
});
|
|
|
|
resolve({
|
|
get onConnection(): Event<TcpConnection> {
|
|
return connectionEmitter.event;
|
|
},
|
|
close: (): Promise<void> => {
|
|
return new Promise((res): void => {
|
|
socketApi.tcpServer.disconnect(serverSocketId, () => {
|
|
res();
|
|
});
|
|
});
|
|
},
|
|
});
|
|
});
|
|
});
|
|
});
|
|
},
|
|
};
|