Thursday, October 20, 2016

Running Flask dev server using HTTPS

Create a self signed certificate

openssl genrsa -out foobar.key 2048
openssl req -new -key foobar.key -out foobar.csr
openssl x509 -req -days 365 -in foobar.csr -signkey foobar.key -out foobar.crt

if os.environ.get("ENV_TESTING") == "True":
    context = (os.environ.get("CERT_PATH"), os.environ.get("KEY_PATH"))
    app.run(host='0.0.0.0', port=9090, ssl_context=context, threaded=True)

Monday, April 4, 2016

Bash script to notify me when a web site is up

One of the systems we depend on is down, and I wanted to get notified when it gets back up. And because I am lazy (Isn't that the motive for everything?) I wanted to write a script that checks it for me and mail me when the system gets back up.

Here is the script content:

curl -s -o "/dev/null" $1
if [ $? -eq 0 ] ; then
        if [ ! -f /data/epricer/utils/notified.status ] ; then
                echo "$1 is now up!" | mail -s "Website is up" $2
                touch /data/epricer/utils/notified.status
        fi

fi

This script takes two params, the first is the URL to check, and the 2nd is the email to notify. The scripts also creates a file when the server gets up, then check for it again when sending the notification, to prevent it from spamming my mail with thousands of "It is up" mails. I only need the notification once.

Save that in a file let's call /data/utils/notifyMe.sh. And make it executable. 

Then setup crontab to run this file periodically (for me it is every 10 mins) by issuing 

crontab -e

and adding the text below to a new line of the file 

10,20,30,40,50,59 * * * * /data/utils/notifyMe.sh https://mysystem.com myemail@domain.com

Save crontab and viola, you will be notified when the system is up. This took around 5 mins, but it is handy. 

Wednesday, March 2, 2016

Use DD-WRT to Join two wireless networks using a wireless link in Repeater mode

So me and my neighbour wanted to share a network, we both have existing separate wireless networks with multiple devices configured to use our networks. The most straightforward way to do that is to run a cable between our apartments (drill a couple of walls, get a long cable and run it from his router to mine, configure my router to work as an access point only with DHCP disabled) but because we were too lazy to do that, we decided to do this wirelessly.

I did some research, and I stumbled upon DD-WRT. DD-WRT is an open source linux based firmware for wireless routers. Although it is an open source project, it became so popular and so powerful that router companies like Buffalo manufactured and shipped routers that runs DD-WRT out of the box.

DD-WRT has plenty of features that I wanted to use, like having OpenVPN server and client, advanced DDNS capabilities, static NAT, and advanced repeater capabilities which exactly what I need.

Here is what I needed to do:

Pick a router that can run DD-WRT: I ended up picking LinSys E1200v2 http://amzn.com/B00AAU54TW. The router is cheap (20$), fully supports DD-WRT, and it has a nice wireless range. Edit: I take the "Fully supports DD-WRT" part back, it does support it fully on paper, but DD-WRT builds vary, some builds are stable enough to run everything perfectly, and some other builds will make your wireless network like a bird in a storm! For me build 25974 worked perfectly. 

Install DD-WRT on the router: I followed the instructions in the official DD-WRT wiki and install the same initial build, and it worked like a charm http://www.dd-wrt.com/wiki/index.php/Linksys_E1200v2. After I was done with the instructions I flashed this bigger more stable build 
ftp://ftp.dd-wrt.com/betas/2015/01-20-2015-r25974/broadcom_K26/dd-wrt.v24-25974_NEWD-2_K2.6_big-nv64k.bin. 
Important: when flashing dd-wrt builds, DO NOT SELECT TO RESET SETTINGS WHILE FLASHING. THIS WILL STOP THE ETHERNET PORTS FROM RUNNING. 

Configure the router: This is the fun part, DD-WRT has two different repeater modes: 
  • Repeater bridged mode: repeats a wireless signal of a primary router while bridging the two networks together. This mode must disable DHCP on the secondary router and use DHCP only on the primary router. Basically making the two networks a single network with a Wifi signal booster installed on the network. In this mode devices on the primary network and devices on the secondary network can access each other directly. 
  • Repeater mode: repeats a wireless signal of a primary router while keeping DHCP on the secondary router working and assigning IPs to its devices. In this mode the secondary router acts as a node in the primary router's network and uses NAT to bridge devices connected to it. In this mode devices on the secondary network can access devices on the primary networks directly, while devices on the primary network can only access devices on the secondary networks using port forwarding, and this is OK in my case so I picked this mode. 
Edit: Unfortunately after using DD-WRT for about 2 months. I have encountered instability issues, the router would unexpectedly disconnect all wireless clients and hang. I had to restart it to be able to connect again, this happend like twice daily. I ended up switching to Shibby Tomato, and it has been solid for almost a week now. I will write a follow up blog on Tomato. 

Friday, January 29, 2016

Deploying Android applications to a device without a USB cable (Hands-free development)

I just answered a question on stackoverflow that I think worth a blog. The asker is developing an Ionic app that uses a connected USB printer, and he was testing his app dong the following

1- Connect the device to his computer using USB
2- Deploy the app to the mobile
3- Disconnect the USB cable form the device
4- Connect the printer cable to the device
5- Test the app.

This sounded much to him and to me -as a lazy developer- but luckily I remembered hearing about adb over Wi-fi that allows deploying the apk file over Wifi network, and I wondered why would not this work with Ioinc, and it turns out it did work. Here is how the first time setup is done:

  1. Connect your android device to your computer using a USB cable
  2. From a command line run adb tcpip 1234 // or any port of your chosing
  3. disconnect your android USB cable
  4. run adb connect <your-phone-ip>:1234
  5. Connect your printer using the USB cable
After that, he can simply test his app by doing  ionic run android to deploy your app and enjoy a cable-switching free development experience. 

Sunday, November 29, 2015

Favourite quotes

JOB: Just Over Broke
TV is a great servant, but a terrible master - Brian Tracy
Fake it till you make it
Birds of a feather flock together
the first hour is the rudder of the day

Sunday, November 1, 2015

Using Bootstrap navbar in AngularJS without adding bootstrap scripts

I wanted to create a web app in AngularJS that uses Bootstrap navbar styles as its navigation menu without adding bootstrap scripts to the application. I like how bootstrap navbar looks:



Here is how I did it:

  • Instead of downloading the whole bootstrap bundle from their web site, I downloaded the css only. There is a bower/npm package that deploys the css files only, so I simply did: bower install bootstrap-css-only --save
  • I included the bootstrap.css file in my main html page:  <link rel="stylesheet" href="bower_components/bootstrap-css-only/css/bootstrap.css">
  • I used the navbar in the bootstrap starter code here as my starting point: https://getbootstrap.com/examples/starter-template/
  • The starter navbar code will work great with Angular (it is css after all) except for two things:
    • The auto collapse problem: Bootstrap navbar auto collapses when the screen width is lower than 768 pixels to look like below. This won't work without the bootstrap scripts, but I figured that its logic is not that complex to be implemented in angular.To fix the auto collapse in the "Angular" way, here is what I did:
      • Added the attributes below to the collapse button ng-init="isCollapsed = true" ng-click="isCollapsed = !isCollapsed"
      • Added the attribute below to the main menu div ng-class="{collapse: isCollapsed}"
    • The drop down problem: Drop down menus in the navbar do not work without the JS. I did not attempt to fix this (maybe because I do not need drop downs in my current app), but I will try to fix it. 

Monday, October 26, 2015

Javascript Hoisting

JavaScript has a unique (and sometimes confusing) feature called Hoisting. Before explaining it let's take a quick quiz, what will be printed from the code below?



var x = 1;

function printX(){
  console.log(x);
  var x = 2;
};

printX();



Well, you are thinking "This should print 1, but it looks like a trick, so I am going to say it will print 2". And you are not alone, this is a common Javascript pitfall. 

To know the answer to this quiz, we need first to understand how javaScript executes code. When JavaScript starts to execute code it does so in two passes, the first pass it scans for all variable declarations and declare them with an undefined value. The second pass it executes the code (and this includes variable assignment). So for JavaScript engine the code above looks like this:



var x = 1;

function printX(){
  var x;
  console.log(x);
  x = 2;
};

printX();



JavaScript considers the variable x inside the function scope as a totally different one from the x defined globally. Which means when x is printed, the value will be undefined.