summaryrefslogtreecommitdiff
path: root/dht
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-12-04 14:05:22 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-01 23:22:52 -0500
commit5b2bddeae815fef5fe46c9d516a3cc6f4645c879 (patch)
tree0b71d6f6a20f37b0eba3bddb5202164a9b823e0a /dht
parent92c01bfc99f5781ff37fbb5a98a73af376b9af44 (diff)
Stagger tcp-probe of port 3389.
Diffstat (limited to 'dht')
-rw-r--r--dht/TCPProber.hs27
1 files changed, 18 insertions, 9 deletions
diff --git a/dht/TCPProber.hs b/dht/TCPProber.hs
index ff68ba07..d58b8b60 100644
--- a/dht/TCPProber.hs
+++ b/dht/TCPProber.hs
@@ -27,6 +27,8 @@ import Network.Kademlia.Search
27import Network.Tox.NodeId 27import Network.Tox.NodeId
28import qualified Network.Tox.TCP as TCP 28import qualified Network.Tox.TCP as TCP
29 29
30-- Probe TCP ports in a staggered fashion to up the odds of discovering
31-- a higher priority port like 443.
30resolvePort :: TCP.RelayClient -> NodeInfo -> IO (Maybe PortNumber) 32resolvePort :: TCP.RelayClient -> NodeInfo -> IO (Maybe PortNumber)
31resolvePort tcp ni = do 33resolvePort tcp ni = do
32 got <- newTVarIO Nothing 34 got <- newTVarIO Nothing
@@ -51,21 +53,28 @@ resolvePort tcp ni = do
51 Nothing -> readTVar cnt >>= check . (== 0) >> return Nothing 53 Nothing -> readTVar cnt >>= check . (== 0) >> return Nothing
52 t443 <- forkPort 443 54 t443 <- forkPort 443
53 t80 <- forkPort 80 55 t80 <- forkPort 80
54 p <- timeout 1000000 readResult >>= \case 56 p <- timeout 500000 readResult >>= \case
55 Just (Just p) -> do 57 Just (Just p) -> do
56 killThread t443 58 killThread t443
57 killThread t80 59 killThread t80
58 return $ Just p 60 return $ Just p
59 _ -> do 61 _ -> do
60 let uport = nodePort ni
61 tudp <- forM (guard $ uport `notElem` [443,80,3389,33445])
62 $ \() -> forkPort uport
63 t3389 <- forkPort 3389 62 t3389 <- forkPort 3389
64 t33445 <- forkPort 33445 63 timeout 500000 readResult >>= \case
65 p <- readResult 64 Just (Just p) -> do
66 mapM_ killThread [t443,t80,t3389,t33445] 65 killThread t3389
67 mapM_ killThread (tudp :: Maybe ThreadId) 66 killThread t443
68 return p 67 killThread t80
68 return $ Just p
69 _ -> do
70 let uport = nodePort ni
71 tudp <- forM (guard $ uport `notElem` [443,80,3389,33445])
72 $ \() -> forkPort uport
73 t33445 <- forkPort 33445
74 p <- readResult
75 mapM_ killThread [t443,t80,t3389,t33445]
76 mapM_ killThread (tudp :: Maybe ThreadId)
77 return p
69 return p 78 return p
70 79
71data TCPProber = TCPProber 80data TCPProber = TCPProber