Web front for my code

Awhile ago I set up my own git server. I’ve been hacking happily using that and Eclipse. In my spare time, I’ve been taking UCSD’s wonderful algorithm course: Algorithmic Design and Techniques. The course provides plenty of programming challenges! I chose the paid version so my code can be evaluated against all tests in the grading server. I solved the programming challenges in 3 languages: Java, Python, and C++. It has been so much fun!

Anyway, I thought it’d be nice to enable a web front end to my own code hosted on my own server. There are a lot of choices: gitweb, gitlab, gitea, etc. A couple of days ago I came across cgit. It’s pretty lightweight, written in C. That’s what I chose for that task. It’s setup now already, please go to https://code.haidongji.com to check it out!

It takes a bit of work to get the clean subdomain working. My environment is Debian 9 running apache2. Below were steps taken to make my environment work. Hope it helps somebody!

  • sudo apt install cgit
    The install creates /etc/cgitrc file and /etc/apache2/conf-available/cgit.conf
  • Here is the content of my apache2 site conf file
    <VirtualHost *:80>
            ServerName code.haidongji.com
            ServerAdmin emailAddress
            DocumentRoot /usr/share/cgit/
    	<Directory "/usr/share/cgit/">
    		AllowOverride None
    		Options ExecCGI
    		Order allow,deny
    		Allow from all
    	Alias /cgit-css/cgit.css /usr/share/cgit/cgit.css
    	Alias /cgit-css/cgit.png /usr/share/cgit/cgit.png
    	ScriptAlias / /usr/lib/cgit/cgit.cgi/
            ErrorLog ${APACHE_LOG_DIR}/codehaidongji.log
            CustomLog ${APACHE_LOG_DIR}/codehaidongji.log combined
  • sudo apt install python3-pygments for syntax highlighting
  • Here is the content of the /etc/cgitrc file
    # enable Pygments syntax highlighting. Must be above scan-path, otherwise it would not work!
  • sudo a2enmod cgid
    sudo a2enconf cgit
  • sudo systemctl restart apache2

Note that in /etc/cgitrc, syntax highlighting line MUST BE ABOVE the scan-path line, otherwise syntax highlighting will not work!

Cheers and happy coding!

fail2ban installation and configuration notes

A couple of days ago one web site I volunteer to manage was under DDOS attack. I installed and configured fail2ban to protect us from future similar attacks. Here are some notes. The server is the RedHat/Fedora/CentOS variety, as you can tell from commands listed below. Please translate them to your distro’s corresponding commands as needed.

  • Installation is easy:
    sudo yum install fail2ban

    To make fail2ban starts automatically after a reboot, run this:

    sudo systemctl enable fail2ban

  • Configuring is relatively easy. It’s recommended that you create your own jail configuration file, using the jail.conf from the installation as a starting point. Three things are noteworthy from my experience:
    1. Make sure that you provide the correct log file. For web server, there are typically one access log file and one error log file. Ensure that you feed the right log file when using a particular filter;
    2. On this server, fail2ban didn’t properly expand the log and file names when I put wildcard characters in them. I got around that by listing them one by one.
    3. In the jail.conf file, no default banaction was defined. I added the following line:
    banaction = iptables-multiport
  • To write your own custom filter, make sure you put a sample log entry inside the filter file as a comment. Use the following command to debug your filter:
    sudo fail2ban-regex /path2testLogfile/test.log /etc/fail2ban/filter.d/my-filter.conf
    Here is a filter that I wrote:

    failregex = ^ -.*”POST \/component\/mailto\/\?tmpl=component\&link=aHR0cHM6.*”$

    ignoreregex =

  • After getting your jail.local ready, run the following command to debug any potential issues. I’ve found that if you have issues with your jail or filter files, “sudo systemctl start fail2ban” doesn’t always give you a good enough error message. Use this instead:

    sudo /usr/bin/fail2ban-client -x start

    You may need to start/stop a couple of times. To stop, run

    sudo /usr/bin/fail2ban-client -x stop

  • After debugging, before you finally start fail2ban service, it’s better to search the current access/error log and see if there is a match to the filter you defined. If yes, then take a note of its IP address and the last time it appears in the log file. Then start fail2ban by running
    sudo systemctl enable fail2ban
  • To verify that it works, run iptables -S and if it catches one offender and puts it in jail, you should see it in the output. Now go back to the access/error log and ensure there is no entry from that IP address since the last timestamp.

Good luck in protecting your servers!

Accessing modem status information with Netgear router

If, like me, you bought your own cable modem and Netgear wireless router for Comcast service, the way to access your modem’s status information is different. When using the equipment provided by Comcast, in my case the Technicolor TC8305C, which is a combo of modem, router, and voice, I can easily see modem information while accessing the router page, because it is one device.

To stop paying Comcast’s 10 dollar monthly equipment leasing fee, I purchased my own cable modem and router. I am currently using Netgear Nighthawk R7000. In this combo, to see the modem status information, you need to go to:

By the way, I am thinking about returning the Nighthawk R7000, for the following reasons:
1. Its web interface is really slow and clunky;
2. Its range, according to this page, is not as good as ASUS;
3. More importantly, when assigning static IP address to a device, the device name cannot be more than 20 characters!

If I do return the Nighthawk R7000, I think I’ll try ASUS RT-AC68U. Do you have suggestions?

Eclipse PDT PHP Web Application Run Configuration

I had some trouble setting up Run Configurations in Eclipse for PHP (PDT plugin). Here is my note for future reference.

Machine: Ubuntu 15 64-bit, with Eclipse Mars. sudo apt-get install php5 installed apache2 for me, so no additional web server install is necessary.

  • Enable userdir mod:
    sudo a2enmod userdir
  • Your /etc/apache2/mods-enabled/userdir.conf should look like this, if not, make it so:
    <ifmodule mod_userdir.c>
            UserDir public_html
            UserDir disabled root
            <directory /home/*/public_html>
                    AllowOverride FileInfo AuthConfig Limit Indexes
                    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
                    <limit GET POST OPTIONS>
                            Require all granted
                    <limitexcept GET POST OPTIONS>
                            Require all denied
  • Comment out the user directories section in /etc/apache2/mods-available/php5.conf, like so:
    # Running PHP scripts in user directories is disabled by default
    # To re-enable PHP in user directories comment the following lines
    # (from <ifmodule ...> to </ifmodule>.) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
    #<ifmodule mod_userdir.c>
        #<directory /home/*/public_html>
            #php_admin_flag engine Off
  • Create a public_html directory under your home directory;
  • Run sudo apt-get install php5-xdebug;
  • Run sudo apt-get install php5-mysql, as necessary;
  • Modify /etc/php5/mods-available/xdebug.ini so it has the following lines:
  • In Eclipse, create your PHP web application project under public_html in your home directory;
  • In Eclipse, while under PHP Perspective, click the drop down next to the green run button, and select “Run Configurations…”;
  • Create a new configuration for PHP Web Application. Pay attention to the following two things:
    a. In the Server tab, Server section, the PHP Server should be “Default PHP Web Server”, this is fine.
    b. Click the “Configure…” button, you’ll see this. Fill in proper “Document Root” value, in my case, /home/haidong/public_html
    c. Click the Debugger tab, and pick “XDebug”
    d. Go back to the original configuration screen, pick the proper file, and fill in th URL info, like below

By the way, Happy 2016 all!




  1. 到这里下载客户端软件。一般来讲你要下载64位的。目前的文件名是openvpn-install-2.3.8-I601-x86_64.exe
  2. 下载完毕后安装。它可能会问要不要安装一个叫TAP的东西。你需要安装;
  3. 安装完毕,把那个.ovpn文件挪到C:\Program Files\OpenVPN\config之下
  4. Run OpenVPN GUI as administrator。这只是开启了程序,现在还没有连接到openvpn服务器上;
  5. 在屏幕右下角的空间里,找到OpenVPN GUI的图标,然后选择“connect/连接”;
  6. 连接成功。打开浏览器,试试访问以前不能访问的网站,看效果如何。

Windows 10注意事项
我发现一些不是通过安装而是通过升级到Windows 10的机器会碰到一些问题,我猜和IPv6兼容有关。我发现如果我用火狐浏览器,就可以避免这个问题。全新的Windows好像没这个问题。


  1. 打开命令行,安装openvpn:
    apt-get install openvpn
  2. sudo openvpn --config /EnterPathTo/xxx.ovpn
  3. 你可以看到vpn连接的信息。在使用openvpn的时候,你不能中断或关闭这个程序。你可以把这个Window缩小
  4. 连接成功。打开浏览器,试试访问以前不能访问的网站,看效果如何。
  5. 用完了openvpn,按Ctrl-C就可以终止了。


在App Store里安装免费的OpenVPN Connect。之后你可以把爱疯连到iTunes上,用File Sharing,点击OpenVPN,然后把那个profile的.ovpn的文件拖进来。之后打开这个app,你可以看到一个新的profile可以import。之后就可以顺利连接。

在App Store里找OpenVPN Connect后安装。然后把profile的.ovpn文件传到Android上。打开后利用Import功能,把profile引进后就可以很方便的应用。


PS. 本系列其它文章
自建vpn之三:搭建openvpn service和生成客户端Profile