1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
haskell Connection library
==========================
Simple network library for all your connection need.
Features:
- Really simple to use
- SSL/TLS
- SOCKS
Usage
-----
Connect to www.example.com on port 4567 (without socks or tls), then send a
byte, receive a single byte, print it, and close the connection:
```haskell
import qualified Data.ByteString as B
import Network.Connection
import Data.Default
main = do
ctx <- initConnectionContext
con <- connectTo ctx $ ConnectionParams
{ connectionHostname = "www.example.com"
, connectionPort = 4567
, connectionUseSecure = Nothing
, connectionUseSocks = Nothing
}
connectionPut con (B.singleton 0xa)
r <- connectionGet con 1
putStrLn $ show r
connectionClose con
```
Using a socks proxy is easy, we just need replacing the connectionSocks
parameter, for example connecting to the same host, but using a socks
proxy at localhost:1080:
```haskell
con <- connectTo ctx $ ConnectionParams
{ connectionHostname = "www.example.com"
, connectionPort = 4567
, connectionUseSecure = Nothing
, connectionUseSocks = Just $ SockSettingsSimple "localhost" 1080
}
```
Connecting to a SSL style socket is equally easy, and need to set the UseSecure fields in ConnectionParams:
```haskell
con <- connectTo ctx $ ConnectionParams
{ connectionHostname = "www.example.com"
, connectionPort = 4567
, connectionUseSecure = Just def
, connectionUseSocks = Nothing
}
```
And finally, you can start TLS in the middle of an insecure connection. This is great for
protocol using STARTTLS (e.g. IMAP, SMTP):
```haskell
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString as B
import Data.ByteString.Char8 ()
import Network.Connection
import Data.Default
main = do
ctx <- initConnectionContext
con <- connectTo ctx $ ConnectionParams
{ connectionHostname = "www.example.com"
, connectionPort = 4567
, connectionUseSecure = Nothing
, connectionUseSocks = Nothing
}
-- talk to the other side with no TLS: says hello and starttls
connectionPut con "HELLO\n"
connectionPut con "STARTTLS\n"
-- switch to TLS
connectionSetSecure ctx con def
-- the connection is from now on using TLS, we can send secret for example
connectionPut con "PASSWORD 123\n"
connectionClose con
```
|