Friday, August 1, 2014

Mac系统国内VPN使用技巧-自动区分国内国外IP-自动重启VPN

自动区分国内国外IP

在国内上网的时候,VPN已经是必不可少的工具了,但是如果所有网站都用VPN连的话,某些国内网站会慢的离谱,有些甚至不能使用,作为懒人一个,当然想要有个简单方便的方法,不需要手动开关VPN。

以下教程只适用于Mac系统,目前使用的是10.9,其他版本的系统理论上也可用。

首先你得要一个支持PPTP的VPN,这个基本上是标配了。(OpenVPN也支持,请参考这里

VPN配置的时候,要在System Preference里选择“所有流量都通过VPN”这一选项,然后开启VPN。

进入 https://github.com/fivesheep/chnroutes ,选择Download ZIP。双击下载的ZIP包解压。

接下来开启命令行,


$cd <zip包文件夹>              #进入解压后的ZIP包文件夹 
$python chnroutes.py -p mac   #生成国内、国外IP过滤文件,此时会生成ip-up跟ip-down两个文件 
$sudo cp ip-up ip-down /etc/ppp    #将生成的2个文件拷贝到/etc/ppp 
$sudo chmod a+x /etc/ppp/ip-up /etc/ppp/ip-down   #将这些文件设置为可执行

然后重启VPN,搞定。

测试方法,用浏览器打开youku,播放一下带版权的电影,如果能正常播放就是OK了,否则会提醒仅限大陆播放。

如果出现国内网站不能访问的情况,尝试以下命令,并重连VPN。
$sudo sh /etc/ppp/ip-down 


自动重启VPN

VPN的链接状态不是很稳定,断线重连好麻烦;我希望开机之后就能自动连上VPN。这里介绍一种方法能自动连接/重连VPN。

打开AppleScript,方法:(Applications > Utilities)。在编辑器中输入以下代码:

on idle
    tell application "System Events"
        tell current location of network preferences
            set myConnection to the service "Your VPN Name"
            if myConnection is not null then
                if current configuration of myConnection is not connected then
                    connect myConnection
                end if
            end if
        end tell
        return 120
    end tell
end idle
将“Your VPN Name"换成自己的VPN名称,勾选“Stay Open”选项框,保存一下。将保存的文件拖到任务栏中,右键点击Options->Open at Login。重新登录用户,搞定。以上代码将以每2分钟一次间隔检测VPN链接状态,可以自行修改120为其他数值(单位为秒)。

广告

如果想要一个便宜又好用的VPN,可以试一下风驰网游。99/年,本人正在使用。

Wednesday, May 21, 2014

Decode challenges from the Python Challenge

There is a fun website for Python beginners. Python Challenge. I will post some code I used to solve the problems.

0. 2 to the power of 38.

1. Translate the string by replace every letter with letter position 2 after. Notice the 'z' should translates to 'b'.
text = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
result = '';
for c in text:
    if 'a' <= c <= 'z':
        result += chr((ord(c) - ord('a') + 2) % 26 + ord('a'))
    elif 'A' <= c <= 'Z':
        result += chr((ord(c) - ord('A') + 2) % 26 + ord('A'))
    else:
        result += c
print result

2. Find the hidden word from string
text='.. text from html source..'
chList = {}
for ch in text:
    if ch in chList.keys():
        chList[ch] = chList[ch] + 1
    else:
        chList[ch] = 1

print chList #a,e,i,l,q,u,t,y = 1

for ch in text:
    if ch in 'aeilquty':
        print ch #equality

3. Find the hidden word by using regex


text='.. text from html source..'

pattern = "[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]"

m = re.findall(pattern, text)
result = ""
for str in m:
    result += str[4]
print result


4. Send http request and parse the returning string

import urllib2
import re


link = urllib2.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345")
while link.getcode() == 200:
    #get another nothing code
    text = link.read()

    print text

    pattern = "the next nothing is [0-9]+"

    m = re.search(pattern,text)
    if m:

        tmp = m.group()

        pattern2 = "[0-9]+"

        m = re.search(pattern2, tmp)

        code = m.group()

    else:
        code = str(int(code) / 2)

    url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + code

    link = urllib2.urlopen(url)

5. 'Peak Hell' implies 'pickle', format the string, you will see the result

from __future__ import print_function
import pickle
import urllib2
import sys

text = urllib2.urlopen("http://www.pythonchallenge.com/pc/def/banner.p").read()

result = pickle.loads(text)



for arr in result:
    for tuple in arr:
        print(tuple[0]*tuple[1], end='')
    print('\n') 

Monday, April 21, 2014

A list of solutions to solve the jar hell

Jar Hell is the situation when you have the different incompatible versions of library existed in your classpath simultaneously. For example, you have  a project that depends on library A version 2.0, and you also need a third party library B which depends on library A version 1.0, then the library version 1.0 and version 2.0 will all be included in your classpath, it will cause some problems because machine doesn't know which version to choose in the right place.

I just encountered this problem in my recent project, so I did a lot of research of it. However, in theory, there's no best way to resolve it. So I list all the possible solutions here:

1. Avoid

First of all, you should consider this problem before you proceed. Find all the dependencies that your library will use, and make sure all library versions are compatible. It will save your a lot of troubles!

2. Find a transition version that works for both sides

If it already happened, then you should consider finding a transition version. Library changes gradually, so you may be luck to find a right version.

3. Use Jarjar

If your project won't involve any shared interface between the incompatible versions, then you should definitely try Jarjar, a jar wrap tool that can rename the embedded class files. It works like a charm, will do a lot of work with your imagination. There's a blog talking about it.
However, there are one limitation you should know:
If the incompatible versions of library have different interfaces, and these interfaces will be used in your project, then you are no lucky because even they have the some interface name, they are in different packages(jarjar changed the package name), so they are definitely different interfaces, you can't assign instances directly. 
But there is also a solution to this case, which is to write adapter classes to wrap one interface to the other, it works in some situations.

4. Modify library source code

If you have access to the library source code, you could also change the incompatible code by yourself and wrap it as a customized jar to fulfill your need. It works well when the difference between versions is not that much.

5. Access Rule & Maven solution

I didn't dig into it too much, but it seems workable to some situation. The Access Rule (see this article) can forbid some classes from using. The Maven also have similar access rules.

Monday, February 10, 2014

Mac OS Commands that make your life easier

Access clipboard(pasteboard)

pbcopy & pbpaste
echo 'hello world!' | pbcopy #copy to the pasteboard
echo `pbpaste`

Power Manager Setting

pmset
pmset sleepnow #sleep the machine immediately

Network

Find info of particular port

sudo lsof -P | grep [PORT_NUM]

Process

Find process using PID
ps aux | grep [PID]
Kill Process with PID
kill [PID]