2015-11-07 11:14:19 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE PackageImports #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
|
|
|
|
module Main (main) where
|
|
|
|
|
|
|
|
import "base" Data.Monoid ((<>))
|
|
|
|
import "bytestring" Data.ByteString.Char8 (pack, unpack)
|
|
|
|
import "http-proxy" Network.HTTP.Proxy (runProxySettings, defaultProxySettings, Settings(..), Request(..))
|
|
|
|
import "http-types" Network.HTTP.Types (status302)
|
2017-12-06 13:21:57 +01:00
|
|
|
import "https-everywhere-rules" Data.HTTPSEverywhere.Rules (getRulesets, rewriteURL, RuleSet)
|
2015-11-07 11:14:19 +01:00
|
|
|
import "network-uri" Network.URI (URI, parseURI)
|
|
|
|
import "wai" Network.Wai (Response, responseLBS)
|
|
|
|
|
|
|
|
(<&>) :: Functor f => f a -> (a -> b) -> f b
|
|
|
|
(<&>) = flip fmap; infixl 1 <&>
|
|
|
|
|
|
|
|
httpRedirect :: URI -> Response
|
|
|
|
httpRedirect to = responseLBS status302 [("Location", pack . show $ to)] ""
|
|
|
|
|
2017-12-06 13:21:57 +01:00
|
|
|
tryHTTPS :: [RuleSet] -> Request -> IO (Either Response Request)
|
|
|
|
tryHTTPS rs Request{..} = case parseURI . unpack $ requestPath <> queryString of
|
|
|
|
Just uri -> rewriteURL rs uri <&> \rewrite -> if rewrite == uri
|
2015-11-07 11:14:19 +01:00
|
|
|
then return Request{..}
|
|
|
|
else Left $ httpRedirect rewrite
|
|
|
|
Nothing -> return $ Right Request{..}
|
|
|
|
|
|
|
|
main :: IO ()
|
2017-12-06 13:21:57 +01:00
|
|
|
main = do
|
|
|
|
rulesets <- getRulesets
|
|
|
|
runProxySettings $ defaultProxySettings { proxyRequestModifier = tryHTTPS rulesets }
|