Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:22:54+00:00 2026-06-07T09:22:54+00:00

I’m trying to figure out how to properly use the OpenSSL.Session API in a

  • 0

I’m trying to figure out how to properly use the OpenSSL.Session API in a concurrent context

E.g. assume I want to implement a stunnel-style ssl-wrapper, I’d expect to have the following basic skeleton structure, which implements a naive full-duplex tcp-port-forwarder:

runProxy :: PortID -> AddrInfo -> IO ()
runProxy localPort@(PortNumber lpn) serverAddrInfo = do
  listener <- listenOn localPort

  forever $ do
    (sClient, clientAddr) <- accept listener

    let finalize sServer = do
            sClose sServer
            sClose sClient

    forkIO $ do
        tidToServer <- myThreadId
        bracket (connectToServer serverAddrInfo) finalize $ \sServer -> do
            -- execute one 'copySocket' thread for each data direction
            -- and make sure that if one direction dies, the other gets
            -- pulled down as well
            bracket (forkIO (copySocket sServer sClient
                             `finally` killThread tidToServer))
                    (killThread) $ \_ -> do
                copySocket sClient sServer -- "controlling" thread

 where
  -- |Copy data from source to dest until EOF occurs on source
  -- Copying may also be aborted due to exceptions
  copySocket :: Socket -> Socket -> IO ()
  copySocket src dst = go
   where
    go = do
        buf <- B.recv src 4096
        unless (B.null buf) $ do
            B.sendAll dst buf
            go

  -- |Create connection to given AddrInfo target and return socket
  connectToServer saddr = do
    sServer <- socket (addrFamily saddr) Stream defaultProtocol
    connect sServer (addrAddress saddr)
    return sServer

How do I transform the above skeleton into a full-duplex ssl-wrapping tcp-forwarding proxy? Where are the dangers W.R.T to concurrent/parallel execution (in the context of the above use-case) of the function calls provided by the HsOpenSSL API?

PS: I’m still struggling to fully comprehend how to make the code robust w.r.t. to exceptions and resource-leaks. So, albeit not being the primary focus of this question, if you notice something bad in the code above, please leave a comment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:22:58+00:00Added an answer on June 7, 2026 at 9:22 am

    To do this you need to replace copySocket with two different functions, one to handle data from the plain socket to SSL and the other from SSL to the plain socket:

      copyIn :: SSL.SSL -> Socket -> IO ()
      copyIn src dst = go
       where
        go = do
            buf <- SSL.read src 4096
            unless (B.null buf) $ do
                SB.sendAll dst buf
                go
    
      copyOut :: Socket -> SSL.SSL -> IO ()
      copyOut src dst = go
       where
        go = do
            buf <- SB.recv src 4096
            unless (B.null buf) $ do
                SSL.write dst buf
                go
    

    Then you need to modify connectToServer so that it establishes an SSL connection

      -- |Create connection to given AddrInfo target and return socket
      connectToServer saddr = do
        sServer <- socket (addrFamily saddr) Stream defaultProtocol
        putStrLn "connecting"
        connect sServer (addrAddress saddr)
        putStrLn "establishing ssl context"
        ctx <- SSL.context
        putStrLn "setting ciphers"
        SSL.contextSetCiphers ctx "DEFAULT"
        putStrLn "setting verfication mode"
        SSL.contextSetVerificationMode ctx SSL.VerifyNone
        putStrLn "making ssl connection"
        sslServer <- SSL.connection ctx sServer
        putStrLn "doing handshake"
        SSL.connect sslServer
        putStrLn "connected"
        return sslServer
    

    and change finalize to shut down the SSL session

    let finalize sServer = do
            putStrLn "shutting down ssl"
            SSL.shutdown sServer SSL.Unidirectional
            putStrLn "closing server socket"
            maybe (return ()) sClose (SSL.sslSocket sServer)
            putStrLn "closing client socket"
            sClose sClient
    

    Finally, don’t forget to run your main stuff within withOpenSSL as in

    main = withOpenSSL $ do
        let hints = defaultHints { addrSocketType = Stream, addrFamily = AF_INET }
        addrs <- getAddrInfo (Just hints) (Just "localhost") (Just "22222")
        let addr = head addrs
        print addr
        runProxy (PortNumber 11111) addr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.