Add the argument --namespace-pid

The argument '--namespace-pid' allows sshuttle to attach to the same net
namespace used by a running process.
This commit is contained in:
Raylan 2025-01-23 14:16:59 -03:00
parent eb5f5d4474
commit 8c96d7a5f5
3 changed files with 21 additions and 9 deletions

View File

@ -39,10 +39,11 @@ def main():
try:
namespace = getattr(opt, 'namespace', None)
if namespace:
namespace_pid = getattr(opt, 'namespace_pid', None)
if namespace or namespace_pid:
prefix = helpers.logprefix
helpers.logprefix = 'ns: '
enter_namespace(namespace)
enter_namespace(namespace, namespace_pid)
helpers.logprefix = prefix
if opt.firewall:

View File

@ -9,8 +9,11 @@ CLONE_NEWNET = 0x40000000
NETNS_RUN_DIR = "/var/run/netns"
def enter_namespace(namespace):
def enter_namespace(namespace, namespace_pid):
if namespace:
namespace_dir = f'{NETNS_RUN_DIR}/{namespace}'
else:
namespace_dir = f'/proc/{namespace_pid}/ns/net'
if not os.path.exists(namespace_dir):
raise Fatal('The namespace %r does not exists.' % namespace_dir)
@ -29,8 +32,9 @@ def enter_namespace(namespace):
libc.setns.errcheck = errcheck # type: ignore
debug1('Entering namespace %r' % namespace)
debug1('Entering namespace %r' % namespace_dir)
with open(namespace_dir) as fd:
libc.setns(fd.fileno(), CLONE_NEWNET)
debug1('Namespace %r successfully set' % namespace)
debug1('Namespace %r successfully set' % namespace_dir)

View File

@ -474,5 +474,12 @@ if sys.platform == 'linux':
parser.add_argument(
'--namespace',
type=parse_namespace,
help="Run it inside of a namespace."
help="Run inside of a net namespace with the given name."
)
parser.add_argument(
'--namespace-pid',
type=int,
help="""
Run inside the net namespace used by the process with
the given pid."""
)