Setup Web Dev Env in MacBook

Since Apple’s MacBook notebook uses OS/X that is built on top of Unix, it’s very like a server environment (while in Windows, we need to install something like Cygwin). So we can use this as a development environment. The problem is that it’s not so apparent to find where and how Apple organizes the things. For web development, it’s as follows:

Apache2

Apache2 is pre-installed.

  • script: apachectl (in path)
  • config: /etc/apache2/*
  • htdocs: /Users/<user_id>/Sites
  • URL: http://localhost/~user_id/
  • log: /var/log/apache2/*

PHP

PHP 5 is also pre-installed, we need to enable it in Apache’s config file: uncomment the line with ‘php’:

LoadModule php5_module        libexec/apache2/libphp5.so

add php type in “<IfModule mime_module>” block as follows:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Restart Apache, we can pass the php test by creating a test.php as follows and put it in ‘Sites’ dir:

<?php phpinfo(); ?>

Perl & CGI

Now comes the good-old Perl CGI. According to Apache’s configuration, the CGI dir is “/Library/WebServer/CGI-Executables”, it’s mapped to URL “http://localhost/cgi-bin”. To make it easier, we can create a soft link in home dir:

ln -s /Library/WebServer/CGI-Executables Cgi

Now it can be tested with following script:

#!/usr/bin/perl -wT

print "Content-Type: text/plain\n\n";

print "Hello CGI!\n";

mod_perl

Add following lines in proper  places:

 # load it
 LoadModule perl_module        libexec/apache2/mod_perl.so

# add a place to test mod_perl scripts
 ScriptAliasMatch ... (omitted, refer to the CGI one)

 <Directory "/var/www/cgi-bin/mod_perl">
 SetHandler perl-script
 PerlHandler ModPerl::Registry
 Options +ExecCGI
 PerlOptions +ParseHeaders
 </Directory>

Now we are pretty much done. The only remaining thing that is good to have is MySQL…

Grab the Internet Trouble Makers!

There are a lot of people trying to crack or spam others’ web sites (in vain)… What they usually do can be categorized into two types: one is trying to post their articles (usually advertisements for drugs and so on) automatically to a lot of forums; the other one is trying to download files that the web masters might put there carelessly (for example, *.mdb, web.zip, etc.).

I don’t care about this too much, unless they keep doing this all the time, which consumes my band width. In order to grab them out and filter their requests out using a firewall, I did the follows:

First, in Apache’s configuration file, filter all these suspecious requests into a separate log file. For example, I am using Apache in Linux, but if someone requests for /forum/post.asp, or /data.mdb, then it’s for sure that they are from crackers or spammers. I put all of these in a file called worm_log (these things used to be from worms…).

Then I wrote a small script to calculate the number of hostile requests:

#!/bin/sh

allIPFile=/tmp/ips.txt
ipListFile=/tmp/ip_list.txt

myTmpFile=/tmp/mytmp.txt

cat worm* | cut -d ' ' -f 1 | sort > $allIPFile

cat $allIPFile | uniq > $ipListFile

lines=`cat $ipListFile`

rm -f $myTmpFile > /dev/null 2>&1

for ip in $lines ; do
        n=`grep $ip $allIPFile | wc -l`
        echo "$ip: $n" >> $myTmpFile
done

cat $myTmpFile | sort -nr -k 2,2 > spammers.txt

The output is something as follows:

222.73.173.10: 404
58.215.65.183: 355
222.73.173.11: 351
210.83.81.80: 314
121.14.212.82: 140
118.102.26.197: 117
221.231.114.10: 56
221.5.6.198: 48
...

Now we are very clear about who are the top trouble makers and should be blocked out of the firewall.

Got permanent residency

After working here for more than 10 years, and waiting for several months, I eventually got the permanent residency on last Friday. I am very happy with this since now I have much bigger freedom than before.

Ordinary visas have strict limitations. I hadn’t been aware of this before, until a friend of mine was sent back to China for working on another job, that is not in the same area as indicated by her visa… That was a terrible lesson.

Getting a permanent residency has following benefits:

  1. Do what you want to with no limitation (if it’s legal…)
  2. much higher creditworthiness (for example, when you apply for a mortgage loan, or do business)
  3. no expiry (but reentry permission has)
  4. unemployment protection: according to the law, a foreigner is to be sent back if he cannot get a job in three months after losing his job. Since unemployment insurance starts the first payment after three months from the time when one loses his job, this is unfair for foreigners who also keep paying for unemployment insurance (and have to do so). With PR, this won’t be a problem.

PC Awaker

Click to download it.

Click to download it.

Usually there are a lot of rules in global companies. One of them is the rule of forcing PC go into screen saving after 5 minutes, and if you want to use the PC again, you have to enter your password to unlock the PC.

I admit that this is necessary for security reasons. But sometimes it’s not so reasonable for some of the PCs. For example, if one needs to support a product in a fast paced environment, they cannot afford entering password every time they need the PC – and in fact, the PC’s screen should never be turned off during the work time at all.

The tricky part is that usually we cannot change the screen saver settings because this feature is locked to enforce this rule. In this case, this ‘PC Awaker’ comes to the rescue.

This application is a tiny Windows utility, it is designed as a ‘tray application’, that is, it usually stays in the ‘tray’ of Windows task bar, unless you bring it out. The way how it keeps PC ‘awake’ is to send simulated key strokes periodically – to press F14 key every 4 minutes, to be precise. These are hardcoded since I don’t think it’s necessary to make these configurable.

The license of this software is ‘HSL‘. Help yourself if you need it.

Download it here. It was built in Windows XP using Visual C++ 6.0. I hope that it can work in Windows 9x/ME/NT/2000/XP. Don’t know if it works in Windows Vista…

My Tetris with AI Functionalities

(click on image to play tetris)

Tetris game was invented by a Russian scientist. The concept is very simple but effective – even today there are a lot of people enjoying this game.

I am not only interested in playing this game, but also interested in writing it (since I am a programmer 🙂 ). It has been my habit to implement Tetris games to practise GUI programming with a newly learned language. The earliest one was done on MS-DOS using C language (in an Borland IDE called Turbo C, by the way), I worked on some tricks like writing directly to Video-RAM to increase the performance, etc; then I did it using Visual Basic 6.0; then Visual C++ 6.0. Unfortunately all these versions are lost due to hardware upgrades.

Then in 2000, I joined a New York based company called Financial Sciences Company. There Java was chosen as the main language to work on their next generation trading applications. I knew nothing about Java at that time (I was accepted because of my C++ skills). So I learned it by reading ‘Core Java 2’ and ‘Professional Java Server Programming (J2EE edition)’, and practised and sharpened the skill by writing some programs in my spare time. This implementation of Tetris was the result of my study and practice of Java GUI programming using Swing. I think that it is the best one among my Tetris series.

Lately I don’t have the time to play with these Swing stuffs any more, so I just got some time to improved the algorithm in AI part to make it demo better. You can choose ‘X’ level to see the demo.

Features:

* Employing M-V-C pattern, game logic, data and the game screen are seperated, this makes the code very clean;
* The game panel is encapsulated as a component, so it can be dropped to any container conveniently using an IDE;
* Except the control buttons etc., it doesn’t use any control in Java API, instead, it draws directly to screen;
* It supports multimedia;
* It supports internationalization (i18n), with built in English, Chinese (simplified & traditional) and Japanese languages support;
* It has a very easy-to-use and intuitive user interface;
* It has an effective AI logic to demonstrate how to play this game.

About AI:

The AI part was implemented in following way:

The basic idea was to try to turn and put the coming block at each possible positions, evaluate the results, then choose the best one. But how to evaluate the result is the main problem.

First, we need to find which factors affect the final result, and get them out. For example, the height is definitely not a good thing (0 is the best); and holes under other blocks are evil, and so on. Then put these factors into a formula, with a set of coefficients, one for each of the factors. Now the remaining problem is to find the optimal vector of coefficients.

In order to do that, a training program is needed. I prefer the ‘quick-death’ way to screen out the bad ones as quickly as possible. So I just put it in a server and let it run during the day while I left home to work, and when I came back home at night, I got the result…

Click here to have fun with this Tetris game!

Japanese Calendar

(click on image to use calendar)

(click on image to use calendar)

This is an online Japanese calendar, based on the wonderful online Chinese calendar written by Sean Lin.

This is very handy to check when you don’t have a calendar at hand. I made mistakes like rushing to office and eventually found that I was the only one there because it was a holiday. This calendar can help to prevent this kind of mistakes.

What I did:

  • replaced Chinese holidays with Japanese holidays
  • added the algorithm of calculating ‘furikae kyuujitu’ (振替休日)
  • added calculation of ‘rokuyou’ (六曜)
  • i18n & l10n
  • designed the GUI or the layout as shown in the screen shot.

Check Japanese calendar here.