长风破浪会有时,直挂云帆济沧海(Flora_ssh-D 1.3发布!)

Posted by Leask on October 25, 2009

Flora_ssh-D 1.3 版本发布!

  1. 本版本重新整理程序,实现模块化;
  2. 引入后台运行机制,时刻检查网络;
  3. 简化设定,功能设置只需要留空,该功能就不会执行了;
  4. 直接使用DNS-O-MATIC的API更新动态IP,不再需要额外的程序了。

不推荐升级这个版本,因为1.73版本已经开发出来了,我下一文就是发布1.73的,先发1.3是因为想保持版本的连续性。大家可以忽略。

Google Code上的下载地址为:http://code.google.com/p/flora-ssh-d/downloads/list (包含源代码)

代码如下:


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

(* ======= Variable Declaration ======= *)
global timeTry
global DNSResponse
global isOLast

(* ======= General Settings ======= *)
(* Custom these before you run this script *)

set SSHServerName to "*******" -- 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 DNSUsername to "*******"
set DNSPassword to "*******"
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)

(* ======= Advanced Settings ======= *)
(* You don't need to change these normally *)

(* ======= Main Script ======= *)
set isOLast to ""

repeat
    delay 10000
end repeat
repeat
    if fnCheckNet() is true then
        if isOLast is not "online" then
            fnSSHCnt()
            fnAppStart()
            fnDNSUpdate()
            fnTwitter()
        end if
        set isOLast to "online"
        say "All done!"
    else
        if isOLast is not "offline" then
            fnAppQuit()
        end if
        set isOLast to "offline"
        say "All done!"
    end if
    -- delay 333
    delay 60
end repeat

(* ======= Functions ======= *)
to fnCheckNet()
    try
        do shell script "curl --connect-timeout 7 apple.com/favicon.ico"
        if isOLast is not "online" then
            say "Great! You are online."
        end if
        return true
    on error
        if isOLast is not "offline" then
            say "Opps! You are offline."
        end if
        return false
    end try
end fnCheckNet

to fnCheckSSHD()
    try
        do shell script "curl --socks5 127.0.0.1:7070 --connect-timeout 7 apple.com/favicon.ico"
        say "OK! SSH D has been successfully connected."
        return true
    on error
        if timeTry > 0 then
            say "Opps! SSH D connection failed."
        end if
        return false
    end try
end fnCheckSSHD

to fnAppStart()
    if (count appStList) > 0 then
        say "Start Applications"
        try
            repeat with intSti from 1 to (count appStList)
                tell application (item intSti of appStList)
                    activate
                end tell
            end repeat
        on error
            say "Opps! Error."
        end try
    end if
end fnAppStart

to fnAppQuit()
    if (count appQuitList) > 0 then
        say "Quit Applications"
        try
            repeat with intSti from 1 to count appQuitList
                tell application (item intSti of appQuitList)
                    quit
                end tell
            end repeat
        on error
            say "Opps! Error."
        end try
    end if
end fnAppQuit

to fnSSHCnt()
    if (length of SSHServerName) > 0 and (length of SSHUserName) > 0 and (length of SSHPasswd) > 0 then
        set timeTry to 0
        say "Create SSH D connection"
        repeat while fnCheckSSHD() is false
            if timeTry > 0 then
                if (button returned of (display dialog "Opps! ssh -D connection failed. Do you want to retry?" & return & "" buttons {"Retry", "Quit"})) is "Quit" then
                    exit repeat
                end if
            end if
            fnShellSSH()
            set timeTry to timeTry + 1
        end repeat
    end if
end fnSSHCnt

to fnShellSSH()
    try
        tell application "Terminal"
            quit
            delay 1
            activate
            delay 1
            do script "rm ~/Downloads/.Flora_ssh-D.out" in window 1
            do script "killall ssh" in window 1
            do script "killall ssh-agent" in window 1
            do script "nohup ssh -D 7070 " & SSHUserName & "@" & SSHServerName & " > Downloads/.Flora_ssh-D.out" in window 1
        end tell
        say "SSH D connection request and log in request have been sent. Waiting for response from remote server."
        tell application "Terminal"
            do script SSHPasswd in window 1
            delay 1
            quit
        end tell
    on error
        say "Opps! Error."
    end try
end fnShellSSH

to fnTwitter()
    if (length of TwitterUsername) > 0 and (length of TwitterPassword) > 0 and (length of TwitterText) > 0 then
        say "Update Twitter state"
        try
            do shell script "curl --user " & (quoted form of (TwitterUsername & ":" & TwitterPassword)) & " --data-binary " & (quoted form of ("status=" & TwitterText)) & " https://twitter.com/statuses/update.json"
            say "OK! Twitter state has been successfully updated."
        on error />            say "Opps! Error."
        end try
    end if
end fnTwitter

to fnDNSShell()
    try
        set DNSResponse to (do shell script "curl https://" & DNSUsername & ":" & DNSPassword & "@updates.dnsomatic.com/nic/update?&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG")
        if first word of DNSResponse is "good" then
            return true
        else
            return false
        end if
    on error
        return false
    end try
end fnDNSShell

to fnDNSUpdate()
    if (length of DNSUsername) > 0 and (length of DNSPassword) > 0 then
        say "Update dynamic IP"
        if fnDNSShell() is true then
            say "OK! Dynamic IP has been successfully updated to " & (second word of DNSResponse)
        else
            say "Opps! Error."
        end if
    end if
end fnDNSUpdate