在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:turboMaCk/koa-sslify开源软件地址:https://github.com/turboMaCk/koa-sslify开源编程语言:JavaScript 100.0%开源软件介绍:Koa.js middleware to enforce HTTPS connection on any incoming requests.
In case of a non-encrypted HTTP request, koa-sslify automatically redirects to an HTTPS address using a Koa SSLify can also work behind reverse proxies (load balancers) like on Heroku, Azure, GCP Ingress etc and supports custom implementations of proxy resolvers. Install
UsageImporting default factory function: const sslify = require('koa-sslify').default; // factory with default options
const Koa = require('koa');
app = new Koa();
app.use(sslify()); Default function accepts several options.
ResolversResolver is a function from classic Koa There are several resolvers provided by this library but it should be very easy to implement any type of custom check as well. for instance, Heroku has a reverse proxy that uses const {
default: sslify, // middleware factory
xForwardedProtoResolver: resolver // resolver needed
} = require('koa-sslify');
const Koa = require('koa');
app = new Koa();
// init middleware with resolver
app.use(sslify({ resolver })); Those are all resolver provided by default:
Some additional information about reverse proxies: Reverse Proxies (Heroku, Nodejitsu, GCE Ingress and others)Heroku, nodejitsu, GCE Ingress and other hosters often use reverse proxies which offer SSL endpoints
but then forward unencrypted HTTP traffic to the website.
This makes it difficult to detect if the original request was indeed via HTTPS. Luckily,
most reverse proxies set the AzureAzure has a slightly different way of signaling encrypted connections.
It uses Defining Custom ResolverIf you're still in a situation where you need to use custom resolver you can implement it as for example following: const { default: sslify } = require('koa-sslify');
app.use(sslify({
resolver: (ctx) => ctx.request.header['x-is-secure'] === 'yup!'
})) Contributions to increase coverage of default resolvers are welcomed. ExamplesThose are full example apps using Koa SSLify to enforce HTTPS. Without Reverse ProxyThis example starts 2 servers for app.
const Koa = require('koa');
const http = require('http');
const https = require('https');
const fs = require('fs');
const { default: enforceHttps } = require('koa-sslify');
const app = new Koa();
// Force HTTPS using default resolver
app.use(enforceHttps({
port: 8081
}));
// index page
app.use(ctx => {
ctx.body = "hello world from " + ctx.request.url;
});
// SSL options
var options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}
// start the server
http.createServer(app.callback()).listen(8080);
https.createServer(options, app.callback()).listen(8081); With Reverse ProxyThis example starts a single http server which is designed to run behind a reverse proxy like Heroku. const Koa = require('koa');
const {
default: enforceHttps,
xForwardedProtoResolver: resolver
} = require('koa-sslify');
var app = new Koa();
// Force HTTPS via x-forwarded-proto compatible resolver
app.use(enforceHttps({ resolver }));
// index page
app.use((ctx) => {
ctx = "hello world from " + ctx.request.url;
});
// proxy will bind this port to it's 443 and 80 ports
app.listen(3000); Advanced Redirect SettingRedirect MethodsBy default only Skip Default Port in Redirect URLBy default port is excluded from redirect url if it's set to LicenseMIT CreditsThis project is heavily inspired by Florian Heinemann's express-sslify and Vitaly Domnikov's koa-force-ssl. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论