Giter VIP home page Giter VIP logo

hs-connection's Introduction

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

hs-connection's People

Contributors

agrafix avatar alainodea avatar blackgnezdo avatar dmjio avatar dpwright avatar erikd avatar joeyadams avatar snoyberg avatar vincenthz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.