Sunday, November 8, 2009

Connect to the restricted port of a remote host via SSH tunnel

There are some scenarios where you can use this technique.

A remote server allows only local connections

To connect to such a server, you usually have to copy your application to the remote host where the server is running. Then login to that host and run your application there. If you are developing or testing your application, you may think that it is very inconvenient when you are frequently changing your source code. It will be much better if you can code, compile and test your application on your local machine against the remote server.

Supposed the restricted server program is listening on port 20001 on a remote host named restrictedhost and it only accepts local connections. You can create an SSH tunnel with the following command on your machine.
         ssh -L 30501:127.0.0.1:20001 restrictedhost

Input your password on restrictedhost to login. After you login, keep the terminal aside and alive. A secure SSH channel has been established. Now open another local terminal on your machine. Run your application and have it connect to the port 30501 of your machine. The connection will be forwarded to restrictedhost as a local connection to 127.0.0.1:20001. The remote server application will think your connection is from local to its port 20001 and accept it.

A remote server allows connections from certain hosts

Supposed the restricted server program is listening on port 20001 on a remote host named restrictedhost and it only accepts connections from a range of specific hosts.

If your machine is not in the range of the allowed hosts, you will have to copy your application to one of those allowed hosts in order to test it against the server. With SSH tunnel, you can run your application on your own machine and pretend that it is making connection from one of the allowed hosts, e.g. allowedhost1.
         ssh -L 30501:restrictedhost:20001 allowedhost1

Input your password on allowedhost1 to login. After you login, keep the terminal aside and alive. A secure SSH channel has been established from your machine to allowedhost1. Run your application and have it connect to the port 30501 of your machine. The connection will be forwarded to allowedhost1 as a connection from allowedhost1 to the port 20001 of restrictedhost. The remote server application will think your connection is from allowedhost1 and accept it.

To sum it up, your application makes a connection to a local port and the SSH tunnel makes it like a connection from the host you login to.

Saturday, November 7, 2009

Windows: free more disk space by removing backup files

Windows XP and Windows Vista automatically backup the old system when you install new software or updates. They create restore points for the backups. So if you want, you can return to any of the previous restore points.

If you have used the system for quite a while, you would see that your disk C: is getting full because Windows keeps all those restore points from the day you bought your computer. If your system runs quite stable and you don't need to return to the ancient restore points, you can remove all but the most recent restore point.

Here is how you can do that in Windows XP (Windows Vista is similar).

- Login as Adminstrator.
- From the Start button, select Accessories|System Tools|Disk Cleanup.
- In the Disk Cleanup dialog, click the More Options tab.
- Click the Clean up button in the System Restore section.
- A warning pop-up asks whether you really want to remove all but the most recent restore point. Click the Yes button.

Friday, November 6, 2009

Javascript/Firefox: context menu popup

The context menu of Firefox behaves differently between the Windows and Linux platforms. On Linux, the context menu pops up as soon as you press down the right button of the mouse; While on Windows, it shows when you press down and release the right button.

The Linux style behavior makes it difficult for an extension to capture and hack the mousedown event. We can do a little hack to emulate the Windows style behavior on the Linux platform.

The idea is that we capture the mousedown event and suppress the context menu. When the user release the mouse button, we capture the mouseup event and fire a fake contextmenu event.

Here is the demo source code.


1. Capture the mousedown event and suppress the context menu.

1.1 Capture the mousedown event
addEventListener("mousedown", myMouseDown, true);

function myMouseDown(event)
{
   if (2 == event.button)    // right-click
   {
      if (navigator.platform != "Win32")   // No need for Windows
      {
         // Capture the contextmenu event.
         addEventListener("contextmenu", myNoContextMenu, true);

         // remove the listener right after to avoid mess up the other
         // contextmenu events.
         setTimeout(function(){ removeEventListener("contextmenu", myNoContextMenu, true); }, 0);
      }
   }
}

1.2 Suppress the context menu
function myNoContextMenu(event)
{
   // Prevent the default action, i.e. context menu poping up
   event.preventDefault();
   event.stopPropagation();
}


2. Capture the mouseup event and pop up the context menu.

1.1 Capture the mouseup event
addEventListener("mouseup", myMouseUp, true);

function myMouseUp(event)
{
   if (2 == event.button)   // right-click
   {
      myShowContextMenu(event);
   }
}


1.2 Pop up the context menu
function myShowContextMenu(event)
{
   if (navigator.platform == "Win32")
      return;  // on Window context menu is already shown on mouseup.

   // create a contextmenu event.
   var newEv = event.view.document.createEvent("MouseEvents");
   newEv.initMouseEvent("contextmenu", true, true, event.view, 1,
             event.screenX, event.screenY, event.clientX, event.clientY,
             false, false, false, false, 2, null);

   // fire the new event.
   event.originalTarget.dispatchEvent(newEv);
}

Wednesday, November 4, 2009

Firefox Extensions: Table Of Contents

Simple Boss Key
A lightweight extension that lets you use the F12 key to minimize the current browser window.

Take A Break
By reminding you when to take a break during surfing, it tries to help you avoid excessive web browsing and keep healthy.

Boss Key and Buttons
You can press the F12 key or both the mouse buttons simultaneously to hide all your browser windows.


Support
If you want to report bugs, please add your comments to the page of the extension in question. Please provide the following information:
  • OS type and version;
  • Firefox version;
  • The version of the extension;
  • The steps to reproduce the bug.
If possible, please try the latest version of the extension first before you report a bug.

Firefox Extension: Boss Key and Buttons

This Firefox extension allows you to quickly minimize your browser windows by pressing the F12 key or both the left and right mouse buttons.


Features
- Press the F12 key to minimize your browser windows.

- Press both the mouse buttons (left and right) down to minimize your browser windows.

- Open and focus on a new tab with the URL you preset.

- Hide the tab-bar.

- Press Shift-F12 to show the tab bar and close the new tab opened by the boss key or buttons.

- Minimize the browser window.

- Do the hiding for all the open browser windows.


Installation
Download and install it here: https://addons.mozilla.org/zh-CN/firefox/addon/46553

You will need to restart Firefox browser after the installation.


Options setting


- Enable F12: enable/disable boss key F12. By default, it is enabled. It may be obvious but I want to mention that the Firefox browser window must be the active window you are using. If it were at the background, it could not capture the key pressing event and would not do anything.

- Enable mouse: enable/disable using mouse to hide windows. By default, it is enabled. You must press down (and hold) both the left and right mouse buttons together inside your browser window. Do NOT simultaneously press both buttons. Instead, press down one button and while holding it down, press down another button. If you are using Linux, avoid clicking on the Flash plug-in, which seems to have a higher priority to capture the mouse pressing events. You might want to practise a little bit.

- Open a new tab and load URL: if checked and the URL is supplied, the boss key or buttons would load the URL in a new tab and bring the new tab to the front. Avoid using this new tab because when you press Shift-F12, it will be closed.

- Hide tab-bar: if checked, the boss key or buttons would hide the tab-bar. Pressing Shift-F12 will show the tab-bar again. If you open a new tab by using menu File|New Tab or Ctrl-T, the tab-bar will show up too.

- Minimize window: if checked, the boss key or buttons would minimize the browser window.

- Apply to all windows: if checked, the above actions would be applied to all the open browser windows when the boss key or buttons were pressed.


Restore
You can press Shift-F12 to restore the browser window to the previous status before you pressed the boss key or buttons. It would close the new tab and show the tab-bar. Please do not use the new tab.

If you accidentally used the new tab and it were closed by Shift-F12, don't panic, you could get it back from the menu History|Recently Closed Tab.


---
If you like this extension, please share it with your friends.

Monday, November 2, 2009

Ubuntu Linux: clean cached apt archives to free up disk space

If you are using Ubuntu or other Debian like Linux, you may notice each upgrade or update may eat up some of your disk space -- depending on your setup. That is because all the update packages you have downloaded are kept under the directory of /var/cache/apt/archives.

From time to time, you could run command:
        sudo apt-get clean
to remove all those downloaded packages.

If you only want to remove the useless old packages, you can use command:
        sudo apt-get autoclean

You can also do it with the graphic tool Synaptic Package Manager. Click its menu Settings|Preferences. In the pop-up Preferences dialog, select Files tab. Under the Temporary Files section, choose what you want to delete and click the Delete Cached Package Files button.

Sunday, November 1, 2009

Google Docs - spreadsheet - insert chart on another sheet

To insert a chart for your spreadsheet data, you can highlight the wanted cells and select menu Insert|Chart.... The new chart will be put somewhere on the same sheet of the data.

If you click on the chart, its menu will be shown.


By selecting its menu Chart|Move to own sheet..., you can move the chart to another sheet. The chart will have its own sheet and it will fill the whole new sheet.

This may be not what you want. Sometimes, you want a chart sheet which contains several charts representing data from other sheets. To do that, you can add a new sheet first by clicking the Add Sheet button at the bottom-left corner.


Select the new sheet. Insert a new chart by menu Insert|Chart.... In the Create Chart dialogue, you need to manually input what data you want to use. If the data are from Sheet1, you need to add Sheet1! right before the cells range.


Click Save chart button and you have a chart for the data from Sheet1. You can add more charts for the data from different sheets in this way, so that you can have a single "chart sheet" for the whole document.