Skip to content

Commit 9bc107b

Browse files
committed
feat(proxy): focus on forwarding all traffic to a single repository
BREAKING CHANGE: repository host and port is not set through the repository name anymore, instead configure it through environment variables. re #9
1 parent e0fc90f commit 9bc107b

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import http from 'http'
22
import https from 'https'
33

4-
const PORT = process.argv[2] || 8080
4+
const PORT: string = process.env?.PORT || '8080'
5+
const REGISTRY_HOST: string = process.env?.REGISTRY_HOST || 'registry.hub.docker.com'
6+
const HTTPS: boolean = true
57

6-
const URL_REGEX = /^\/([\w\d]*)\/(?:([^\/]*)\/(\d{1,6})\/([\s\S]+)\/(manifests|blobs)\/([\w\d:]+))?$/
8+
const URL_REGEX = /^\/([\w\d]*)\/(?:([\s\S]+)\/(manifests|blobs)\/([\w\d:]+))?$/
79

810
const server = http.createServer((req, res) => {
911
const failRequest = (message: string) => {
@@ -20,18 +22,15 @@ const server = http.createServer((req, res) => {
2022
return failRequest(`Failed parsing path "${req.url}".`)
2123
}
2224

23-
const [_, version, domain, port, image, method, reference]: string[] = matches
25+
const [, version, image, method, reference]: string[] = matches
2426

2527
console.log(`Method: ${method}`)
2628

2729
let url: string
28-
if (!domain) {
29-
// Return a plain 200 when the domain was not part of the url
30-
res.statusCode = 200
31-
res.end()
32-
return
30+
if (!method) {
31+
url = `${REGISTRY_HOST}/${version}/`
3332
} else if (method == 'manifests' || method == 'blobs') {
34-
url = `http://${domain}:${port}/${version}/${image}/${method}/${reference}`
33+
url = `${REGISTRY_HOST}/${version}/${image}/${method}/${reference}`
3534
} else {
3635
failRequest(`Unknown method ${method}`)
3736
return
@@ -44,12 +43,12 @@ const server = http.createServer((req, res) => {
4443
// Pause the ongoing request until the forwarded request returns
4544
req.pause()
4645

47-
const connection = http.request(
48-
url,
46+
const connection = (HTTPS ? https : http).request(
47+
`${HTTPS ? 'https' : 'http'}://${url}`,
4948
{
5049
headers: {
5150
...req.headers,
52-
host: domain, // Overwrite the host as the prevent certificate issues
51+
host: REGISTRY_HOST, // Overwrite the host as the prevent certificate issues
5352
},
5453
method: req.method,
5554
agent: false,

0 commit comments

Comments
 (0)