1. Keep learning forward

    To be updated ...
  2. #TIL : Define property of an object in hacking way

    Sometimes, we want to define a property of an advanced object (has setter and getter function).

    Now, we could use this helper function Object.defineProperty to define property of an object in a cool way.

    Example :

    const foo = {};
    
    Object.defineProperty(a, 'bar', {
    	value: 'hogehoge',
    	writable: false,
    });
    
    console.log(foo.bar); // 'hogehoge'
    foo.bar = 'foo bar'; // throw an error in strict mode
    console.log(foo.bar); // still be 'hogehoge'

    Modifying setter, getter function

    // Get callstack which function is getting or setting cookie value
    Object.defineProperty(document, 'cookie', {
    	get: function() {
    		console.log('get !');
    		console.trace();
    	},
    	set: function(val) {
    		console.log('set = ', val);
    		console.trace();
    	},
    }); 
  3. #TIL : Debug js code using console.trace

    Browsers provide an useful function help you debug easier than using simple console.log function.

    That is console.trace, which prints a stack trace to called function.

    Example :

    function foo() {
    	var a = 1;
    	bar(a);
    }
    function bar(x) {
    	console.log(x);
    	console.trace();
    }
    
    foo();
  4. #TIL : Sleeping connections in MySQL

    When you check your MySQL process list via command show processlist;, it will show you a useful table which provide all current connection details.

    "Sleep" state connections are most connection pointer waiting for the timeout to terminate. Then they still count as a connection. (Can cause MySQL connection limit error, which default equal 150 connections)

    So next time, remember to close your connection before terminating your app.

    Every connection counts ;)

  5. #TIL : HSTS rule in browser

    HTTP Strict Transport Security (HSTS) is a web security policy mechanism which helps to protect websites against protocol downgrade attacks and cookie hijacking.

    Enabling HSTS on your web will make your browser validate every SSL issues more strictly :

    • User can not visit http version on browser
    • User can not add SSL exception for the domain to ignore the warning. (when SSL cert expire or invalid common name)

    Note : You can manually remove a domain from HSTS in Chrome by accessing this page URL chrome://net-internals/#hsts

    So remember to add HSTS to your website !

  6. #TIL : Create cross-platform downloading app URL

    You have a mobile app for both platforms iOS and Android, each platform has different download URL. But your user doesn't know which platform he using. Clicking wrong URL will lead to user bounce-rate.

    Solution is making only 1 URL to download your app, which can redirect to right place depends on using platform. So how we achieve this ??

    The key of problem is detecting user platform, which can be done by extracting the User-Agent header from http request.

    PlatformUser-Agent pattern
    iOScontains iPhone or iPad or iPod
    Androidcontains Android

    This is how I implement using Caddy web server, you can do same thing in Apache or NGINX

    app.yourcompany.com:443 {
        timeouts 1m
    
        redir 302 {
            if_op or
            if {>User-agent} has iPhone
            if {>User-agent} has iPod
            if {>User-agent} has iPad
    	/ [apple-store-url]
        }
    
        redir 302 {
            if {>User-agent} has Android
    	/ [google-play-url]
        }
    
        redir 302 {
            if {>User-agent} not_has iPhone
            if {>User-agent} not_has iPod
            if {>User-agent} not_has iPad
            if {>User-agent} not_has Android
            / [your-landing-page-which-user-visit-on-desktop-device]
        }
    }
    

    Then we get cool and easy to remember link, right ?

  7. #TIL : Using web proxy to bypass firewalls

    Someday, you will be blocked by a firewall while trying crawling or accessing some website. The reason is they block your IP address from accessing the server.

    One solution is using a web proxy (http proxy, socks4 or socks5) to bypass the firewall, by adding the middle-man server between you and target. It's a bit unsecured but you could use for https site only.

    Some HTTP Proxy supports https will stream TLS data from target to you (so don't worry about proxy server can read you data). Btw, it only knows which domain and IP address you're connecting.

    To find a free proxy from the internet, try this service : https://gimmeproxy.com/

    It provides a cool API to fetch new proxy from its database.

    Example this endpoint will return JSON response including proxy anonymous, supports HTTPS, from Japan and minimum speed more than 100KB

    http://gimmeproxy.com/api/getProxy?anonymityLevel=1&supportsHttps=false&country=JP&minSpeed=100 
    

    In case you need more requests per day, try a subscription (cancelable and refundable). I tried last days, and really like their service (although I cancelled subscription b/c I don't need proxy anymore).

    Break the rules ! ;)

  8. #TIL : Fastly conflict detector script

    Last month, I built a CI solution for our project and adding a conflict detector to our build commands. This script runned so slow because it will check all application files (and our application codebase has many of css, js files).

    This was the script

    #!/bin/bash
    
    grep -rli --exclude=conflict_detector.sh --exclude-dir={.git,vendor,node_modules} "<<<<<<< HEAD" .
    
    if [ $? -eq 0 ]; then
        exit 1
    else
        exit 0
    fi

    Today, I think why don't we just check recently updated files (in the latest commit) ??? Then I have this new script

    #!/bin/bash
    
    # New way :D
    CHANGED_FILES=$(git log --pretty=format: --name-only HEAD^..HEAD | sort | uniq)
    
    for f in $CHANGED_FILES
    do
        if grep --exclude=conflict_detector.sh -q "<<<<<<< HEAD" $f
        then
            exit 1
        fi
    done
    
    exit 0

    conflict_detector.sh is the filename of script, we exclude it from check to make sure changing this file doesn't make it failed.

    You can use this approach to check linter, coding standard or run preprocessor ;)

    Result (in my context) :

    • Old script : 12 seconds
    • New script : ~ 50ms (200 times faster)

    be Automated, be Fast, but be Careful !

  9. #TIL : Getting your external IP

    We can get our external IP address by following ways :

    1. Call http request : curl http://wtfismyip.com/text or curl http://ifconfig.me/ip
    2. Lookup A record for hostname nslookup myip.opendns.com resolver1.opendns.com (this only works when you use resolver of OpenDNS)

    Bonus : curl https://v6.ident.me/ for IPv6

  10. #TIL : using git hooks to improve working flow

    We can improve our team workflow by defining some git hooks that trigger on specified events.
    You can read all events and their usecases here : https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks

    This is what I implemented to my today-i-learned repo. I used pre-commit to update Table of Contents in the README.md file, so every content in my repo will be updated on Github repo page.

    $ ln pre-commit .git/hooks/pre-commit

    pre-commit file :

    #!/bin/sh
    
    echo 'Running pre-commit hook' 
    
    python til_update_readme.py
    git add README.md

    So it will run a Python script that update new TOC and then add the file to git.

    Automation ! Automation ! AND .... Automation !!! 🤖

  11. #TIL : Reduce init time MySQL docker image

    Original MySQL docker image uses a script to generate ssl certificates for service. Sometime we don't really need it (connect via a docker network link or need a fast enough database service to build a automated test).

    We can reduce init time by removing the script from original Docker image

    FROM mysql:5.7
    
    # Remove mysql_ssl_rsa_setup to ignore setup SSL certs
    RUN rm -f /usr/bin/mysql_ssl_rsa_setup
    

    FAST as a FEATURE !!! 🚀

  12. #TIL : Using watch command to tracking changes in period time

    watch to a good command to run a command every N seconds.

    And like its name, means you can watch something, its output changes with flag -d

    It's a great tool to help you learn a new language without hitting compile and run everytime you save a file.

    $ watch -n 1 -d go run learn.go

    This command will compile and run learn.go every 1 second

    More flags :

    • -t : no title
    • -b : beep on non-zero exit code
    • -e : stop loop on error and exit on a keypress
    • -g : exit on change
    • -c : support colors
    • -h : you know ! ;)
  13. #TIL : Using netcat to wait a TCP service

    When doing a CI/CD testing, you would need to connect a external service (RDBMS, HTTP server or generic TCP server service). So you need waiting the service before running your test app.

    One way to do right waiting instead of sleep for a specified time is using netcat tool

    $ while ! echo -e '\x04' | nc [service_host] [service_port]; do sleep 1; done;

    Examples

    • MySQL service on port 3306
    $ while ! echo -e '\x04' | nc 127.0.0.1 3306; do sleep 1; done;
    $ ./run_test.sh

    Explanation :

    echo -e '\x04' will send an EOT (End Of Transmission) to the TCP every second to check if it's ready !

  14. #TIL : Indexes on multiple columns

    Let's say you have an indexes on 2 columns (A, B) of the table (X). So this is three use cases happen :

    1. You query data based on both of 2 columns => Indexes will be considered
    2. You query data based on (A) => Indexes will be considered
    3. You query data based on (B) => Indexes will be ignored because database indexes your data by B-tree algo. So it can't search node via a B => If you want, just create another indexes on B column

    I said will be considered because it depends on your query and your data (query optimizer will decide it !)

  15. #TIL : Using netcat as tiny TCP debug tool

    You can use netcat or nc as a debugging TCP tool. It can be a TCP sender and receiver with a short session (auto close when connection is closed)

    Examples :

    Scan ports

    $ nc -zv 127.0.0.1 20-80

    Check redis status

    $ echo 'info' | nc 127.0.0.1 6379

    Retrieve http response

    $ printf "GET /xinchao HTTP/1.1\r\n\r\n" | nc 127.0.0.1 8000 | tee xinchao.txt

    Change to IPv6 : nc -6

    Want more ??

    $ nc -h
  16. #TIL : Simple HTTP server function helper

    I use python3 (3.4+) to create a bash function to help me start quickly a simple http server on specified port

    function server() {
      local port="${1:-8000}" # Getting port number
      google-chrome "http://127.0.0.1:$port" # Open URL in browser, could change to firefox --new-tab "http://127.0.0.1:$port"
      python3 -m http.server $port --bind 127.0.0.1
    }
  17. #TIL : TIME command output meaning

    When you want to know how long does it take to run a process, just use time command as a prefix

    $ time my_program arg1 arg2
    real        0m0.003s
    user        0m0.000s
    sys         0m0.004s
    • real : wall clock time, mean time to start to finish your process
    • user : CPUs-time outside the kernel
    • sys : CPUs-time within the kernel

    real+sys result is total multi CPUs time (so if you have a multi core CPUs, it is often bigger than real)

  18. #TIL : How SMTP works

    When a email send through an SMTP (with authentication), every SMTP server is a hop in mail routing. So it will transfer to localmail or forward the email to next hop (shortest distance via DNS MX record).

    And standard port of SMTP is 25 (unsecured, but can upgrade to TLS via STARTTLS command).

    $ nslookup -type=mx gmail.com 8.8.8.8
    Server:         8.8.8.8       
    Address:        8.8.8.8#53    
    
    Non-authoritative answer:     
    gmail.com       mail exchanger = 20 alt2.gmail-smtp-in.l.google.com.                                                    
    gmail.com       mail exchanger = 5 gmail-smtp-in.l.google.com.                                                          
    gmail.com       mail exchanger = 30 alt3.gmail-smtp-in.l.google.com.                                                    
    gmail.com       mail exchanger = 10 alt1.gmail-smtp-in.l.google.com.                                                    
    gmail.com       mail exchanger = 40 alt4.gmail-smtp-in.l.google.com.                                                    
    
    Authoritative answers can be found from:

    So shortest SMTP of gmail.com domain is gmail-smtp-in.l.google.com

    $ telnet gmail-smtp-in.l.google.com 25
  19. #TIL : Send ENTER key to kernel

    When you try to send an Enter keyboard to linux kernel, it looks like nothing happens.

    This is because you only send a key press (KEY DOWN) but don't send an key release (KEY UP) event after that.

  20. #TIL : BASH tracing commands

    Thank Hiro Ishii for teaching me this

    set -x will print all running commands in your bash script

    So I dove in and look for all set options of BASH.

    And this is what I got , http://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

  21. #TIL : BASH return a value in function

    Creating function is a good way to refactor your bash script. But BASH doesn't support returning a value in true way, so it makes a bit of challenge to handle that.

    You can use this trick

    hello() {
    	echo "Hello $1"
    }
    
    hw=$(hello "KhanhIceTea")
    echo $hw

    But what if you want to echo log message in hello function, it will be merged to returned value.

    hello() {
    	echo "System is weaking up and brush its teeth :))"
    	echo "Hello $1"
    }
    
    hw=$(hello "KhanhIceTea")
    echo "This is returned value of hello function :"
    echo $hw

    This is a how to resolve it, forwarding log message to stderr instead of stdout by default

    hello() {
    	echo "System is weaking up and brush its teeth :))" >&2
    	echo "Hello $1"
    }
    
    hw=$(hello "KhanhIceTea")
    echo "This is returned value of hello function :"
    echo $hw

    Where there is a shell, there is a way !

  22. #TIL : BASH exiting on first error

    Setting a flag set -e to bash script will let the script exit on first error occurs, so if you want to ignore a command just adding || true to suffix

    set -e
    
    errorCmd $1 || true
    echo "Run here !"

    And opposite of set -e is set +e, haha of course !

    set +e
    
    errorCmd $1
    echo "Still run here !"
  23. #TIL : Zip compressing list of files

    To specify a list of compressed files when using zip cli tool, you could use -@ [file_list] flag. And file_list is a file contains list of compressed file (new line separated)

    Example

    $ zip changed.zip -@ changed_files.txt

    Or use stdin pipe

    $ find . -mmin -60 -print | zip changed_1_hour_ago -@

    This will zip all changed files 1 hour ago

  24. #TIL : Blocking specified country to prevent from DDOS

    Last day I checked system logs and got a lot of warning messages mentioned that my server has been attack via Brute-force. So I decided to blocked some countries from connecting to attacked ports (21, 25). They are China, Russia and US.

    This site provides a list of IP blocks of specified country

    http://www.ipdeny.com/ipblocks/

  25. #TIL : TCP FIN timeout

    The TCP FIN timeout belays the amount of time a port must be inactive before it can reused for another connection. The default is often 60 seconds, but can normally be safely reduced to 30 or even 15 seconds:

    net.ipv4.tcp_fin_timeout = 15
    

    Ref : https://www.linode.com/docs/web-servers/nginx/configure-nginx-for-optimized-performance

  26. #TIL : Lock and unlock a user password

    In Linux, you can prevent a user from login by locking it.

    Lock

    $ sudo passwd -l [user]

    Unlock

    $ sudo passwd -u [user]
  27. #TIL : Generate dhparam file faster

    openssl uses strong prime (which is useless for security but requires an awful lot more computational effort. A "strong prime" is a prime p such that (p-1)/2 is also prime). So it will be faster if we add option -dsaparam to the command

    $ openssl dhparam -dsaparam -out /etc/ssl/private/dhparam.pem 4096

    Ref : https://security.stackexchange.com/a/95184

  28. #TIL : Grep : find a string in folder

    Grep is a greate tool for searching a string in files.

    Syntax

    $ grep -nr '[string]' [folder]

    If you want to show surrounding lines the result, add flag -C [number] to the command

    $ grep -nr -C 3 'hello' src
  29. #TIL : Ansible playbook : skip to task

    You can skip to a task by its name by adding parameter --start-at

    $ ansible-playbook playbook.yml --start-at="[your task name]"
  30. #TIL : Mycli : a new good cli MySql Client

    This tool is written in Python with super cool features (auto-complete and colors).

    Worth a shot !

    Install

    $ pip install mycli

    Usage

    $ mycli -h 127.0.0.1 -P 3306 -u root

    Screencast

    mycli

  31. #TIL : Enable reverse proxy in CentOS

    CentOS with SELinux enabled by default will block any http proxy connection. So you have to enable this permission.

    Temporary enable

    $ /usr/sbin/setsebool httpd_can_network_connect 1

    Permanent enable

    $ /usr/sbin/setsebool -P httpd_can_network_connect 1