diff options
Diffstat (limited to 'accounts/gkleen@sif/xmonad/lib/XMonad/Prompt/MySsh.hs')
| -rw-r--r-- | accounts/gkleen@sif/xmonad/lib/XMonad/Prompt/MySsh.hs | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/accounts/gkleen@sif/xmonad/lib/XMonad/Prompt/MySsh.hs b/accounts/gkleen@sif/xmonad/lib/XMonad/Prompt/MySsh.hs new file mode 100644 index 00000000..729941aa --- /dev/null +++ b/accounts/gkleen@sif/xmonad/lib/XMonad/Prompt/MySsh.hs | |||
| @@ -0,0 +1,246 @@ | |||
| 1 | module XMonad.Prompt.MySsh | ||
| 2 | ( -- * Usage | ||
| 3 | -- $usage | ||
| 4 | sshPrompt, | ||
| 5 | Ssh, | ||
| 6 | Override (..), | ||
| 7 | mkOverride, | ||
| 8 | Conn (..), | ||
| 9 | moshCmd, | ||
| 10 | moshCmd', | ||
| 11 | sshCmd, | ||
| 12 | inTmux, | ||
| 13 | withEnv | ||
| 14 | ) where | ||
| 15 | |||
| 16 | import XMonad | ||
| 17 | import XMonad.Util.Run | ||
| 18 | import XMonad.Prompt | ||
| 19 | |||
| 20 | import System.Directory | ||
| 21 | import System.Environment | ||
| 22 | import qualified Control.Exception as E | ||
| 23 | |||
| 24 | import Control.Monad | ||
| 25 | import Data.Maybe | ||
| 26 | |||
| 27 | import Text.Parsec.String | ||
| 28 | import Text.Parsec | ||
| 29 | import Data.Char (isSpace) | ||
| 30 | |||
| 31 | econst :: Monad m => a -> E.IOException -> m a | ||
| 32 | econst = const . return | ||
| 33 | |||
| 34 | -- $usage | ||
| 35 | -- 1. In your @~\/.xmonad\/xmonad.hs@: | ||
| 36 | -- | ||
| 37 | -- > import XMonad.Prompt | ||
| 38 | -- > import XMonad.Prompt.Ssh | ||
| 39 | -- | ||
| 40 | -- 2. In your keybindings add something like: | ||
| 41 | -- | ||
| 42 | -- > , ((modm .|. controlMask, xK_s), sshPrompt defaultXPConfig) | ||
| 43 | -- | ||
| 44 | -- Keep in mind, that if you want to use the completion you have to | ||
| 45 | -- disable the "HashKnownHosts" option in your ssh_config | ||
| 46 | -- | ||
| 47 | -- For detailed instruction on editing the key binding see | ||
| 48 | -- "XMonad.Doc.Extending#Editing_key_bindings". | ||
| 49 | |||
| 50 | data Override = Override | ||
| 51 | { oUser :: Maybe String | ||
| 52 | , oHost :: String | ||
| 53 | , oPort :: Maybe Int | ||
| 54 | , oCommand :: Conn -> String | ||
| 55 | } | ||
| 56 | |||
| 57 | mkOverride = Override { oUser = Nothing, oHost = "", oPort = Nothing, oCommand = sshCmd } | ||
| 58 | sshCmd c = concat | ||
| 59 | [ "ssh -t " | ||
| 60 | , if isJust $ cUser c then (fromJust $ cUser c) ++ "@" else "" | ||
| 61 | , cHost c | ||
| 62 | , if isJust $ cPort c then " -p " ++ (show $ fromJust $ cPort c) else "" | ||
| 63 | , " -- " | ||
| 64 | , cCommand c | ||
| 65 | ] | ||
| 66 | moshCmd c = concat | ||
| 67 | [ "mosh " | ||
| 68 | , if isJust $ cUser c then (fromJust $ cUser c) ++ "@" else "" | ||
| 69 | , cHost c | ||
| 70 | , if isJust $ cPort c then " --ssh=\"ssh -p " ++ (show $ fromJust $ cPort c) ++ "\"" else "" | ||
| 71 | , " -- " | ||
| 72 | , cCommand c | ||
| 73 | ] | ||
| 74 | moshCmd' p c = concat | ||
| 75 | [ "mosh " | ||
| 76 | , "--server=" ++ p ++ " " | ||
| 77 | , if isJust $ cUser c then (fromJust $ cUser c) ++ "@" else "" | ||
| 78 | , cHost c | ||
| 79 | , if isJust $ cPort c then " --ssh=\"ssh -p " ++ (show $ fromJust $ cPort c) ++ "\"" else "" | ||
| 80 | , " -- " | ||
| 81 | , cCommand c | ||
| 82 | ] | ||
| 83 | inTmux Nothing c | ||
| 84 | | null $ cCommand c = c { cCommand = "tmux new-session" } | ||
| 85 | | otherwise = c { cCommand = "tmux new-session \"" ++ (cCommand c) ++ "\"" } | ||
| 86 | inTmux (Just h) c | ||
| 87 | | null $ cCommand c = c { cCommand = "tmux new-session -As " <> h } | ||
| 88 | | otherwise = c { cCommand = "tmux new-session \"" ++ (cCommand c) ++ "\"" } | ||
| 89 | withEnv :: [(String, String)] -> Conn -> Conn | ||
| 90 | withEnv envs c = c { cCommand = "env" ++ (concat $ map (\(n, v) -> ' ' : (n ++ "=" ++ v)) envs) ++ " " ++ (cCommand c) } | ||
| 91 | |||
| 92 | data Conn = Conn | ||
| 93 | { cUser :: Maybe String | ||
| 94 | , cHost :: String | ||
| 95 | , cPort :: Maybe Int | ||
| 96 | , cCommand :: String | ||
| 97 | } deriving (Eq, Show, Read) | ||
| 98 | |||
| 99 | data Ssh = Ssh | ||
| 100 | |||
| 101 | instance XPrompt Ssh where | ||
| 102 | showXPrompt Ssh = "SSH to: " | ||
| 103 | commandToComplete _ c = c | ||
| 104 | nextCompletion _ = getNextCompletion | ||
| 105 | |||
| 106 | toConn :: String -> Maybe Conn | ||
| 107 | toConn = toConn' . parse connParser "(unknown)" | ||
| 108 | toConn' :: Either ParseError Conn -> Maybe Conn | ||
| 109 | toConn' (Left _) = Nothing | ||
| 110 | toConn' (Right a) = Just a | ||
| 111 | |||
| 112 | connParser :: Parser Conn | ||
| 113 | connParser = do | ||
| 114 | spaces | ||
| 115 | user' <- optionMaybe $ try $ do | ||
| 116 | str <- many1 $ satisfy (\c -> (not $ isSpace c) && (c /= '@')) | ||
| 117 | char '@' | ||
| 118 | return str | ||
| 119 | host' <- many1 $ satisfy (not . isSpace) | ||
| 120 | port' <- optionMaybe $ try $ do | ||
| 121 | space | ||
| 122 | string "-p" | ||
| 123 | spaces | ||
| 124 | int <- many1 digit | ||
| 125 | (space >> return ()) <|> eof | ||
| 126 | return $ (read int :: Int) | ||
| 127 | spaces | ||
| 128 | command' <- many anyChar | ||
| 129 | eof | ||
| 130 | return $ Conn | ||
| 131 | { cHost = host' | ||
| 132 | , cUser = user' | ||
| 133 | , cPort = port' | ||
| 134 | , cCommand = command' | ||
| 135 | } | ||
| 136 | |||
| 137 | sshPrompt :: [Override] -> XPConfig -> X () | ||
| 138 | sshPrompt o c = do | ||
| 139 | sc <- io sshComplList | ||
| 140 | mkXPrompt Ssh c (mkComplFunFromList sc) $ ssh o | ||
| 141 | |||
| 142 | ssh :: [Override] -> String -> X () | ||
| 143 | ssh overrides str = do | ||
| 144 | let cmd = applyOverrides overrides str | ||
| 145 | liftIO $ putStr "SSH Command: " | ||
| 146 | liftIO $ putStrLn cmd | ||
| 147 | runInTerm "" cmd | ||
| 148 | |||
| 149 | applyOverrides :: [Override] -> String -> String | ||
| 150 | applyOverrides [] str = "ssh " ++ str | ||
| 151 | applyOverrides (o:os) str = case (applyOverride o str) of | ||
| 152 | Just str -> str | ||
| 153 | Nothing -> applyOverrides os str | ||
| 154 | |||
| 155 | applyOverride :: Override -> String -> Maybe String | ||
| 156 | applyOverride o str = let | ||
| 157 | conn = toConn str | ||
| 158 | in | ||
| 159 | if isNothing conn then Nothing else | ||
| 160 | case (fromJust conn) `matches` o of | ||
| 161 | True -> Just $ (oCommand o) (fromJust conn) | ||
| 162 | False -> Nothing | ||
| 163 | |||
| 164 | matches :: Conn -> Override -> Bool | ||
| 165 | a `matches` b = and | ||
| 166 | [ justBool (cUser a) (oUser b) (==) | ||
| 167 | , (cHost a) == (oHost b) | ||
| 168 | , justBool (cPort a) (oPort b) (==) | ||
| 169 | ] | ||
| 170 | |||
| 171 | justBool :: Eq a => Maybe a -> Maybe a -> (a -> a -> Bool) -> Bool | ||
| 172 | justBool Nothing _ _ = True | ||
| 173 | justBool _ Nothing _ = True | ||
| 174 | justBool (Just a) (Just b) match = a `match` b | ||
| 175 | |||
| 176 | sshComplList :: IO [String] | ||
| 177 | sshComplList = uniqSort `fmap` liftM2 (++) sshComplListLocal sshComplListGlobal | ||
| 178 | |||
| 179 | sshComplListLocal :: IO [String] | ||
| 180 | sshComplListLocal = do | ||
| 181 | h <- getEnv "HOME" | ||
| 182 | s1 <- sshComplListFile $ h ++ "/.ssh/known_hosts" | ||
| 183 | s2 <- sshComplListConf $ h ++ "/.ssh/config" | ||
| 184 | return $ s1 ++ s2 | ||
| 185 | |||
| 186 | sshComplListGlobal :: IO [String] | ||
| 187 | sshComplListGlobal = do | ||
| 188 | env <- getEnv "SSH_KNOWN_HOSTS" `E.catch` econst "/nonexistent" | ||
| 189 | fs <- mapM fileExists [ env | ||
| 190 | , "/usr/local/etc/ssh/ssh_known_hosts" | ||
| 191 | , "/usr/local/etc/ssh_known_hosts" | ||
| 192 | , "/etc/ssh/ssh_known_hosts" | ||
| 193 | , "/etc/ssh_known_hosts" | ||
| 194 | ] | ||
| 195 | case catMaybes fs of | ||
| 196 | [] -> return [] | ||
| 197 | (f:_) -> sshComplListFile' f | ||
| 198 | |||
| 199 | sshComplListFile :: String -> IO [String] | ||
| 200 | sshComplListFile kh = do | ||
| 201 | f <- doesFileExist kh | ||
| 202 | if f then sshComplListFile' kh | ||
| 203 | else return [] | ||
| 204 | |||
| 205 | sshComplListFile' :: String -> IO [String] | ||
| 206 | sshComplListFile' kh = do | ||
| 207 | l <- readFile kh | ||
| 208 | return $ map (getWithPort . takeWhile (/= ',') . concat . take 1 . words) | ||
| 209 | $ filter nonComment | ||
| 210 | $ lines l | ||
| 211 | |||
| 212 | sshComplListConf :: String -> IO [String] | ||
| 213 | sshComplListConf kh = do | ||
| 214 | f <- doesFileExist kh | ||
| 215 | if f then sshComplListConf' kh | ||
| 216 | else return [] | ||
| 217 | |||
| 218 | sshComplListConf' :: String -> IO [String] | ||
| 219 | sshComplListConf' kh = do | ||
| 220 | l <- readFile kh | ||
| 221 | return $ map (!!1) | ||
| 222 | $ filter isHost | ||
| 223 | $ map words | ||
| 224 | $ lines l | ||
| 225 | where | ||
| 226 | isHost ws = take 1 ws == ["Host"] && length ws > 1 | ||
| 227 | |||
| 228 | fileExists :: String -> IO (Maybe String) | ||
| 229 | fileExists kh = do | ||
| 230 | f <- doesFileExist kh | ||
| 231 | if f then return $ Just kh | ||
| 232 | else return Nothing | ||
| 233 | |||
| 234 | nonComment :: String -> Bool | ||
| 235 | nonComment [] = False | ||
| 236 | nonComment ('#':_) = False | ||
| 237 | nonComment ('|':_) = False -- hashed, undecodeable | ||
| 238 | nonComment _ = True | ||
| 239 | |||
| 240 | getWithPort :: String -> String | ||
| 241 | getWithPort ('[':str) = host ++ " -p " ++ port | ||
| 242 | where (host,p) = break (==']') str | ||
| 243 | port = case p of | ||
| 244 | ']':':':x -> x | ||
| 245 | _ -> "22" | ||
| 246 | getWithPort str = str | ||
