aboutsummaryrefslogtreecommitdiff
path: root/Command
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2016-12-22 13:59:21 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2016-12-22 13:59:21 -0400
commit6c089cff615bc44ca94cc4ca77dc69f281dd2ab8 (patch)
tree028c8958b20c841864e4f9925389f0ea1fcd8bc8 /Command
parent4218d2a77aa4aab0bb09d31463670cce3067bd9c (diff)
close
Diffstat (limited to 'Command')
-rw-r--r--Command/EnableTor.hs36
1 files changed, 32 insertions, 4 deletions
diff --git a/Command/EnableTor.hs b/Command/EnableTor.hs
index c81968a55..2b7d62635 100644
--- a/Command/EnableTor.hs
+++ b/Command/EnableTor.hs
@@ -10,18 +10,19 @@
module Command.EnableTor where
import Command
+import qualified Annex
import P2P.Address
import Utility.Tor
import Annex.UUID
import Config.Files
+import P2P.IO
+import Utility.ThreadScheduler
#ifndef mingw32_HOST_OS
import Utility.Su
import System.Posix.User
#endif
--- This runs as root, so avoid making any commits or initializing
--- git-annex, or doing other things that create root-owned files.
cmd :: Command
cmd = noCommit $ dontCheck repoExists $
command "enable-tor" SectionSetup "enable tor hidden service"
@@ -30,6 +31,8 @@ cmd = noCommit $ dontCheck repoExists $
seek :: CmdParams -> CommandSeek
seek = withWords start
+-- This runs as root, so avoid making any commits or initializing
+-- git-annex, or doing other things that create root-owned files.
start :: [String] -> CommandStart
start os = do
uuid <- getUUID
@@ -42,11 +45,12 @@ start os = do
Nothing -> giveup "Need user-id parameter."
Just userid -> go uuid userid
else do
- liftIO $ putStrLn "Need root access to enable tor..."
+ showStart "enable-tor" ""
+ showLongNote "Need root access to enable tor..."
gitannex <- liftIO readProgramFile
let ps = [Param (cmdname cmd), Param (show curruserid)]
ifM (liftIO $ runAsRoot gitannex ps)
- ( stop
+ ( next $ next checkHiddenService
, giveup $ unwords $
[ "Failed to run as root:" , gitannex ] ++ toCommand ps
)
@@ -59,3 +63,27 @@ start os = do
addHiddenService torAppName userid (fromUUID uuid)
storeP2PAddress $ TorAnnex onionaddr onionport
stop
+
+checkHiddenService :: CommandCleanup
+checkHiddenService = do
+ showLongNote "Tor hidden service is configured. Checking connection to it. This may take a few minutes."
+ go (150 :: Int) =<< filter istoraddr <$> loadP2PAddresses
+ where
+ istoraddr (TorAnnex _ _) = True
+
+ go 0 _ = giveup "Still unable to connect to hidden service. It might not yet be usable by others. Please check Tor's logs for details."
+ go _ [] = giveup "Somehow didn't get an onion address."
+ go n addrs@(addr:_) = do
+ g <- Annex.gitRepo
+ -- Connect to ourselves; don't bother trying to auth,
+ -- we just want to know if the circuit works.
+ cv <- liftIO $ tryNonAsync $ connectPeer g addr
+ case cv of
+ Left e -> do
+ warning $ "Unable to connect to hidden service. It may not yet have propigated to the Tor network. (" ++ show e ++ ") Will retry.."
+ liftIO $ threadDelaySeconds (Seconds 2)
+ go (n-1) addrs
+ Right conn -> do
+ liftIO $ closeConnection conn
+ showLongNote "Tor hidden service is working."
+ return True