分类: Linux Development

  • Debian Dynamic DNS

    http://perceptionistruth.com/2013/05/running-your-own-dynamic-dns-service-on-debian/

    Running your own Dynamic DNS Service (on Debian)

    I used to have a static IP address on my home ADSL connection, but then I moved to BT Infinity, and they don’t provide that ability.  For whatever reason, my Infinity connection resets a few times a week, and it always results in a new IP address.

    Since I wanted to be able to connect to a service on my home IP address, I signed up to dyn.com and used their free service for a while, using a CNAME with my hosting provider (Gandi) so that I could use a single common host, in my own domain, and point it to the dynamic IP host and hence, dynamic IP address.

    While this works fine, I’ve had a few e-mails from dyn.com where either the update process hasn’t been enough to prevent the ’30 day account closure’ process, or in recent times, a mail saying they’re changing that and you now need to log in on the website once every 30 days to keep your account.

    I finally decided that since I run a couple of VPSs, and have good control over DNS via Gandi, I may as well run my own bind9 service and use the dynamic update feature to handle my own dynamic DNS needs.  Side note: I think Gandi do support DNS changes through their API, but I couldn’t get it working.  Also, I wanted something agnostic of my hosting provider in case I ever move DNS in future (I’m not planning to, since I like Gandi very much).

    The basic elements of this are,

    1. a bind9 service running somewhere, which can host the domain and accept the updates.
    2. delegation of a subdomain to that bind9 service.  Since Gandi runs my top level domain for me, I need to create a subdomain and delegate to it, and then make dynamic updates into that subdomain.  I can still use CNAMEs in the top level domain to hide the subdomain if I wish.
    3. configuration of the bind9 service to accept secure updates.
    4. a script to do the updates.

    In the interests of not re-inventing the wheel, I copied most of the activity from this post.  But I’ll summarise it here in case that ever goes away.

    Installing / Configuring bind9

    You’ll need somewhere to run a DNS (bind9 in my case) service.  This can’t be on the machine with the dynamic IP address for obvious reasons.  If you already have a DNS service somewhere, you can use that, but for me, I installed it on one of my Debian VPS machines.  This is of course trivial with Debian (I don’t use sudo, so you’ll need to be running as root to execute these commands),

    apt-get install bind9 bind9-doc

    If the machine you’ve installed bind9 onto has a firewall, don’t forget to open ports 53 (both TCP and UDP).  You now need to choose and configure your subdomain.  You’ll be creating a single zone, and allowing dynamic updates.

    The default config for bind9 on Debian is in /etc/bind, and that includes zone files.  However, dynamically updated zones need a journal file, and need to be modified by bind.  I didn’t even bother trying to put the file into /etc/bind, on the assumption bind won’t have write access, so instead, for dynamic zones, I decided to create them in /var/lib/bind.  I avoided /var/cache/bind because the cache directory, in theory, is for transient files that applications can recreate.  Since bind can’t recreate the zone file entirely, it’s not appropriate to store it there.

    I added this section to /etc/bind/named.conf.local,

    // Dynamic zone
      zone "home.example.com" {
        type master;
        file "/var/lib/bind/home.example.com";
        update-policy {
          // allow host to update themselves with a key having their own name
          grant *.home.example.com self home.example.com.;
        };
      };

    This sets up the basic entry for the master zone on this DNS server.

    Create Keys

    So I’ll be honest, I’m following this section mostly by rote from the article I linked.  I’m pretty sure I understand it, but just so you know.  There are a few ways of trusting dynamic updates, but since you’ll likely be making them from a host with a changing IP address, the best way is to use a shared secret.  That secret is then held on the server and used by the client to identify itself.  The configuration above allows hosts in the subdomain to update their own entry, if they have a key (shared secret) that matches the one on the server.  This stage creates those keys.

    This command creates two files.  One will be the server copy of the key file, and can contain multiple keys, the other will be a single file named after the host that we’re going to be updating, and needs to be moved to the host itself, for later use.

    ddns-confgen -r /dev/urandom -q -a hmac-md5 -k thehost.home.example.com -s thehost.home.example.com. | tee -a /etc/bind/home.example.com.keys > /etc/bind/key.thehost.home.example.com

    The files will both have the same content, and will look something like this,

    key "host.home.example.com" {
    algorithm hmac-md5;
    secret "somesetofrandomcharacters";
    };

    You should move the file key.thehost.home.example.com to the host which is going to be doing the updating.  You should also change the permissions on the home.example.com.keys file,

    chown root:bind /etc/bind/home.example.com.keys
    chmod u=rw,g=r,o= /etc/bind/home.example.com.keys

    You should now return to /etc/bind/named.conf.local and add this section (to use the new key you have created),

    // DDNS keys
    include "/etc/bind/home.example.com.keys";

    With all that done, you’re ready to create the empty zone.

    Creating the empty Zone

    The content of the zone file will vary, depending on what exactly you’re trying to achieve.  But this is the one I’m using.  This is created in /var/lib/bind/home.example.com,

    $ORIGIN .
    $TTL 300 ; 5 minutes
    home.example.com IN SOA nameserver.example.com. root.example.com. (
        1 ; serial
        3600 ; refresh (1 hour)
        600 ; retry (10 minutes)
        604800 ; expire (1 week)
        300 ; minimum (5 minutes)
        )
    NS nameserver.example.com.
    $ORIGIN home.example.com.

    In this case, namesever.example.com is the hostname of the server you’ve installed bind9 onto.  Unless you’re very careful, you shouldn’t add any static entries to this zone, because it’s always possible they’ll get overwritten, although of course, there’s no technical reason to prevent it.

    At this stage, you can recycle the bind9 instance (/etc/init.d/bind9 reload), and resolve any issues (I had plenty, thanks to terrible typos and a bad memory).

    Delegation

    You can now test your nameserver to make sure it responds to queries about the home.example.com domain.  In order to properly integrate it though, you’ll need to delegate the zone to it, from the nameserver which handles example.com.  With Gandi, this was as simple as adding the necessary NS entry to the top level zone.  Obviously, I only have a single DNS server handling this dynamic zone, and that’s a risk, so you’ll need to set up some secondaries, but that’s outside the scope of this post.  Once you’ve done the delegation, you can try doing lookups from anywhere on the Internet, to ensure you can get (for example) the SOA for home.example.com.

    Making Updates

    You’re now able to update the target nameserver, from your source host using the nsupdate command.  By telling it where your key is (-k filename), and then passing it commands you can make changes to the zone.  I’m using exactly the same format presented in the original article I linked above.

    cat <<EOF | nsupdate -k /path/to/key.thehost.home.example.com
    server nameserver.example.com
    zone home.example.com.
    update delete thehost.home.example.com.
    update add thehost.home.example.com. 60 A 192.168.0.1
    update add thehost.home.example.com. 60 TXT "Updated on $(date)"
    send
    EOF

    Obviously, you can change the TTL’s to something other than 60 if you prefer.

    Automating Updates

    The last stage, is automating updates, so that when your local IP address changes, you can update the relevant DNS server.  There are a myriad ways of doing this.  I’ve opted for a simple shell script which I’ll run every couple of minutes via cron, and have it check and update DNS if required.  In my instance, my public IP address is behind a NAT router, so I can’t just look at a local interface, and so I’m using dig to get my IP address from the opendns service.

    This is my first stab at the script, and it’s absolutely a work in progress (it’s too noisy at the moment for example),

    #!/bin/sh

    # set some variables
    host=thehost
    zone=home.example.com
    dnsserver=nameserver.example.com
    keyfile=/home/bob/conf/key.$host.$zone
    #

    # get current external address
    ext_ip=`dig +short @resolver1.opendns.com myip.opendns.com`

    # get last ip address from the DNS server
    last_ip=`dig +short @$dnsserver $host.$zone`

    if [ ! -z “$ext_ip” ]; then
    if [ ! -z “$last_ip” ]; then
    if [ “$ext_ip” != “$last_ip” ]; then
    echo “IP addresses do not match (external=$ext_ip, last=$last_ip), sending an update”

    cat <

    http://www.foell.org/justin/diy-dynamic-dns-with-openwrt-bind/

    http://blog.infertux.com/2012/11/25/your-own-dynamic-dns/

    http://idefix.net/~koos/dyndnshowto/dyndnshowto.html

    https://blog.hqcodeshop.fi/archives/76-Doing-secure-dynamic-DNS-updates-with-BIND.html

    https://0x2c.org/rfc2136-ddns-bind-dnssec-for-home-router-dynamic-dns/

    http://agiletesting.blogspot.com/2014/12/dynamic-dns-updates-with-nsupdate-new.html

     

     

  • Handler and Activity’s life cycle, take care about orphan threads!!

    http://android2ee.blogspot.com/2011/11/handler-and-activity-life-cycle-take.html

    Handler and Activity’s life cycle, take care about orphan threads!!

    Hello,

    You have an Activity which uses a Handler. You create your handler and overwrite the handleMessage method, you launch the handler’s thread and that’s it for the handler management… Most of us do such a thing and it’s a huge mistake!!! What happens to your thread when your activity pauses and resumes and worst when it dies and (re)creates?
    You thread becomes an orphan thread !
    So you have written something like that :

    (BAD CODE EXAMPLE DO NOT USE)

    /**

    * The handler

    */

    private final Handler slowDownDrawingHandler;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    // handler definition

    slowDownDrawingHandler = new Handler() {

    /** (non-Javadoc)*/

    @Override

    public void handleMessage(Message msg) {

    super.handleMessage(msg);

    redraw();

    }

    };

    // Launching the Thread to update draw

    Thread background = new Thread(new Runnable() {

    /**

    * The message exchanged between this thread and the handler

    */

    Message myMessage;

    // Overriden Run method

    public void run() {

    try {

    while (true) {

    // Sleep

    Thread.sleep(100);

    // Do something

    myMessage = slowDownDrawingHandler.obtainMessage();

    // then send the message

    slowDownDrawingHandler.sendMessage(myMessage);

    }

    }

    } catch (Throwable t) {

    // just end the background thread

    }

    }

    });

    // start the thread

    background.start();

    Using such a code, when your activity pauses or dies your thread is still alive and become an orphan thread. Nothing can stop it, neither inter-acts with it and it continues to run. This is a big fail.

    What is the right way to do it: You have to manage your thread state according to your activity state. In other words, when your activity pauses, you have to pauses your thread, when it resumes you have to resume your thread, when your activity dies, you thread must die….

    A simple way to do that is to use two atomic Booleans (synchronized boolean), isPausing and isStopping, change their value in the onResume, onPause, onCreate and onDestroy methods of your activity and use that boolean to pause or stop your thread.

    So the right code should look like that:
    Good Code Example CAN BE USED

    /** * The handler  */

    private final Handler slowDownDrawingHandler;

    /** * An atomic boolean to manage the external thread’s destruction */

    AtomicBoolean isRunning = new AtomicBoolean(false);

    /** * An atomic boolean to manage the external thread’s destruction */

    AtomicBoolean isPausing = new AtomicBoolean(false);

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

    // handler definition

    slowDownDrawingHandler = new Handler() {

    @Override

    public void handleMessage(Message msg) {

    super.handleMessage(msg);

    redraw();

    }

    };

    // Launching the Thread to update draw

    Thread background = new Thread(new Runnable() {

    /**

    * The message exchanged between this thread and the handler

    */

    Message myMessage;

    // Overriden Run method

    public void run() {

    try {

    while (isRunning.get()) {

    if(isPausing.get()) {

    Thread.sleep(2000);

    }else {

    // Sleep

    Thread.sleep(100);

    // Do something

    myMessage = slowDownDrawingHandler.obtainMessage();

    // then send the message

    slowDownDrawingHandler.sendMessage(myMessage);

    }

    }

    }

    } catch (Throwable t) {

    // just end the background thread

    }

    }

    });

    // Initialize the threadSafe booleans

    isRunning.set(true);

    isPausing.set(false);

    background.start();

    }

    /*(non-Javadoc) */

    @Override

    protected void onPause() {

    //and don’t forget to stop the thread

    isPausing.set(true);

    super.onPause();

    }

    /*(non-Javadoc) */

    @Override

    protected void onResume() {

    //and don’t forget to relaunch the thread

    isPausing.set(false);

    super.onResume();

    }

    /*(non-Javadoc) */

    @Override

    protected void onDestroy() {

    //and don’t forget to kill the thread

    isRunning.set(false);

    super.onDestroy();

    }
    So, Thanks who?
    Thanks, Android2ee, the Android Programming Ebooks :o)

    Mathias Séguy
    mathias.seguy.it@gmail.com
    Auteur Android2EE
    Ebooks to learn Android Programming.

    Retrouvez moi sur Google+
    Suivez moi sur Twitter
    Rejoignez mon réseau LinkedIn ou Viadeo

  • dnstap

    http://dnstap.info/slides/dnstap_nanog61.pdf

     

    dnstap

     –  What  is  it?

    • High  speed  DNS  logging  without  packet  capture
    • Encoding  uses  Protocol  Buffers
    • Binary  clean
    • Efficient  encoding
    • Extendable
    • Implementa6ons  available  for  many  programming
  • libnet-dev

    http://sourceforge.net/projects/libnet-dev/files/?source=navbar

    A portable framework for low-level network packet construction

  • Regular Expression in C/C++

    SLRE:
    Super Light Regular Expression library
    An ISO C library that implements a subset of Perl regular expression syntax
    Simple API
    Dosen’t use heap

    T-Rex
    A minimalistic regular expression library written in ANSI C, supports the following POSIX expressions: ?,*,+,^,$,.,[a-b],() plus the perl style greedy closures {n} . It can be conditionally compiled to support 8-bits or 16-bits character strings.
    Uses heap

    TRE
    A lightweight, robust, and efficient POSIX compliant regexp matching library with some exciting features such as approximate (fuzzy) matching.
    PCRE
    Perl Compatible Regular Expressions
    heavyish, fully-fledged, Unicode support, industry-standard (used in Apache etc).

    RE2
    C++ library
    RE2 is a fast, safe, thread-friendly alternative to backtracking regular expression engines
    Oniguruma:
    Support different character encoding

  • Update NO-IP DDNS

    Updating no-ip ddns on

    DDWRT Router:  

    with the help of inadyn tool

    https://github.com/torglobit/inadyn

     

     

     

    Linux box:

    With the help of noip-udc-linux

    http://www.no-ip.com/client/linux

     

    Failed Issus:

    No direct internet link

       DNS resolve too slow

    inadyn:   timeout=IP_DEFAULT_TIMEOUT=20s

    noip-udc:   using gethostbyname  linux api to get IP address,  the timeout can be defined in /etc/resolv.conf,  the default value in linux is 5 ms,

    Example of resolv.conf:

    nameserver 8.8.8.8 options timeout:30

     

     

  • Install VMWare ESXi HP MicroServer Gen8

    Install ESXi on hard disk:  Manage HardDisk with SSA, Launched from SPP

    Download HP SPP image from HP web site (http://blog.zhenglei.net/?p=254958)
    Create bootable USB disk with HP SPP image
    Update Firmware with SPP USB disk (Automatic)
    Create Logic disk with SPP USB disk (Interactive)

     

    Install ESXi on SD Card:  Select SD Card as the first Removable Media

    Power on
    Press  F9 during POST, Enter into Setup ROM Utility
    System Option / USB option

     


     

     Install ESXi

    1. Download VMware ESXi ISO image from HP/VMWare website
    2. Launch iLO4 remote console
    3. Mount Virtual DVD disk with VMware ESXi ISO image within iLO4 remote console
    4. Boot HP Gen8 Server with Virtual DVD disk
    5. Install VMware ESXi  (on SD disk)
    6. Reboot
    7. Download VSphere Client for Windows from http://MicroServer_NIC_IP
    8. Install VSphere Client into windows7 workstation
    9. Launch VSphere Client and Install Ubuntu 12 on virtual machine
    10. Generate VSphere License Key on Ubuntu  & register within VSphere Client
    •   sudo apt-get update
    •   sudo apt-get install wine
    •   sudo apt-get unrar
    •   wget http://blog.zhenglei.net/wp-content/uploads/2015/12/VMv6.rar
    •   unrar e VMv6
    •   wine KEYGEN.EXE
    •   Choose  VMware vSphere 6 Enterprise Plus & generate key
    •   Copy the generate key to VSphere client