(* Flora_ssh-D // Version 1.73 // Code by Leask Huang // www.leaskh.com // i@leaskh.com *)
(* ======= Variable Declaration ======= *)
global timeTry
global DNSResponse
global isOLast
global iTsCurDBID
global iTsORSoundVolume
global strSNGArtist
global strSNGName
global strSNGAlbum
global strSNGTrackNumber
global strSNGDuration
(* ======= General Settings ======= *)
(* Custom these before you run this script *)
property SSHServerName : "***.*******.com" -- Set ssh -D ServerName (or leave it blank)
property SSHUserName : "*******" -- Set UserName (or leave it blank)
property SSHPasswd : "*******" -- Set Password (or leave it blank)
property TwitterUsername : "*******" -- If you don't want to update Twitter status, leave it blank
property TwitterPassword : "*******" -- same as above
property TwitterText : "@" & TwitterUsername & " is online now! // " & (current date) -- same as above
property DNSUsername : "*******" -- Set DNS UserName (or leave it blank)
property DNSPassword : "*******" -- Set Password (or leave it blank)
property LastfmUsername : "*******" -- Set Last.fm UserName (or leave it blank)
property LastfmPassword : "*******" -- Set Password (or leave it blank)
property appStList : {"***", "***"} -- these aplications will launch while you are online (or leave it blank)
property appQuitList : {"***", "***"} -- these aplications will quit while you are offline (or leave it blank)
(* ======= Main Script ======= *)
on run
set isOLast to ""
set iTsCurDBID to ""
end run
on quit
continue quit
end quit
on idle
if fnCheckNet() is true then
fnSSHCnt()
if isOLast is not "online" then
fnAppStart()
fnDNSUpdate()
fnTwitter()
end if
fnLastfm()
set isOLast to "online"
else
if isOLast is not "offline" then fnAppQuit()
set isOLast to "offline"
end if
return 180
end idle
(* ======= Functions ======= *)
to fnCheckNet()
try
do shell script "curl --connect-timeout 33 \"apple.com/favicon.ico\""
if isOLast is not "online" then SmartSay("Online")
return true
on error
if isOLast is not "offline" then SmartSay("Offline")
return false
end try
end fnCheckNet
to fnAppStart()
if (count appStList) > 0 then
SmartSay("Start Apps")
try
repeat with intSti from 1 to (count appStList)
tell application (item intSti of appStList) to activate
end repeat
SmartSay("OK")
on error
SmartSay("Failed")
end try
end if
end fnAppStart
to fnAppQuit()
if (count appQuitList) > 0 then
SmartSay("Quit Applications")
try
repeat with intSti from 1 to count appQuitList
tell application (item intSti of appQuitList) to quit
end repeat
SmartSay("OK")
on error
SmartSay("Failed")
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
repeat while fnCheckSSHD() is false
SmartSay("Connect SSH D")
if timeTry > 0 then if (button returned of (display dialog "ssh -D connection failed." & return & "" buttons {"Retry", "Ignore"})) is "Ignore" then exit repeat
fnShellSSH()
set timeTry to timeTry + 1
end repeat
end if
end fnSSHCnt
to fnCheckSSHD()
try
do shell script "curl --socks5 127.0.0.1:7070 --connect-timeout 33 \"apple.com/favicon.ico\""
if timeTry > 0 then SmartSay("OK")
return true
on error
if timeTry > 0 then SmartSay("Failed")
return false
end try
end fnCheckSSHD
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
SmartSay("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
end try
end fnShellSSH
to fnTwitter()
if (length of TwitterUsername) > 0 and (length of TwitterPassword) > 0 and (length of TwitterText) > 0 then
SmartSay("Update Twitter state")
try
do shell script "curl --connect-timeout 33 --user " & (quoted form of (TwitterUsername & ":" & TwitterPassword)) & " --data-binary " & (quoted form of ("status=" & TwitterText)) & " \"https://twitter.com/statuses/update.json\""
SmartSay("OK")
on error
SmartSay("Failed")
end try
end if
end fnTwitter
to fnDNSShell()
try
set DNSResponse to (do shell script "curl --connect-timeout 33 \"https://" & DNSUsername & ":" & DNSPassword & "@updates.dnsomatic.com/nic/update?&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG\"")
if first word of DNSResponse is "good" then -- (second word of DNSResponse) is IP address
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
SmartSay("Update dynamic IP")
if fnDNSShell() is true then
SmartSay("OK")
else
SmartSay("Failed")
end if
end if
end fnDNSUpdate
to fnURLec(strToUEC)
return do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' \"" & strToUEC & "\""
end fnURLec
to fniTunesisActive()
tell application "System Events" to return (name of processes contains "iTunes")
end fniTunesisActive
to fuisiTunesNUD()
if fniTunesisActive() is true then
tell application "iTunes"
if player state is playing then
set iTsNewDBID to (get database ID of current track)
if iTsNewDBID is not iTsCurDBID then
set strSNGArtist to (get artist of current track)
set strSNGName to (get name of current track)
set strSNGAlbum to (get album of current track)
set strSNGTrackNumber to (get track number of current track)
set strSNGDuration to (get duration of current track as integer)
set iTsCurDBID to iTsNewDBID
return true
else
return false
end if
else
return false
end if
end tell
else
return false
end if
end fuisiTunesNUD
to SmartSay(strToSay)
set isiTPing to false
if fniTunesisActive() is true then
tell application "iTunes" to if player state is playing then set isiTPing to true
end if
if isiTPing is true then
tell application "iTunes"
set iTsORSoundVolume to sound volume
set sound volume to iTsORSoundVolume * 0.333
end tell
say "" & strToSay
tell application "iTunes" to set sound volume to iTsORSoundVolume
else
say "" & strToSay
end if
end SmartSay
to fnLastfm()
if fuisiTunesNUD() is true then
if (length of strSNGName) > 0 or (length of strSNGArtist) > 0 then
tell application "iTunes"
set iTsORSoundVolume to sound volume
set sound volume to iTsORSoundVolume * 0.333
end tell
if (length of strSNGName) > 0 then
say strSNGName
delay 1
end if
if (length of strSNGArtist) > 0 then say strSNGArtist
tell application "iTunes" to set sound volume to iTsORSoundVolume
end if
if (length of LastfmUsername) > 0 and (length of LastfmPassword) > 0 then
try
do shell script "curl --connect-timeout 33 \"http://lastfmstats.livefrombmore.com/universalscrobbler/scrobble.php?submissionType=track" & "&username=" & LastfmUsername & "&password=" & LastfmPassword & "&artist=" & fnURLec(strSNGArtist) & "&track=" & fnURLec(strSNGName) & "&album=" & fnURLec(strSNGAlbum) & "&number=" & strSNGTrackNumber & "&duration=" & strSNGDuration & "\""
on error
SmartSay("Scrobbling Failed")
end try
end if
end if
end fnLastfm