2024-06-18 14:51:37 +00:00
|
|
|
{- proxying
|
|
|
|
-
|
|
|
|
- Copyright 2024 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.Proxy where
|
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import P2P.Proxy
|
|
|
|
import P2P.Protocol
|
|
|
|
import P2P.IO
|
2024-06-28 17:22:56 +00:00
|
|
|
import qualified Remote
|
|
|
|
import qualified Types.Remote as Remote
|
|
|
|
import qualified Remote.Git
|
2024-06-18 14:51:37 +00:00
|
|
|
import Remote.Helper.Ssh (openP2PShellConnection', closeP2PShellConnection)
|
2024-06-28 21:07:01 +00:00
|
|
|
import Annex.Content
|
2024-06-28 17:22:56 +00:00
|
|
|
import Annex.Concurrent
|
2024-06-28 19:32:00 +00:00
|
|
|
import Annex.Tmp
|
|
|
|
import Utility.Tmp.Dir
|
|
|
|
import Utility.Metered
|
2024-06-18 14:51:37 +00:00
|
|
|
|
2024-06-28 17:22:56 +00:00
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
2024-06-28 19:32:00 +00:00
|
|
|
import qualified System.FilePath.ByteString as P
|
2024-06-28 17:22:56 +00:00
|
|
|
|
|
|
|
proxyRemoteSide :: ProtocolVersion -> Bypass -> Remote -> Annex RemoteSide
|
|
|
|
proxyRemoteSide clientmaxversion bypass r
|
|
|
|
| Remote.remotetype r == Remote.Git.remote =
|
|
|
|
proxyGitRemoteSide clientmaxversion bypass r
|
|
|
|
| otherwise =
|
|
|
|
proxySpecialRemoteSide clientmaxversion r
|
|
|
|
|
|
|
|
proxyGitRemoteSide :: ProtocolVersion -> Bypass -> Remote -> Annex RemoteSide
|
|
|
|
proxyGitRemoteSide clientmaxversion bypass r = mkRemoteSide r $
|
2024-06-27 18:36:55 +00:00
|
|
|
openP2PShellConnection' r clientmaxversion bypass >>= \case
|
|
|
|
Just conn@(OpenConnection (remoterunst, remoteconn, _)) ->
|
|
|
|
return $ Just
|
|
|
|
( remoterunst
|
|
|
|
, remoteconn
|
|
|
|
, void $ liftIO $ closeP2PShellConnection conn
|
|
|
|
)
|
|
|
|
_ -> return Nothing
|
2024-06-28 17:22:56 +00:00
|
|
|
|
|
|
|
proxySpecialRemoteSide :: ProtocolVersion -> Remote -> Annex RemoteSide
|
|
|
|
proxySpecialRemoteSide clientmaxversion r = mkRemoteSide r $ do
|
|
|
|
let protoversion = min clientmaxversion maxProtocolVersion
|
|
|
|
remoterunst <- Serving (Remote.uuid r) Nothing <$>
|
|
|
|
liftIO (newTVarIO protoversion)
|
|
|
|
ihdl <- liftIO newEmptyTMVarIO
|
|
|
|
ohdl <- liftIO newEmptyTMVarIO
|
2024-06-28 21:07:01 +00:00
|
|
|
iwaitv <- liftIO newEmptyTMVarIO
|
|
|
|
owaitv <- liftIO newEmptyTMVarIO
|
2024-06-28 17:22:56 +00:00
|
|
|
endv <- liftIO newEmptyTMVarIO
|
|
|
|
worker <- liftIO . async =<< forkState
|
2024-06-28 21:07:01 +00:00
|
|
|
(proxySpecialRemote protoversion r ihdl ohdl owaitv endv)
|
2024-06-28 17:22:56 +00:00
|
|
|
let remoteconn = P2PConnection
|
|
|
|
{ connRepo = Nothing
|
|
|
|
, connCheckAuth = const False
|
2024-06-28 21:07:01 +00:00
|
|
|
, connIhdl = P2PHandleTMVar ihdl iwaitv
|
|
|
|
, connOhdl = P2PHandleTMVar ohdl owaitv
|
2024-06-28 17:22:56 +00:00
|
|
|
, connIdent = ConnIdent (Just (Remote.name r))
|
|
|
|
}
|
|
|
|
let closeremoteconn = do
|
|
|
|
liftIO $ atomically $ putTMVar endv ()
|
|
|
|
join $ liftIO (wait worker)
|
|
|
|
return $ Just
|
|
|
|
( remoterunst
|
|
|
|
, remoteconn
|
|
|
|
, closeremoteconn
|
|
|
|
)
|
|
|
|
|
|
|
|
-- Proxy for the special remote, speaking the P2P protocol.
|
|
|
|
proxySpecialRemote
|
|
|
|
:: ProtocolVersion
|
|
|
|
-> Remote
|
|
|
|
-> TMVar (Either L.ByteString Message)
|
|
|
|
-> TMVar (Either L.ByteString Message)
|
|
|
|
-> TMVar ()
|
2024-06-28 21:07:01 +00:00
|
|
|
-> TMVar ()
|
2024-06-28 17:22:56 +00:00
|
|
|
-> Annex ()
|
2024-06-28 21:07:01 +00:00
|
|
|
proxySpecialRemote protoversion r ihdl ohdl owaitv endv = go
|
2024-06-28 17:22:56 +00:00
|
|
|
where
|
2024-06-28 19:32:00 +00:00
|
|
|
go :: Annex ()
|
|
|
|
go = liftIO receivemessage >>= \case
|
2024-06-28 17:22:56 +00:00
|
|
|
Just (CHECKPRESENT k) -> do
|
|
|
|
tryNonAsync (Remote.checkPresent r k) >>= \case
|
2024-06-28 19:32:00 +00:00
|
|
|
Right True -> liftIO $ sendmessage SUCCESS
|
|
|
|
Right False -> liftIO $ sendmessage FAILURE
|
|
|
|
Left err -> liftIO $ propagateerror err
|
2024-06-28 17:22:56 +00:00
|
|
|
go
|
|
|
|
Just (LOCKCONTENT _) -> do
|
|
|
|
-- Special remotes do not support locking content.
|
2024-06-28 19:32:00 +00:00
|
|
|
liftIO $ sendmessage FAILURE
|
2024-06-28 17:22:56 +00:00
|
|
|
go
|
|
|
|
Just (REMOVE k) -> do
|
toward SafeDropProof expiry checking
Added Maybe POSIXTime to SafeDropProof, which gets set when the proof is
based on a LockedCopy. If there are several LockedCopies, it uses the
closest expiry time. That is not optimal, it may be that the proof
expires based on one LockedCopy but another one has not expired. But
that seems unlikely to really happen, and anyway the user can just
re-run a drop if it fails due to expiry.
Pass the SafeDropProof to removeKey, which is responsible for checking
it for expiry in situations where that could be a problem. Which really
only means in Remote.Git.
Made Remote.Git check expiry when dropping from a local remote.
Checking expiry when dropping from a P2P remote is not yet implemented.
P2P.Protocol.remove has SafeDropProof plumbed through to it for that
purpose.
Fixing the remaining 2 build warnings should complete this work.
Note that the use of a POSIXTime here means that if the clock gets set
forward while git-annex is in the middle of a drop, it may say that
dropping took too long. That seems ok. Less ok is that if the clock gets
turned back a sufficient amount (eg 5 minutes), proof expiry won't be
noticed. It might be better to use the Monotonic clock, but that doesn't
advance when a laptop is suspended, and while there is the linux
Boottime clock, that is not available on other systems. Perhaps a
combination of POSIXTime and the Monotonic clock could detect laptop
suspension and also detect clock being turned back?
There is a potential future flag day where
p2pDefaultLockContentRetentionDuration is not assumed, but is probed
using the P2P protocol, and peers that don't support it can no longer
produce a LockedCopy. Until that happens, when git-annex is
communicating with older peers there is a risk of data loss when
a ssh connection closes during LOCKCONTENT.
2024-07-04 16:23:46 +00:00
|
|
|
tryNonAsync (Remote.removeKey r Nothing k) >>= \case
|
2024-06-28 19:32:00 +00:00
|
|
|
Right () -> liftIO $ sendmessage SUCCESS
|
|
|
|
Left err -> liftIO $ propagateerror err
|
2024-06-28 17:22:56 +00:00
|
|
|
go
|
2024-06-28 21:07:01 +00:00
|
|
|
Just (PUT (ProtoAssociatedFile af) k) -> do
|
|
|
|
proxyput af k
|
|
|
|
go
|
2024-06-28 19:32:00 +00:00
|
|
|
Just (GET offset (ProtoAssociatedFile af) k) -> do
|
|
|
|
proxyget offset af k
|
|
|
|
go
|
2024-06-28 18:07:23 +00:00
|
|
|
Just (BYPASS _) -> go
|
2024-06-28 17:22:56 +00:00
|
|
|
Just (CONNECT _) ->
|
|
|
|
-- Not supported and the protocol ends here.
|
2024-06-28 19:32:00 +00:00
|
|
|
liftIO $ sendmessage $ CONNECTDONE (ExitFailure 1)
|
2024-06-28 17:22:56 +00:00
|
|
|
Just NOTIFYCHANGE -> do
|
2024-06-28 19:32:00 +00:00
|
|
|
liftIO $ sendmessage $
|
|
|
|
ERROR "NOTIFYCHANGE unsupported for a special remote"
|
2024-06-28 17:22:56 +00:00
|
|
|
go
|
2024-07-01 15:19:02 +00:00
|
|
|
Just _ -> giveup "protocol error M"
|
2024-06-28 17:22:56 +00:00
|
|
|
Nothing -> return ()
|
|
|
|
|
|
|
|
getnextmessageorend =
|
|
|
|
liftIO $ atomically $
|
|
|
|
(Right <$> takeTMVar ohdl)
|
|
|
|
`orElse`
|
2024-06-28 19:32:00 +00:00
|
|
|
(Left <$> readTMVar endv)
|
2024-06-28 17:22:56 +00:00
|
|
|
|
|
|
|
receivemessage = getnextmessageorend >>= \case
|
|
|
|
Right (Right m) -> return (Just m)
|
|
|
|
Right (Left _b) -> giveup "unexpected ByteString received from P2P MVar"
|
|
|
|
Left () -> return Nothing
|
2024-06-28 21:07:01 +00:00
|
|
|
|
|
|
|
receivebytestring = atomically (takeTMVar ohdl) >>= \case
|
|
|
|
Left b -> return b
|
|
|
|
Right _m -> giveup "did not receive ByteString from P2P MVar"
|
2024-06-28 17:22:56 +00:00
|
|
|
|
2024-06-28 19:32:00 +00:00
|
|
|
sendmessage m = atomically $ putTMVar ihdl (Right m)
|
|
|
|
|
|
|
|
sendbytestring b = atomically $ putTMVar ihdl (Left b)
|
2024-06-28 17:42:25 +00:00
|
|
|
|
|
|
|
propagateerror err = sendmessage $ ERROR $
|
|
|
|
"proxied special remote reports: " ++ show err
|
2024-06-28 19:32:00 +00:00
|
|
|
|
|
|
|
-- Not using gitAnnexTmpObjectLocation because there might be
|
|
|
|
-- several concurrent GET and PUTs of the same key being proxied
|
|
|
|
-- from this special remote or others, and each needs to happen
|
|
|
|
-- independently. Also, this key is not getting added into the
|
|
|
|
-- local annex objects.
|
|
|
|
withproxytmpfile k a = withOtherTmp $ \othertmpdir ->
|
|
|
|
withTmpDirIn (fromRawFilePath othertmpdir) "proxy" $ \tmpdir ->
|
|
|
|
a (toRawFilePath tmpdir P.</> keyFile k)
|
2024-06-28 21:07:01 +00:00
|
|
|
|
|
|
|
proxyput af k = do
|
2024-07-01 14:53:49 +00:00
|
|
|
liftIO $ sendmessage $ PUT_FROM (Offset 0)
|
|
|
|
withproxytmpfile k $ \tmpfile -> do
|
|
|
|
let store = tryNonAsync (Remote.storeKey r k af (Just (decodeBS tmpfile)) nullMeterUpdate) >>= \case
|
|
|
|
Right () -> liftIO $ sendmessage SUCCESS
|
2024-06-28 21:07:01 +00:00
|
|
|
Left err -> liftIO $ propagateerror err
|
|
|
|
liftIO receivemessage >>= \case
|
|
|
|
Just (DATA (Len _)) -> do
|
|
|
|
b <- liftIO receivebytestring
|
|
|
|
liftIO $ L.writeFile (fromRawFilePath tmpfile) b
|
|
|
|
-- Signal that the whole bytestring
|
2024-07-01 14:53:49 +00:00
|
|
|
-- has been received.
|
2024-06-28 21:07:01 +00:00
|
|
|
liftIO $ atomically $ putTMVar owaitv ()
|
|
|
|
if protoversion > ProtocolVersion 1
|
2024-07-01 14:53:49 +00:00
|
|
|
then liftIO receivemessage >>= \case
|
|
|
|
Just (VALIDITY Valid) ->
|
|
|
|
store
|
|
|
|
Just (VALIDITY Invalid) ->
|
|
|
|
return ()
|
2024-07-01 15:19:02 +00:00
|
|
|
_ -> giveup "protocol error N"
|
2024-07-01 14:53:49 +00:00
|
|
|
else store
|
2024-07-01 15:19:02 +00:00
|
|
|
_ -> giveup "protocol error O"
|
2024-06-28 19:32:00 +00:00
|
|
|
|
|
|
|
proxyget offset af k = withproxytmpfile k $ \tmpfile -> do
|
|
|
|
-- Don't verify the content from the remote,
|
|
|
|
-- because the client will do its own verification.
|
|
|
|
let vc = Remote.NoVerify
|
|
|
|
tryNonAsync (Remote.retrieveKeyFile r k af (fromRawFilePath tmpfile) nullMeterUpdate vc) >>= \case
|
|
|
|
Right v ->
|
2024-06-28 21:07:01 +00:00
|
|
|
ifM (verifyKeyContentPostRetrieval Remote.RetrievalVerifiableKeysSecure vc v k tmpfile)
|
2024-06-28 19:32:00 +00:00
|
|
|
( liftIO $ senddata offset tmpfile
|
|
|
|
, liftIO $ sendmessage $
|
|
|
|
ERROR "verification of content failed"
|
|
|
|
)
|
|
|
|
Left err -> liftIO $ propagateerror err
|
|
|
|
|
|
|
|
senddata (Offset offset) f = do
|
|
|
|
size <- fromIntegral <$> getFileSize f
|
|
|
|
let n = max 0 (size - offset)
|
|
|
|
sendmessage $ DATA (Len n)
|
|
|
|
withBinaryFile (fromRawFilePath f) ReadMode $ \h -> do
|
|
|
|
hSeek h AbsoluteSeek offset
|
|
|
|
sendbs =<< L.hGetContents h
|
|
|
|
-- Important to keep the handle open until
|
|
|
|
-- the client responds. The bytestring
|
|
|
|
-- could still be lazily streaming out to
|
|
|
|
-- the client.
|
|
|
|
waitclientresponse
|
|
|
|
where
|
|
|
|
sendbs bs = do
|
|
|
|
sendbytestring bs
|
|
|
|
when (protoversion > ProtocolVersion 0) $
|
|
|
|
sendmessage (VALIDITY Valid)
|
|
|
|
|
|
|
|
waitclientresponse =
|
|
|
|
receivemessage >>= \case
|
|
|
|
Just SUCCESS -> return ()
|
|
|
|
Just FAILURE -> return ()
|
2024-07-01 15:19:02 +00:00
|
|
|
Just _ -> giveup "protocol error P"
|
2024-06-28 19:32:00 +00:00
|
|
|
Nothing -> return ()
|
|
|
|
|