Microsocks on NetBSD

2024/10/28

Have you ever wanted a proxy server that you can run on a toaster (literally)? Well, enter today’s setup, Microsocks on NetBSD. Microsocks is a simple, fast and efficient SOCKS5 server. NetBSD is a highly portable UNIX system that you can literally run on a toaster.

Prerequisites

Getting microsocks

Microsocks is not in the NetBSD repositories as far as i can tell, so we’ll just compile it.

git clone https://github.com/rofl0r/microsocks
cd microsocks
make
make install

And now we have installed Microsocks. Yay! You could just run it in a screen or something, but that’s not very practical and actually pretty lame, so we’ll setup an rc script.

Setting up the rc script

Firstly, open /etc/rc.d/microsocks with your preferred editor (I use Vim BTW) and add the script below

#!/bin/sh
#
# PROVIDE: microsocks
# REQUIRE: DAEMON

. /etc/rc.subr

name="microsocks"
rcvar=$name
command="/usr/local/bin/microsocks"
pidfile="/var/run/${name}.pid"
start_cmd="microsocks_start"
stop_cmd="microsocks_stop"

microsocks_start() {
    if [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>/dev/null; then
        echo "$name is already running."
    else
        echo "Starting $name..."
        ${command} &
        echo $! > "$pidfile"
        echo "$name started with PID $(cat "$pidfile")."
    fi
}

microsocks_stop() {
    if [ -f "$pidfile" ]; then
        echo "Stopping $name..."
        kill -TERM $(cat "$pidfile") && rm -f "$pidfile"
        echo "$name stopped."
    else
        echo "$name is not running."
    fi
}

load_rc_config $name
run_rc_command "$1"

Direct your attention towards line 10. There we have the command that starts Microsocks. It doesn’t necesarrily need arguments, as such, we can leave it as such, but that’s not reccomended. Consult this page for more information on what arguments you can use.

Now to test everything is working, you can use this command:

service microsocks onestart

If you’re able to connect to your proxy, you’ve configured it properly. We can now enable it in /etc/rc.conf, where we’ll append:

microsocks=YES

And now we can restart and you’ll see that our proxy is still alive.

Conclusion

NetBSD is pretty cool.