-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from JacobLinCool/tunnel-class
Tunnel class
- Loading branch information
Showing
10 changed files
with
429 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"cloudflared": minor | ||
--- | ||
|
||
Tunnel class with custom output parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Tunnel, ConfigHandler } from "cloudflared"; | ||
|
||
const token = process.env.CLOUDFLARED_TOKEN; | ||
if (!token) { | ||
throw new Error("CLOUDFLARED_TOKEN is not set"); | ||
} | ||
|
||
const tunnel = Tunnel.withToken(token); | ||
const handler = new ConfigHandler(tunnel); | ||
|
||
handler.on("config", ({ config }) => { | ||
console.log("Config", config); | ||
}); | ||
|
||
tunnel.on("url", (url) => { | ||
console.log("Tunnel is ready at", url); | ||
}); | ||
|
||
tunnel.on("connected", (connection) => { | ||
console.log("Connected to", connection); | ||
}); | ||
|
||
tunnel.on("disconnected", (connection) => { | ||
console.log("Disconnected from", connection); | ||
}); | ||
|
||
tunnel.on("stdout", (data) => { | ||
console.log("Tunnel stdout", data); | ||
}); | ||
|
||
tunnel.on("stderr", (data) => { | ||
console.error("Tunnel stderr", data); | ||
}); | ||
|
||
tunnel.on("exit", (code, signal) => { | ||
console.log("Tunnel exited with code", code, "and signal", signal); | ||
}); | ||
|
||
tunnel.on("error", (error) => { | ||
console.error("Error", error); | ||
}); | ||
|
||
process.on("SIGINT", () => { | ||
console.log("Tunnel stopped", tunnel.stop()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
const { tunnel } = require("cloudflared"); | ||
const { Tunnel } = require("cloudflared"); | ||
|
||
console.log("Cloudflared Tunnel Example."); | ||
main(); | ||
|
||
async function main() { | ||
// run: cloudflared tunnel --hello-world | ||
const { url, connections, child, stop } = tunnel({ "--hello-world": null }); | ||
const tunnel = Tunnel.quick(); | ||
|
||
// show the url | ||
const url = new Promise((resolve) => tunnel.once("url", resolve)); | ||
console.log("LINK:", await url); | ||
|
||
// wait for the all 4 connections to be established | ||
const conns = await Promise.all(connections); | ||
|
||
// show the connections | ||
console.log("Connections Ready!", conns); | ||
const conn = new Promise((resolve) => tunnel.once("connected", resolve)); | ||
console.log("CONN:", await conn); | ||
|
||
// stop the tunnel after 15 seconds | ||
setTimeout(stop, 15_000); | ||
setTimeout(tunnel.stop, 15_000); | ||
|
||
child.on("exit", (code) => { | ||
tunnel.on("exit", (code) => { | ||
console.log("tunnel process exited with code", code); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
import { tunnel } from "cloudflared"; | ||
import { Tunnel } from "cloudflared"; | ||
|
||
console.log("Cloudflared Tunnel Example."); | ||
main(); | ||
|
||
async function main() { | ||
// run: cloudflared tunnel --hello-world | ||
const { url, connections, child, stop } = tunnel({ "--hello-world": null }); | ||
const tunnel = Tunnel.quick(); | ||
|
||
// show the url | ||
const url = new Promise((resolve) => tunnel.once("url", resolve)); | ||
console.log("LINK:", await url); | ||
|
||
// wait for the all 4 connections to be established | ||
const conns = await Promise.all(connections); | ||
|
||
// show the connections | ||
console.log("Connections Ready!", conns); | ||
const conn = new Promise((resolve) => tunnel.once("connected", resolve)); | ||
console.log("CONN:", await conn); | ||
|
||
// stop the tunnel after 15 seconds | ||
setTimeout(stop, 15_000); | ||
setTimeout(tunnel.stop, 15_000); | ||
|
||
child.on("exit", (code) => { | ||
tunnel.on("exit", (code) => { | ||
console.log("tunnel process exited with code", code); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.