Part of HTTP Toolkit: powerful tools for building, testing & debugging HTTP(S)
A module for serving HTTP, HTTPS and HTTP/2 connections, all over the same port.
Forked from the original httpolyglot
to fix various issues required for HTTP Toolkit, including:
- Support for HTTP/2
- Fixing
tlsClientError
: mscdex/httpolyglot#11 - Include initially sniffed bytes aren't lost in subsequent
clientError
events (mscdex/httpolyglot#13) - Dropping support for very old versions of Node (and thereby simplifying the code somewhat)
- Converting to TypeScript
- Event subscription support (subscribe to
server.on(x, ...)
to hear aboutx
from all internal servers - HTTP/2, HTTP/1, TLS and net) - Adding support for SOCKS connections
- Adding support for custom handling of unknown protocols
- node.js -- v20 or newer
npm install @httptoolkit/httpolyglot
- Simple usage:
import * as httpolyglot from '@httptoolkit/httpolyglot';
import * as fs from 'fs';
httpolyglot.createServer({
tls: {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}
}, function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end((req.socket.encrypted ? 'HTTPS' : 'HTTP') + ' Connection!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
// visit http://localhost:9000 and https://localhost:9000 in your browser ...
});
- Simple redirect of all http connections to https:
import * as httpolyglot from '@httptoolkit/httpolyglot';
import * as fs from 'fs';
httpolyglot.createServer({
tls: {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}
}, function(req, res) {
if (!req.socket.encrypted) {
res.writeHead(301, { 'Location': 'https://localhost:9000' });
return res.end();
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome, HTTPS user!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
// visit http://localhost:9000 and https://localhost:9000 in your browser ...
});
- createServer([< object >config, ]< function >requestListener) - Server - Creates and returns a new Server instance.
If no config is provided, this server handles HTTP/1 & HTTP/2 in plain text on the same port.
If a config is provided, it can contain:
tls
- Either TLS configuration options fortls.createServer
, or an existingtls.Server
instance. Setting this option enables TLS, so that HTTP/1 & HTTP/2 are accepted over both plain-text & encrypted (HTTPS) connections on the same port. If configuration options are provided, Httpolyglot will handle TLS automatically. If a server is provided, the connection will be passed to it (by emitting theconnection
event) and Httpolyglot will wait for the server to emitsecureConnection
(the default TLS server event) to handle the content within.socks
- Anet.Server
instance, which will handle any incoming SOCKS connections. If this is provided, SOCKSv4 and SOCKSv5 connections will be emitted asconnection
events on this server. If not, all SOCKS connections will be treated like any other unknown protocol.unknownProtocol
- Anet.Server
instance, which will handle any unknown protocols. If this is provided, unrecognized protocols will be emitted asconnection
events on this server. If it's not provided, these connections will be passed to the HTTP server by default, which will typically result in aclientError
event and a 400 HTTP response being sent to the client.
TLS, HTTP, HTTP/2, SOCKS and other connections are easy to distinguish based on the first byte sent by clients trying to connect. See this comment for more information.