<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Simon Thörnqvist on Medium]]></title>
        <description><![CDATA[Stories by Simon Thörnqvist on Medium]]></description>
        <link>https://medium.com/@Drowzy?source=rss-652e699f0327------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*OKUEbTOY8x2EQ4ie.jpg</url>
            <title>Stories by Simon Thörnqvist on Medium</title>
            <link>https://medium.com/@Drowzy?source=rss-652e699f0327------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 05:00:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Drowzy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing SSH tunnels in Elixir]]></title>
            <link>https://medium.com/@Drowzy/implementing-ssh-tunnels-in-elixir-e7ad9d1af01a?source=rss-652e699f0327------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7ad9d1af01a</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[ssh]]></category>
            <dc:creator><![CDATA[Simon Thörnqvist]]></dc:creator>
            <pubDate>Wed, 21 Mar 2018 22:04:41 GMT</pubDate>
            <atom:updated>2018-03-21T22:04:41.789Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N1ypqpu8qeVbQLH3cS710Q.jpeg" /></figure><h3>Introduction</h3><p>Erlang OTP includes a SSH <a href="http://erlang.org/doc/man/ssh.html">application</a> I thought I would attempt to implement SSH tunneling in Elixir, similar to how we can define tunnels using openSSH.</p><p>With openSSH one can define a local tunnel using the following command:</p><pre>ssh -nNT -L 8585:127.0.0.1:9000 user@192.168.90.1</pre><p>This will forward any traffic on localhost:8585 to 127.0.0.1:9000 through 192.168.90.1 . In other words localhost:8585 will connect to 192.168.90.1:9000.</p><p>The -nNT option tells SSH to run no command, redirect Null to stdin, and not allocate a TTY. So it’s not possible to run any commands through the tunnel. -L is for Local bind of the address, doing it in reverse is also possible by specifying the -R option and it even works for sockets too:</p><pre>ssh -nNT -L 9000:/var/lib/mysql/mysql.sock user@192.168.90.1</pre><p>I’ve been writing this post alongside doing the implementation so it’s sort of a recollection of how it was implemented. The gists are for the most part correct and the full source can be found <a href="https://github.com/drowzy/ssh_tunnel">here</a>.</p><h4>Connecting to a ssh server</h4><p>We’ll define a module with a simple connect to create a connection to a ssh server:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa28a256250a3be57f7136db53646070/href">https://medium.com/media/fa28a256250a3be57f7136db53646070/href</a></iframe><p>Since we are calling the into erlang the host string (binary) needs to be encoded as a charlist.<br>Here we are using username / password for authentication as well as not allowing user interactions, any options requiring interactions will fail the connection attempt. There are a lot more options <a href="http://erlang.org/doc/man/ssh.html#connect-4\">available</a> for more fine grained control, but for us these options will suffice.</p><p>The ssh application needs to be started before we try to connect to the server:</p><pre># mix.exs<br>def application do<br>  [<br>    extra_applications: [:logger, :ssh]<br>  ]<br>end</pre><p>And we can try it out:</p><pre>iex(1)&gt; {:ok, pid} = SSHt.connect(host: &quot;192.168.90.15&quot;, user: &quot;ubuntu&quot;, password: &quot;&quot;)<br><br>{:ok, #PID&lt;0.181.0&gt;}</pre><p>It works! With this we are ready to start implementing the tunnels!</p><h4>Types of tunnels</h4><p>There are two kinds of tunnels which we are interested in</p><ul><li><a href="https://tools.ietf.org/html/rfc4254#section-7.2">directtcp-ip</a></li><li><a href="https://github.com/openssh/openssh-portable/blob/master/PROTOCOL#L235]">direct-streamlocal</a></li></ul><p>directtcp-ip allows us (the client) to connect to an ip:port using the ssh server and direct-streamlocal allows us to connect to unix domain socket.</p><p>directtcp-ip forwarding has been a part of erlang :sshapplication in the past but has since been removed, however we can implement it by using :ssh_connection_handler:open_channel/6 (<a href="https://github.com/erlang/otp/blob/83e20c62057ebc1d8064bf57b01be560cd244e1d/lib/ssh/src/ssh_connection_handler.erl#L173">source</a>). This is the function used internally for creating channels ( :ssh_connection.session_channel/2/4 for instance).</p><p>For reference this is what it looks like</p><pre>:ssh_connection:open_channel(ConnectionHandler, ChannelType, ChannelSpecificData, InitialWindowSize, MaxPacketSize, Timeout)</pre><ul><li>ConnectionHandler is the pid we receive from :ssh.connect</li><li>ChannelType is the type of message for us this will be either &quot;dirrect-tcpip&quot; or &quot;direct-streamlocal@openssh.com&quot;</li><li>ChannelSpecificData is the messag we’ll construct from the message format below</li><li>InitalWindowSize Initial TCP window size</li><li>MaxPacketSize Max allowed packet size</li></ul><p>and the directtcp-ip message format:</p><pre>byte      SSH_MSG_CHANNEL_OPEN<br>string    &quot;direct-tcpip&quot;<br>uint32    sender channel<br>uint32    initial window size<br>uint32    maximum packet size<br>string    host to connect<br>uint32    port to connect<br>string    originator IP address<br>uint32    originator port</pre><p>We’ll define a direct_tcpip/3 function in the ssht.ex</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eca926cff602a387de88c52339b75539/href">https://medium.com/media/eca926cff602a387de88c52339b75539/href</a></iframe><p>On line 21 we create a message by translating from directtcp-ip message format to a binary, due to the excellent bit syntax it reads basically the same as the original message format from the specification.</p><p>Since the host fields can be of variable size, the length is prepended. Note that the SSH_MSG_CHANNEL_OPEN and sender channel fields are not part of our message. These will be set internally in :ssh_connection_handler.open_channel.</p><p>If you’re interested in reading more about binary pattern matching I think <a href="http://www.zohaib.me/binary-pattern-matching-in-elixir/">this</a> article does a good job explaining it.<br><br>The type @direct_tcpip is defined as a module attribute, remember since we are calling an erlang application it needs to be represented as a charlist instead of a string. @max_window_size is set to the 32k as specified in <a href="https://www.ietf.org/rfc/rfc4253.txt">rfc4253</a></p><blockquote>6.1. Maximum Packet Length</blockquote><blockquote>All implementations MUST be able to process packets with an<br> uncompressed payload length of 32768 bytes or less and a total packet<br> size of 35000 bytes or less (including ‘packet_length’,<br> ‘padding_length’, ‘payload’, ‘random padding’, and ‘mac’).<br>….</blockquote><p>@ini_window_size is trickier since I don’t know the impact of setting a value which is too low (or too high). We’ll set it to 105kb since I’m pretty sure I’ve seen it set to 1024 * 1024 somewhere so we just go with that.</p><p>If our call to:ssh_connection_handler.open_channel/6 is successful we’ll receive a {:open, channel} . We’ll use the channel and the connection pid to call :ssh_connection.send/3 and send data to our forwarded ip. Let’s try it out by sending a raw HTTP message:</p><pre>iex(1)&gt;  {:ok, pid} = SSHt.connect(host: &quot;192.168.90.15&quot;, user: &quot;ubuntu&quot;, password: &quot;&quot;)<br>{:ok, #PID&lt;0.166.0&gt;}<br>iex(2)&gt; data = &quot;GET / HTTP/1.1\r\nHost: localhost:8080\r\nUser-Agent: ssht/0.0.1\r\nAccept: */*\r\n\r\n&quot;<br>iex(3)&gt;  {:open, ch} = SSHt.direct_tcpip(pid, {&quot;127.0.0.1&quot;, 8080}, {&quot;192.168.90.15&quot;, 80})<br>{:open, 0}<br>iex(4)&gt; :ssh_connection.send(pid, ch, data)<br># handle the data returned on the connection with a receive block <br>iex(4)&gt; receive do<br>...(4)&gt; {:ssh_cm, _, {:data, ^ch, _, data}} -&gt; IO.puts(&quot;#{data}&quot;)<br>...(4)&gt; end<br>end</pre><p>We can receive messages using a receive block or by creating the channel inside a process ( GenServer for instance) and receive it using handle_info/2 callback:</p><pre>def handle_info({:ssh_cm, _, {:data, _ch, _, data}}, state) do<br>  IO.puts(&quot;Received data #{length(data)}<br>  {:noreply, state}<br>end</pre><p><strong>NOTE!</strong> I should say that it is required to have something actually responding on the forwarded ip otherwise it will fail. I have a VM setup with a private ip 192.168.90.15 with nginx running on port 80.</p><p>So far we’ve achieved</p><ul><li>Connecting to a ssh server</li><li>Creating a forwarded directtcp-ip channel which we can read &amp; write to</li></ul><p>What we don’t have is an actual TCP server to relay traffic from our host to the ssh server.</p><h3>A TCP Server</h3><p>For this I’m using <a href="https://github.com/ninenines/ranch">ranch</a>, add ranch to the deps:</p><pre>defp deps do<br>  [<br>    {:ranch, &quot;~&gt; 1.4&quot;}<br>  ]<br>end</pre><p>For this part we are going to do the following:</p><ul><li>On demand TCP-servers</li><li>Relay traffic from a TCP client to forwarded ssh channel and back</li><li>Allow connecting using ip:port and a unix domain socket</li></ul><h4>The supervisor</h4><p>The TCP servers need to be started on demand for this we’ll use DynamicSupervisor which is introduced in elixir 1.5 and deprecates the :simple_one_for_one strategy.</p><p>A cool feature unrelated to any of this is that DynamicSupervisorallows you to have different kinds of workers, since the only requirement is that you provided a valid <a href="https://hexdocs.pm/elixir/Supervisor.html#module-child_spec-1">child_spec</a> when starting child processes.</p><p>We’ll define a lib/ssht/application.ex and put the following in it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ecb364264a638602cc05e35bf16bdc4/href">https://medium.com/media/7ecb364264a638602cc05e35bf16bdc4/href</a></iframe><p>Here we set that we want to use the dynamic supervisor and give it a name. The strategy needs to be set to :one_for_one since it is the only supervison strategy supported by DynamicSupervisor. Notable is that since DynamicSupervisor implements the child_spec/1 function we can use the tuple shorthand. For more see the <a href="https://hexdocs.pm/elixir/Supervisor.html#module-child_spec-1">Supervisor docs</a>.</p><p>Children can be started using:</p><pre>DynamicSupervisor.start_child(SSHt.TunnelSupervisor, {MyGenServer, []})</pre><p>First argument is the supervisor name, second is a child_spec. As long as the module implements a child_spec/1 it is safe to use the tuple shorthand. GenServer, Task, Supervisor all implements this so if you’re deriving your module from any of those you’re good to go. The second argument in the tuple are arguments to the child being started. In the example above we have no arguments so we specify an empty list.</p><p>We’ll define lib/ssht/tunnel.ex to use as our interface for creating TCP listeners.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e2baa586aea67cff0fca11bddd30d1c/href">https://medium.com/media/3e2baa586aea67cff0fca11bddd30d1c/href</a></iframe><p>start_link/2 accepts a ssh connection pid and a tuple which can either be {:tcpip, {local_port, {ip_addr, remote_port}} or {:local, socket_path} .</p><p>We use :ranch.child_spec in worker_spec/2 to create the specification for our TCP listener. worker_spec takes name, target from the options list and pattern matches on the target to determine the kind of listener we should use :tcpip for starting a port listener and :local for a domain socket listener.</p><p>We have yet to define the SSHt.Tunnel.TCPHandler which will be the process responsible for handling a TCP client connection. So let’s do that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88f16b6429a50b95c2680263c7ebf972/href">https://medium.com/media/88f16b6429a50b95c2680263c7ebf972/href</a></iframe><p>As you’ve probably already seen this looks a little bit different than usually. The reason for using :proc_lib.spawn_link is due to how GenServer.start_link works. start_link does not return until the init returns. Calling :ranch.accept_ack would cause a deadlock. We use :gen_server.enter_loop/3 to fallback to the normal GenServer execution loop after the initialization. (More <a href="https://ninenines.eu/docs/en/ranch/1.4/guide/protocols/">here</a>)</p><p>Time to receive TCP messages and send them onto the forwarded channel:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcb2e89fad23c430653a6c3718ccffcb/href">https://medium.com/media/bcb2e89fad23c430653a6c3718ccffcb/href</a></iframe><p>Whenever we get a tcp message we pattern match on {:tcp,_, data}and send data on to the ssh channel, when we receive a {:ssh_cm, _ {:data,_,_, data} message we send it on to the tcp socket. We also implement the terminate/2 callback to close the ssh channel when the client closes the socket, a EOF is received or for any unexpected behavior.</p><p>There’s a potential problem with this implementation however, since we need to differentiate between the connections there’s a channel created for every TCP connection. I’m not certain if it is a problem our not, initially I thought I would define a tunnel (channel) per TCP server, but as the channel is linked to the process creating it the TCP server would receive all ssh messages with no apparent way of telling messages apart. By doing it this way, we’re certain that the ssh messages received are destined to the correct TCP client.</p><h3>Conclusion / Final words</h3><p>I think it’s pretty cool that we are able to implement this in pure Elixir in about 200 lines. It’s just a testament to how incredible Erlang/OTP and Elixir really is!</p><p>The source in it’s entirety can be found <a href="https://github.com/drowzy/ssh_tunnel">here</a>.</p><p>Some caveats:</p><ul><li>The ssh pid should be monitored by each TCP server and trap it’s exit, since running a tunnel does not make much sense if the connection to the remote server is dead.</li><li>Defining a new channel for every connection might be sub-optimal</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7ad9d1af01a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick-tip: Elixir Distillery multi release]]></title>
            <link>https://medium.com/@Drowzy/quick-tip-elixir-distillery-multi-release-da6cb9f55ac1?source=rss-652e699f0327------2</link>
            <guid isPermaLink="false">https://medium.com/p/da6cb9f55ac1</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Simon Thörnqvist]]></dc:creator>
            <pubDate>Wed, 21 Feb 2018 08:05:04 GMT</pubDate>
            <atom:updated>2018-02-21T08:05:04.771Z</atom:updated>
            <content:encoded><![CDATA[<p>In the distillery rel/config.exs one can specify several releases. In my case I had an umbrella with two different applications with a shared dependency in the same umbrella, and wanted two different docker images.</p><pre>release :server do<br>  set version: &quot;0.1.0&quot;<br>  set applications: [<br>    :runtime_tools,<br>    receiver: :permanent,<br>    grpc_server: :permanent<br>  ]<br>end</pre><pre>release :client do<br>  set version: &quot;0.1.0&quot;<br>  set applications: [<br>    :runtime_tools,<br>    client: :permanent,<br>    receiver: :permanent<br>  ]<br>end</pre><p>The shared application receiveris included in both releases. You can specify how many releases as you like. If no default has been set, mix release will default to building the first one. <br>To specify the release to run use the name option: <br>mix release --name=client</p><p>To package the client &amp; server in different images we create a two Dockerfiles Dockerfile.client and Dockerfile.server in the root of the umbrella project.</p><pre># Dockerfile.client<br>FROM elixir:1.6-alpine</pre><pre>RUN apk add --no-cache git</pre><pre>WORKDIR /app</pre><pre>ADD mix.exs /app<br>ADD mix.lock /<br>RUN mix local.hex --force &amp;&amp; \<br>    mix local.rebar --force &amp;&amp; \<br>    mix deps.get &amp;&amp; \<br>    mix deps.compile</pre><pre>ARG MIX_ENV=prod</pre><pre>COPY . ./<br>RUN mix compile --env=prod &amp;&amp; \<br>    mix release --env=prod --name=client # `client` release </pre><pre>FROM alpine as client<br>RUN apk add --no-cache ncurses-libs libcrypto1.0 bash</pre><pre>WORKDIR /app</pre><pre>COPY --from=0 /app/_build/prod/ ./</pre><pre>EXPOSE 4000<br>ENV REPLACE_OS_VARS true</pre><pre>CMD [&quot;./rel/client/bin/client&quot;, &quot;foreground&quot;]</pre><pre># Dockerfile.server<br>FROM elixir:1.6-alpine</pre><pre>RUN apk add --no-cache git</pre><pre>WORKDIR /app</pre><pre>ADD mix.exs /app<br>ADD mix.lock /<br>RUN mix local.hex --force &amp;&amp; \<br>    mix local.rebar --force &amp;&amp; \<br>    mix deps.get &amp;&amp; \<br>    mix deps.compile</pre><pre>ARG MIX_ENV=prod</pre><pre>COPY . ./</pre><pre>RUN mix compile --env=prod &amp;&amp; \<br>    mix release --env=prod --name=server # `server` release</pre><pre>FROM alpine<br>RUN apk add --no-cache ncurses-libs libcrypto1.0 bash</pre><pre>WORKDIR /app</pre><pre>COPY --from=0 /app/_build/prod/ ./</pre><pre>EXPOSE 4400<br>ENV REPLACE_OS_VARS true</pre><pre>CMD [&quot;./rel/server/bin/server&quot;, &quot;foreground&quot;]</pre><p>Building each release is just a matter of docker build -t client:latest -f Dockerfile.client . or docker build -t server:latest -f Dockerfile.server .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da6cb9f55ac1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tramp in Spacemacs]]></title>
            <link>https://medium.com/@Drowzy/tramp-in-spacemacs-ef82b9e703ee?source=rss-652e699f0327------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef82b9e703ee</guid>
            <category><![CDATA[spacemacs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[emacs]]></category>
            <dc:creator><![CDATA[Simon Thörnqvist]]></dc:creator>
            <pubDate>Sat, 23 Sep 2017 11:29:44 GMT</pubDate>
            <atom:updated>2017-09-23T11:29:44.964Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>TRAMP (Transparent Remove Access, Multiple Protocols) is a package for editing remote files from within emacs.</h4><p>One of my most ‘wat’ moments using emacs is discovering the incredible tramp mode.</p><p>My previous workflow for ssh was open a shell, ssh to host, edit file or whatever close connection. A lot of times what a really want to do is just paste something which I have from an already open buffer into a remote file, or copy a line etc. It turns out this is really easy to do from emacs.</p><p>SPC f f will bring up the file find in Spacemacs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*TBEcHWj8Bsx854xj8d8khQ.png" /><figcaption>ssh emacs</figcaption></figure><p>Typing /ssh: will auto complete your hosts, if you haven’t added your public key to authorized_keyson the host machine you can use the user@&lt;ip_or_hostname&gt; syntax and spacemacs will prompt for the<br> password when connecting.</p><p>You can continue to auto complete your way to the file you want to edit, <br>/ssh:&lt;hostname&gt;:/path/to/file and emacs will open the file in a new buffer.</p><h3>Opening a shell</h3><p>If you rather get a open shell on the remote machine you can do cd /ssh:&lt;hostname&gt;:/path/to/dir from eshell and emacs will open a shell in the specified directory. Other commands such as lsworks equally well.</p><p>Using SPC f f within the eshell will give you the context of the remote host<br>so you can open remote files using find file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*RvW5KElMvdBZ3naQdVtCxA.png" /><figcaption>cd with eshell</figcaption></figure><h3>Editing as sudo</h3><p>If you need to edit a file as sudo: SPC f f and use the /su:: or /sudo:: syntax, emacs will prompt for your sudo password, select a file, edit, save. Very convenient!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*-iIFjSwoPW141SN5JIP0EA.png" /></figure><h3>Docker &amp; Vagrant</h3><p>The <a href="https://github.com/Silex/docker.el">docker.el</a> package provides tramp integration, in spacemacs it’s just a matter adding the docker layer to your .spacemacs config.</p><p>This enables you to type /docker:/&lt;container_id&gt; which will use docker exec to run commands within the container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*4kOaCExlz1_5X1mwZpZysQ.png" /></figure><p>The same goes for vagrant just use /vagrant:&lt;vm_name&gt;:/</p><h3>Learn more</h3><p>Tramp is very feature rich and I encourage you to check out the <a href="https://www.gnu.org/software/tramp/">manual</a> for more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef82b9e703ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>