11import http from 'http'
22import 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 ] + ) \/ ( m a n i f e s t s | b l o b s ) \/ ( [ \w \d : ] + ) ) ? $ /
8+ const URL_REGEX = / ^ \/ ( [ \w \d ] * ) \/ (?: ( [ \s \S ] + ) \/ ( m a n i f e s t s | b l o b s ) \/ ( [ \w \d : ] + ) ) ? $ /
79
810const 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