Code Blog

Displaying 1-4 of 4 results.
2016/04/28 CSS

I had some problems with image flicker when changing background images. Found this tips at stackoverflow how to preload images in CSS, works very well. Just add more urls to preload more images.

 content: url(path/to/image.jpg) url(path/to/image2.jpg); 
2016/04/26 Javascript,Yii

I had read about how to do a parallax effect for my background image (so effectively 1 layer parallax effect). I means that the background image slowly moves along as I scroll the text on my blog. In my case the background moves with 1/5 of the speed of the web page scrolling.

However I ran into some troubles when I happened to write too long post (i.e. many photos in posts quickly adds up in height). I had added some margin by using a tall background image in order to avoid this, but that was just a quick hack, and finally that just didn't seem to be able to solve it. 

Actually I would like the image to wrap around once it runs out of pixels to cover the whole web page window. I guess the setting the CSS background property to "repeat-y" and then use a javascript call to update the height as I scroll should do the trick but I opted for another solution.

 I decided to create 2 images that are shifted half-way in Y direction and swap between them as I scroll down. 

Illustration of switching between the two background images. One good tips I found is to use the "offset" filter in Photoshop and enter a 50% vertical offset and then re-save the image.

  background-position: 50% 0;  
  position: fixed;
  width: 1000px;
  height: 2000px;
  z-index: -1;	

  background-image: url('images/bg1.jpg');

  background-image: url('images/bg2.jpg');

I added a div at the top of the web page: <div class="bg bg1"></div>

function parallax(){
  var scrolled = $(window).scrollTop();
  var imageheight = 2000;
  var bgscroll = scrolled*0.2 % imageheight;

  if ( bgscroll >= imageheight/2 ) {
  } else {

I can then use the CSS class property to switch between background images for the bg div element.

When should the switch happen to the 2nd image? When half the first image has been scrolled up and out of sight. I therefore check for the background image scroll There is a half image height offset for the 2nd image. The image needs to be at least twice at high as the web browser window inner height (not incl toolbars/scrollbars). Please note the image dimension I have used is 1000 x 2000px for this example (width x height) and these values need to be changed for the code to work correctly with other dimensions.

2016/04/22 VPN,Hosting

How to set up Shadowsocks on AWS - This information available at many other websites, my purpose it just to make sure I have all the information collected in my code blog. So this is how to do it:

Log in to the AWS console. During the sign up for EC2 service you will need to confirm your phone number.

Launch instance

Select Ubuntu (I my case Ubuntu Server 14.04 LTS)

Select t2.micro -> Review and launch

Create security group

Configure security group - add rule for tcp 8000-8388 - only allow my ip

Step 7: Click Launch

Optional: Create billing alerts - warn if you pass over a limit 

Create elastic ip - assign to running instance


A good guide is available at


2016/04/03 Network

Linux comes with a built-in firewall called IPTables. It has 3 default chains - INPUT, OUTPUT and FORWARD.

Input chain Coming from outside the firewall to destination inside the firewall
Output chain From inside the firewall going to outside the firewall
Forward chain Routing through the firewall (coming from the outside the firewall passing on to outside the firewall) 

 IPTables rules are normally put in a script file and run during start-up. Each chain will need to have a default policy (set with -P, see example below). The default policy for a chain can be either DROP or ACCEPT. 

Usage Examples:

iptables -P INPUT DROP // Set policy for chain to deny connections (drop packets)

iptables -A INPUT -p tcp --dport 8080 -j ACCEPT   // Add rule to end of chain: Accept incoming tcp connections at port 8080

iptables -A INPUT -j SHADOWSOCKS // Add to end of chain: Jump from Input chain to Shadowssocks chain (and will automatically go back to Input chain after finishing Shadowsocks rules)  

 Nat table (network address translation)

 iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080 // Redirect incoming tcp traffic from port 80 to port 8080

 iptables -I INPUT -j SHADOWSOCKS // Insert at beginning of Input chain

// Count traffic coming in/out from

iptables -A FORWARD -s

iptables -A FORWARD -d 

iptables -nvx -L FORWARD // Print traffic counters

iptables -Z  // Clear all counters (for all rules)

iptables -Z FORWARD // Clear counters for Forward rule

iptables -R FORWARD 1 -s // Replace rule nr 1 in Forward chain with same rule. This will reset counter

iptables -I FORWARD -i eth0 -j TRAFFIC_ACCT_IN // incoming interface eth0

iptables -I FORWARD -o eth0 -j TRAFFIC_ACCT_OUT // outgoing interface eth0

iptables -vnxL FORWARD | awk '{print $2}' // print the 2nd column

iptables -vnxL FORWARD | awk '/delegate_forward/ {print $2}' // Print 2nd word in line that contains 'delegate_forward' text

The important rules regarding NAT are found in the 'nat'-table. This table has three predefinded chains: PREROUTING, OUTPUT und POSTROUTING.


// How is Shadowsocks set-up? I want to count the total traffic going through the router, but I also want to count the amount of data sent through Shadowsocks tunnel. The Shadowsocks tunnel is listening at port 1080. 

iptables -N SHADOWSOCKS // Create new chain with name "Shadowsocks"


iptables -A SHADOWSOCKS -p tcp // Count all incoming traffic

iptables -A SHADOWSOCKS -p tcp -dport 1080 // Does this work?

iptables -L SHADOWSOCKS -n -v -x // Display traffic data for Shadowsocks chain

The -x flag means you will get the exact value of the packet and byte counters, instead of only the rounded number in K’s (multiples of 1000) M’s (multiples of 1000K) or G’s (multiples of 1000M).