diff --git a/Command/EnableTor.hs b/Command/EnableTor.hs index d12a6e446d..c81968a559 100644 --- a/Command/EnableTor.hs +++ b/Command/EnableTor.hs @@ -56,6 +56,6 @@ start os = do where go uuid userid = do (onionaddr, onionport) <- liftIO $ - addHiddenService "tor-annex" userid (fromUUID uuid) + addHiddenService torAppName userid (fromUUID uuid) storeP2PAddress $ TorAnnex onionaddr onionport stop diff --git a/P2P/Address.hs b/P2P/Address.hs index 1b1f66059e..d911f7b4b2 100644 --- a/P2P/Address.hs +++ b/P2P/Address.hs @@ -90,3 +90,6 @@ storeP2PAddress addr = do p2pAddressCredsFile :: FilePath p2pAddressCredsFile = "p2paddrs" + +torAppName :: AppName +torAppName = "tor-annex" diff --git a/RemoteDaemon/Transport/Tor.hs b/RemoteDaemon/Transport/Tor.hs index 3f70fb1fbb..0fbe9a7200 100644 --- a/RemoteDaemon/Transport/Tor.hs +++ b/RemoteDaemon/Transport/Tor.hs @@ -41,7 +41,7 @@ server th@(TransportHandle (LocalRepo r) _) = do u <- liftAnnex th getUUID uid <- getRealUserID let ident = fromUUID u - go u =<< getHiddenServiceSocketFile uid ident + go u =<< getHiddenServiceSocketFile torAppName uid ident where go u (Just sock) = do q <- newTBMQueueIO maxConnections diff --git a/Utility/Tor.hs b/Utility/Tor.hs index 64a6ae11da..4e7c0ef43f 100644 --- a/Utility/Tor.hs +++ b/Utility/Tor.hs @@ -66,7 +66,7 @@ addHiddenService appname uid ident = do writeFile torrc $ unlines $ ls ++ [ "" - , "HiddenServiceDir " ++ hiddenServiceDir uid ident + , "HiddenServiceDir " ++ hiddenServiceDir appname uid ident , "HiddenServicePort " ++ show newport ++ " unix:" ++ sockfile ] @@ -95,7 +95,7 @@ addHiddenService appname uid ident = do waithiddenservice :: Int -> OnionPort -> IO (OnionAddress, OnionPort) waithiddenservice 0 _ = giveup "tor failed to create hidden service, perhaps the tor service is not running" waithiddenservice n p = do - v <- tryIO $ readFile $ hiddenServiceHostnameFile uid ident + v <- tryIO $ readFile $ hiddenServiceHostnameFile appname uid ident case v of Right s | ".onion\n" `isSuffixOf` s -> return (OnionAddress (takeWhile (/= '\n') s), p) @@ -105,13 +105,14 @@ addHiddenService appname uid ident = do -- | A hidden service directory to use. -- --- The "hs" is used in the name to prevent too long a path name, --- which could present problems for socketFile. -hiddenServiceDir :: UserID -> UniqueIdent -> FilePath -hiddenServiceDir uid ident = torLibDir "hs_" ++ show uid ++ "_" ++ ident +-- Has to be inside the torLibDir so tor can create it. +-- +-- Has to end with "uid_ident" so getHiddenServiceSocketFile can find it. +hiddenServiceDir :: AppName -> UserID -> UniqueIdent -> FilePath +hiddenServiceDir appname uid ident = torLibDir appname ++ "_" ++ show uid ++ "_" ++ ident -hiddenServiceHostnameFile :: UserID -> UniqueIdent -> FilePath -hiddenServiceHostnameFile uid ident = hiddenServiceDir uid ident "hostname" +hiddenServiceHostnameFile :: AppName -> UserID -> UniqueIdent -> FilePath +hiddenServiceHostnameFile appname uid ident = hiddenServiceDir appname uid ident "hostname" -- | Location of the socket for a hidden service. -- @@ -126,18 +127,20 @@ hiddenServiceSocketFile appname uid ident = varLibDir appname show uid + -- | Parse torrc, to get the socket file used for a hidden service with -- the specified UniqueIdent. -getHiddenServiceSocketFile :: UserID -> UniqueIdent -> IO (Maybe FilePath) -getHiddenServiceSocketFile uid ident = +getHiddenServiceSocketFile :: AppName -> UserID -> UniqueIdent -> IO (Maybe FilePath) +getHiddenServiceSocketFile _appname uid ident = parse . map words . lines <$> catchDefaultIO "" (readFile torrc) where parse [] = Nothing parse (("HiddenServiceDir":hsdir:[]):("HiddenServicePort":_hsport:hsaddr:[]):rest) - | "unix:" `isPrefixOf` hsaddr && hsdir == hsdir_want = + | "unix:" `isPrefixOf` hsaddr && hasident hsdir = Just (drop (length "unix:") hsaddr) | otherwise = parse rest parse (_:rest) = parse rest - hsdir_want = hiddenServiceDir uid ident + -- Don't look for AppName in the hsdir, because it didn't used to + -- be included. + hasident hsdir = (show uid ++ "_" ++ ident) `isSuffixOf` takeFileName hsdir -- | Sets up the directory for the socketFile, with appropriate -- permissions. Must run as root.