5d62604582
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
151 lines
5.5 KiB
Haskell
151 lines
5.5 KiB
Haskell
{-# LANGUAGE DeriveAnyClass #-}
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
{-# LANGUAGE StrictData #-}
|
|
|
|
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 qualified Network.Socket as N hiding (recv, recvFrom,
|
|
send, sendTo)
|
|
import qualified Network.Socket.ByteString as N
|
|
|
|
import qualified Network.WebSockets.Connection as WS
|
|
import System.IO.Unsafe (unsafeDupablePerformIO)
|
|
|
|
|
|
instance Hashable PortNumber where
|
|
hashWithSalt s p = hashWithSalt s (fromEnum p)
|
|
|
|
deriving instance Generic N.SockAddr
|
|
deriving instance Hashable N.SockAddr
|
|
|
|
|
|
{-# NOINLINE defaultRecvBufferSize #-}
|
|
defaultRecvBufferSize :: Int
|
|
defaultRecvBufferSize = unsafeDupablePerformIO $
|
|
bracket (N.socket N.AF_INET N.Stream 0) N.close (\sock -> N.getSocketOption sock N.RecvBuffer)
|
|
|
|
defaultSendBufferSize :: Int
|
|
defaultSendBufferSize = defaultRecvBufferSize
|
|
|
|
sO_MARK :: N.SocketOption
|
|
sO_MARK = N.CustomSockOpt (1, 36) -- https://elixir.bootlin.com/linux/latest/source/arch/alpha/include/uapi/asm/socket.h#L64
|
|
|
|
{-# NOINLINE sO_MARK_Value #-}
|
|
sO_MARK_Value :: IORef Int
|
|
sO_MARK_Value = unsafeDupablePerformIO $ (newIORef 0)
|
|
|
|
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
|
|
, upgradeCredentials
|
|
:: ByteString
|
|
, tlsSNI :: ByteString
|
|
, hostHeader :: ByteString
|
|
, udpTimeout :: Int
|
|
, websocketPingFrequencySec :: 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)
|