def forward_ports(remote_host, local_host, local_listen_ports,
remote_listen_ports):
"""Forwards ports such that multiplayer works between machines.
Args:
remote_host: Where to ssh to.
local_host: "127.0.0.1" or "::1".
local_listen_ports: Which ports to listen on locally to forward remotely.
remote_listen_ports: Which ports to listen on remotely to forward locally.
Returns:
The ssh process.
Raises:
ValueError: if it can't find ssh.
"""
if ":" in local_host and not local_host.startswith("["):
local_host = "[%s]" % local_host
ssh = whichcraft.which("ssh") or whichcraft.which("plink")
if not ssh:
raise ValueError("Couldn't find an ssh client.")
args = [ssh, remote_host]
for local_port in local_listen_ports:
args += ["-L", "%s:%s:%s:%s" % (local_host, local_port,
local_host, local_port)]
for remote_port in remote_listen_ports:
args += ["-R", "%s:%s:%s:%s" % (local_host, remote_port,
local_host, remote_port)]
logging.info("SSH port forwarding: %s", " ".join(args))
return subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE, close_fds=(os.name == "posix"))