Starbeamrainbowlabs

Stardust
Blog


Archive

Mailing List Articles Atom Feed Comments Atom Feed Twitter Reddit Facebook

Tag Cloud

3d account algorithms announcement architecture archives arduino artificial intelligence artix assembly async audio bash batch blog bookmarklet booting c sharp c++ challenge chrome os code codepen coding conundrums coding conundrums evolved command line compilers compiling css dailyprogrammer debugging demystification distributed computing downtime electronics email embedded systems encryption es6 features event experiment external first impressions future game github github gist graphics hardware hardware meetup holiday html html5 html5 canvas infrastructure interfaces internet io.js jabber javascript js bin labs learning library linux low level lua maintenance manjaro network networking node.js operating systems performance photos php pixelbot portable privacy programming problems project projects prolog protocol protocols pseudo 3d python reddit reference release releases resource review rust secrets security series list server software sorting source code control statistics svg technical terminal textures three thing game three.js tool tutorial tutorials twitter ubuntu university update updates upgrade version control virtual reality virtualisation visual web website windows windows 10 xmpp xslt

Splitting your C♯ Code into Multiple Files

I have just started to work out how to split my C♯ code into multiple files, and thought that I would share it with you. This post will be about what I believe to be static linking, but I could be wrong. Anyway, it is actually quite simple:

Here is the contents of filea.cs:

using System;

class ClassA
{
    public static void Main()
    {
        Console.WriteLine("This is a test from file A");
        Someplace.ClassB.PrintHello();
    }
}

and here is the contents of fileb.cs:

using System;

namespace Someplace
{
    class ClassB
    {
        public static void PrintHello()
        {
            Console.WriteLine("Another hello from file B!");
        }
    }
}

Then when you compile, you should do something like this:

csc filea.cs fileb.cs

This will tell the C Sharp compiler to grab both filea.cs and fileb.cs, and to output a single filea.exe.

Next I will try to figure out how to create a .dll file and include that - then I can build my own libraries.

Securing a Linux Server Part 1: Firewall

Welcome to a new tutorial series, where I will show you what I have learnt so far about making sure that your linux server (and desktop too!) are secure so that nobody can get in (easily) and assume control.

Disclaimer: This tutorial series will not cover everything, and should not be taken to. There probably will be some mistakes in this post too. Check other guides online or consult a professional to make sure that your machine is secure. Please suggest improvements or point out mistakes in the comments.

To start this tutorial session off, I will talk about firewalls. Firewalls control how data is allowed to travel in and out of your computer. In Ubuntu, a firewall called ufw, the 'uncomplicated firewall' is already present. It acts as a nice frontend to iptables, which I find to be difficult to understand and use. We will be using that as our firewall.

I have done an asciinema recording on a virtual machine of this whole process:

Enabling the firewall

Ufw by default allows all outgoing connections and denys all incoming connections. This means that if you are using ssh to connect to your server, you will need to open the appropriate ports first before enabling ufw. Do that like this:

~$ sudo ufw allow 22/tcp

Ufw will automatically configure iptables to allow incoming connections on port 22 that use tcp. I will talk more about allowing and denying different connections later.

Just in case ufw blocks your ssh connection and you are unable to get back in, you can use another program called at to schedule the disabling of the ufw so that you can get back in again. If you don't have it installed, you can install it with sudo apt-get install at.

~$ sudo at -vM now +10 minutes
ufw disable
^D

Where ^D stands for CTRL + D. Now that you have it set such that ufw will disable itself in 10 minutes time, we go ahead and turn ufw on:

~$ sudo ufw enable

It will warn you that this may disrupt any existing ssh connections you have open. Reply yes to this. Once it have been enabled successfully, you should check that you can still ssh into your server (if that is the method that you are using to control it). If yes, great! If not, ufw will disable itself in 10 minutes and then you can try again.

Now that we have ufw enabled, we can cancel the at job we created to disable ufw. Type sudo atq to list the jobs you have schedules, and sudo atrm <number> to remove it, where <number> is the number of the jobs that you want to delete.

You may also want to cheeck the status of ufw to make sure that it is enabled, or to get a list of the rules that are currently in force. You can do that like this:

~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
80/tcp                     ALLOW       Anywhere
80/tcp (v6)                ALLOW       Anywhere

Allowing connections

Allowing connections through the firewall is easy. Simply type something like this:

~$ sudo ufw allow 80/tcp

Ufw will automatically configure iptables, in this example, to allow all connections on port 80 that use tcp. It will also configure it appropriately for both ipv4 and ipv6. Replace 80 with the port number you want to allow, and tcp with udp if needed. Ufw also understands several protocol names, and can configure itself accordingly:

~$ sudo ufw allow http
~$ sudo ufw allow imap

Denying connections

Denying all connections on a given port is very similar., Simply type something like this:

~$ sudo ufw deny 4722/tcp

The above would deny all tcp connections on port 4722.

You can also prevent a particular ip from gaining access to your server:

~$ sudo ufw deny from 123.123.123.123

The above would block all packets from the ip address 123.123.123.123. It works with IPv6 addresses too:

~$ sudo ufw deny from 2607:f8b0:4003:c05::65

The above would block all packets from the ip address 2607:f8b0:4003:c05::65, which just happens to belong to Google.

Port Ranges

You can open a range of ports with a colon:

~$ sudo ufw allow 60000:61000/udp

The above will allow udp connections on any port in the range 60,000 - 61,000 (the ports used for mosh).

Deleting Rules

Deleting rules can be done like this:

~$ sudo ufw delete allow 4724/tcp

The above would delete the rule(s) allowing tcp connections on port 4724.

Summary

In this post, I have shown you how to activate and configure a simple firewall that is bundled with Ubuntu. Next time, I will talk about securing you ssh daemon.

If you spotted a mistake in this post, have a suggestion, or are having trouble following along, please leave a comment below.

Other useful posts

These posts helped me to understand and use the uncomplicated firewall:

Dailyprogammer Challenge #199 - Bank Numbers Pt 1

I have attempted another dailyprogammer challenge on reddit.

This time, the challenge is to take a number in and print a 'banner' representing that number, like this:

Input: 47262

Output:
    _  _  _  _
|_|  | _||_  _|
  |  ||_ |_||_

Here is my solution:

using System;

public class BigDigits
{
    static string[,] bannerTemplates = new string[,]{
        { " _ ", "| |", "|_|" },
        { "   ", "  |", "  |" },
        { " _ ", " _|", "|_ " },
        { " _ ", " _|", " _|" },
        { "   ", "|_|", "  |" },
        { " _ ", "|_ ", " _|" },
        { " _ ", "|_ ", "|_|" },
        { " _ ", "  |", "  |" },
        { " _ ", "|_|", "|_|" },
        { " _ ", "|_|", " _|" }
    };

    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("This program converts a number to a banner.");
            Console.WriteLine("\nUse it like this: ");
            Console.WriteLine("    bigintbanners.exe <number>");
            Console.WriteLine("\n<number>: The number you want to convert.");
            return;
        }

        char[] intChars = args[0].ToCharArray();
        string[] resultLines = new string[3];
        int currentDigit = 0;

        int i = 0;

        for(i = 0; i < intChars.Length; i++)
        {
            currentDigit = int.Parse(intChars[i].ToString());
            for (int j = 0; j < 3; j++)
            {
                resultLines[j] += bannerTemplates[currentDigit,j];
            }
        }

        for(i = 0; i < resultLines.Length; i++)
        {
            Console.WriteLine(resultLines[i]);
        }
    }
}

I find the dailyprogrammer challenges to be a great way to practice a language that you are learning.

32 bit binary, SHA1: 0fc2483dacf151b162e22b3f9b4c5c64e6fe5bdf

Reddit post link

Ask below if you need a different binary (e.g. 64 bit, ARM, etc)

Stardust Update: Email Notifications

Welcome to another blog update.

Yesterday I added email notifications to the blog comments. You may have noticed that there is an extra optional field for you to put your email address in when commenting. If you do so, anybody who replies to your comment will trigger an email notification which will go to the email address that you specified.

Currently the only method I have for you to unsubscribe from these emails is to email emails at starbeamrainbowlabs dot com, and I will process your request manually. Make sure that you include a copy of the email that you don't want anymore, since it contains information about the comment that you posted that I will have to edit. In the future I hope to have an automated system that you will be able to use.

If you notice any issues with this, please leave a comment below.

The other thing that I added is email notifications for myself. Now, every time somebody comments on my blog I will receive an email notification telling me about it. This is mainly because I forgot to check for new comments - you should receive a reply to your comment much faster now :)

Finding Favicons with PHP

There hasn't been a post here for a little while because I have been ill. I am back now though :)

While writing more Bloworm, I needed a function that would automatically detect the url of the favicon that is associated with a given url. I wrote a quick function to do this a while ago - and have been improving it little by little.

I now have it at a point where it finds the correct url 99% of the time, so I thought that I would share it with you.

/*
 * @summary Given a url, this function will attempt to find it's correspending favicon.
 *
 * @returns The url of the corresponding favicon.
 */
function auto_find_favicon_url($url)
{
    if(!validate_url($url))
        senderror(new api_error(400, 520, "The url you specified for the favicon was invalid."));

    // todo protect against downloading large files
    // todo send HEAD request instead of GET request
    try {
        $headers = get_headers($url, true);
    } catch (Exception $e) {
        senderror(new api_error(502, 710, "Failed to fetch the headers from url: $url"));
    }
    $headers = array_change_key_case($headers);

    $urlparts = [];
    preg_match("/^([a-z]+)\:(?:\/\/)?([^\/?#]+)(.*)/i", $url, $urlparts);

    $content_type = $headers["content-type"];
    if(!is_string($content_type)) // account for arrays of content types
        $content_type = $content_type[0];

    $faviconurl = "images/favicon-default.png";
    if(strpos($content_type, "text/html") !== false)
    {
        try {
            $html = file_get_contents($url);
        } catch (Exception $e) {
            senderror(new api_error(502, 711, "Failed to fetch url: $url"));
        }
        $matches = [];
        if(preg_match("/rel=\"shortcut(?: icon)?\" (?:href=[\'\"]([^\'\"]+)[\'\"])/i", $html, $matches) === 1)
        {
            $faviconurl = $matches[1];
            // make sure that the favicon url is absolute
            if(preg_match("/^[a-z]+\:(?:\/\/)?/i", $faviconurl) === 0)
            {
                // the url is not absolute, make it absolute
                $basepath = dirname($urlparts[3]);

                // the path should not include the basepath if the favicon url begins with a slash
                if(substr($faviconurl, 0, 1) === "/")
                {
                    $faviconurl = "$urlparts[1]://$urlparts[2]$faviconurl";
                }
                else
                {
                    $faviconurl = "$urlparts[1]://$urlparts[2]$basepath/$faviconurl";
                }
            }
        }
    }

    if($faviconurl == "images/favicon-default.png")
    {
        // we have not found the url of the favicon yet, parse the url
        // todo guard against invalid urls

        $faviconurl = "$urlparts[1]://$urlparts[2]/favicon.ico";
        $faviconurl = follow_redirects($faviconurl);
        $favheaders = get_headers($faviconurl, true);
        $favheaders = array_change_key_case($favheaders);

        if(preg_match("/2\d{3}/i", $favheaders[0]) === 0)
            return $faviconurl;
    }

    return $faviconurl;
}

This code is pulled directly from the Bloworm source code - so you will need to edit it slightly to suit your needs. It is not perfect, and will probably will be updated from time to time.

Cool Resources: Subtle Patterns

An example of a pattern from subtlepatterns.com.

I have a different sort of post for you today: A post about pair of websites that I have found that are really quite useful. Quite often when I am creating a website or webpage for some reason or other (today I needed one for bloworm), I keep finding that it always looks a bit plain. These websites that I have found today help to partially solve that problem. They are full of simple tileable textures that look great as backgrounds in many different contexts.

The first is called subtlepatterns.com. It contains a bunch of free to use textures that go well in the background of something without standing out.

The second is called transparenttextures.com. It is similar to the website above - but the textures it provides are transparent so that you can overlay them on top of something else. These are good when you have a colour in mind, but want to add a little something to it to make it look a little less plain.

Dailyprogrammer challenge #197 - Validating ISBN Numbers

Hello again!

I now have a reddit account. You can find it here: https://www.reddit.com/user/starbeamrainbowlabs

I have attempted the latest Daily Programmer challenge.

This time I have written it in javascript. The challenge was to validate an ISBN-10 number. To validate an ISBN-10 number, you add 10 times the first number to 9 times the second number to 8 times the third number and so on. This total should leave no remainder when divided by 11. In addition, the letter X stands for a value of 10.

Here is my solution:

function validate_isbn(isbn) {
    var i = 10,
        tot = isbn.replace(/-/g, "").split("").reduce(function (total, char) {
            if (char.toLowerCase() == "x")
                total += i * 10;
            else
                total += i * parseInt(char);
            i--;
            return total;
        }, 0);

    if (tot % 11 === 0)
        return true;
    else
        return false;
}

I minified it by hand too:

function validate_isbn(a){var i = 10;if(a.replace(/-/g,"").split("").reduce(function(b, c){if(c.toLowerCase()=="x")b+=i*10;else b+=i* parseInt(c);i--;return b;},0)%11==0)return true;else return false;}

I should probably attempt the next challenge in C♯ so that I keep practising it.

The daily programmer challenge can be found here: Daily Programmer Challenge #197 - ISBN Validator

Sorting Algorithms 3 / 5: Insertion Sort

Hello again!

This is the 3rd of 5 sorting algorithms that I will be implementing and posting about.

Today I have a reverse insertion sorter for you.

An (reverse) insertion sort takes number one from the end of the array, and shuffles it along to the right until it is in the right place. Then it picks the next number along and does the same, until the whole array is sorted.

/// <summary>
/// Performs an insertion sort in the array.
/// </summary>
/// <param name="array">A reference to the array to sort.</param>
static void insertion_sort(ref int[] array)
{
    for (int i = array.Length - 2; i >= 0; i--)
    {
        int shp = i;
        //                                                        |
        //make sure that we don't fall off the end of the array V
        while(shp < array.Length - 1 && array[shp] > array[shp + 1]) 
        {
            swap_places(ref array, shp, shp + 1);
            shp++;
        }

        Console.Write("i: {0} ", i);
        print_array(ref array);
    }
}

Here is some example output:

[ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 14, 53, 1, 38, 47, 63, 68, 91, 93 ]
i: 23 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 14, 53, 1, 38, 47, 63, 68, 91, 93 ]
i: 22 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 14, 53, 1, 38, 47, 63, 68, 91, 93 ]
i: 21 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 14, 53, 1, 38, 47, 63, 68, 91, 93 ]
i: 20 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 14, 53, 1, 38, 47, 63, 68, 91, 93 ]
i: 19 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 14, 53, 1, 38, 47, 63, 68, 91, 93 ]
i: 18 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 14, 53, 1, 38, 47, 63, 68, 91, 93 ]
i: 17 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 14, 1, 38, 47, 53, 63, 68, 91, 93 ]
i: 16 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 67, 1, 14, 38, 47, 53, 63, 68, 91, 93 ]
i: 15 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 97, 1, 14, 38, 47, 53, 63, 67, 68, 91, 93 ]
i: 14 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 69, 1, 14, 38, 47, 53, 63, 67, 68, 91, 93, 97 ]
i: 13 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 96, 1, 14, 38, 47, 53, 63, 67, 68, 69, 91, 93, 97 ]
i: 12 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 92, 1, 14, 38, 47, 53, 63, 67, 68, 69, 91, 93, 96, 97 ]
i: 11 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 95, 1, 14, 38, 47, 53, 63, 67, 68, 69, 91, 92, 93, 96, 97 ]
i: 10 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 41, 1, 14, 38, 47, 53, 63, 67, 68, 69, 91, 92, 93, 95, 96, 97 ]
i: 9 [ 17, 10, 83, 67, 8, 50, 46, 63, 2, 1, 14, 38, 41, 47, 53, 63, 67, 68, 69, 91, 92, 93, 95, 96, 97 ]
i: 8 [ 17, 10, 83, 67, 8, 50, 46, 63, 1, 2, 14, 38, 41, 47, 53, 63, 67, 68, 69, 91, 92, 93, 95, 96, 97 ]
i: 7 [ 17, 10, 83, 67, 8, 50, 46, 1, 2, 14, 38, 41, 47, 53, 63, 63, 67, 68, 69, 91, 92, 93, 95, 96, 97 ]
i: 6 [ 17, 10, 83, 67, 8, 50, 1, 2, 14, 38, 41, 46, 47, 53, 63, 63, 67, 68, 69, 91, 92, 93, 95, 96, 97 ]
i: 5 [ 17, 10, 83, 67, 8, 1, 2, 14, 38, 41, 46, 47, 50, 53, 63, 63, 67, 68, 69, 91, 92, 93, 95, 96, 97 ]
i: 4 [ 17, 10, 83, 67, 1, 2, 8, 14, 38, 41, 46, 47, 50, 53, 63, 63, 67, 68, 69, 91, 92, 93, 95, 96, 97 ]
i: 3 [ 17, 10, 83, 1, 2, 8, 14, 38, 41, 46, 47, 50, 53, 63, 63, 67, 67, 68, 69, 91, 92, 93, 95, 96, 97 ]
i: 2 [ 17, 10, 1, 2, 8, 14, 38, 41, 46, 47, 50, 53, 63, 63, 67, 67, 68, 69, 83, 91, 92, 93, 95, 96, 97 ]
i: 1 [ 17, 1, 2, 8, 10, 14, 38, 41, 46, 47, 50, 53, 63, 63, 67, 67, 68, 69, 83, 91, 92, 93, 95, 96, 97 ]
i: 0 [ 1, 2, 8, 10, 14, 17, 38, 41, 46, 47, 50, 53, 63, 63, 67, 67, 68, 69, 83, 91, 92, 93, 95, 96, 97 ]

Full source code (pastebin)

Binary (MD5: 01b2d774c70ce46d8e0641da948bc25e, SHA1: 537ee665162887f629200d8839b108a3e4098d38)

Algorithms 2 / 5: Reverse Selection Sort

It has been a while since I have implemented a sorting algorithm - I should probably have implemented these a little bit earlier than I have done :)

Today I bring you the C sharp version of the selection sort I posted earlier. To mix it up a bit htough this implementation is in reverse.

  1. Find the largest number in the sequence
  2. If it is larger than the number at the end of the array, swap them
  3. Find the next largest number
  4. If it is larger than the next number along from the end of the array, swap them
  5. Repeat steps 3 and 4 until all the numbers have been sorted.

Here is the code:

/// <summary>
/// Performs a selection sort on an array of ints.
/// </summary>
/// <param name="array">The array to sort.</param>
static void selection_sort(ref int[] array)
{
    int limit = array.Length - 1;
    while(limit > 0)
    {
        //find the index with the maximum value
        int max_index = 0; //set the max to the first element in the array
        //don't search the first element in the array, we have already done that on the line above
        for(int i = limit - 1; i > 0; i--)
        {
            if(array[i] > array[max_index])
                max_index = i;
        }
        if(array[max_index] > array[limit])
        {
            //we have found an index with a high value than the current limit
            swap_places(ref array, max_index, limit);
        }

        limit--;
    }
}

Full Source Code (pastebin) Link to binary (MD5: ef5e0f15c9bc181d36b193160e3f8ad9 SHA1: ad49eb97675ca6ec9cc9dfebb63fbe03cfa27534)

Next up: Insertion sorting.

Twitter Account

I now have a twitter account!

You can find it here.

It will be mostly automated, once I rewrite the blog posting system. Until then I will post manually.

I plan to have the blog automatically tweet about new blog posts, but I will have to build some kind of system that will be able to tell the difference between new and updated posts first, since I make usually make several corrections to each my blog posts once I have posted them.

Art by Mythdael