November 25, 2015

jquery ajax error handling

$.ajax({
    url: 'http://10.2.3.4/api/user',
    type: 'GET',
    dataType: 'json',
    success: function() { alert("Success"); },
    error: function(jqXHR, textStatus, errorThrown) {
        alert('An error occurred');
        $('#result').html('<p>status code: '+jqXHR.status+'</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
        console.log('jqXHR:');
        console.log(jqXHR);
        console.log('textStatus:');
        console.log(textStatus);
        console.log('errorThrown:');
        console.log(errorThrown);
    },
});

PHP code to analyze an the section of the 8 empty boxes below and find out how many boxes (letters in the word) are there. In this case, it should be 8.



<?php
$filename="JYkXXDylGfWH.jpg";
$tmp_img=$filename;

if(preg_match('/[.](jpg)$/', $filename)) {
    $img = imagecreatefromjpeg($tmp_img);
} else if (preg_match('/[.](gif)$/', $filename)) {
    $img = imagecreatefromgif($tmp_img);
} else if (preg_match('/[.](png)$/', $filename)) {
    $img = imagecreatefrompng($tmp_img);
}

list($width, $height) = getimagesize($tmp_img);
$y=$height/2;

$sum0=1000;
$count=0;

for ($j = 0; $j < $width; $j++) {
    $x = $j; // Get X coords

    $rgb = imagecolorat($img, $x, $y); // Get pixel color
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    $sum=$r+$g+$b;
    printf("%d ",$sum);
    if ($sum0>80 && $sum<40){
        $count++;
        echo "Box $count\n";
    }
    if ($sum>80 || $sum<40){ //80 is light threshold, 40 is dard threshold
        $sum0=$sum;
    }
}

November 24, 2015

use runit to manage user space daemons

In debian/Ubuntu, install "runit".

then in /etc/service directory, create your user service directory. In this example, we use "nc" (netcat listen)

cd /etc/service; mkdir nc
now create a file called "run", with the following content:
#!/bin/sh
d=`date`;
DIR=$(dirname $(readlink -f "$0"))
name=$(basename "$DIR")
echo "$d service $name started" >> /var/log/runit.log
exec nc -l -p 8889

Only the first line and last line are must have. The mittle 4 lines are for logging purpose. 
Now "chmod +x run" 

Now nc should be running (automatically picked up by runsvdir which scans /etc/service directory for changes). list of commands:
sv status nc
sv stop nc (or sv down nc)
sv start nc (or sv up nc)
sv restart nc
sv reload nc (send HUP signal)

sv status /etc/service/* (check all service status)

touch a file "nc/down" to stop the auto-restart

create a file "nc/finish" with the following content:
#!/bin/sh
d=`date`;
DIR=$(dirname $(readlink -f "$0"))
name=$(basename "$DIR")
echo "$d service $name  stopped" >> /var/run/runit.log

This script is run every time nc exits.

Internally, 3 core executables: sv, runsv (the actual daemon monitor), and runsvdir (monitors the entire /etc/service directory)

bash get script directory

DIR=$(dirname "$(readlink -f "$0")")
echo $DIR

Openwrt iptables add NFQUEUE support

opkg install kmod-nfnetlink_3.10.49-1_ar71xx.ipk
opkg install kmod-nfnetlink-queue_3.10.49-1_ar71xx.ipk
opkg install kmod-ipt-nfqueue_3.10.49-1_ar71xx.ipk
opkg install iptables-mod-nfqueue_1.4.21-1_ar71xx.ipk
modprobe xt_NFQUEUE

modprobe nfnetlink_queue
(the last command automatically loads nfnetlink module)

Application program will need the following libraries:

libmnl_1.0.3-1_ar71xx.ipk
libnfnetlink_1.0.1-1_ar71xx.ipk

libnetfilter_queue.so.1 (libnetfilter_queue in openwrt 14.07 seems to be in the "old" package directory. You can build your own).



Then you can direct desired traffic to the user space's queue application using iptables:

 iptables -A OUTPUT -p TCP --dport 54321 -j NFQUEUE

Queue application has to be running. Otherwise, packet will stop flowing.

November 17, 2015

How to solve: automake is missing on your system

rm aclocal.m4
automake

and then do "./configure"

November 9, 2015

supervisord add new program

After adding new program in /etc/supervisord.conf, do:

sudo supervisorctl reread
sudo supervisorctl update

This will make it run

November 3, 2015

apache .htaccess pass URI to PHP

1. enable mod_rewrite : a2enmod rewrite
2. enable .htaccess by adding the following to the enabled site conf file:
        <Directory "/var/www/html">
        AllowOverride All
        </Directory>


 3. Create  .htaccess at /var/www/html with the following content
  Options +FollowSymLinks
  RewriteEngine On

  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteCond %{SCRIPT_FILENAME} !-f

  RewriteRule ^.*$ ./index.php