Ryan Harrison My blog, portfolio and technology related ramblings

Brave Browser for Android

Excessive ads and tracker scripts are a pain when on desktop, let alone mobile browsers with limited network connectivity. Nothing worse than loading a couple megabytes of Javascript on a ‘modern’ website, along with a bunch of tracking scripts, ads and an autoplaying video just for good measure. Not only does it now take ages to load a simple site - even on a reasonable 3g/4g connection - but it starts to eat through your data plan pretty quickly.

On desktop browsers this is most commonly solved by installing blocking extensions such as uBlock Origin, Adblock Plus, Ghostery etc. Unfortunately on mobile devices however, the app are much more limited and most don’t have support for installing custom extensions or addons. N.B - ok, yes the Firefox mobile app does allow this, and yes I have tried it. Even with just one extension installed however, the app seems too sluggish and laggy - especially when compared to the stock Chrome experience on Android devices (I am running a Nexus 6P so CPU performance shouldn’t be a factor).

I have however found what seems to be a good middle ground that gives you the speed and responsiveness of Chrome whilst also blocking most ads and trackers - the Brave browser.

Brave browser screenshot

The browser is based on Chromium so you get the familiar interface and speed, but it will also block a ton of the major trackers and ads by default. The site has some stats on how the load times are improved, which are probably under ideal circumstances, but I can definitely tell that there is a good improvement when surfing the web on my phone. I also get the same feeling of horror when I use any other browser on my phone as compared with using a stock browser on desktop. It’s amazing how fast the web could be without all the excess crap we push through the wire (or air).

To set your default browser on Android source:

  1. Open Settings.
  2. Go to Apps.
  3. On the All tabs, look for your default browser and tap on it.
  4. Under Launch by Default, press the “Clear defaults” button, to reset default 1. browser.
  5. Then open a link, you are asked to select a browser, select Opera , select Always.

You can get the Brave browser through the Google Play Store/Apple App store. There is also a desktop version available although I haven’t tried it yet.

Read More

Sending Email with Python 3

A common task in any popular programming language is the ability to send emails to your users, be it as a password reset or as a contact page submission on a blog just like this one.

The below snippet can be used to accomplish this, just change the variables depending on your use case. It assumes that you have a local SMTP server running on the machine you are running the script on (Postfix for example), however you can also login to another service if you want to send via Gmail or Outlook etc.

sender = "sender@domain.co.uk"
sender_name = "Sender Name"
receiver = "receiver@example.co.uk"
receiver_name = "Receiver Name"
subject = "This is the subject line"

message_str = "This is the body of the message"

mime = """From: {0} <{1}>
To: {2} <{3}>
MIME-Version: 1.0
Content-type: text/plain
Subject: {4}
""".format(sender_name, sender, receiver_name, receiver, subject, message_str)

    smtpObj = smtplib.SMTP('localhost')
    smtpObj.sendmail(sender, receiver, mime)
    return "Successfully sent email"
except smtplib.SMTPException:
    return "Error: unable to send email"

You can also add additional properties to the MIME message for extra functionality:

  • Reply-To: Name <address> to specify who the receiver should send their replies to
  • Change the Content-type to text/html if you want to use HTML within your message body for styling

For more information take a look at the smptlib docs.

Read More

Apache - Redirect HTTP to HTTPS

With all the focus these days on cybersecurity and the push towards a more secure web, it’s more important than ever to ensure as many users as possible are using the HTTPS version of your site instead of the default HTTP version. Google search results will now rank secure versions better than their variations and some browsers are actively warning users if they are not browsing through HTTPS and there is a password input field on the page for example.

As such, having all of your standard HTTP traffic automatically redirect to their HTTPS equivalent can not only help reassure your visitors, but also potentially increase traffic as a whole. There really aren’t any excuses for not using HTTPS now when Let’s Encrypt is available.

Configuring redirects within your web server of choice is quite trivial. In this post I will focus on Apache, however I am sure the config is equally as straightforward if you are an Nginx user.

The first step is of course to make sure that you have a HTTPS version of your site available to the public. There are a thousand guides online about how to to this so I won’t cover it again here, but in short get a certificiate for your domain from Let’s Encrypt via certbot, create a new VirtualHost within Apache listening on port 443 and enable SSLEngine - pointing to your certificate. At this point your should have two versions of your site available - one through HTTP and one through HTTPS. At the end of this all of the HTTP traffic will be redirected the HTTPS virtual host.

Navigate to your main Apache config file which contains your HTTP virtual host definition. This is normally in /etc/apache2/sites-available and might be called something like 000-default.conf if you haven’t renamed it. If the first line of the file is <VirtualHost *:80> you are in the right place.

Open this file with your favourite text editor (you will need root privileges) and add the following, replacing “your-domain-name” with, of course, your domain name:

# Redirect to HTTPS
Redirect permanent / https://your-domain-name.com/

And that’s it. Run the command sudo service apache2 restart to pick up the changes and then test it out. Navigating to any of your pages through HTTP should be automatically redirected to HTTPS resulting in that nice green padlock in your browser.

Read More

Firefox - Disable autoplay of videos in background tabs

In Chrome, when you open a link in a new background tab (e.g middle click), any videos on that page (YouTube etc) will not start to play until you directly visit that tab (bringing it to the foreground). This is is something I make use of quite often, however this behaviour is not the default in Firefox. Thankfully though, due to the great configuration options in Firefox, this can easily be fixed:

  1. Type about:config in the search bar to open up all of the configuration options.
  2. Filter the results by entering autoplay into the search box.
  3. Toggle the preference media.block-autoplay-until-in-foreground to true.

Firefox configuration options

From then on, any videos will not begin until you bring that tab into the foreground. When media is being blocked from playing, a play icon will appear in the tab (similar to the mute button for audio). You can click on this to begin playing without bringing the tab to the foreground.

Read More

WSL 'Bash Here' Context Menu Item

Similarly to how you can hold shift whilst you right click on a directory within Windows explorer to open a new command prompt at that directory, it is also possible to do the same with the new WSL Bash. This can make it much easier to work with files within explorer from within the WSL as opposed to lengthy cd commands to the relevant mnt.

WSL Explorer Context Menu

To add the new context menu item for Bash within explorer, you need to create a new key within the registry and a few new values underneath it:

  1. Open the registry editor by searching for regedit or going through Win+R. You will need admin privileges to open the editor.
  2. (Optional) Backup your registry via File->Export.
  3. Navigate to the key HKEY_CLASSES_ROOT\Directory\Background\shell.
  4. Create a new key called WSL under shell and and another key underneath WSL called command. This should reflect the structure in the cmd key within shell. The newly created structure should be HKEY_CLASSES_ROOT\Directory\Background\shell\WSL\command.
  5. Change the Default value under the WSL key to the value you want to see in the context menu e.g. ‘Bash Prompt Here’.
  6. (Optional) Create a new String value called Icon under the WSL key with a value of %USERPROFILE%\\AppData\\Local\\lxss\\bash.ico. This will make the bash icon appear in the context menu as seen in the screenshot above.
  7. Change the Default value under the command key to cmd.exe /c pushd "%V" && bash.exe.
  8. Exit the editor and test by right clicking the white space within any directory in Windows Explorer.

You should something corresponding to the screenshot. Selecting the new menu item will open up a new WSL Bash prompt starting at the current directory.

Here is the contents of a .reg file you can also use instead of the above. Simply copy the contents into a file with the .reg extension and then double click on it to merge it into your registry.

Windows Registry Editor Version 5.00

@="Bash Prompt Here"

@="cmd.exe /c pushd \"%V\" && bash.exe"
Read More