Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/drowzy/ssh_tunnel
https://github.com/drowzy/ssh_tunnel
elixir ssh ssh-tunnel
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/drowzy/ssh_tunnel
- Owner: drowzy
- License: mit
- Created: 2018-03-10T14:13:21.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-09T09:46:21.000Z (about 1 year ago)
- Last Synced: 2024-10-19T06:10:05.798Z (2 months ago)
- Topics: elixir, ssh, ssh-tunnel
- Language: Elixir
- Size: 31.3 KB
- Stars: 35
- Watchers: 5
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SSHTunnel
Create SSH tunnels in Elixir
[Documentation for SSHTunnel is available online.](https://hexdocs.pm/ssh_tunnel)
## Installation
Add SSHTunnel to your `mix.exs` and run `mix deps.get`
```elixir
def deps do
[
{:ssh_tunnel, "~> 0.1.3"}
]
end
```## Usage
SSHTunnel can be used to create forwarded SSH channels, similair to channels created using `:ssh_connection`.
Sending messages can be done using `:ssh_connection.send/3`.SSHTunnel also provide on-demand created tunnels, this is eqvivalent to using `ssh -nNT -L 8080:sshserver.example.com:80 [email protected]`.
The tunnel process will forward messages from a TCP client to a ssh connection and back.### As channels
* `directtcp-ip`
```elixir
msg = "GET / HTTP/1.1\r\nHost: localhost:8080\r\nUser-Agent: ssht/0.1.1\r\nAccept: */*\r\n\r\n"{:ok, pid} = SSHTunnel.connect(host: "sshserver.example.com", user: "user", password: "password")
{:ok, ch} = SSHTunnel.direct_tcpip(pid, {"127.0.0.1", 8080}, {"sshserver.example.com", 80})
:ok = :ssh_connection.send(pid, ch, msg)
receive do
{:ssh_cm, _, {:data, channel, _, data}} -> IO.puts("Data: #{(data)}")
end
```* `streamlocal forward`
```elixir
msg = "GET /images/json HTTP/1.1\r\nHost: /var/run/docker.sock\r\nAccept: */*\r\n\r\n"{:ok, pid} = SSHTunnel.connect(host: "sshserver.example.com", user: "user", password: "password")
{:ok, ch} = SSHTunnel.stream_local_forward(pid, "/var/run/docker.sock")
:ok = :ssh_connection.send(pid, ch, msg)receive do
{:ssh_cm, _, {:data, channel, _, data}} -> IO.puts("Data: #{(data)}")
end
```### Tunnels
* `directtcp-ip`
```elixir
{:ok, ssh_ref} = SSHTunnel.connect(host: "sshserver.example.com", user: "user", password: "password")# Will start a tcp server listening on port 8080.
# Any TCP messages received on `127.0.0.1:8080` will be forwarded to `sshserver.example.com:80`
{:ok, pid} = SSHTunnel.start_tunnel(pid, {:tcpip, {8080, {"sshserver.example.com", 80}}})# Send a TCP message
%HTTPoison.Response{body: body} = HTTPoison.get!("127.0.0.1:8080")
IO.puts("Received body: #{body})
```* `streamlocal forward`
```elixir
{:ok, ssh_ref} = SSHTunnel.connect(host: "sshserver.example.com", user: "user", password: "password")# Will start a tcp server listening on the provided path.
# Any TCP messages received on `/path/to/socket.socket` will be forwarded to the `/path/`to/remote.sock` on sshserver.example.com
{:ok, pid} = SSHTunnel.start_tunnel(pid, {:local, {"/path/to/socket.sock", {"sshserver.example.com", "/path/to/remote.sock"}}})# Send a TCP message
%HTTPoison.Response{body: body} = HTTPoison.get!("http+unix://#{URI.encode_www_form("/path/to/socket.sock")}")
IO.puts("Received body: #{body})
```It is also possible to mix and match:
```elixir
# From a local port to a remote socket
{:ok, pid} = SSHTunnel.start_tunnel(pid, {:tcpip, {8080, {"sshserver.example.com", "/path/to/remote.sock"}}})# From a local socket to a remote port
{:ok, pid} = SSHTunnel.start_tunnel(pid, {:local, {"/path/to/socket.sock", {"sshserver.example.com", 80}}})
```## Testing
```bash
mix test
```