need to auth with the peer
authorJoey Hess <joeyh@joeyh.name>
Tue, 6 Dec 2016 19:49:39 +0000 (15:49 -0400)
committerJoey Hess <joeyh@joeyh.name>
Tue, 6 Dec 2016 19:50:02 +0000 (15:50 -0400)
P2P/IO.hs
Remote/P2P.hs

index f63b2808b3252acd0666bd5a7f27db61f5a72613..9abefb8a0668e84347d61c7ce04a3923b169fbd5 100644 (file)
--- a/P2P/IO.hs
+++ b/P2P/IO.hs
@@ -11,6 +11,7 @@ module P2P.IO
        ( RunProto
        , P2PConnection(..)
        , connectPeer
+       , closeConnection
        , setupHandle
        , runNetProto
        , runNet
@@ -60,6 +61,11 @@ connectPeer g (TorAnnex onionaddress onionport) = do
                , connOhdl = h
                }
 
+closeConnection :: P2PConnection -> IO ()
+closeConnection conn = do
+       hClose (connIhdl conn)
+       hClose (connOhdl conn)
+
 setupHandle :: Socket -> IO Handle
 setupHandle s = do
        h <- socketToHandle s ReadWriteMode
index 0c7ca05748833cdeda1f6db73687c72e208a0754..68b75924f3febc0b66ce5ca2ad4a884e3e64aa2a 100644 (file)
@@ -16,13 +16,16 @@ import qualified P2P.Protocol as P2P
 import P2P.Address
 import P2P.Annex
 import P2P.IO
+import P2P.Auth
 import Types.Remote
 import Types.GitConfig
 import qualified Git
+import Annex.UUID
 import Config
 import Config.Cost
 import Remote.Helper.Git
 import Utility.Metered
+import Utility.AuthToken
 import Types.NumCopies
 
 import Control.Concurrent
@@ -128,8 +131,7 @@ runProto' a (OpenConnection conn) = do
        if isJust r
                then return (OpenConnection conn, r)
                else do
-                       liftIO $ hClose (connIhdl conn)
-                       liftIO $ hClose (connOhdl conn)
+                       liftIO $ closeConnection conn
                        return (ClosedConnection, r)
 
 -- Uses an open connection if one is available in the ConnectionPool;
@@ -165,5 +167,16 @@ openConnection addr = do
        g <- Annex.gitRepo
        v <- liftIO $ tryNonAsync $ connectPeer g addr
        case v of
-               Right conn -> return (OpenConnection conn)
+               Right conn -> do
+                       myuuid <- getUUID
+                       authtoken <- fromMaybe nullAuthToken
+                               <$> loadP2PRemoteAuthToken addr
+                       res <- liftIO $ runNetProto conn $
+                               P2P.auth myuuid authtoken
+                       case res of
+                               Just (Just _theiruuid) ->
+                                       return (OpenConnection conn)
+                               _ -> do
+                                       liftIO $ closeConnection conn
+                                       return ClosedConnection
                Left _e -> return ClosedConnection