-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to have miniflare accept https requests
- Loading branch information
Showing
7 changed files
with
118 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
secret: | ||
ignored-paths: | ||
- packages/miniflare/src/http/cert.ts | ||
version: 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Generated via | ||
// openssl ecparam -name prime256v1 -genkey -noout -out key.pem | ||
export const KEY = ` | ||
-----BEGIN EC PRIVATE KEY----- | ||
MHcCAQEEIC+umAaVUbEfPqGA9M7b5zAP7tN2eLT1bu8U8gpbaKbsoAoGCCqGSM49 | ||
AwEHoUQDQgAEtrIEgzogjrUHIvB4qgjg/cT7blhWuLUfSUp6H62NCo21NrVWgPtC | ||
mCWw+vbGTBwIr/9X1S4UL1/f3zDICC7YSA== | ||
-----END EC PRIVATE KEY----- | ||
`; | ||
|
||
// Genereated via | ||
// openssl req -new -x509 -days 36500 -config openssl.cnf -key key.pem -out cert.pem | ||
export const CERT = ` | ||
-----BEGIN CERTIFICATE----- | ||
MIICcDCCAhegAwIBAgIUE97EcbEWw3YZMN/ucGBSzJ/5qA4wCgYIKoZIzj0EAwIw | ||
VTELMAkGA1UEBhMCVVMxDjAMBgNVBAgMBVRleGFzMQ8wDQYDVQQHDAZBdXN0aW4x | ||
EzARBgNVBAoMCkNsb3VkZmxhcmUxEDAOBgNVBAsMB1dvcmtlcnMwIBcNMjMwNjIy | ||
MTg1ODQ3WhgPMjEyMzA1MjkxODU4NDdaMFUxCzAJBgNVBAYTAlVTMQ4wDAYDVQQI | ||
DAVUZXhhczEPMA0GA1UEBwwGQXVzdGluMRMwEQYDVQQKDApDbG91ZGZsYXJlMRAw | ||
DgYDVQQLDAdXb3JrZXJzMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEtrIEgzog | ||
jrUHIvB4qgjg/cT7blhWuLUfSUp6H62NCo21NrVWgPtCmCWw+vbGTBwIr/9X1S4U | ||
L1/f3zDICC7YSKOBwjCBvzAdBgNVHQ4EFgQUSXahTksi00c6KhUECHIY4FLW7Sow | ||
HwYDVR0jBBgwFoAUSXahTksi00c6KhUECHIY4FLW7SowDwYDVR0TAQH/BAUwAwEB | ||
/zAsBglghkgBhvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUw | ||
CwYDVR0PBAQDAgL0MDEGA1UdJQQqMCgGCCsGAQUFBwMBBggrBgEFBQcDAgYIKwYB | ||
BQUHAwMGCCsGAQUFBwMIMAoGCCqGSM49BAMCA0cAMEQCIE2qnXbKTHQ8wtwI+9XR | ||
h4ivDyz7w7iGxn3+ccmj/CQqAiApdX/Iz/jGRzi04xFlE4GoPVG/zaMi64ckmIpE | ||
ez/dHA== | ||
-----END CERTIFICATE----- | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import fs from "fs/promises"; | ||
import { PluginSharedOptions } from "../index"; | ||
import { HEADER_CF_BLOB, SERVICE_ENTRY, SOCKET_ENTRY } from "../plugins"; | ||
import { HttpOptions, Socket, Socket_Https } from "../runtime"; | ||
import { Awaitable } from "../shared"; | ||
import { CERT, KEY } from "./cert"; | ||
|
||
export async function configureEntrySocket( | ||
sharedOpts: PluginSharedOptions | ||
): Promise<Socket> { | ||
const coreOpts = sharedOpts.core; | ||
|
||
const httpOptions = { | ||
// Even though we inject a `cf` object in the entry worker, allow it to | ||
// be customised via `dispatchFetch` | ||
cfBlobHeader: HEADER_CF_BLOB, | ||
}; | ||
|
||
let privateKey: string | undefined = undefined; | ||
let certificateChain: string | undefined = undefined; | ||
|
||
if ( | ||
(coreOpts.httpsKey || coreOpts.httpsKeyPath) && | ||
(coreOpts.httpsCert || coreOpts.httpsCertPath) | ||
) { | ||
privateKey = await valueOrFile(coreOpts.httpsKey, coreOpts.httpsKeyPath); | ||
certificateChain = await valueOrFile( | ||
coreOpts.httpsCert, | ||
coreOpts.httpsCertPath | ||
); | ||
} else if (coreOpts.https) { | ||
privateKey = KEY; | ||
certificateChain = CERT; | ||
} | ||
|
||
let options: { http: HttpOptions } | { https: Socket_Https }; | ||
|
||
if (privateKey && certificateChain) { | ||
options = { | ||
https: { | ||
options: httpOptions, | ||
tlsOptions: { | ||
keypair: { | ||
privateKey: privateKey, | ||
certificateChain: certificateChain, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} else { | ||
options = { | ||
http: httpOptions, | ||
}; | ||
} | ||
|
||
return { | ||
name: SOCKET_ENTRY, | ||
service: { name: SERVICE_ENTRY }, | ||
...options, | ||
}; | ||
} | ||
|
||
function valueOrFile( | ||
value?: string, | ||
filePath?: string | ||
): Awaitable<string | undefined> { | ||
return value ?? (filePath && fs.readFile(filePath, "utf8")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters