packages feed

network-anonymous-tor 0.9.0 → 0.9.1

raw patch · 4 files changed

+70/−26 lines, 4 filesnew-component:exe:tor-relay

Files

+ examples/Relay.hs view
@@ -0,0 +1,44 @@+{-# LANGUAGE OverloadedStrings #-}
+
+import           System.Environment    (getArgs)
+
+import           Control.Concurrent    (threadDelay)
+import           Network               (withSocketsDo)
+import qualified Network.Anonymous.Tor as Tor
+import qualified Network.Socks5.Types  as SocksT
+
+getPortmap :: IO (Integer, Integer)
+getPortmap = do
+  [pub, priv] <- getArgs
+  return (read pub, read priv)
+
+whichControlPort :: IO Integer
+whichControlPort = do
+  ports <- Tor.detectPort [9051, 9151]
+  return (head ports)
+
+main = withSocketsDo $ do
+  torPort <- whichControlPort
+  portmap <- getPortmap
+
+  Tor.withSession torPort (withinSession portmap)
+
+  return ()
+
+  where
+    withinSession (publicPort, privatePort) controlSock = do
+      onion <- Tor.accept controlSock publicPort (newConnection privatePort)
+
+      putStrLn ("hidden service descriptor: " ++ show onion)
+
+      -- If we would leave this function at this point, our connection with
+      -- the Tor control service would be lost, which would cause Tor to clean
+      -- up any mappings and hidden services we have registered.
+      --
+      -- Since this is just an example, we will now wait for 5 minutes and then
+      -- exit.
+      threadDelay 300000000
+
+    newConnection privatePort sock = do
+      putStrLn "Got new connection!"
+      return ()
network-anonymous-tor.cabal view
@@ -1,6 +1,6 @@ name: network-anonymous-tor
 category: Network
-version: 0.9.0
+version: 0.9.1
 license: MIT
 license-file: LICENSE
 copyright: (c) 2014 Leon Mergen
@@ -15,7 +15,7 @@ build-type: Simple
 data-files: LICENSE, README.md
 cabal-version: >= 1.10
-tested-with: GHC == 7.8, GHC == 7.10
+tested-with: GHC == 7.6, GHC == 7.8, GHC == 7.10
 
 library
   hs-source-dirs:      src
@@ -74,6 +74,21 @@ 
                      , network-anonymous-tor
 
+executable tor-relay
+  default-language:    Haskell2010
+  hs-source-dirs:      examples
+  main-is:             Relay.hs
+  
+  build-depends:       base                     >= 4.3          && < 5
+                     , exceptions
+
+                     , network
+                     , network-simple
+                     , socks
+                     
+                     , network-anonymous-tor
+                     
+                     
 source-repository head
   type: git
   location: git://github.com/solatis/haskell-network-anonymous-tor.git
src/Network/Anonymous/Tor.hs view
@@ -146,11 +146,12 @@ --   port is dropped, which means that any port mappings, connections and hidden
 --   services you have registered within the session will be cleaned up. This
 --   is by design, to prevent stale mappings when an application crashes.
-withSession :: Integer                  -- | Port the Tor control server is listening at. Use
+
+withSession :: Integer                  -- ^ Port the Tor control server is listening at. Use
                                         --   'detectPort' to probe possible ports.
-            -> (Network.Socket -> IO a) -- | Callback function called after a session has been
+            -> (Network.Socket -> IO a) -- ^ Callback function called after a session has been
                                         --   established succesfully.
-            -> IO a                     -- | Returns the value returned by the callback.
+            -> IO a                     -- ^ Returns the value returned by the callback.
 withSession port callback =
   NST.connect "127.0.0.1" (show port) (\(sock, _) -> do
                                             _ <- P.authenticate sock
@@ -160,18 +161,16 @@ --   connections for it. Note that this creates a new local server at the same
 --   port as the public port, so ensure that the port is not yet in use.
 accept :: MonadIO m
-       => Network.Socket            -- | Connection with Tor control server
-       -> Integer                   -- | Port to listen at
-       -> (Network.Socket -> IO ()) -- | Callback function called for each incoming connection
-       -> m B32.Base32String        -- | Returns the hidden service descriptor created
+       => Network.Socket            -- ^ Connection with Tor control server
+       -> Integer                   -- ^ Port to listen at
+       -> (Network.Socket -> IO ()) -- ^ Callback function called for each incoming connection
+       -> m B32.Base32String        -- ^ Returns the hidden service descriptor created
                                     --   without the '.onion' part.
 accept sock port callback = do
   -- First create local service
   _ <- liftIO $ forkIO $
-       NST.listen "*" (show port) (\(lsock, _) -> do
-                                        putStrLn ("started server on port " ++ show port ++ ", now accepting connections")
+       NST.listen "*" (show port) (\(lsock, _) ->
                                         NST.accept lsock (\(csock, _) -> do
-                                                               putStrLn "accepted connection, now executing callback"
                                                                _ <- callback csock
                                                                threadDelay 1000000
                                                                return ()))
src/Network/Anonymous/Tor/Protocol.hs view
@@ -46,8 +46,6 @@ import qualified Network.Anonymous.Tor.Protocol.Parser.Ast as Ast
 import qualified Network.Anonymous.Tor.Protocol.Types      as T
 
-import           Debug.Trace                               (trace)
-
 sendCommand :: MonadIO m
             => Network.Socket -- ^ Our connection with the Tor control port
             -> BS.ByteString  -- ^ The command / instruction we wish to send
@@ -61,13 +59,9 @@              -> BS.ByteString  -- ^ The command / instruction we wish to send
              -> m [Ast.Line]
 sendCommand' sock status errorType msg = do
-  trace ("sending data: " ++ show msg) (return ())
-
   _   <- liftIO $ Network.sendAll sock msg
   res <- liftIO $ NA.parseOne sock (Atto.parse Parser.reply)
 
-  trace ("got response: " ++ show res) (return ())
-
   when (Ast.statusCode res /= status)
     (E.torError (E.mkTorError errorType))
 
@@ -109,8 +103,6 @@ socksPort s = do
   reply <- sendCommand s (BS8.pack "GETCONF SOCKSPORT\n")
 
-  liftIO $ putStrLn ("got socksport reply: " ++ show reply)
-
   return . fst . fromJust . BS8.readInteger . fromJust . Ast.tokenValue . head . Ast.lineMessage . fromJust $ Ast.line (BS8.pack "SocksPort") reply
 
 -- | Connect through a remote using the Tor SOCKS proxy. The remote might me a
@@ -126,13 +118,7 @@         -> (Network.Socket -> IO a) -- ^ Computation to execute once connection has been establised
         -> m a
 connect sport remote callback = liftIO $ do
-
-  putStrLn ("Now establishing anonymous connection with remote: " ++ show remote)
-
   (sock, _) <- Socks.socksConnect conf remote
-
-  putStrLn "Established connection!"
-
   callback sock
 
   where