e3c9462b6b
Former-commit-id: 07be9bcd252c92551aeac5c658f92f74243105e1 Former-commit-id: b341e9cf83887e8916850743b19530e2ae62bedc [formerly b0dd237c1af7232c65504086f32c543e2969f21c] [formerly 5f84fe76bfaf2ea3150d2524b2d8df3bcc202b42 [formerly df2a945ea4f815d78825bbd6eb47355fa7862146 [formerly df2a945ea4f815d78825bbd6eb47355fa7862146 [formerly df2a945ea4f815d78825bbd6eb47355fa7862146 [formerly f3baa2138e3ecd75e07adaa8e8390e0d1d3eeed6]]]]] Former-commit-id: 5a12d3b8f84beb6967fd2273700830e11a919dcb [formerly 23f65ae564b717c82f4f878bebcfb23696470c28] Former-commit-id: c2dda31dea3d0634127eb6fb4b2557d41c8ee442 Former-commit-id: 886a28263ded5e3f13a642e8739eb4f83c0615f3 Former-commit-id: 937d535ef551e00fe83c69c16f715cd71ebda6dd Former-commit-id: 4aae739c2c9a054011d743112ab937f750c179c2 [formerly 6a87e7206c043684e0f0c5acf8d83cd5bcfb6014] Former-commit-id: 58fbcd0de59b4c8ff87286b9b61e90425df334bb
128 lines
4.7 KiB
Haskell
128 lines
4.7 KiB
Haskell
{-# LANGUAGE DeriveAnyClass #-}
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
|
|
module Types where
|
|
|
|
import ClassyPrelude
|
|
import Data.Maybe
|
|
import System.IO (stdin, stdout)
|
|
import Data.ByteString (hGetSome, hPutStr)
|
|
|
|
import qualified Data.Streaming.Network as N
|
|
import qualified Network.Connection as NC
|
|
import Network.Socket (HostName, PortNumber(..))
|
|
import Network.Socket.Internal (PortNumber(..))
|
|
import qualified Network.Socket as N hiding (recv, recvFrom,
|
|
send, sendTo)
|
|
import qualified Network.Socket.ByteString as N
|
|
|
|
import qualified Network.WebSockets.Connection as WS
|
|
|
|
deriving instance Generic PortNumber
|
|
deriving instance Hashable PortNumber
|
|
deriving instance Generic N.SockAddr
|
|
deriving instance Hashable N.SockAddr
|
|
|
|
|
|
data Protocol = UDP | TCP | STDIO | SOCKS5 deriving (Show, Read, Eq)
|
|
|
|
data StdioAppData = StdioAppData
|
|
|
|
data UdpAppData = UdpAppData
|
|
{ appAddr :: N.SockAddr
|
|
, appSem :: MVar ByteString
|
|
, appRead :: IO ByteString
|
|
, appWrite :: ByteString -> IO ()
|
|
}
|
|
|
|
instance N.HasReadWrite UdpAppData where
|
|
readLens f appData = fmap (\getData -> appData { appRead = getData}) (f $ appRead appData)
|
|
writeLens f appData = fmap (\writeData -> appData { appWrite = writeData}) (f $ appWrite appData)
|
|
|
|
data ProxySettings = ProxySettings
|
|
{ host :: HostName
|
|
, port :: PortNumber
|
|
, credentials :: Maybe (ByteString, ByteString)
|
|
} deriving (Show)
|
|
|
|
data TunnelSettings = TunnelSettings
|
|
{ proxySetting :: Maybe ProxySettings
|
|
, localBind :: HostName
|
|
, localPort :: PortNumber
|
|
, serverHost :: HostName
|
|
, serverPort :: PortNumber
|
|
, destHost :: HostName
|
|
, destPort :: PortNumber
|
|
, protocol :: Protocol
|
|
, useTls :: Bool
|
|
, useSocks :: Bool
|
|
, upgradePrefix :: String
|
|
, udpTimeout :: Int
|
|
}
|
|
|
|
instance Show TunnelSettings where
|
|
show TunnelSettings{..} = localBind <> ":" <> show localPort
|
|
<> (if isNothing proxySetting
|
|
then mempty
|
|
else " <==PROXY==> " <> host (fromJust proxySetting) <> ":" <> (show . port $ fromJust proxySetting)
|
|
)
|
|
<> " <==" <> (if useTls then "WSS" else "WS") <> "==> "
|
|
<> serverHost <> ":" <> show serverPort
|
|
<> " <==" <> show (if protocol == SOCKS5 then TCP else protocol) <> "==> " <> destHost <> ":" <> show destPort
|
|
|
|
|
|
data Connection = Connection
|
|
{ read :: IO (Maybe ByteString)
|
|
, write :: ByteString -> IO ()
|
|
, close :: IO ()
|
|
, rawConnection :: Maybe N.Socket
|
|
}
|
|
|
|
class ToConnection a where
|
|
toConnection :: a -> Connection
|
|
|
|
instance ToConnection StdioAppData where
|
|
toConnection conn = Connection { read = Just <$> hGetSome stdin 512
|
|
, write = hPutStr stdout
|
|
, close = return ()
|
|
, rawConnection = Nothing
|
|
}
|
|
|
|
instance ToConnection WS.Connection where
|
|
toConnection conn = Connection { read = Just <$> WS.receiveData conn
|
|
, write = WS.sendBinaryData conn
|
|
, close = WS.sendClose conn (mempty :: LByteString)
|
|
, rawConnection = Nothing
|
|
}
|
|
|
|
instance ToConnection N.AppData where
|
|
toConnection conn = Connection { read = Just <$> N.appRead conn
|
|
, write = N.appWrite conn
|
|
, close = N.appCloseConnection conn
|
|
, rawConnection = Nothing
|
|
}
|
|
|
|
instance ToConnection UdpAppData where
|
|
toConnection conn = Connection { read = Just <$> appRead conn
|
|
, write = appWrite conn
|
|
, close = return ()
|
|
, rawConnection = Nothing
|
|
}
|
|
|
|
instance ToConnection NC.Connection where
|
|
toConnection conn = Connection { read = Just <$> NC.connectionGetChunk conn
|
|
, write = NC.connectionPut conn
|
|
, close = NC.connectionClose conn
|
|
, rawConnection = Nothing
|
|
}
|
|
|
|
data Error = ProxyConnectionError String
|
|
| ProxyForwardError String
|
|
| LocalServerError String
|
|
| TunnelError String
|
|
| WebsocketError String
|
|
| TlsError String
|
|
| Other String
|
|
deriving (Show)
|