Sunday, December 20, 2015

Tools Posts

  1. F3 - an alternative to h2testw

#diskChecking #microsd #analysis

SCENARIO

前段時間從二手版上買了張 microSD,想檢查看看記憶卡裏面的磁區的狀況,然後就發現了這個國外大大寫的 tools。
測試方法基本上就是直接把一個個 1GB 的檔案寫入記憶卡裏,然後讀出在檢查 CheckSum,
感覺應該蠻準?不過測試起來頗費時間就是了(不要問要多久,depend on 記憶卡的讀取速度跟容量。 64G class-10 大概兩個小時?

REFERENCES

[1] http://oss.digirati.com.br/f3/

Thursday, October 29, 2015

Ubuntu's ufw 設置 - 將WAN's IP某個Port轉到LAN's IP某個Port

REASON BEHIND....

因為前個週末和沛那邊提供的虛擬機沒有收回,想說可以用剩下的resource在開一台vm,可是Floating IP 只有一個,所以需要自己在接到Floating IP 的 vm 上做端口轉發來連接到另一個vm的ssh 端口。


WORKAROUND...



  1. enable forwarding in "/etc/default/ufw"
    • sudo cp /etc/default/{ufw,ufw.bak}
    • sudo vim /etc/default/ufw
      • ...
        #DEFUALT_FORWARD_POLICY = "DROP"
        DEFAULT_FORWARD_POLICY = "ACCEPT"
        ....

  2. enable ipv4 forwarding in "/etc/ufw/sysctl.conf"

    • sudo cp /etc/ufw/{sysctl.conf,sysctl.conf.bak}
    • sudo vim /etc/ufw/sysctl.conf
    • uncomment the line "net/ipv4/ip_forward=1"
      • ...
        # Uncomment this to allow ....
        net/ipv4/ip_forward=1
        #net/ipv6/conf/default/forwarding=1
        #net/ipv6/conf/all/forwarding=1
        ...
    • reload sysctl settings from "/etc/ufw/sysctl.conf" 
      • sudo sysctl -p /etc/ufw/sysctl.conf

  3. define forwarding rules in "/etc/ufw/before.rules"

    • sudo cp /etc/ufw/{before.rules,before.rules.bak}
    • sudo vim /etc/ufw/before.rules
    • add nat table rules right below the comment block at starting of the file (before the line "*filter")
      • # NAT table rules
        *nat
        :PREROUTING ACCEPT [0:0]
        :POSTROUTING ACCEPT [0:0]

        # -F to drop all previous nat rule in "iptables"
        -F
        #Port Forwardings
        -A PREROUTING -p tcp --dport <sourcePort> -j DNAT --to <destinationIP>:<destinationPort>
        # Forward traffic through eth0
        -A POSTROUTING -o eth0 -j MASQUERADE

        COMMIT

  4. reload ufw rules
    • sudo ufw disable && sudo ufw enable && sudo ufw reload
    • use "nc" to check if the port is listening
      • nc -zv <publicIP> <sourcePort>

Sunday, October 25, 2015

2015 Hackathon 隨筆

這篇是在一個 “既然都肯花了兩天去當炮灰,幹嘛再多花兩個小時好好整理這兩天遇到的問題跟學到的東西的心得“ 心情下的產物。(其實是一個在等女朋友沒事情還不想寫作業就隨便記錄一下的概念。


整篇文章主要分成兩個部分。
第一個記錄的是部分是把 github 上別人提供的 hackathon-starter setup在 我們的 virutalMachine 上面的時候遇到的問題跟最後我們解決那些問題的 workaround。
第二個部分就著重在記錄說這兩天自己終於開啟了 Javascript 寫 backend api server 的小小小心得跟一些筆記。


簡單來說這次整個 hackathon 時間幾乎都花在了解 mean(? 的架構。

這次一開始就從 github 上面 clone 了個 “hackathon starter” 下來用。
安裝過程其實 github 裡面的 README.md 寫得很清楚這邊就不累贅了。

不過在安裝過程中出現了幾個小狀況讓我門的 express simple server(? 一直沒辦法成功 host 起來。
第一個遇到的問題跟這篇 blogPost 裡面作者描述的蠻類似的。簡單來說就是 "git remote rm origin" 之後要下 "npm install" 的時候就出現了一些 weird error,吐出來的 bug 有整萬行我也不知道該從哪裡看起。
Even 到現在我還是沒有辦法明確判斷說到底是什麼原因導致了這個問題的發生。而我最後的 workaround 就是照著上面 blogPost 里描述的在 "npm install" 之前先 "apt-get install node-legacy"。

第二個遇到的問題是 server 成功 Host 起來了,可是沒有辦法從外部 access 到 host 起來的 server。 這個部分導致這個問題發生的原因是因為說 和沛科技 提供的 virtualMachine 不是所有端口都有 default 開啟讓外部 access 的,所以可能要聯繫一下工作人員看要怎麼把外部的 Port Forward 到 virtualMachine 的 Port 上面。

我們第三個碰到的問題是沒辦法下 "npm install mongo-express" 安裝 "mongo-express",整體來說問題的敘述個這篇 blogPost 描述的很像。這個問題最後的 workaround 也是按照 Post 底下人家回答的,把一些對應的 library 補齊再試著安裝就 OK 了!


Tuesday, September 29, 2015

Modify FAT16 file-system

The FAT-16 file-system consists of:

  • Reserved area
    • boot sector
    • optional file system information sector (FAT-32) 
  • FATs
    • number of copies is specified in boot sector
  • Data area
    • a root directory
      FAT-12 & FAT-16 has fixed root dir size, while it's customizable in FAT-32. 
    • data clusters

Boot sector

    Bytes/AddrPurpose
    0-2/ 0x00-0x02Assembly code instructions to jump to boot code (mandatory in bootable partition)
    3-10 0x03-0x0AOEM name in ASCII
    11-12/ 0x0B-0x0CBytes per sector (512, 1024, 2048, or 4096)
    13/ 0x0DSectors per cluster (Must be a power of 2 and cluster size must be <=32 KB)
    14-15/ 0x0E-0x0FSize of reserved area, in sectors
    16/  0x10Number of FATs (usually 2)
    17-18/ 0x11-0x12Maximum number of files in the root directory (FAT12/16; 0 for FAT32)
    19-20/ 0x13-0x14Number of sectors in the file system; if 2 B is not large enough, set to 0 and use 4 B value in bytes 32-35 below
    21/ 0x15Media type (0xf0=removable disk, 0xf8=fixed disk)
    22-23/ 0x16-0x17Size of each FAT, in sectors, for FAT12/16; 0 for FAT32
    24-25/ 0x18-0x19Sectors per track in storage device
    26-27/ 0x1A-0x1BNumber of heads in storage device
    28-31/ 0x1C-0x1FNumber of sectors before the start partition
    32-35/ 0x20-0x23Number of sectors in the file system; this field will be 0 if the 2B field above (bytes 19-20) is non-zero

while for our case, 

screenshot of hexedit
===============================================
ADDR : VALUE       MEANING
===============================================
00-02: eb 3c 90    Instructions to jump to boot code
03-0a: 6d 6B 64 6f 73 66 73 00
                   Name string (mkdosfs.)
0b-0c: 00 02       Bytes/sector (0x0200 = 512)
0d   : 20          Sectors/cluster (20)
0e-0f: 20 00       Size of reserved area (20 sector)
10   : 02          Number of FATs (2)
11-12: 00 02       Max. number of root directory entries (0x0200 = 512)
13-14: 00 00       Total number of sectors (value equal to 0, check value in 0x20-0x23)
15   : F8          Media type (fixed disk)
16-17: 00 01       FAT size (0x0100 = 256 sectors)
18-19: 3F 00       Sectors/track (0x003F = 63)
1a-1b: FF 00       Number of heads (0x00FF = 255)
1c-1f: 00 00 00 00 Number of sector before partition (0)
20-23: 00 F8 1F 00 Total number of sectors (0xF800001F = 4160749599)??
24   : 00          Drive number (0)
25   : 00          Unused
26   : 29          Extended boot signature
27-2a: F5 7E 18 E5 Volume serial number (.~..)
2b-35: 20 20 20 20 20 20 20 20 20 20 20
                   Volume label ("           ")
36-3d: 46 41 54 31 36 20 20 20
                   File system type label ("FAT16 ")
3e-1fd : [snip]    Not used
1fe-1ff: 55 aa     Signature value (0xaa55)


to be continue....

Tuesday, September 22, 2015

Wifi router setup with RaspberryPi2

Do you want a separate WiFi network for your guests? or would you like to have network running an alternative DNS configuration in your house? You no longer have to buy a new WiFi router to do this – you can turn your Raspberry Pi into a WiFi router.
This post will guide you through the process of setting up your Raspberry Pi as a WiFi network.

Prerequisites & Equipment

You are going to need the following:
Raspberry Pi with Edimax Wifi Adapter
Raspberry Pi with Edimax Wifi Adapter
Before we proceed I want to point out the importance of buying the right USB WiFi Adapter. As you may have experienced with other types of hardware devices not all devices are plug-n-play. Sometimes you will need to download a driver to make them work. While drivers are normally readily available for Windows computers – it is a different world for Linux and Raspberry Pi’s. This is why it is very important to buy a WiFi Adapter that mentions “Linux” in the product description or package. I use the Edimax – Wireless 802.11b/g/n nano USB adapter because I have found it to be cheap and very easy to work with. I very recommend you do the same – it could save you a lot of headaches.


Getting WiFi adapter running on the Raspberry Pi


Plug the USB WiFi adapter into one of the free USB ports on the Raspberry Pi. Power up the Raspberry Pi. At this point you need to either connect to the Raspberry Pi via an Ethernet cable or use a keyboard and a monitor to access the Raspberry Pi.
If you need help connecting to the Raspberry Pi look at this post remotely using a wired network connection.
After booting and logging-in you want to make sure that the Raspberry Pi found your new wireless adapter. To look at which peripherals the operating system found when booting run the following command:
dmesg | more
You can use the spacebar to scroll down a page at a time – towards the end you will see something similar to the following lines:
[ 3.282651] usb 1-1.2: new high-speed USB device number 4 using dwc_otg
[ 3.394810] usb 1-1.2: New USB device found, idVendor=7392, idProduct=7811
[ 3.407489] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3.420530] usb 1-1.2: Product: 802.11n WLAN Adapter
This means that the operating system recognized the USB WiFi Adapter using one of the built-in drivers (you can return to the terminal by pressing “q”). All that is left is to configure your WiFi connection.

Install the router software

In order for the Raspberry Pi to act as a WiFi router and access point you need to install some extra software on the Raspberry. You are going to need the following pieces of software:
  • hostapdHostAPD is a user space daemon for access point and authentication servers. That means it can will turn your Raspberry Pi into a access point that other computers can connect to. It will also handle security such that you can setup a WiFi password.
  • isc-dhcp-serverisc-dhcp-server is the Internet Systems Consortium’s implementation of a DHCP server. A DHCP server is responsible for assigning addresses to computers and devices connection to the WiFi access point.
To install the DHCP software run the following command:
sudo apt-get install isc-dhcp-server
Next up is the HostAPD software. Because our USB stick needs an access point driver not supported by the native HostAPD application we must install a custom version compiled with the driver we need. This can be done by running the following commands one at a time.
The whole crux of the issue is that the apt hosted copy of "hostapd" is not compatible with the RTL8188CUS chipset. But, thanks to the Edimax team, I've got a replacement "hostapd" binary to resolve this issue. This tutorial will not work without it.
To download and replace the installed binary version of "hostapd" we just installed, issue the following commands:
$ wget https://drive.google.com/open?id=0B9rimmaVzNREd0daY3dGYU5lMmM
  // or $ wget https://github.com/jenssegers/RTL8188-hostapd/archive/v1.1.tar.gz
$ tar -xf hostapd_v1.1.tar.gz
$ make
$ make install
*Note, for those who don't like the idea of installing from untrusted 3rd parties, and would rather compiled their own binary, you can download the realtek driver here. You will have to navigate to the ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd and run a make, make install, then hostapd like what i did above.

Configure the ISC-DHCP-Server

To configure the DHCP server open the file /etc/dhcp/dhcpd.conf in your favorite text editor. You can open it in nano using this command:
sudo nano /etc/dhcp/dhcpd.conf
Find the following section and comment it out by placing a hashtag at the beginning of the line.
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
Next find the section below and un-comment the word authoritative (remove hastag):
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
The file now looks like this:
dhcpconfig1
Next we need to define the network and network addresses that the DHCP server will be serving. This is done by adding the following block of configuration to the end of file:
subnet 192.168.10.0 netmask 255.255.255.0 {
 range 192.168.10.10 192.168.10.20;
 option broadcast-address 192.168.10.255;
 option routers 192.168.10.1;
 default-lease-time 600;
 max-lease-time 7200;
 option domain-name "local-network";
 option domain-name-servers 8.8.8.8, 8.8.4.4;
}
This will enable the DHCP server to hand out the ip addresses from 192.168.10.10 to 192.168.10.20 in its own local network. People who are skilled in network configuration can change these values if they wish to use some other network addresses and/or other DNS servers. This configuration will use the Google DNS servers at 8.8.8.8 and 8.8.4.4. 
To save the file press Ctrl+O this will write the file to the disk – afterwards you can exit nano by pressing Ctrl+X. If nano asks if you want to Save modified buffer? press “Y” followed by hitting enter to confirm the filename.
Next file to edit is /etc/default/isc-dhcp-serveryou can open it in nano using this command:
sudo nano /etc/default/isc-dhcp-server
Scroll down to the line saying interfaces and update the line to say:
INTERFACES="wlan0"
This will make the DHCP server hand out network addresses on the wireless interface. Save the file and exit nano.
The last step in configuring the DHCP server is to configure a static ip address for the wireless network adapter. This is done in the file /etc/network/interfaces – before opening it make sure the WLAN interface is down. Do this with the following commands:
sudo ifdown wlan0
sudo nano /etc/network/interfaces
Change the file to look like this:
interfaces
This will make the wireless adapter take the address 192.168.10.1 in our new local network. Remember to comment out the last 3 lines of the file.
This concludes the setup of the DHCP server – however we still cannot connect to our new network because we have not setup the access point yet. This is done by configuring the hostapd application. 

Configuring HostAPD

To configure HostAPD, open the file called /etc/hostapd/hostapd.conf in your favorite text editor. You can open it in nano using this command:
sudo nano /etc/hostapd/hostapd.conf
The standard configuration will create a new wireless network called wifi with the password YourPassPhrase. You can change the parameter “ssid=wifi” to the SSID wifi name you want and the parameter “wpa_passphrase=YourPassPhrase” to your own password.

This concludes the configuration of our access point software HostAPD. Next up is enabling NAT.

Enable NAT

The last step before we can start the access point is setting up Network Address Translation (NAT). This will make sure that our network traffic will be able to reach the internet using the Raspberry Pi’s Ethernet cable connection to your internet router.
Open /etc/sysctl.conf with:
sudo nano /etc/sysctl.conf
Scroll down to the last line of the file and add the line:
net.ipv4.ip_forward=1
Next start the translation right away by running:
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Start the wireless network by running:
sudo ifup wlan0
Next step is setting up the actual translation between the ethernet port called eth0 and the wireless card called wlan0.
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
With the NAT configured it is now time to run your access point for the first time.

Starting your wireless router

You are now ready to start the DHCP server and the HostAPD access point application.You can do so by running:
sudo service isc-dhcp-server start
sudo hostapd -dd /etc/hostapd/hostapd.conf
At this point you should be able to find your wireless network on your laptop and connect to it and the internet!

Finishing up …

While it is pretty cool you now have your Raspberry Pi running as a wireless access point it is not so cool you have to login every time it reboots to start the HostAPD and DHCP software…
To avoid this run the following commands:
sudo update-rc.d hostapd enable 
sudo update-rc.d isc-dhcp-server enable
Adds "hostapd" to startup by editing 
sudo nano /etc/default/hostapd
and uncomment the following line.
DAEMON_CONF="/etc/hostapd/hostapd.conf"
To avoid having to configure NAT every time the Raspberry Pi reboots you can do the following.
Run this command to backup the NAT configuration.
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
Add the following to the end of the file /etc/network/interfaces to restore the configuration when the network interface comes up:
up iptables-restore < /etc/iptables.ipv4.nat
The file should now look like:
interfaces_iptables
At this point try to reboot the raspberry pi just to make sure everything works as intended – you can reboot with the command:
sudo reboot
Hopefully you will see your new access point come online again. After it comes up again you should be able to connect to it and access the internet!

Friday, February 13, 2015

Setting Up Streamyx For TP-LINK Modem

STEP ONE

Login into TP-LINK modem through any web browser by entering "192.168.1.1" in the address bar(192.168.1.1 is usually the default router address). It will then ask you to enter username & password, which cloud possibly found under the modem or the box of the modem. If neither of those place can password be found, just google for the default username and password for the specified modem model.


STEP TWO

When Setting up the modem, noted that VC(virtual circuit) is needed to be well-set by entering the correct VPI & VCI that TM is using --- That is
  • VPI = 0
  • VCI = 35


STEP THREE

For Encapsulation column, choose PPPoA/ PPPoE(select PPPoE if you can choose for either PPPoA or PPPoE). You will then need to enter a username & password for the PPPoE. Enter your Streamyx credentials so your modem knows how to connect to the ISP. Hit save and you are done.
  • Username: username@streamyx
  • Password: Your Streamyx password
 If you have forgotten your password, you can call TM at 100 to reset your password. (Dial 100, select report line fault, key in your house phone number and speak to the human being)


Sunday, January 4, 2015

Git Push After Removing Large File

Don't revert the commit and then push because the huge file will still be carried around in the history.
Given that you haven't yet pushed it and that the commit you want to redo is the most recent, remove that commit from your history:
$ git reset HEAD^
This will return your index to the state it was in on the parent commit (HEAD^). Now you have a mulligan: add and commit the way you meant to the first time around.
If you've made other subsequent commits, you'll need to git rebase -i <commit> where <commit> is the SHA-1 of the bad commit's parent. SHA-1 can be obtain by git log.
For example (and note the SHA-1 will be different in your repo)
$ git rebase -i 57d0b28
will drop you in an editor that resembles
pick 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar

# Rebase 57d0b28..121802a onto 57d0b28
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
Replace pick with edit on the line with the heavy commit
edit 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar
Save and quit your editor to return to your shell, where you'll see a message of the form
Stopped at 366eca1... This has a huge file
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue
From there, delete the offending file (--cached removes the file from the index only)
$ git rm --cached big-nasty-file
rm 'big-nasty-file'
amend the commit
$ git commit --amend
and finish the rebase
$ git rebase --continue