Skip to content

Commit

Permalink
Merge pull request #121 from ngrok/del/forward-app_protocol
Browse files Browse the repository at this point in the history
feat: plumb app_protocol for forwarded http_endpoint
  • Loading branch information
OfTheDelmer authored Feb 22, 2024
2 parents 2b5fd9b + df7af05 commit 40e14be
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
45 changes: 42 additions & 3 deletions __test__/connect.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,42 @@ import axios, { AxiosError } from "axios";
import axiosRetry from "axios-retry";
import * as fs from "fs";
import * as http from "http";
import * as http2 from "http2";
import * as retry from "./retry-config.mjs";
import * as path from "path";
import exp from "constants";

axiosRetry(axios, retry.retryConfig);
const expected = "Hello";

function createHttpServer() {
function createHttpServer({ protocol }) {
if (protocol === "http2") {
return createHttp2Server();
}

return http.createServer(function (req, res) {
res.writeHead(200);
res.write(expected);
res.end();
});
}

async function makeHttp(useUnixSocket) {
const server = createHttpServer();
function createHttp2Server() {
const server = http2.createServer();

server.on("stream", (stream, headers) => {
stream.respond({
":status": 200,
});
stream.end(expected);
});

return server;
}

async function makeHttp(options = {}) {
const { useUnixSocket = false, useHttp2 = false } = options;
const server = createHttpServer({ protocol: useHttp2 ? "http2" : "http" });
const listenTo = useUnixSocket ? "tun-" + Math.floor(Math.random() * 1000000) : 0;
const socket = await server.listen(listenTo);
server.socket = socket;
Expand Down Expand Up @@ -60,6 +80,25 @@ test("forward https", async (t) => {
await validateShutdown(t, httpServer, url);
});

test("forward http2", async (t) => {
const httpServer = await makeHttp({useHttp2: true});
const listener = await ngrok.forward({
// numeric port
addr: parseInt(httpServer.listenTo.split(":")[1], 10),
// authtoken from env
authtoken: process.env["NGROK_AUTHTOKEN"],
// The L7 app_protocol
app_protocol: "http2",
});

const url = listener.url();
t.truthy(url.startsWith("https://"), url);
const res = await validateShutdown(t, httpServer, url);

t.assert(res.status === 200);
t.assert(res.data.includes(expected));
});

test("connect number", async (t) => {
const httpServer = await makeHttp();
ngrok.authtoken(process.env["NGROK_AUTHTOKEN"]);
Expand Down
1 change: 1 addition & 0 deletions examples/ngrok-forward-full.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ngrok.consoleLog("INFO"); // turn on info logging
// session configuration
addr: `unix:${UNIX_SOCKET}`,
// addr: `localhost:8080`,
// app_protocol: "http2",
// authtoken: "<authtoken>",
authtoken_from_env: true,
on_status_change: (addr, error) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/ngrok-http2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ngrok = require("../index.js");
const http2 = require("node:http2");
const ngrok = require("@ngrok/ngrok");

const server = http2.createServer();
server.on("error", (err) => console.error(err));
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Config {
#[napi(ts_type = "number|string")]
pub addr: Option<String>,
/// The L7 application protocol to use for this edge, e.g. "http2" or "http1".
#[napi(js_name = "app_protocol")]
pub app_protocol: Option<String>,
// Synonym for basic_auth
#[napi(ts_type = "string|Array<string>")]
Expand Down
1 change: 1 addition & 0 deletions src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ async fn http_endpoint(session: &Session, cfg: &Config) -> Result<String> {
plumb_vec!(bld, cfg, scheme, schemes);
plumb!(bld, cfg, domain, hostname); // synonym for domain
plumb!(bld, cfg, domain);
plumb!(bld, cfg, app_protocol);
plumb_vec!(bld, cfg, mutual_tlsca, mutual_tls_cas, vecu8);
plumb_bool!(bld, cfg, compression);
plumb_bool!(bld, cfg, websocket_tcp_conversion, websocket_tcp_converter);
Expand Down

0 comments on commit 40e14be

Please sign in to comment.