2016-12-17 20:58:05 +00:00
|
|
|
{- Magic Wormhole integration
|
|
|
|
-
|
|
|
|
- Copyright 2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
2016-12-18 20:50:58 +00:00
|
|
|
module Utility.MagicWormhole (
|
2016-12-17 22:25:33 +00:00
|
|
|
Code,
|
|
|
|
mkCode,
|
2016-12-18 20:50:58 +00:00
|
|
|
toCode,
|
|
|
|
fromCode,
|
2016-12-17 22:25:33 +00:00
|
|
|
validCode,
|
|
|
|
CodeObserver,
|
|
|
|
CodeProducer,
|
|
|
|
mkCodeObserver,
|
|
|
|
mkCodeProducer,
|
|
|
|
waitCode,
|
|
|
|
sendCode,
|
|
|
|
WormHoleParams,
|
2017-02-03 19:06:17 +00:00
|
|
|
appId,
|
2016-12-17 22:25:33 +00:00
|
|
|
sendFile,
|
|
|
|
receiveFile,
|
2016-12-18 21:01:15 +00:00
|
|
|
isInstalled,
|
2016-12-17 22:25:33 +00:00
|
|
|
) where
|
2016-12-17 20:58:05 +00:00
|
|
|
|
|
|
|
import Utility.Process
|
|
|
|
import Utility.SafeCommand
|
|
|
|
import Utility.Monad
|
|
|
|
import Utility.Misc
|
2016-12-17 21:28:08 +00:00
|
|
|
import Utility.Env
|
2016-12-18 21:01:15 +00:00
|
|
|
import Utility.Path
|
2020-06-03 17:15:01 +00:00
|
|
|
import Utility.Exception
|
2016-12-17 20:58:05 +00:00
|
|
|
|
|
|
|
import System.IO
|
|
|
|
import System.Exit
|
|
|
|
import Control.Concurrent
|
2018-07-04 19:14:03 +00:00
|
|
|
import Control.Concurrent.Async
|
2016-12-17 20:58:05 +00:00
|
|
|
import Data.Char
|
2016-12-18 20:50:58 +00:00
|
|
|
import Data.List
|
2016-12-30 15:10:20 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Prelude
|
2016-12-17 20:58:05 +00:00
|
|
|
|
|
|
|
-- | A Magic Wormhole code.
|
2016-12-17 22:25:33 +00:00
|
|
|
newtype Code = Code String
|
2016-12-18 20:50:58 +00:00
|
|
|
deriving (Eq, Show)
|
2016-12-17 22:25:33 +00:00
|
|
|
|
|
|
|
-- | Smart constructor for Code
|
|
|
|
mkCode :: String -> Maybe Code
|
|
|
|
mkCode s
|
|
|
|
| validCode s = Just (Code s)
|
|
|
|
| otherwise = Nothing
|
2016-12-17 20:58:05 +00:00
|
|
|
|
2016-12-18 20:50:58 +00:00
|
|
|
-- | Tries to fix up some common mistakes in a homan-entered code.
|
|
|
|
toCode :: String -> Maybe Code
|
|
|
|
toCode s = mkCode $ intercalate "-" $ words s
|
|
|
|
|
|
|
|
fromCode :: Code -> String
|
|
|
|
fromCode (Code s) = s
|
|
|
|
|
2016-12-17 20:58:05 +00:00
|
|
|
-- | Codes have the form number-word-word and may contain 2 or more words.
|
|
|
|
validCode :: String -> Bool
|
|
|
|
validCode s =
|
|
|
|
let (n, r) = separate (== '-') s
|
|
|
|
(w1, w2) = separate (== '-') r
|
|
|
|
in and
|
|
|
|
[ not (null n)
|
|
|
|
, all isDigit n
|
|
|
|
, not (null w1)
|
|
|
|
, not (null w2)
|
|
|
|
, not $ any isSpace s
|
|
|
|
]
|
|
|
|
|
2016-12-17 22:25:33 +00:00
|
|
|
newtype CodeObserver = CodeObserver (MVar Code)
|
2016-12-17 20:58:05 +00:00
|
|
|
|
2016-12-17 22:25:33 +00:00
|
|
|
newtype CodeProducer = CodeProducer (MVar Code)
|
2016-12-17 20:58:05 +00:00
|
|
|
|
|
|
|
mkCodeObserver :: IO CodeObserver
|
2016-12-17 22:25:33 +00:00
|
|
|
mkCodeObserver = CodeObserver <$> newEmptyMVar
|
|
|
|
|
|
|
|
mkCodeProducer :: IO CodeProducer
|
|
|
|
mkCodeProducer = CodeProducer <$> newEmptyMVar
|
2016-12-17 20:58:05 +00:00
|
|
|
|
|
|
|
waitCode :: CodeObserver -> IO Code
|
2016-12-27 20:26:26 +00:00
|
|
|
waitCode (CodeObserver o) = readMVar o
|
2016-12-17 20:58:05 +00:00
|
|
|
|
2016-12-17 22:25:33 +00:00
|
|
|
sendCode :: CodeProducer -> Code -> IO ()
|
|
|
|
sendCode (CodeProducer p) = putMVar p
|
|
|
|
|
|
|
|
type WormHoleParams = [CommandParam]
|
2016-12-17 20:58:05 +00:00
|
|
|
|
2017-02-03 19:06:17 +00:00
|
|
|
-- | An appid should be provided when using wormhole in an app, to avoid
|
|
|
|
-- using the same channel space as ad-hoc wormhole users.
|
|
|
|
appId :: String -> WormHoleParams
|
|
|
|
appId s = [Param "--appid", Param s]
|
|
|
|
|
2016-12-17 22:25:33 +00:00
|
|
|
-- | Sends a file. Once the send is underway, and the Code has been
|
|
|
|
-- generated, it will be sent to the CodeObserver. (This may not happen,
|
|
|
|
-- eg if there's a network problem).
|
2016-12-17 20:58:05 +00:00
|
|
|
--
|
|
|
|
-- Currently this has to parse the output of wormhole to find the code.
|
|
|
|
-- To make this as robust as possible, avoids looking for any particular
|
|
|
|
-- output strings, and only looks for the form of a wormhole code
|
2016-12-17 21:36:55 +00:00
|
|
|
-- (number-word-word).
|
|
|
|
--
|
|
|
|
-- Note that, if the filename looks like "foo 1-wormhole-code bar", when
|
|
|
|
-- that is output by wormhole, it will look like it's output a wormhole code.
|
2016-12-17 20:58:05 +00:00
|
|
|
--
|
|
|
|
-- A request to make the code available in machine-parsable form is here:
|
|
|
|
-- https://github.com/warner/magic-wormhole/issues/104
|
|
|
|
sendFile :: FilePath -> CodeObserver -> WormHoleParams -> IO Bool
|
2016-12-17 22:25:33 +00:00
|
|
|
sendFile f (CodeObserver observer) ps = do
|
2016-12-17 21:28:08 +00:00
|
|
|
-- Work around stupid stdout buffering behavior of python.
|
|
|
|
-- See https://github.com/warner/magic-wormhole/issues/108
|
|
|
|
environ <- addEntry "PYTHONUNBUFFERED" "1" <$> getEnvironment
|
2020-11-19 20:21:17 +00:00
|
|
|
runWormHoleProcess p { env = Just environ} $ \_hin hout herr ph -> do
|
|
|
|
(inout, inerr) <- concurrently
|
|
|
|
(findcode ph hout)
|
|
|
|
(findcode ph herr)
|
2018-07-04 19:14:03 +00:00
|
|
|
return (inout || inerr)
|
2016-12-17 20:58:05 +00:00
|
|
|
where
|
|
|
|
p = wormHoleProcess (Param "send" : ps ++ [File f])
|
2020-11-19 20:21:17 +00:00
|
|
|
findcode ph h = findcode' =<< getwords ph h []
|
2018-07-04 19:14:03 +00:00
|
|
|
findcode' [] = return False
|
|
|
|
findcode' (w:ws) = case mkCode w of
|
2016-12-17 22:25:33 +00:00
|
|
|
Just code -> do
|
2018-07-04 19:14:03 +00:00
|
|
|
_ <- tryPutMVar observer code
|
2016-12-17 20:58:05 +00:00
|
|
|
return True
|
2018-07-04 19:14:03 +00:00
|
|
|
Nothing -> findcode' ws
|
2020-11-19 20:21:17 +00:00
|
|
|
getwords ph h c = hGetLineUntilExitOrEOF ph h >>= \case
|
|
|
|
Nothing -> return $ concatMap words $ reverse c
|
|
|
|
Just l -> getwords ph h (l:c)
|
2016-12-17 20:58:05 +00:00
|
|
|
|
|
|
|
-- | Receives a file. Once the receive is under way, the Code will be
|
2016-12-17 22:25:33 +00:00
|
|
|
-- read from the CodeProducer, and fed to wormhole on stdin.
|
|
|
|
receiveFile :: FilePath -> CodeProducer -> WormHoleParams -> IO Bool
|
2020-11-19 20:21:17 +00:00
|
|
|
receiveFile f (CodeProducer producer) ps = runWormHoleProcess p $ \hin _hout _herr _ph -> do
|
2016-12-27 20:26:26 +00:00
|
|
|
Code c <- readMVar producer
|
2016-12-17 22:25:33 +00:00
|
|
|
hPutStrLn hin c
|
2016-12-17 20:58:05 +00:00
|
|
|
hFlush hin
|
|
|
|
return True
|
|
|
|
where
|
|
|
|
p = wormHoleProcess $
|
|
|
|
[ Param "receive"
|
|
|
|
, Param "--accept-file"
|
|
|
|
, Param "--output-file"
|
|
|
|
, File f
|
|
|
|
] ++ ps
|
|
|
|
|
|
|
|
wormHoleProcess :: WormHoleParams -> CreateProcess
|
|
|
|
wormHoleProcess = proc "wormhole" . toCommand
|
|
|
|
|
2020-11-19 20:21:17 +00:00
|
|
|
runWormHoleProcess :: CreateProcess -> (Handle -> Handle -> Handle -> ProcessHandle -> IO Bool) -> IO Bool
|
2020-06-03 17:15:01 +00:00
|
|
|
runWormHoleProcess p consumer =
|
|
|
|
withCreateProcess p' go `catchNonAsync` const (return False)
|
2016-12-17 20:58:05 +00:00
|
|
|
where
|
2020-06-03 17:15:01 +00:00
|
|
|
p' = p
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = CreatePipe
|
|
|
|
, std_err = CreatePipe
|
|
|
|
}
|
|
|
|
go (Just hin) (Just hout) (Just herr) pid =
|
2020-11-19 20:21:17 +00:00
|
|
|
consumer hin hout herr pid <&&> waitbool pid
|
2020-06-03 17:15:01 +00:00
|
|
|
go _ _ _ _ = error "internal"
|
|
|
|
waitbool pid = do
|
2016-12-17 20:58:05 +00:00
|
|
|
r <- waitForProcess pid
|
|
|
|
return $ case r of
|
|
|
|
ExitSuccess -> True
|
|
|
|
ExitFailure _ -> False
|
2016-12-18 21:01:15 +00:00
|
|
|
|
|
|
|
isInstalled :: IO Bool
|
2021-02-02 23:01:45 +00:00
|
|
|
isInstalled = inSearchPath "wormhole"
|