From 831c6cb4b7ed2f00521c9eef92e904c8370dcad0 Mon Sep 17 00:00:00 2001 From: Curt Tudor Date: Mon, 25 Mar 2024 12:37:49 -0600 Subject: [PATCH] adjust sdk example: tcpTunnel must be private --- sdk/nodejs/examples/pastebin/src/index.ts | 105 ++++++++-------------- 1 file changed, 35 insertions(+), 70 deletions(-) diff --git a/sdk/nodejs/examples/pastebin/src/index.ts b/sdk/nodejs/examples/pastebin/src/index.ts index 28aba3ba..8d01382f 100644 --- a/sdk/nodejs/examples/pastebin/src/index.ts +++ b/sdk/nodejs/examples/pastebin/src/index.ts @@ -1,9 +1,7 @@ const { Command } = require("commander"); const zrok = require("zrok") const ziti = require('@openziti/ziti-sdk-nodejs') -const express = require('express') var readlineSync = require('readline-sync'); -import fs from "node:fs" const program = new Command(); @@ -17,34 +15,26 @@ program console.log("data is: ", data) let root = zrok.Load() - await zrok.init(root); - //await ziti.init( root.env.ZitiIdentity ).catch(( err: Error ) => { console.error(err); return process.exit(1) }); - ziti.setLogLevel(0) + 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.PUBLIC_SHARE_MODE, "pastebin", ["public"])); - // console.log("zrok share is: ",shr) - // console.log("setting up app") - // let service = "ns5ix2brb61f" - // console.log("attempting to bind to service: "+ shr.Token) + 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 listener = await zrok.NewListener(shr.Token, root) - // console.log("zrok listener is: ", listener) - - - let app = ziti.express( express, shr.Token ); - // console.log("after setting up app") + let app = zrok.express( shr.Token ); app.get('/',function(_: Request,res: any){ - // console.log("received a GET request") res.write(data) res.end() }); - // console.log("after setting up get") app.listen(undefined, () => { - // console.log(`Example app listening!`) }) - // console.log("after listen") - // zrok.DeleteShare(root, shr); + + // Delete the private share upon CTRL-C + process.on('SIGINT', async () => { + console.log("Now deleting your private zrok share...") + await zrok.DeleteShare(root, shr) + process.exit(15); + }); + }); program @@ -53,64 +43,39 @@ program .description("command to paste content from coptyo") .action(async (shrToken: string) => { - // ziti.setLogLevel(99) let root = zrok.Load(); await zrok.init(root).catch((err: any) => { console.log(err) }); let acc = await zrok.CreateAccess(root, new zrok.AccessRequest(shrToken)) - // console.log("zrok.CreateAccess returned: ", acc) - // console.log("about to ziti.httpRequest: ", shrToken) + ziti.httpRequest( + shrToken, + undefined, + 'GET', + '/', + [], + (data: any) => { // on_req_cb + console.log("in on_req_cb") + console.log("data is: ", data) + }, + (data: any) => { // on_resp_cb + console.log("in on_resp_cb") + console.log("data is: ", data) + }, + 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()); - // setTimeout(function() { - - ziti.httpRequest( - shrToken, - undefined, - 'GET', - '/', - [], - (data: any) => { // on_req_cb - console.log("in on_req_cb") - console.log("data is: ", data) - }, - (data: any) => { // on_resp_cb - console.log("in on_resp_cb") - console.log("data is: ", data) - }, - 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); - } + await zrok.DeleteAccess(root, acc) + process.exit(0); } - ); - // }, 5000); - - - - // zrok.dialer( - // root, - // shrToken, - // (conn: any) => { - // console.log("in connectCallback") - // console.log("conn is: ", conn) - - - // }, - // (dataData: any) => { - // console.log("in dataCallback") - // console.log(dataData.toString()) - // } - // ); + } + ); }); program.parse(process.argv)