menfon
menfon

Reputation: 1817

How to configure Yesod to only listen on localhost?

I am using a minimal template and only found where to change port:

main :: IO ()
main = warp 3000 App

but I have no clue where to set anything else. In App there is some magic happening with routes generation, but no hint of any configuration.

According to lsof -i Yesod is listening on all interfaces which I don't want. I want to limit it only to loopback (127.0.0.1).


I came up with a workaround which can be used at a top of any handler (route) to check if client's IP matches:

onlyAllowedFromLocalhost :: Handler ()
onlyAllowedFromLocalhost = do
  let allowedIp = "127.0.0.1"
  host <- waiRequest <&> W.remoteHost <&> tshow
  unless (T.isPrefixOf (allowedIp <> ":") host) $
    sendResponseStatus forbidden403 ("Access is allowed only from " <> allowedIp)

For a proper solution see answer from snak

Upvotes: 1

Views: 180

Answers (1)

snak
snak

Reputation: 6703

Instead of using warp convenience method, you can convert your site to Application using toWaiApp, then run it with runSettings. Now, configure Settings to bind only a loopback using setHost and pass it to runSettings.

Upvotes: 1

Related Questions