diff options
author | Joey Hess <joeyh@joeyh.name> | 2016-11-20 15:45:01 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2016-11-20 15:48:12 -0400 |
commit | dce8e76ef443e33d88b8301c86ebf080fceff511 (patch) | |
tree | 204d7f7b2eaaeaa4acd7ed2e1182fb208b829c2c /Utility | |
parent | 8e28135b26db1c920ebde7438db9bad87d3026ee (diff) |
remotedaemon: serve tor hidden service
Diffstat (limited to 'Utility')
-rw-r--r-- | Utility/Tor.hs | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/Utility/Tor.hs b/Utility/Tor.hs index a0a609008..b673c7105 100644 --- a/Utility/Tor.hs +++ b/Utility/Tor.hs @@ -15,6 +15,7 @@ import Data.Char type OnionPort = Int type OnionAddress = String type OnionSocket = FilePath +type UniqueIdent = String -- | Adds a hidden service connecting to localhost, using some kind -- of unique identifier. @@ -27,7 +28,7 @@ type OnionSocket = FilePath -- -- If there is already a hidden service for the specified unique -- identifier, returns its information without making any changes. -addHiddenService :: UserID -> String -> IO (OnionAddress, OnionPort, OnionSocket) +addHiddenService :: UserID -> UniqueIdent -> IO (OnionAddress, OnionPort, OnionSocket) addHiddenService uid ident = do ls <- lines <$> readFile torrc let portssocks = mapMaybe (parseportsock . separate isSpace) ls @@ -39,7 +40,7 @@ addHiddenService uid ident = do writeFile torrc $ unlines $ ls ++ [ "" - , "HiddenServiceDir " ++ hsdir + , "HiddenServiceDir " ++ hiddenServiceDir uid ident , "HiddenServicePort " ++ show newport ++ " unix:" ++ sockfile ] @@ -58,13 +59,12 @@ addHiddenService uid ident = do return (p, drop 1 (dropWhile (/= ':') l)) parseportsock _ = Nothing - hsdir = libDir </> "hidden_service_" ++ show uid ++ "_" ++ ident - sockfile = runDir uid </> ident ++ ".sock" + sockfile = socketFile uid ident waithiddenservice :: Int -> OnionPort -> IO (OnionAddress, OnionPort, OnionSocket) waithiddenservice 0 _ = error "tor failed to create hidden service, perhaps the tor service is not running" waithiddenservice n p = do - v <- tryIO $ readFile (hsdir </> "hostname") + v <- tryIO $ readFile $ hiddenServiceHostnameFile uid ident case v of Right s | ".onion\n" `isSuffixOf` s -> return (takeWhile (/= '\n') s, p, sockfile) @@ -80,3 +80,12 @@ libDir = "/var/lib/tor" runDir :: UserID -> FilePath runDir uid = "/var/run/user" </> show uid + +socketFile :: UserID -> UniqueIdent -> FilePath +socketFile uid ident = runDir uid </> ident ++ ".sock" + +hiddenServiceDir :: UserID -> UniqueIdent -> FilePath +hiddenServiceDir uid ident = libDir </> "hidden_service_" ++ show uid ++ "_" ++ ident + +hiddenServiceHostnameFile :: UserID -> UniqueIdent -> FilePath +hiddenServiceHostnameFile uid ident = hiddenServiceDir uid ident </> "hostname" |