Code Monkey home page Code Monkey logo

ssh_tunnel's Introduction

SSHTunnel

Create SSH tunnels in Elixir

Documentation for SSHTunnel is available online.

Installation

Add SSHTunnel to your mix.exs and run mix deps.get

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
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
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
{: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
{: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:

# 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

mix test

ssh_tunnel's People

Contributors

drowzy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.