From 5d62604582765849f4591bd6e5fa14f5648d7daf Mon Sep 17 00:00:00 2001 From: Romain GERARD Date: Mon, 7 Dec 2020 10:59:13 +0100 Subject: [PATCH] #57 Allow to specify websocket ping frequency Former-commit-id: 31c30bf3ff09e54b8fb8e7610aabae0b1247502f Former-commit-id: 3149f827351c421bc99edad7381643bbac567d56 [formerly 084fb35038a11bf8f4016f1eca51295199534d68] [formerly d6284387041bc84ebc3859d2a357cb7e9b678e9e [formerly 8413b4a3244698b20e3356cb5f9d76b24122d93c [formerly 8413b4a3244698b20e3356cb5f9d76b24122d93c [formerly 8413b4a3244698b20e3356cb5f9d76b24122d93c [formerly fddae990cae36aba019f83124e522d2545b93ecf]]]]] Former-commit-id: 186beb54d83f454ae545c45194e1ba3e9eab1495 [formerly a7d06a8c6e0064c48eee331b9fa6f87496ba0f37] Former-commit-id: 5f88e7d16eb372828bbfeaba017699f62db92c6c Former-commit-id: a7fb9dc52d52d177d26ebdd78ab69dadddd17939 Former-commit-id: b1a292297fb943c3f555ff639802fcdf81005240 Former-commit-id: ea8f23d6c60e0b316358255156bbd644330a90e9 [formerly 961596cfff926e301cf39cf0b2114d58a62b7e9a] Former-commit-id: 1b85c73b4c55a63dc96af4c7af6e1a5edc3d9616 --- app/Main.hs | 13 +++++++++++-- src/Tunnel.hs | 2 +- src/Types.hs | 1 + test/Spec.hs | 3 +++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index fbf6c87..e687da5 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -33,8 +33,8 @@ data WsTunnel = WsTunnel , pathPrefix :: String , hostHeader :: String , tlsSNI :: String - , wsTunnelCredentials - :: String + , websocketPingFrequencySec :: Int + , wsTunnelCredentials :: String } deriving (Show, Data, Typeable) data WsServerInfo = WsServerInfo @@ -77,6 +77,8 @@ cmdLine = WsTunnel &= help "If set, use custom string in the SNI during TLS handshake" &= typ "String" &= groupname "Client options" , soMark = def &= explicit &= name "soMark" &= help "(linux only) Mark network packet with SO_MARK sockoption with the specified value. You need to use {root, sudo, capabilities} to run wstunnel when using this option" &= typ "int" + , websocketPingFrequencySec = def &= explicit &= name "websocketPingFrequencySec" + &= help "do a hearthbeat ping every x seconds to maintain websocket connection" &= typ "int" , wsTunnelServer = def &= argPos 0 &= typ "ws[s]://wstunnelServer[:port]" , serverMode = def &= explicit &= name "server" @@ -180,6 +182,9 @@ main = do , Main.udpTimeout = if Main.udpTimeout cfg' == 0 then 30 * 10^(6 :: Int) else if Main.udpTimeout cfg' == -1 then -1 else Main.udpTimeout cfg' * 10^(6:: Int) + , Main.websocketPingFrequencySec = if Main.websocketPingFrequencySec cfg' == 0 + then 30 + else Main.websocketPingFrequencySec cfg' } let serverInfo = parseServerInfo (WsServerInfo False "" 0) (wsTunnelServer cfg) @@ -236,6 +241,7 @@ runApp cfg serverInfo , udpTimeout = Main.udpTimeout cfg , tlsSNI = BC.pack $ Main.tlsSNI cfg , hostHeader = BC.pack $ Main.hostHeader cfg + , websocketPingFrequencySec = Main.websocketPingFrequencySec cfg } toTcpLocalToRemoteTunnelSetting cfg serverInfo (TunnelInfo lHost lPort rHost rPort) = @@ -255,6 +261,7 @@ runApp cfg serverInfo , udpTimeout = Main.udpTimeout cfg , tlsSNI = BC.pack $ Main.tlsSNI cfg , hostHeader = BC.pack $ Main.hostHeader cfg + , websocketPingFrequencySec = Main.websocketPingFrequencySec cfg } toUdpLocalToRemoteTunnelSetting cfg serverInfo (TunnelInfo lHost lPort rHost rPort) = @@ -274,6 +281,7 @@ runApp cfg serverInfo , udpTimeout = Main.udpTimeout cfg , tlsSNI = BC.pack $ Main.tlsSNI cfg , hostHeader = BC.pack $ Main.hostHeader cfg + , websocketPingFrequencySec = Main.websocketPingFrequencySec cfg } toDynamicTunnelSetting cfg serverInfo (TunnelInfo lHost lPort _ _) = @@ -293,4 +301,5 @@ runApp cfg serverInfo , udpTimeout = Main.udpTimeout cfg , tlsSNI = BC.pack $ Main.tlsSNI cfg , hostHeader = BC.pack $ Main.hostHeader cfg + , websocketPingFrequencySec = Main.websocketPingFrequencySec cfg } diff --git a/src/Tunnel.hs b/src/Tunnel.hs index 40e09d7..62754d6 100644 --- a/src/Tunnel.hs +++ b/src/Tunnel.hs @@ -76,7 +76,7 @@ tunnelingClientP cfg@TunnelSettings{..} app conn = onError $ do connectionToStream Connection{..} = WS.makeStream read (write . toStrict . fromJust) onError = flip catch (\(e :: SomeException) -> return . throwError . WebsocketError $ show e) run cnx = do - WS.forkPingThread cnx 30 + WS.forkPingThread cnx websocketPingFrequencySec app (toConnection cnx) diff --git a/src/Types.hs b/src/Types.hs index 3730f09..63da02a 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -82,6 +82,7 @@ data TunnelSettings = TunnelSettings , tlsSNI :: ByteString , hostHeader :: ByteString , udpTimeout :: Int + , websocketPingFrequencySec :: Int } instance Show TunnelSettings where diff --git a/test/Spec.hs b/test/Spec.hs index b428ee2..268c012 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -50,6 +50,7 @@ testTCPLocalToRemote useTLS = do , upgradeCredentials = "" , hostHeader = "toto.com" , tlsSNI = "toto.com" + , websocketPingFrequencySec = 30 } let client = runClient tunnelSetting @@ -110,6 +111,7 @@ testUDPLocalToRemote useTLS = do , upgradeCredentials = "" , hostHeader = "toto.com" , tlsSNI = "toto.com" + , websocketPingFrequencySec = 30 } let client = runClient tunnelSetting @@ -169,6 +171,7 @@ testSocks5Tunneling useTLS = do , upgradeCredentials = "" , hostHeader = "toto.com" , tlsSNI = "toto.com" + , websocketPingFrequencySec = 30 } let client = runClient tunnelSetting