-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix mapping ipv6-based port mappings
- Loading branch information
1 parent
963b515
commit e7013df
Showing
5 changed files
with
91 additions
and
57 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
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,31 @@ | ||
const mapPorts = ( | ||
ports: string | ||
): Array<{ | ||
mapped?: { address: string; port: number } | ||
exposed: { port: number; protocol: string } | ||
}> => { | ||
const result = !ports | ||
? [] | ||
: (() => { | ||
return ports.split(',').map((untypedPort) => { | ||
const exposedFragments = untypedPort.trim().split('->') | ||
|
||
console.log(exposedFragments) | ||
|
||
const [port, protocol] = | ||
exposedFragments.length === 1 | ||
? exposedFragments[0].split('/') | ||
: exposedFragments[1].split('/') | ||
const [address, mappedPort] = | ||
exposedFragments.length === 2 ? exposedFragments[0].split(':') : [] | ||
return { | ||
exposed: { port: Number(port), protocol }, | ||
...(address && | ||
mappedPort && { mapped: { port: Number(mappedPort), address } }) | ||
} | ||
}) | ||
})() | ||
return result | ||
} | ||
|
||
export default mapPorts |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import mapPorts from "../src/port-mapper"; | ||
|
||
test('map ports for empty string', () => { | ||
expect(mapPorts('')).toEqual([]) | ||
}) | ||
|
||
test('map ports for exposed tcp', () => { | ||
expect(mapPorts('80/tcp')).toEqual([ | ||
{ exposed: { port: 80, protocol: 'tcp' } } | ||
]) | ||
}) | ||
|
||
test('map ports for exposed tcp on ivp4 interface', () => { | ||
expect(mapPorts('0.0.0.0:443->443/tcp')).toEqual([ | ||
{ | ||
exposed: { port: 443, protocol: 'tcp' }, | ||
mapped: { address: '0.0.0.0', port: 443 } | ||
} | ||
]) | ||
}) | ||
|
||
test('map multiple tcp ports exposed on ivp4 interfaces', () => { | ||
expect(mapPorts('0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp')).toEqual([ | ||
{ | ||
exposed: { port: 443, protocol: 'tcp' }, | ||
mapped: { address: '0.0.0.0', port: 443 } | ||
}, | ||
{ | ||
exposed: { port: 80, protocol: 'tcp' }, | ||
mapped: { address: '0.0.0.0', port: 80 } | ||
} | ||
]) | ||
}) | ||
|
||
test('map multiple tcp ports exposed on ipv4 and ipv6 interfaces', () => { | ||
expect(mapPorts('0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp')).toEqual([ | ||
{ | ||
exposed: { port: 443, protocol: 'tcp' }, | ||
mapped: { address: '0.0.0.0', port: 443 }, | ||
}, | ||
{ | ||
exposed: { port: 443, protocol: 'tcp' }, | ||
mapped: { address: ':::', port: 443 }, | ||
}, | ||
{ | ||
exposed: { port: 80, protocol: 'tcp' }, | ||
mapped: { address: '0.0.0.0', port: 80 }, | ||
}, | ||
{ | ||
exposed: { port: 80, protocol: 'tcp' }, | ||
mapped: { address: ':::', port: 80 }, | ||
}, | ||
]) | ||
}) |