我优雅一转身,墙应声而倒。我挥一挥衣袖,不带走一片砖瓦。

来源:http://honeonet.spaces.live.com/Blog/cns!15BAC1A170471DB!15070.entry

Screen shot 2009-10-24 at 4.04.09 PM 

又到了周末翻墙的时间了。
使用了一段时间的ssh -D,我终于找回了做人的感觉,自由的空气如此新鲜,信息的绿洲让人神往,流连忘返。
ssh -D如此便利,速度如此流畅,让人印象深刻,但是美中不足的是每次都需要先连接,虽然上次已经分享过我写的一段脚本,实现在Mac上自动翻墙。但上次的脚本比较粗糙,仓促,于是又有了一些新的想法,目标是让翻墙更优雅、潇洒。

上次的版本是0.3,这次改动比较大我把版本设置为0.7,下面简单说一下几点改进:

  1. 自动判断在线状态,如果你还没有上网,是不会执行翻墙程序的;
  2. 自动判断翻墙结果,如果翻墙失败,程序将自动重新尝试;
  3. 自动更新动态IP,你或许在自己的电脑上使用OpenDNS,host网站,或者其他原因需要更新动态IP(近段时间我将发文谈谈Mac上动态IP的问题,请关注);
  4. 自动执行程序,如果你有一些程序,如聊天软件,邮件检查软件等需要在上网的时候开启,也可以设定;
  5. 自动退出程序,如果你有一些程序,如邮件检查程序,Last.fm等,需要在断网的时候关闭,也可以设定;
  6. 设定是否在翻墙成功的时候更新Twitter状态;
  7. 全程语音提示,告诉你程序目前正在干什么,十分Cool;

主要也就7点打改进,新的脚本还包含了错误语音提示,当过程中发生错误,将语音提醒你是什么原因发生错误。
十分建议把脚本设置为开机执行。

程序已经提交到Google Code,并开放源代码:
地址如下:
http://code.google.com/p/flora-ssh-d/
注意!访问Google Code你可能需要先翻墙。

下面贴出源代码,大家粘贴到AppleScript Editor.app里面,修改参数然后执行就可以了。
有不明白的可以邮件或者GTalk联系我,我的邮件/GTalk/MSN/QQ同为:i@leaskh.com

(* Flora_ssh-D // Version 0.7 // Code by Leask Huang // www.leaskh.com // i@leaskh.com *)

(* ======= Variable Declaration ======= *)
global YourName
global SSHServerName -- SSH Server Name
global SSHLocalPorts -- Local Port
global SSHUserName -- User Name on SSH Server
global SSHPasswd -- Password for SSH Server
global TwitterAPI -- Twitter API URL
global TwitterUsername -- Twitter User Name
global TwitterPassword -- Twitter Password
global TwitterLogin
global TwitterStatus
global JobID
global URLTestNet
global URLTestGFW
global appDNSUpdate
global appStList
global appQuitList
global isConnected
global isSSHCnt
global isUpdateTwitter
global isRunExSoft
global isQuitExSoftware
global isUpdateDNS
global isDelnohup
global strnohup
global timeTry
global isforceQuit

(* ======= General Settings ======= *)
(* You don't need to change these normally *)
set URLTestGFW to "twitter.com" -- Use this URL to check is your are Blocked by GFW
set URLTestNet to "leask.com" -- Use this URL to check if you are online
set SSHLocalPort to "7070" -- Set Socks 5 local port
set TwitterAPI to "https://twitter.com/statuses/update.json" -- Twitter API URL
-- set TwitterAPI to "http://twitter.com/statuses/update.json" -- Simple HTTP Twitter API URL (NOT Recommend)
set isDelnohup to true -- Set it as true to del SSH output informtions every time
set strnohup to "Downloads/.Flora_ssh-D.out" -- Config your SSH output file location
set isUpdateDNS to true -- Config if you want to update your dynamic IP address for OpenDNS / DLinkDDNS / DynDNS
set isUpdateTwitter to true -- Config if you want to update Twitter status every time
set isRunExSoft to true -- Config if you want to launch some aplications when you are online
set isQuitExSoftware to true -- Config if you want to quit some aplications when you are offline

(* ======= User Settings ======= *)
(* Custom these before you run this script *)
set SSHServerName to "***.*******.com" -- Set ServerName, You must set this
set SSHUserName to "*******" -- Set UserName, You must set this
set SSHPasswd to "*******" -- Set Password, You must set this
set TwitterUsername to "" -- If you don't want to update Twitter status, leave it blank
set TwitterPassword to "" -- same as above
set TwitterText to "@" & TwitterUsername & " is online now! // " & (current date) -- same as above
set appDNSUpdate to "DNS-O-Matic Updater" -- Use this application to update your dynamic IP address (or leave it blank)
set appStList to {"Adium", "CoverSutra", "Google Notifier"} -- these aplications will launch while you are online (or leave it blank)
set appQuitList to {"CoverSutra", "Google Notifier"} -- these aplications will quit while you are offline (or leave it blank)

(* ======= Main Script ======= *)
set isConnected to false
set isSSHCnt to false
set timeTry to 0
set isforceQuit to false

try
    do shell script "curl " & URLTestNet
    set isConnected to true
on error
    set isConnected to true -- false
    say "Opps! Your internet isn't ready."
    if (isQuitExSoftware is true) and ((count appQuitList) > 0) then
        repeat with intSti from 1 to count appQuitList
            tell application (item intSti of appQuitList)
                quit
            end tell
        end repeat
    end if
end try

if isConnected is true then
    repeat while isSSHCnt is false
        tell application "System Events"
            if exists process "Terminal" then
                tell application "Terminal"
                    quit
                end tell
                delay 3
                tell application "Terminal"
                    activate
                end tell
            else
                tell application "Terminal"
                    activate
                end tell
            end if
        end tell
        if (isUpdateDNS is true) and ((length of appDNSUpdate) > 0) and (timeTry is 0) then
            tell application appDNSUpdate
                activate
            end tell
        end if
        tell application "Terminal"
            if (isDelnohup is true) and (timeTry is 0) then
                do script "rm ~/" & strnohup in window 1
                delay 1
            end if
            do script "killall ssh" in window 1
            delay 1
            do script "killall ssh-agent" in window 1
            delay 1
            do script "nohup ssh -D " & SSHLocalPort & " " & SSHUserName & "@" & SSHServerName & " > " & strnohup in window 1
        end tell
        if timeTry is 0 then
            say "Hi, " & (short user name of (system info)) & ". I am your Mac. I am configuring the network for you."
            set JobID to 1
            if isUpdateDNS is true then
                say "Process " & JobID & ": Update DNS"
                set JobID to JobID + 1
            end if
        else
            set JobID to JobID + 1
            say "Process " & JobID & ": Update DNS"
            set JobID to JobID + 1
            say "Process " & JobID & ": Get ready to retry."
        end if
        tell application "Terminal"
            do script SSHPasswd in window 1
        end tell
        say "Process " & JobID & ": Create SSH D connection"
        try
            do shell script "curl --socks5 127.0.0.1:" & SSHLocalPort & " " & URLTestGFW
            say "OK! SSH D has been successfully connected."
            set isSSHCnt to true
        on error
            set isSSHCnt to false
            say "Opps! SSH D connection fail."
            if (button returned of (display dialog "Opps! ssh -D connection fail. Do you want to retry?" & return & "" buttons {"Retry", "Quit"})) is "Retry" then
                set timeTry to timeTry + 1
            else
                exit repeat
            end if
        end try
    end repeat
    if isSSHCnt is true then
        if (isUpdateTwitter is true) and ((length of TwitterUsername) > 0) and ((length of SSHPasswd) > 0) and ((length of TwitterText) > 0) then
            try
                set JobID to JobID + 1
                say "Process " & JobID & ": Update twitter state"
                set TwitterLogin to quoted form of (TwitterUsername & ":" & TwitterPassword)
                set TwitterStatus to quoted form of ("status=" & TwitterText)
                do shell script "curl --user " & TwitterLogin & " --data-binary " & TwitterStatus & " " & TwitterAPI
                say "OK! Twitter state has been successfully updated."
            on error
                say "Opps! Error while updating Twitter state."
            end try
        end if
        if (isRunExSoft is true) and ((count appStList) > 0) then
            set JobID to JobID + 1
            say "Process " & JobID & ": Start Applications"
            repeat with intSti from 1 to count appStList
                tell application (item intSti of appStList)
                    activate
                end tell
            end repeat
        end if
    end if
    tell application "Terminal"
        quit
    end tell
    if (isUpdateDNS is true) and ((length of appDNSUpdate) > 0) then
        tell application appDNSUpdate
            quit
        end tell
    end if
    say "All done! Bye!"
end if


没有评论: