Skip to content

Commit

Permalink
Merge pull request #23 from JorisAerts/feature/tls
Browse files Browse the repository at this point in the history
Feature/tls
  • Loading branch information
JorisAerts authored Aug 5, 2024
2 parents 0836977 + cbd3992 commit 1b78c9d
Show file tree
Hide file tree
Showing 24 changed files with 830 additions and 408 deletions.
29 changes: 0 additions & 29 deletions cert/cert.pem

This file was deleted.

7 changes: 0 additions & 7 deletions cert/generate.sh

This file was deleted.

52 changes: 0 additions & 52 deletions cert/key.pem

This file was deleted.

21 changes: 21 additions & 0 deletions cert/root/rootCA.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDbzCCAlegAwIBAgIBATANBgkqhkiG9w0BAQsFADBcMQkwBwYDVQQGEwAxCTAH
BgNVBAgTADEJMAcGA1UEBxMAMQkwBwYDVQQKEwAxCTAHBgNVBAsTADESMBAGA1UE
AxMJbG9jYWxob3N0MQ8wDQYJKoZIhvcNAQkBEwAwHhcNMjQwODA1MTcwMjQyWhcN
MzQwODA1MTcwMjQyWjBcMQkwBwYDVQQGEwAxCTAHBgNVBAgTADEJMAcGA1UEBxMA
MQkwBwYDVQQKEwAxCTAHBgNVBAsTADESMBAGA1UEAxMJbG9jYWxob3N0MQ8wDQYJ
KoZIhvcNAQkBEwAwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC8N1w+
9h7tEhI5fqUVYw+UbPEvZS4Kuc9KemVZdUgSX5f7WlQ8s/4xZcRZfF/aN4iGmu9f
Ayfwf1PO+7/Wyu0m/JDyG7oFUCyNqjapyKS4rVcBExD0brnsbjahkasiLXRfHV0v
IoBimcMs1jSy/zermxXxIYzbN4FBM7tftLQ40WJUBPQSScxYccx0g8wg6rH2XJ+r
6gdHNP2umlQdQVbtbfWbgYu1I6OP4v5nmj5OVdfV0//fDltIOm7CuDZpIY1xeLe0
fi0bBXs6QSgkrt3w5Wbrl+lx4ZziGkx9D8G7TyWi20tpxmiI8/kpQRCS4ZqgsliM
JROS8xKALeSnl4M3AgMBAAGjPDA6MAwGA1UdEwQFMAMBAf8wCwYDVR0PBAQDAgL0
MB0GA1UdDgQWBBS21zNQm6YTFbQRluTCb8HKnnJqtDANBgkqhkiG9w0BAQsFAAOC
AQEAlj4ATMEQ1d11rNCJNVEPug1h5S+pj6beNqfW9lxcMbZ9nEr0oaTAbT4/bAj3
9NfDNvbobjNqeWoINCNcQ7s13tvtu9K82lA6vTgTvQf8nUlC28SqzQKI6m5OdTPq
DsPbV/GTpHNPtmTjesEH6VpR1X8QngfWoP2l85lXKsNPR7cBhS3PbHHzqlvdv7gd
fV21fkHVitYVPerBx8+NeJFFB3XqX0AEsNUmrmGjC2RHtNkFtfaE11R0+KzPFSIQ
bjFChz63BJK/Or2BqTbWT5/Mdh5fhJ3xvB3GpQKWPfyr0NZ+o7QNkhyEYiNT5szv
2yFXlDulcNOX1mrG8kreOJrdCg==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions cert/root/rootCA.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAvDdcPvYe7RISOX6lFWMPlGzxL2UuCrnPSnplWXVIEl+X+1pU
PLP+MWXEWXxf2jeIhprvXwMn8H9Tzvu/1srtJvyQ8hu6BVAsjao2qcikuK1XARMQ
9G657G42oZGrIi10Xx1dLyKAYpnDLNY0sv83q5sV8SGM2zeBQTO7X7S0ONFiVAT0
EknMWHHMdIPMIOqx9lyfq+oHRzT9rppUHUFW7W31m4GLtSOjj+L+Z5o+TlXX1dP/
3w5bSDpuwrg2aSGNcXi3tH4tGwV7OkEoJK7d8OVm65fpceGc4hpMfQ/Bu08lottL
acZoiPP5KUEQkuGaoLJYjCUTkvMSgC3kp5eDNwIDAQABAoIBAE60Y6mcZ9BAx0gt
kkDAPMqXR7/G8prfmjQNvIXNCkdMnBQqcKE1ryze3l2BKd/pavjGnyQ/AJuL0vLx
yQW7s7Gi2cW0HHSOUyMmM1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J8lP129UF
S/tu3zSEG5bAvVyHDyvi7g05JIqMwUR6zLXUcmG4NOvqOCEGI+E=
-----END RSA PRIVATE KEY-----
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
"dependencies": {
"node-forge": "^1.3.1",
"picocolors": "^1.0.1",
"uuid": "^10.0.0",
"ws": "^8.18.0"
},
"devDependencies": {
"@eslint/js": "9.8.0",
"@types/node": "22.1.0",
"@types/node-forge": "^1.3.11",
"@types/uuid": "^10",
"@types/ws": "^8.5.12",
"@vitejs/plugin-vue": "5.1.2",
"@vitejs/plugin-vue-jsx": "4.0.0",
Expand Down
10 changes: 7 additions & 3 deletions src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { start } from './server'
import process from 'node:process'
import { createProxy } from './proxy'
import { openBrowser } from './utils/open-browser'
import { displayServerInfo } from './server/server-info'

start().then((address) => {
if (process.argv.includes('--open')) openBrowser(address)
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

createProxy().then(({ url, server, logger }) => {
displayServerInfo({ logger, server })
if (process.argv.includes('--open')) openBrowser(url)
})
31 changes: 31 additions & 0 deletions src/node/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
import * as readline from 'node:readline'

export enum LogLevels {
/**
* Logs that contain the most detailed messages. These messages may contain sensitive application data. These messages are disabled by default and should never be enabled in a production environment.
*/
Trace,
/**
* Logs that are used for interactive investigation during development. These logs should primarily contain information useful for debugging and have no long-term value.
*/
Debug,
/**
* Logs that track the general flow of the application. These logs should have long-term value.
*/
Info,
/**
* Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop.
*/
Warn,
/**
* Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure.
*/
Error,
/**
* Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention.
*/
Critical,
/**
* Not used for writing log messages. Specifies that a logging category should not write any messages.
*/
None,
}

export type Logger = {
clear: () => void

Expand Down
74 changes: 74 additions & 0 deletions src/node/proxy/incoming.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// https://github.com/nodejitsu/node-http-proxy
import { outgoing } from './outgoing'
import type { IncomingMessage, ServerResponse } from 'http'
import * as http from 'http'
import * as https from 'https'
import { isReqHttps, setupOutgoing } from './utils'
import type { CreateProxyOptions } from './proxy'

export interface IncomingRequest {
(req: IncomingMessage, res: ServerResponse, options: CreateProxyOptions): void
}

const inc = [
/**
* Sets `content-length` to '0' if request is of DELETE type.
*/
function (req: IncomingMessage) {
if (req.method === 'DELETE' && !req.headers['content-length']) {
req.headers['content-length'] = '0'
}
},

/**
* Sets `x-forwarded-*` headers if specified in config.
*/
function (req: IncomingMessage) {
const values = {
for: /* req.connection.remoteAddress || */ req.socket.remoteAddress,
port: /* req.connection.remotePort || */ req.socket.remotePort,
proto: isReqHttps(req) ? 'https' : 'http',
}
Object.keys(values).forEach((header) => {
req.headers[`x-forwarded-${header}`] =
(req.headers[`x-forwarded-${header}`] || '') +
(req.headers[`x-forwarded-${header}`] ? ',' : '') +
values[header as keyof typeof values]
})
},

/**
* Pipe to the outgoing pipeline
*/
function (
req: IncomingMessage,
res: ServerResponse,
options: CreateProxyOptions
) {
function response(proxyRes: IncomingMessage) {
outgoing(req, res, proxyRes)
proxyRes.pipe(res)
}

function onError(err: string) {
console.error(`error in ${req.url}`)
console.error(err)
}

const requestOptions = setupOutgoing({}, req, res, options)
if (requestOptions) {
const proxyReq = (isReqHttps(req) ? https : http).request(
requestOptions,
response
)
proxyReq.on('error', onError)
req.pipe(proxyReq)
}
},
] as IncomingRequest[]

export const incoming = (
req: IncomingMessage,
res: ServerResponse,
options: CreateProxyOptions
) => inc.forEach((come) => come(req, res, options))
2 changes: 2 additions & 0 deletions src/node/proxy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type * from './proxy'
export { createProxy } from './proxy'
72 changes: 72 additions & 0 deletions src/node/proxy/outgoing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { IncomingMessage, ServerResponse } from 'http'

// https://github.com/nodejitsu/node-http-proxy

export interface OutgoingRequest {
(req: IncomingMessage, res: ServerResponse, proxyRes: IncomingMessage): void
}

const go = [
/**
* If is a HTTP 1.0 request, remove chunk headers
*/
function (
req: IncomingMessage,
res: ServerResponse,
proxyRes: IncomingMessage
) {
if (req.httpVersion === '1.0') {
delete proxyRes.headers['transfer-encoding']
}
},

/**
* If is a HTTP 1.0 request, set the correct connection header or if connection header not present, then use `keep-alive`
*/
function (
req: IncomingMessage,
res: ServerResponse,
proxyRes: IncomingMessage
) {
if (req.httpVersion === '1.0') {
proxyRes.headers.connection = req.headers.connection || 'close'
} else if (!proxyRes.headers.connection) {
proxyRes.headers.connection = req.headers.connection || 'keep-alive'
}
},

/**
* Copy headers from ServerResponse to response set each header in response object.
*/
function (
req: IncomingMessage,
res: ServerResponse,
proxyRes: IncomingMessage
) {
Object.keys(proxyRes.headers)
.filter((key) => proxyRes.headers[key])
.forEach(function (key) {
res.setHeader(
key,
proxyRes.headers[key as keyof typeof proxyRes.headers]!
)
})
},

/**
* Set the statusCode from the ServerResponse
*/
function (
req: IncomingMessage,
res: ServerResponse,
proxyRes: IncomingMessage
) {
res.writeHead(proxyRes.statusCode ?? 200, proxyRes.statusMessage)
},
] as OutgoingRequest[]

export const outgoing = (
req: IncomingMessage,
res: ServerResponse,
proxyRes: IncomingMessage
) => go.forEach((come) => come(req, res, proxyRes))
Loading

0 comments on commit 1b78c9d

Please sign in to comment.