February 24, 2016

dokuwiki remove register link on top page

lib/tpl/dokuwiki/tpl_header.php, search for "register" and comment out the line.

February 9, 2016

Simplifying your network with a bridge - Making an FIOs ActionTec MI424-WR a Network Bridge

http://www.hanselman.com/blog/SimplifyingYourNetworkWithABridgeMakingAnFIOsActionTecMI424WRANetworkBridge.aspx

Start your own DOCSIS lab

To setup a DOCSIS Lab you'll need:
- 1x CMTS (no it's not possible to use a Linux based PC convert into a Cable router.Ok? We close this forever or until a hardware manufacturer will sell PCIExpress Coax interfaces. BUT!!!!! If you know a manufacturer who do this, please share!!)
- 1x RG-6 2way Splitter (you have to combine US/DS RF signals into a single Coax cable, yes RF splitter can combine)
- 3x RF attenuator +20dB (you are in lab, so it's not a good idea to blow up your equipments radio)
- 1x 3Way RF splitter (optional, but if you want to test 3 CM simultaneously it's a good idea)
- 1x Return path filter (you are in LAB and have to combine the US and DS, so to clear the risk to have some RF signal harmonic on your DS, this filter is a good idea)
- 1x Linux Box (I've use Debian or use any distro of your choice and provide theses services: DHCP, TFTP, ToD, Syslog and DNS)
- 1x Switch L2 (to connect your Linux box, CMTS and Internet access. Use managable switch caused if you have to trouble shoot the L3 packets, wireshark and a port mirror will became your month employee)

Your connection desing, see: http://commons.wikimedia.org/wiki/File:HFC.jpg
You will have to replace the fiber devices by the coax/RF spliters. They do the same job's on different cable type.

Yes you will have to put money on the table and buy devices. You can found really good deal into the refurbished market but for sure it will not free.

If it's OK, you will continued with the RF plan, Cable modem DOCSIS standard, OID options, Cable modem TEK and required security, DHCP provisioning/relaying and routing.

It's really possible to do it. But always keep in mind that it's not easy, you will spend many hours without results, spend money in devices and materials but it's possible.

Source:
http://www.docsis.org/node/1686

January 23, 2016

.vimrc edti binary files in hex mode

Add the following to your ~/.vimrc file, and vim will be able to edit *.bin, *.exe, and *.o files in HEX mode:

if has ("autocmd")
" vim -b : edit binary using xxd-format!
augroup Binary
au BufReadPre  *.bin,*.exe,*.o let &binary=1
au BufReadPost * if &binary | %!xxd
au BufReadPost * so $VIMRUNTIME/syntax/xxd.vim | set filetype=xxd | endif
au BufWritePre * if &binary | %!xxd -r
au BufWritePre * endif
au BufWritePost * if &binary | %!xxd
au BufWritePost * set nomod | endif
augroup END
endif

January 14, 2016

lsyncd.conf file

settings = {
        delay        = 0.1,
        maxProcesses = 3,
        logfile      = "/tmp/lsyncd.log",
}

targetlist = {
        "192.168.5.203",
        "192.168.5.204"
}

for _, server in ipairs(targetlist) do
sync{
        default.rsyncssh,
        source="/home/me/mysyncdir",
        host=server,
        targetdir="mysyncdir"
}
end

Xbox one firewall ports

Here is what we actually need to make this work.
  protocol port direction
DNS UDP 53 outbound if you don’t have DNS services on your subnet
HTTP TCP 80 outbound
Kerberos UDP 88 inbound and outbound (yes, Xbox Live uses Kerberos for authentication.)
Xbox UDP 3074 inbound and outbound
Xbox TCP 3074 inbound and outbound
SIP UDP 5060-5061 inbound and outbound

January 13, 2016

travel agents to buy international tickets

飞翔旅游 SBP Travel 951-200-3308
909-614-4648
626-275-2811
619-209-7736
飞霖旅游 FeiLin Travel Inc 951-461-8723
951-200-0172
626-539-5608
佳友旅遊 Luckyer Travel 626-281-2568
Bravo Travel 626-571-1899
来来旅行社 Lai Lai Travel 626-286-6123
完美旅游 Perfect Trans & Travel Service 626-300-3888
800-341-7983          

December 31, 2015

php code to normalize US phone number

This is the power of regex
Source: http://stackoverflow.com/questions/4708248/formatting-phone-numbers-in-php

This is a US phone formatter that works on more versions of numbers than any of the current answers.
$numbers = explode("\n", '(111) 222-3333
((111) 222-3333
1112223333
111 222-3333
111-222-3333
(111)2223333
+11234567890
    1-8002353551
    123-456-7890   -Hello!
+1 - 1234567890 
');


foreach($numbers as $number)
{
    print preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
}

And here is a breakdown of the regex:
Cell: +1 999-(555 0001)

.*          zero or more of anything "Cell: +1 "
(\d{3})     three digits "999"
[^\d]{0,7}  zero or up to 7 of something not a digit "-("
(\d{3})     three digits "555"
[^\d]{0,7}  zero or up to 7 of something not a digit " "
(\d{4})     four digits "0001"
.*          zero or more of anything ")"
Updated: March 11, 2015 to use {0,7} instead of {,7}

December 15, 2015

Linux routing based on IPtables MARK

http://www.linuxhorizon.ro/iproute2.html

Backup:

This page is a small HOWTO about the advanced linux routing...

First of all let me tell you where you can find the best source of information about the advanced routing under Linux. Most of you probably know or heard about the Linux Advanced Routing & Traffic Control site. There you can see a very comprehensive source of knowledge based not only on documentation but by easy to understand examples...
Credits: Linux Advanced Routing & Traffic Control, Thea
Ok, then...
This page will show you how to set a linux box to use 2 different ISPs on the same time...

First example:
Goal: To route packets that came from 4 network to different ISPs

Let's presume that you have two ISPs. In the following examples I'll use RDS and ASTRAL (two large ISPs from my country)
For the ASCII art and lynx console browser fans I'll use this kind of chart:
                                                                   ________
                                           +-------------+        /
                                           |    ISP 1    |       /
                             +-------------+    (RDS)    +------+
                             |             | gw 10.1.1.1 |     /
                      +------+-------+     +-------------+    / 
+----------------+    |     eth1     |                       /
|                |    |              |                      |
| Local networks +----+ Linux router |                      |  Internet cloud
|                |    |              |                      |
+----------------+    |     eth2     |                       \
                      +------+-------+     +-------------+    \
                             |             |    ISP 2    |     \
                             +-------------+  (ASTRAL)   +------+
                                           | gw 10.8.8.1 |       \
                                           +-------------+        \________
We will work only on Linux router box. From the root prompter do:
echo 1 RDS >> /etc/iproute2/rt_tables
echo 2 ASTRAL >> /etc/iproute2/rt_tables
The /etc/iproute2/rt_tables content after previous commands:
#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
1 RDS
2 ASTRAL
Now we have three routing tables as follows: RDS table, ASTRAL table and the main table...
Let's fill up every table with the defaults routes:

The next step is to have some routing rules and routes:

For the RDS table:
ip route add default via 10.1.1.1 dev eth1 table RDS
ip rule add from 10.11.11.0/24 table RDS
ip rule add from 10.12.12.0/24 table RDS 
For the ASTRAL table:
ip route add default via 10.8.8.1 dev eth2 table ASTRAL
ip rule add from 10.22.22.0/24 table ASTRAL
ip rule add from 10.33.33.0/24 table ASTRAL
To see the routing tables:
ip route show table ASTRAL
ip route show table RDS
ip route show table main  # it's the same as "route -n" but in different format...
To see the routing tables:
ip rule show   # all the rule list
ip rule show | grep ASTRAL # only for ASRAL
ip rule show | grep RDS  # only for RDS
Let me explain the above rules.
The packets that came from the 10.11.11.0/24 and 10.12.12.0/24 networks will go to the RDS routing table and then (because we have a default route) will be passed to the RDS gateway. And similar, the packets that came from the 10.22.22.0/24 and 10.33.33.0/24 network will go to the ASTRAL gateway...
What is happening with the packets that came from other networks that are not shown in the above rules? Well, they just simply go to main routing table and follow the routing rules that reside there... If you want to block them to go to internet just delete the default route from the main table... (of course, doing that your router can not longer go to interent).


Second example:
Goal: To route the packets having the destination port 22/tcp to the RDS and 80/tcp to the ASTRAL (no matter what network generates them).
This example it is almost the same as the first one except that we will use iptables to mark the packets.

Same chart...
                                                                   ________
                                           +-------------+        /
                                           |    ISP 1    |       /
                             +-------------+    (RDS)    +------+
                             |             | gw 10.1.1.1 |     /
                      +------+-------+     +-------------+    / 
+----------------+    |     eth1     |                       /
|                |    |              |                      |
| Local networks +----+ Linux router |                      |  Internet cloud
|                |    |              |                      |
+----------------+    |     eth2     |                       \
                      +------+-------+     +-------------+    \
                             |             |    ISP 2    |     \
                             +-------------+  (ASTRAL)   +------+
                                           | gw 10.8.8.1 |       \
                                           +-------------+        \________

Same /etc/iproute2/rt_tables content:
#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
1 RDS
2 ASTRAL
Before you start check your iptables configuration. I strongly recommend to read about iptables if you are unsure about what you will doing next.
For more documentation go to iptables home page or you can download a good documentation from this site (Security & Privacy Section) or directly from here.

To mark the packets that have the 22 and 80 as destination port we will use the MANGLE table...
iptables -A PREROUTING -t mangle -i eth0 -p tcp --dport 22 -j MARK --set-mark 1
iptables -A PREROUTING -t mangle -i eth0 -p tcp --dprot 80 -j MARK --set-mark 2
For the RDS table:
ip route add default via 10.1.1.1 dev eth1 table RDS # the same like in the first example
For the ASTRAL table:
ip route add default via 10.8.8.1 dev eth2 table ASTRAL # the same like in the first example
The next step is to have some routing rules based by the marked packets:

For the RDS:
ip rule add from all fwmark 1 table RDS
For the ASTRAL:
ip rule add from all fwmark 2 table ASTRAL
You can use the same commands to see the routing tables and rule lists as in the first example.
Now you have a routing solution based by the destination port...

December 14, 2015

RE: How to receive a million packets per second

https://blog.cloudflare.com/how-to-receive-a-million-packets/

rsyslogd dynamically create log file based on msg content

This is tested on a Ubuntu 10.04 system (should work on newer Ubuntu/Debian system too)

1. Create file /etc/rsyslog.d/30test.conf, with the following content:

$template DynFile,"/tmp/test-%msg:7:18%.log"
:msg,startswith," ABCD-001122334455" ?DynFile
#:syslogtag,startswith,"test" ?DynFile
#:syslogtag,startswith,"test" /tmp/test.log

2. Open file /etc/rsyslog.conf, and make the following modification for the following line:
$FileCreateMode 0644
comment out the following lines:
#$PrivDropToUser syslog
#$PrivDropToGroup syslog

3. sudo service rsyslog restart
4. logger -t test "ABCD-001122334455sdfsfsdfsdfsdfsdfs-----------"
5. Now check file /tmp/test-*.log, and you should see file /tmp/test-001122334455.log

To Log message with a defined template
1. First define the template
      $template shortlog,"%msg:10:1000%"
2. Use the template by appending ";template-name" to the end of the output file

Notes:

a. Property msg starts with a space " ". 
b. Eventually we should try to figure out why dropPriviledge does causes issues here.

TIPS:

A good debugs tip:
1. add /etc/rsyslog.d/debug.conf
*.* /var/log/all.log;RSYSLOG_DebugFormat
#Note here ";RSYSLOG_DebugFormat" is the output template
2. restart rsyslog
3. watch the file /var/log/all.log to see all log messages with property names

Log with UNIX Timestamp
$template unixTS,"%timegenerated:::date-unixtimestamp%,%msg%\n"
:msg,contains,"[UFW " /var/log/ufw.log;unixTS

#Note here ";unixTS" is the output template

http://www.rsyslog.com/tag/use-a-template/
Also refer to the rsyslog PDF version of the manual (Chapter 1) for overview of how rsyslogd processes messages)

Syntax Check
rsyslogd -N1

===============UPDATE: New Syntax for Syslog version 7 and newer =============
rsyslog's new syntax is called "RainerScript". To generate dynamic files using output templates, generate a file in /etc/rsyslogd.conf/10-mytest.conf with the following content, and then restart service rsyslogd. The following example only logs message from the application "abc" and the message itself starts with "ABC" to a dynamic file name.

template(name="myfilename" type="string" string="/tmp/my-%programname%.log")
template(name="shortlog" type="string" string="%msg:10:$%\n")
if  $programname == 'abc' and $msg startswith " ABC"  then {
    action(type="omfile" dynaFile="myfilename" template="shortlog" )

}


=============UPDATE: More examples==================
1. variables are set using "set" and they start with "$!" in Rainer Scripts
2. variables only work inside the if block
3. Inside template the variables are surrounded by %%. e.g. "%$!devid%"
4. "stop" has replaced the "~" to discard a particular message.

module(load="omprog")
template(name="devid" type="string" string="%$!devid%")
if ($programname == 'charon') then {
    if ($msg contains "deleting IKE_SA")  then {
        set $!devid= re_extract($msg, "([A-Z0-9])+-([A-Z0-9])+.vpnclient.meetcircle.co", 0, 0, "unknown");
        #action(type="omfile" file="/var/log/delete.log" template="devid" )
        action(type="omfwd" Target="192.168.1.2" Port="15140" Protocol="tcp" template="devid" )
        #action(type="omprog" binary="/usr/bin/rsyslogpost.sh" template="devid")
    }
}

:programname,isequal,"charon" stop

December 2, 2015

dnsmasq with Ubuntu 14.04

To make it work, you may need to edit the file
/etc/default/dnsmasq.conf

and uncomment this line:
IGNORE_RESOLVCONF=yes

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