Skip to content

Commit 45a3a24

Browse files
committed
feat: add https support
1 parent b7d3520 commit 45a3a24

File tree

7 files changed

+38
-10
lines changed

7 files changed

+38
-10
lines changed

API.md

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class MyStack extends cdk.Stack {
7878
// options: {
7979
// extension: {
8080
// layerVersionName: "tailscale-extension",
81+
// nodeTlsRejectUnauthorized: false, // Sets the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0'
8182
// },
8283
// lambda: {
8384
// functionName: "tailscale-proxy",
@@ -124,6 +125,8 @@ use the [aws4](https://www.npmjs.com/package/aws4) package to sign requests.
124125
When calling the Proxy, include the following headers to specify the target machine:
125126
- `ts-target-ip`: The IP address of the Tailscale-connected machine/device.
126127
- `ts-target-port`: The port of the Tailscale-connected machine/device.
128+
- `ts-https`: OPTIONAL, if undefined, the default behaviour is to use https when the port is 443. If specified then it
129+
will override the default behaviour.
127130

128131
These `ts-` headers are removed before the request is forwarded to the target machine.
129132

npm-link-manual.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#!/bin/bash
22

33
npm run default
4+
npm run bundle
45
npm run compile
6+
cp assets/lambda/tailscale-proxy/index.js lib/lambda/tailscale-proxy/index.js
7+
58

69
# For this proxy package to be used in the -caller project
710
npm link

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { TailscaleLambdaExtension } from 'tailscale-lambda-extension';
99

1010
export interface TailscaleLambdaProxyPropsLambdaOption {
1111
readonly functionName?: string;
12+
readonly nodeTlsRejectUnauthorized?: boolean;
1213
}
1314

1415
export interface TailscaleLambdaProxyPropsOptions {
@@ -51,6 +52,7 @@ export class TailscaleLambdaProxy extends Construct {
5152
environment: {
5253
TS_SECRET_API_KEY: props.tsSecretApiKey.secretArn,
5354
TS_HOSTNAME: props.tsHostname,
55+
...(props.options?.lambda?.nodeTlsRejectUnauthorized === false) ? { NODE_TLS_REJECT_UNAUTHORIZED: '0' } : { },
5456
},
5557
timeout: cdk.Duration.minutes(15),
5658
memorySize: 256,

src/lambda/tailscale-proxy/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as http from 'http';
2+
import * as https from 'https';
23
import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics';
34
import {
45
APIGatewayProxyEventV2, APIGatewayProxyResultV2,
@@ -7,6 +8,7 @@ import { SocksProxyAgent } from 'socks-proxy-agent';
78

89
async function proxyHttpRequest(
910
target: Pick<http.RequestOptions, 'hostname' | 'port' | 'agent'>,
11+
isHttps: boolean | undefined,
1012
request: {
1113
path: string;
1214
method: string;
@@ -16,7 +18,10 @@ async function proxyHttpRequest(
1618
): Promise<APIGatewayProxyResultV2> {
1719
return new Promise((resolve, reject) => {
1820
const chunks: Buffer[] = [];
19-
const apiRequest = http.request({
21+
const httpLib = isHttps == undefined ?
22+
(target.port == 443 ? https : http) :
23+
(isHttps ? https : http);
24+
const apiRequest = httpLib.request({
2025
...target,
2126
path: request.path,
2227
method: request.method,
@@ -58,6 +63,7 @@ export async function handler(event: APIGatewayProxyEventV2): Promise<APIGateway
5863
try {
5964
const socksProxyAgent = new SocksProxyAgent('socks://localhost:1055');
6065

66+
let isHttps = undefined; // Auto-detect, will be set for port 443
6167
if (!event.headers['ts-target-ip']) {
6268
return {
6369
statusCode: 400,
@@ -74,7 +80,9 @@ export async function handler(event: APIGatewayProxyEventV2): Promise<APIGateway
7480
},
7581
};
7682
}
77-
83+
if (event.headers['ts-https']) {
84+
isHttps = event.headers['ts-https'] === 'true';
85+
}
7886
if (event.headers['ts-metric-service']) {
7987
metrics = new Metrics({ namespace: 'tailscale-service', serviceName: event.headers['ts-metric-service'] });
8088
if (event.headers['ts-metric-dimension-name'] && event.headers['ts-metric-dimension-value']) {
@@ -95,7 +103,8 @@ export async function handler(event: APIGatewayProxyEventV2): Promise<APIGateway
95103
hostname: event.headers['ts-target-ip'],
96104
port: event.headers['ts-target-port'],
97105
agent: socksProxyAgent,
98-
}, {
106+
}, isHttps,
107+
{
99108
path: event.requestContext.http.path,
100109
headers: targetHeaders,
101110
method: event.requestContext.http.method,

0 commit comments

Comments
 (0)