Cogs and Levers A blog full of technical stuff

SSH tunneling

SSH Tunneling is a technique that allows you to provide access to a network or service, through another access point. This is particularly useful when you aren’t afforded immediate access to the network or service that you’re trying to reach. Marshalling this traffic through a network protocol that you can immediately access (and allowing that secondary point to on-forward your network requests) you can achieve the access that you require.

From the wikipedia article:

In computer networks, a tunneling protocol allows a network user to access or provide a network service that the underlying network does not support or provide directly.

In today’s article, I’ll demonstrate a basic setup for tunneling to different services.

The basic format that the command takes that you’ll use, will look like this:

ssh -f remote-user@remote-host -L local-port:remote-host:remote-port -N

-f tells the ssh command to drop into the background after its invocation. -L maps a local-port through the remote-host onto the remote-port. -N tells OpenSSH to not execute a command, remotely.

Examples of the local-port:remote-host:remote-port might look as follows.

A firewall that you’re implicitly connected through (as a result of being at Starbucks, or at a hotel) isn’t allowing you to connect to your home email server (on port 25). You setup a tunnel using 5000:my-email-host-at-home.com:25 and connect your email client to localhost on port 5000. Data is now encrypted through the tunnel and is marshalled over the requested port.

Your company doesn’t allow IRC traffic through it’s firewall. You use 9000:irc.server.com:6667 to get around these restrictions; sending your chat data encrypted through the firewall.

Type Families language pragma

The Type Families language pragma provides the developer the ability to attribute an association between two different types. This is going to allow us to write the same function for different types.

In this example, the type class Falsey is somewhat of a loose boolean test on every day values . . not just Bool values. By allowing the developer to specify type within the class and instance we establish the association between the types:

{-# LANGUAGE TypeFamilies #-}

class Falsey a where
  type Value a
  isFalsey :: a -> Bool

instance Falsey [a] where
  type Value [a] = a
  isFalsey [] = True
  isFalsey _  = False

instance Falsey Bool where
  type Value Bool = Bool
  isFalsey x  = x

main :: IO ()
main = do
  print $ isFalsey []
  print $ isFalsey [1, 2, 3]
  print $ isFalsey True
  print $ isFalsey False

OverloadedStrings Language Pragma

The OverloadedStrings language pragma can be enabled either by passing the -XOverloadedStrings switch to GHC or you can just add the following to the top of your Haskell source:

{-# LANGUAGE OverloadedStrings #-}

The OverloadedStrings language pragma changes the way that literal strings identify themselves, in a way that favours performance. [Char] is a rather cumbersome type to be used when dealing with something as primitive as a string.

Prelude> :t "Hello, world"
"Hello, world" :: [Char]
Prelude> :set -XOverloadedStrings
Prelude> :t "Hello, world"
"Hello, world" :: Data.String.IsString t => t

The literal string "Hello, world" now identifies as a call to the fromString function out of the IstString type class. You can define instances like so:

import GHC.Exts ( IsString(..) )

data Colour = Red | Green | Blue | Other String deriving Show

instance IsString Colour where
  fromString "Red" = Red
  fromString "Green" = Green
  fromString "Blue" = Blue 
  fromString xs = Other xs

Now we just cast our strings to our type, and the fromString functions are invoked for us:

Prelude GHC.Exts> "Red" :: Colour
Red
Prelude GHC.Exts> "Yellow" :: Colour
Other "Yellow"

Animation in Java

The abstract window toolkit provide the programmer with a great level of flexibility when creating user interfaces. Today’s blog post is going to go through the basic setup of a double-buffered animation loop implemented in Java, using AWT.

Settings

First off, we start by making some system settings; setting up to use OpenGL, etc:

static {
  System.setProperty("sun.java2d.trace", "timestamp,log,count");
  
  System.setProperty("sun.java2d.transaccel", "True");
  System.setProperty("sun.java2d.opengl", "True");
  
  System.setProperty("sun.java2d.d3d", "false"); //default on windows
  System.setProperty("sun.java2d.ddforcevram", "true");
}

The particulars of these flags can be found in the documentation. These flags,

  • Setup trace logging
  • Use hardware acceleration for translucency
  • Use OpenGL
  • Turn off Direct3D
  • Put images into vram

Canvas

We’ll draw to a Canvas and flip that onto our Frame. We need to configure the Canvas so that it’ll behave in a render-loop fashion, rather than responding to paint messages as it does normally.

We ignore these repaints using setIgnoreRepaint.

Now comes the double-buffer part. We create a BufferStrategy using createBufferStrategy. The strategy is what holds our graphics objects that we’ll render to.

this.createBufferStrategy(2);
strategy = this.getBufferStrategy();

Rendering

The pump for the application is the renderer. It’s pretty simple:

public void render() {
  // get the graphics object
  Graphics2D bkG = (Graphics2D) strategy.getDrawGraphics();

  // start with a black canvas
  bkG.setPaint(backgroundGradient);
  bkG.fillRect(0, 0, getWidth(), getHeight());

  // TODO: Here's where the render code goes

  // release the resources held by the background image
  bkG.dispose();

  // flip the back buffer now
  strategy.show();
  Toolkit.getDefaultToolkit().sync();
}

Get running

Here is a class that you can use to get running immediately.

Commit squashing

Git provides some excellent features when you need to amend your commit history before pushing your changes into a repository. In today’s article, I’ll run through a relativly simple use-case of the rebase command and how you can squash commits down.

This blog

I’ve recently changed text editors from Sublime back to vim. In the repository of this blog, I have a script that bootstraps a blog post with the title and date; and then opens my text editor ready for me to write.

Today I also patched a small bug where, when the file name is calculated from the title of the blog I specify - consecutive letters were being de-duplicated. Made it a real pain for a lot of words . . so, this changed. These two changes occured on the same file, new-post.sh; but I didn’t think about changing my text editor until after.

The first change

So, the first change went into the file. I staged the change and then committed it:

➜  tuttlem.github.io git:(master) ✗ git add .
➜  tuttlem.github.io git:(master) ✗ git commit -m "Patched create script"
[master 5b7b3b4] Patched create script
 1 file changed, 1 insertion(+), 1 deletion(-)

The second change

Then, thinking about it, I changed my text editor after.

➜  tuttlem.github.io git:(master) ✗ git add .
➜  tuttlem.github.io git:(master) ✗ git commit -m "Changed editor"
[master 7408fdf] Changed editor
 1 file changed, 1 insertion(+), 1 deletion(-)

This sucks. Really only wanted this in the one commit. So, I kicked off rebase in interactive mode (with -i).

➜  tuttlem.github.io git:(master) git rebase -i origin/master

This opened up a text editor for me, allowing me to make changes.

Changing history!

You’re presented with a list of commits that haven’t yet been pushed to the server. You’re also given the opportunity to change what goes on with these before you push:

pick 50412a2 New article
pick 3029ddc New article
pick e8df37a New article
pick ef5028c New article
pick e151644 New article
pick 378fc34 New article
pick eaccbd8 New article
pick 05c2d82 New article
pick d60be6a New articles
pick 5b7b3b4 Patched create script
pick 7408fdf Changed editor

# Rebase 5c2c93d..7408fdf onto 5c2c93d (11 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

I just want 7408fdf squashed into 5b7b3b4. I also want to change the commit message of 5b7b3b4. Afterwards, I’d learned that git will give me the opportunity to re-word this to combine commit messages.

pick 50412a2 New article
pick 3029ddc New article
pick e8df37a New article
pick ef5028c New article
pick e151644 New article
pick 378fc34 New article
pick eaccbd8 New article
pick 05c2d82 New article
pick d60be6a New articles
reword 5b7b3b4 Patched create script and changed editor
squash 7408fdf Changed editor

Saving this file off, we’re now presented with another text editor that allows us to update the commit message accordingly. After a bit of processing, we’re done:

➜  tuttlem.github.io git:(master) git rebase -i origin/master
[detached HEAD f0b825a] Patched create script
 Date: Wed Mar 8 11:54:00 2017 +1000
 1 file changed, 1 insertion(+), 1 deletion(-)
[detached HEAD 7bc6902] Patched create script
 Date: Wed Mar 8 11:54:00 2017 +1000
 1 file changed, 2 insertions(+), 2 deletions(-)
Successfully rebased and updated refs/heads/master.

Your git log should now confirm that everything has gone to plan:

commit 7bc6902d2543eb87dad14b088edc9a5506295809
Author: Michael Tuttle <tuttlem@gmail.com>
Date:   Wed Mar 8 11:54:00 2017 +1000

  Patched create script

  Changed editor

  Patched create script and changed editor

commit d60be6ae1acc508669193a5bfead887913284aef
Author: Michael Tuttle <tuttlem@gmail.com>
Date:   Fri Mar 3 20:04:06 2017 +1000

  New articles

Push to the server.