2016-11-21 21:27:38 +00:00
|
|
|
{- git-remote-tor-annex program
|
|
|
|
-
|
|
|
|
- Copyright 2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module CmdLine.GitRemoteTorAnnex where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Git.CurrentRepo
|
2016-11-24 20:36:16 +00:00
|
|
|
import P2P.Protocol
|
|
|
|
import P2P.IO
|
2016-11-21 21:27:38 +00:00
|
|
|
import Utility.Tor
|
2016-11-22 18:18:34 +00:00
|
|
|
import Utility.AuthToken
|
2016-11-21 21:27:38 +00:00
|
|
|
import Annex.UUID
|
2016-11-30 19:26:16 +00:00
|
|
|
import P2P.Address
|
|
|
|
import P2P.Auth
|
2016-11-21 21:27:38 +00:00
|
|
|
|
|
|
|
run :: [String] -> IO ()
|
2017-12-05 19:00:50 +00:00
|
|
|
run (_remotename:address:[]) = forever $
|
|
|
|
getLine >>= \case
|
2016-11-21 23:24:55 +00:00
|
|
|
"capabilities" -> putStrLn "connect" >> ready
|
2016-11-21 21:27:38 +00:00
|
|
|
"connect git-upload-pack" -> go UploadPack
|
|
|
|
"connect git-receive-pack" -> go ReceivePack
|
2017-12-05 19:00:50 +00:00
|
|
|
l -> error $ "git-remote-helpers protocol error at " ++ show l
|
2016-11-21 21:27:38 +00:00
|
|
|
where
|
|
|
|
(onionaddress, onionport)
|
|
|
|
| '/' `elem` address = parseAddressPort $
|
|
|
|
reverse $ takeWhile (/= '/') $ reverse address
|
|
|
|
| otherwise = parseAddressPort address
|
|
|
|
go service = do
|
2016-11-21 23:24:55 +00:00
|
|
|
ready
|
2018-09-25 20:49:59 +00:00
|
|
|
connectService onionaddress onionport service >>= \case
|
|
|
|
Right exitcode -> exitWith exitcode
|
|
|
|
Left e -> giveup $ describeProtoFailure e
|
2016-11-21 23:24:55 +00:00
|
|
|
ready = do
|
2016-11-21 21:27:38 +00:00
|
|
|
putStrLn ""
|
|
|
|
hFlush stdout
|
2016-11-21 23:24:55 +00:00
|
|
|
|
2016-11-21 21:27:38 +00:00
|
|
|
run (_remotename:[]) = giveup "remote address not configured"
|
|
|
|
run _ = giveup "expected remote name and address parameters"
|
|
|
|
|
|
|
|
parseAddressPort :: String -> (OnionAddress, OnionPort)
|
|
|
|
parseAddressPort s =
|
|
|
|
let (a, sp) = separate (== ':') s
|
|
|
|
in case readish sp of
|
|
|
|
Nothing -> giveup "onion address must include port number"
|
|
|
|
Just p -> (OnionAddress a, p)
|
|
|
|
|
2018-09-25 20:49:59 +00:00
|
|
|
connectService :: OnionAddress -> OnionPort -> Service -> IO (Either ProtoFailure ExitCode)
|
2016-11-21 21:27:38 +00:00
|
|
|
connectService address port service = do
|
|
|
|
state <- Annex.new =<< Git.CurrentRepo.get
|
|
|
|
Annex.eval state $ do
|
|
|
|
authtoken <- fromMaybe nullAuthToken
|
2016-11-30 19:26:16 +00:00
|
|
|
<$> loadP2PRemoteAuthToken (TorAnnex address port)
|
2016-11-21 21:27:38 +00:00
|
|
|
myuuid <- getUUID
|
|
|
|
g <- Annex.gitRepo
|
2016-12-06 19:40:31 +00:00
|
|
|
conn <- liftIO $ connectPeer g (TorAnnex address port)
|
2018-03-12 19:19:40 +00:00
|
|
|
runst <- liftIO $ mkRunState Client
|
|
|
|
liftIO $ runNetProto runst conn $ auth myuuid authtoken noop >>= \case
|
2017-12-05 19:00:50 +00:00
|
|
|
Just _theiruuid -> connect service stdin stdout
|
|
|
|
Nothing -> giveup $ "authentication failed, perhaps you need to set " ++ p2pAuthTokenEnv
|