transition 'pastefrom' example to use 'ziti.listen', 'ziti.write', 'ziti.dial'

This commit is contained in:
Curt Tudor 2024-04-09 16:11:09 -06:00
parent 0b2379d6d1
commit c8d48af5c4
No known key found for this signature in database
GPG Key ID: B3CD225AF4EC8E96
3 changed files with 51 additions and 41 deletions

View File

@ -14,19 +14,35 @@ program
var data = readlineSync.question('Input some text... ');
console.log("data is: ", data)
var enc = new TextEncoder();
var buf = enc.encode(data); //
let root = zrok.Load()
await zrok.init( root ).catch(( err: Error ) => { console.error(err); return process.exit(1) });
zrok.setLogLevel(0)
console.log("setting up zrok.CreateShare...")
let shr = await zrok.CreateShare(root, new zrok.ShareRequest(zrok.TCP_TUNNEL_BACKEND_MODE, zrok.PRIVATE_SHARE_MODE, "pastebin", ["private"]));
console.log("access your pastebin using 'pastefrom ", shr.Token)
let app = zrok.express( shr.Token );
app.get('/',function(_: Request,res: any){
res.write(data)
res.end()
});
app.listen(undefined, () => {
})
console.log(`access your pastebin using 'pastefrom ${shr.Token}'`)
zrok.listener(
shr.Token,
(data: any) => { // listenCallback
},
(data: any) => { // listenClientCallback
},
(data: any) => { // clientConnectCallback
// when we receive a client connection, then write the data to them
zrok.write(
data.client,
buf,
(data: any) => { // writeCallback
}
);
},
(data: any) => { // clientDataCallback
},
);
// Delete the private share upon CTRL-C
process.on('SIGINT', async () => {
@ -47,35 +63,26 @@ program
await zrok.init(root).catch((err: any) => {
console.log(err)
});
zrok.setLogLevel(0)
let acc = await zrok.CreateAccess(root, new zrok.AccessRequest(shrToken))
ziti.httpRequest(
var dec = new TextDecoder("utf-8");
zrok.dialer(
root,
shrToken,
undefined,
'GET',
'/',
[],
(data: any) => { // on_req_cb
console.log("in on_req_cb")
console.log("data is: ", data)
(data: any) => { // on_connect_cb
},
(data: any) => { // on_resp_cb
console.log("in on_resp_cb")
console.log("data is: ", data)
(data: any) => { // on_data_cb
console.log("data is: ", dec.decode(data));
process.exit(0);
},
async (data: any) => { // on_resp_data_cb
console.log("in on_resp_data_cb")
console.log("data is: ", data)
if (data.body) {
console.log('----------- pastefrom is: ', data.body.toString());
await zrok.DeleteAccess(root, acc)
process.exit(0);
}
}
);
process.on('SIGINT', async () => {
process.exit(15);
});
});
program.parse(process.argv)

View File

@ -61,16 +61,10 @@ export class Root {
public async Client(): Promise<Configuration> {
let apiEndpoint = this.ApiEndpoint()
//let headers = {
// "X-TOKEN": this.env.Token
//}
let conf = new Configuration({
basePath: apiEndpoint.endpoint + '/api/v1',
apiKey: this.env.Token,
//headers: headers
})
let conf = new Configuration({
basePath: apiEndpoint.endpoint + '/api/v1',
apiKey: this.env.Token,
})
let mapi = new MetadataApi(conf)
let ver: Promise<string> = mapi.version()

View File

@ -14,6 +14,15 @@ export function dialer(root: Root, shrToken: string, connectCallback: any, dataC
ziti.dial(shrToken, false, connectCallback, dataCallback)
}
export function listener(shrToken: string, listenCallback: any, listenClientCallback: any, clientConnectCallback: any, clientDataCallback: any): ziti.listener {
ziti.listen(shrToken, 0, listenCallback, listenClientCallback, clientConnectCallback, clientDataCallback)
}
export function write(conn: any, buf: any, writeCallback: any ){
ziti.write(conn, buf, writeCallback)
}
export function setLogLevel(level:number) {
ziti.setLogLevel(level)
}