From dce8e76ef443e33d88b8301c86ebf080fceff511 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 20 Nov 2016 15:45:01 -0400 Subject: remotedaemon: serve tor hidden service --- RemoteDaemon/Core.hs | 9 ++++++-- RemoteDaemon/Transport.hs | 4 ++++ RemoteDaemon/Transport/Tor.hs | 51 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 RemoteDaemon/Transport/Tor.hs (limited to 'RemoteDaemon') diff --git a/RemoteDaemon/Core.hs b/RemoteDaemon/Core.hs index 3b3f6d98d..446948da6 100644 --- a/RemoteDaemon/Core.hs +++ b/RemoteDaemon/Core.hs @@ -45,7 +45,9 @@ runInteractive = do let controller = runController ichan ochan -- If any thread fails, the rest will be killed. - void $ tryIO $ reader `concurrently` writer `concurrently` controller + void $ tryIO $ reader + `concurrently` writer + `concurrently` controller runNonInteractive :: IO () runNonInteractive = do @@ -59,7 +61,9 @@ runNonInteractive = do void $ atomically $ readTChan ochan let controller = runController ichan ochan - void $ tryIO $ reader `concurrently` writer `concurrently` controller + void $ tryIO $ reader + `concurrently` writer + `concurrently` controller type RemoteMap = M.Map Git.Repo (IO (), TChan Consumed) @@ -70,6 +74,7 @@ runController ichan ochan = do h <- genTransportHandle m <- genRemoteMap h ochan startrunning m + mapM_ (\s -> async (s h)) remoteServers go h False m where go h paused m = do diff --git a/RemoteDaemon/Transport.hs b/RemoteDaemon/Transport.hs index 0e2040d1f..6605012de 100644 --- a/RemoteDaemon/Transport.hs +++ b/RemoteDaemon/Transport.hs @@ -10,6 +10,7 @@ module RemoteDaemon.Transport where import RemoteDaemon.Types import qualified RemoteDaemon.Transport.Ssh import qualified RemoteDaemon.Transport.GCrypt +import qualified RemoteDaemon.Transport.Tor import qualified Git.GCrypt import qualified Data.Map as M @@ -22,3 +23,6 @@ remoteTransports = M.fromList [ ("ssh:", RemoteDaemon.Transport.Ssh.transport) , (Git.GCrypt.urlScheme, RemoteDaemon.Transport.GCrypt.transport) ] + +remoteServers :: [TransportHandle -> IO ()] +remoteServers = [RemoteDaemon.Transport.Tor.server] diff --git a/RemoteDaemon/Transport/Tor.hs b/RemoteDaemon/Transport/Tor.hs new file mode 100644 index 000000000..1527939b1 --- /dev/null +++ b/RemoteDaemon/Transport/Tor.hs @@ -0,0 +1,51 @@ +{- git-remote-daemon, tor hidden service transport + - + - Copyright 2016 Joey Hess + - + - Licensed under the GNU GPL version 3 or higher. + -} + +module RemoteDaemon.Transport.Tor (server) where + +import Common +import RemoteDaemon.Types +import RemoteDaemon.Common +import Utility.Tor +import Utility.FileMode +import Remote.Helper.P2P +import Remote.Helper.P2P.IO +import Annex.UUID +import Types.UUID + +import System.PosixCompat.User +import Network.Socket +import Control.Concurrent +import System.Log.Logger (debugM) + +-- Run tor hidden service. +server :: TransportHandle -> IO () +server th@(TransportHandle (LocalRepo r) _) = do + u <- liftAnnex th getUUID + uid <- getRealUserID + let ident = fromUUID u + let sock = socketFile uid ident + nukeFile sock + soc <- socket AF_UNIX Stream defaultProtocol + bind soc (SockAddrUnix sock) + -- Allow everyone to read and write to the socket; tor is probably + -- running as a different user. Connections have to authenticate + -- to do anything, so it's fine that other local users can connect. + modifyFileMode sock $ addModes + [groupReadMode, groupWriteMode, otherReadMode, otherWriteMode] + listen soc 2 + debugM "remotedaemon" "tor hidden service running" + forever $ do + (conn, _) <- accept soc + forkIO $ do + debugM "remotedaemon" "handling a connection" + h <- socketToHandle conn ReadWriteMode + hSetBuffering h LineBuffering + hSetBinaryMode h False + runNetProtoHandle h r (serve u) + hClose h + -- cgit v1.2.3