crypton-connection: Simple and easy network connection API

[ bsd3, library, network ] [ Propose Tags ] [ Report a vulnerability ]

Simple network library for all your connection needs.

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]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.3.1, 0.3.2, 0.4.0, 0.4.1, 0.4.2, 0.4.3
Change log CHANGELOG.md
Dependencies base (>=3 && <5), basement, bytestring, containers, crypton-x509 (>=1.5), crypton-x509-store (>=1.5), crypton-x509-system (>=1.5), crypton-x509-validation (>=1.5), data-default, network (>=2.6.3), socks (>=0.6), tls (>=1.7 && <2.2) [details]
License BSD-3-Clause
Copyright Vincent Hanquez <vincent@snarc.org>
Author Vincent Hanquez <vincent@snarc.org>
Maintainer Kazu Yamamoto <kazu@iij.ad.jp>
Category Network
Home page https://github.com/kazu-yamamoto/crypton-connection
Source repo head: git clone https://github.com/kazu-yamamoto/crypton-connection
Uploaded by KazuYamamoto at 2024-12-06T05:38:35Z
Distributions Arch:0.3.2, LTSHaskell:0.4.3, NixOS:0.3.2, Stackage:0.4.3, openSUSE:0.3.2
Reverse Dependencies 23 direct, 1740 indirect [details]
Downloads 5283 total (604 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-12-06 [all 1 reports]

Readme for crypton-connection-0.4.3

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

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):

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