connection: Simple and easy network connections API

[ bsd3, library, network ] [ Propose Tags ]

Simple network library for all your connection need.

Features: Really simple to use, SSL/TLS, SOCKS

This library provides a very simple api to create sockets to a destination with the choice of SSL/TLS, and SOCKS.


[Skip to Readme]
Versions [faq] 0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.3.1, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2.5, 0.2.6, 0.2.7, 0.2.8, 0.3.0, 0.3.1
Dependencies base (>=3 && <5), bytestring, certificate (>=1.3.0 && <1.4.0), containers, cprng-aes, data-default, network (>=2.3 && <2.9), socks (>=0.4 && <0.6), tls (>=1.0), tls-extra (>=0.5) [details]
License BSD-3-Clause
Copyright Vincent Hanquez <vincent@snarc.org>
Author Vincent Hanquez <vincent@snarc.org>
Maintainer Vincent Hanquez <vincent@snarc.org>
Revised Revision 2 made by HerbertValerioRiedel at 2019-04-22T13:06:59Z
Category Network
Home page http://github.com/vincenthz/hs-connection
Source repo head: git clone git://github.com/vincenthz/hs-connection
Uploaded by VincentHanquez at 2012-10-22T04:59:59Z
Distributions Arch:0.3.1, Debian:0.2.8, Fedora:0.3.1, FreeBSD:0.2.5, LTSHaskell:0.2.8, NixOS:0.3.1, Stackage:0.3.0, openSUSE:0.3.1
Downloads 150268 total (837 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs uploaded by user
Build status unknown [no reports yet]

Modules

[Index]

Flags

NameDescriptionDefaultType
test

Build unit test

DisabledAutomatic

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

For package maintainers and hackage trustees


Readme for connection-0.1.0

[back to package description]

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:

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      = fromIntegral 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:

con <- connectTo ctx $ ConnectionParams
                       { connectionHostname  = "www.example.com"
                       , connectionPort      = fromIntegral 4567
                       , connectionUseSecure = Nothing
                       , connectionUseSocks  = Just $ SockSettingsSimple "localhost" (fromIntegral 1080)
                       }

Connecting to a SSL style socket is equally easy, and need to set the UseSecure fields in ConnectionParams:

con <- connectTo ctx $ ConnectionParams
                       { connectionHostname  = "www.example.com"
                       , connectionPort      = fromIntegral 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):

{-# 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      = fromIntegral 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