2020-05-09 00:27:53 +03:00
|
|
|
{-|
|
|
|
|
Runs a single HTTP (or HTTPS) server for the eyre driver.
|
|
|
|
|
|
|
|
A server is given:
|
|
|
|
|
|
|
|
- A port, or a range or ports.
|
|
|
|
- Opens a socket on one of those ports.
|
|
|
|
- If this fails, try again repeatedly.
|
|
|
|
- Once a socket is opened, runs an HTTP server on the specified port.
|
|
|
|
- Once the server is up, calls a callback with the port that was opened.
|
|
|
|
- Once we have chosen a port, we commit to that port (ignoring the
|
|
|
|
original range).
|
|
|
|
- If the socket ever goes down, keep trying to reopen that port forever.
|
|
|
|
- When the server is shutdown, make sure the socket is closed.
|
|
|
|
|
|
|
|
TODO How to detect socket closed during server run?
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -Wno-deprecations #-}
|
|
|
|
|
|
|
|
module Urbit.Vere.Eyre.Serv
|
2020-05-11 01:27:02 +03:00
|
|
|
( ServApi(..)
|
|
|
|
, TlsConfig(..)
|
2020-05-12 22:45:39 +03:00
|
|
|
, MultiTlsConfig(..)
|
2020-05-09 00:27:53 +03:00
|
|
|
, ReqApi(..)
|
|
|
|
, ServType(..)
|
|
|
|
, ServPort(..)
|
|
|
|
, ServHost(..)
|
|
|
|
, ServConf(..)
|
|
|
|
, configCreds
|
|
|
|
, serv
|
2020-05-13 02:55:49 +03:00
|
|
|
, fakeServ
|
2020-05-09 00:27:53 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Urbit.Prelude hiding (Builder)
|
|
|
|
|
|
|
|
import Data.Default (def)
|
|
|
|
import Data.List.NonEmpty (NonEmpty((:|)))
|
|
|
|
import Network.TLS (Credential, Credentials(..), ServerHooks(..))
|
|
|
|
import Network.TLS (credentialLoadX509ChainFromMemory)
|
|
|
|
import RIO.Prelude (decodeUtf8Lenient)
|
|
|
|
|
2020-05-13 03:07:30 +03:00
|
|
|
import qualified Control.Monad.STM as STM
|
2020-05-09 00:27:53 +03:00
|
|
|
import qualified Data.Char as C
|
|
|
|
import qualified Network.Socket as Net
|
|
|
|
import qualified Network.Wai as W
|
|
|
|
import qualified Network.Wai.Handler.Warp as W
|
|
|
|
import qualified Network.Wai.Handler.WarpTLS as W
|
|
|
|
import qualified Urbit.Ob as Ob
|
|
|
|
import qualified Urbit.Vere.Eyre.Wai as E
|
|
|
|
|
|
|
|
|
|
|
|
-- Internal Types --------------------------------------------------------------
|
|
|
|
|
2020-05-11 01:27:02 +03:00
|
|
|
data ServApi = ServApi
|
|
|
|
{ saKil :: STM ()
|
|
|
|
, saPor :: STM W.Port
|
|
|
|
}
|
|
|
|
|
2020-05-09 00:27:53 +03:00
|
|
|
data TlsConfig = TlsConfig
|
|
|
|
{ tcPrKey :: ByteString
|
|
|
|
, tcCerti :: ByteString
|
|
|
|
, tcChain :: [ByteString]
|
|
|
|
}
|
2020-05-12 22:45:39 +03:00
|
|
|
deriving (Show)
|
2020-05-09 00:27:53 +03:00
|
|
|
|
2020-05-13 03:07:30 +03:00
|
|
|
newtype MultiTlsConfig = MTC (TVar (Map Ship (TlsConfig, Credential)))
|
2020-05-12 22:45:39 +03:00
|
|
|
|
|
|
|
instance Show MultiTlsConfig where
|
|
|
|
show = const "MultiTlsConfig"
|
2020-05-09 00:27:53 +03:00
|
|
|
|
2020-05-11 22:51:51 +03:00
|
|
|
data ReqApi = ReqApi
|
|
|
|
{ rcReq :: Ship -> Word64 -> E.ReqInfo -> STM ()
|
|
|
|
, rcKil :: Ship -> Word64 -> STM ()
|
2020-05-09 00:27:53 +03:00
|
|
|
}
|
|
|
|
|
2020-05-12 22:45:39 +03:00
|
|
|
instance Show ReqApi where
|
|
|
|
show = const "ReqApi"
|
|
|
|
|
2020-05-09 00:27:53 +03:00
|
|
|
data ServType
|
2020-05-11 22:51:51 +03:00
|
|
|
= STHttp Ship ReqApi
|
|
|
|
| STHttps Ship TlsConfig ReqApi
|
|
|
|
| STMultiHttp ReqApi
|
|
|
|
| STMultiHttps MultiTlsConfig ReqApi
|
2020-05-12 22:45:39 +03:00
|
|
|
deriving (Show)
|
2020-05-09 00:27:53 +03:00
|
|
|
|
|
|
|
data ServPort
|
|
|
|
= SPAnyPort
|
|
|
|
| SPChoices (NonEmpty W.Port)
|
2020-05-12 22:45:39 +03:00
|
|
|
deriving (Show)
|
2020-05-09 00:27:53 +03:00
|
|
|
|
|
|
|
data ServHost
|
|
|
|
= SHLocalhost
|
|
|
|
| SHAnyHostOk
|
2020-05-12 22:45:39 +03:00
|
|
|
deriving (Show)
|
2020-05-09 00:27:53 +03:00
|
|
|
|
|
|
|
data ServConf = ServConf
|
|
|
|
{ scType :: ServType
|
|
|
|
, scHost :: ServHost
|
|
|
|
, scPort :: ServPort
|
|
|
|
, scRedi :: Maybe W.Port
|
2020-05-13 02:55:49 +03:00
|
|
|
, scFake :: Bool
|
2020-05-09 00:27:53 +03:00
|
|
|
}
|
2020-05-12 22:45:39 +03:00
|
|
|
deriving (Show)
|
2020-05-09 00:27:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Opening Sockets -------------------------------------------------------------
|
|
|
|
|
|
|
|
getBindAddr :: String -> W.Port -> IO Net.SockAddr
|
|
|
|
getBindAddr hos por =
|
|
|
|
Net.getAddrInfo Nothing (Just hos) (Just (show por)) >>= \case
|
|
|
|
[] -> error "this should never happen."
|
|
|
|
x : _ -> pure (Net.addrAddress x)
|
|
|
|
|
|
|
|
bindListenPort :: String -> W.Port -> Net.Socket -> IO Net.PortNumber
|
|
|
|
bindListenPort hos por sok = do
|
|
|
|
Net.bind sok =<< getBindAddr hos por
|
|
|
|
Net.listen sok 1
|
|
|
|
Net.socketPort sok
|
|
|
|
|
|
|
|
tcpSocket :: IO (Either IOError Net.Socket)
|
|
|
|
tcpSocket =
|
|
|
|
tryIOError (Net.socket Net.AF_INET Net.Stream Net.defaultProtocol)
|
|
|
|
|
|
|
|
tryOpen :: String -> W.Port -> IO (Either IOError (W.Port, Net.Socket))
|
|
|
|
tryOpen hos por =
|
|
|
|
tcpSocket >>= \case
|
|
|
|
Left exn -> pure (Left exn)
|
|
|
|
Right sok -> tryIOError (bindListenPort hos por sok) >>= \case
|
|
|
|
Left exn -> Net.close sok $> Left exn
|
|
|
|
Right por -> pure (Right (fromIntegral por, sok))
|
|
|
|
|
|
|
|
openFreePort :: String -> IO (Either IOError (W.Port, Net.Socket))
|
|
|
|
openFreePort hos = do
|
|
|
|
tcpSocket >>= \case
|
|
|
|
Left exn -> pure (Left exn)
|
|
|
|
Right sok -> tryIOError (doBind sok) >>= \case
|
|
|
|
Left exn -> Net.close sok $> Left exn
|
|
|
|
Right ps -> pure (Right ps)
|
|
|
|
where
|
|
|
|
doBind sok = do
|
|
|
|
adr <- Net.inet_addr hos
|
|
|
|
Net.bind sok (Net.SockAddrInet Net.defaultPort adr)
|
|
|
|
Net.listen sok 1
|
|
|
|
port <- Net.socketPort sok
|
|
|
|
pure (fromIntegral port, sok)
|
|
|
|
|
|
|
|
retry :: HasLogFunc e => RIO e (Either IOError a) -> RIO e a
|
|
|
|
retry act = act >>= \case
|
|
|
|
Right res -> pure res
|
2020-05-13 21:29:50 +03:00
|
|
|
Left exn -> do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDbg ctx ("Failed to open ports. Waiting 5s, then trying again.", exn)
|
2020-05-13 21:29:50 +03:00
|
|
|
threadDelay 5_000_000
|
2020-05-09 00:27:53 +03:00
|
|
|
retry act
|
2020-05-13 21:29:50 +03:00
|
|
|
where
|
|
|
|
ctx = ["EYRE", "SERV", "retry"]
|
2020-05-09 00:27:53 +03:00
|
|
|
|
|
|
|
tryOpenChoices
|
|
|
|
:: HasLogFunc e
|
|
|
|
=> String
|
|
|
|
-> NonEmpty W.Port
|
|
|
|
-> RIO e (Either IOError (W.Port, Net.Socket))
|
|
|
|
tryOpenChoices hos = go
|
|
|
|
where
|
|
|
|
go (p :| ps) = do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug (displayShow ("EYRE", "Trying to open port.", p))
|
2020-05-09 00:27:53 +03:00
|
|
|
io (tryOpen hos p) >>= \case
|
|
|
|
Left err -> do
|
|
|
|
logError (displayShow ("EYRE", "Failed to open port.", p))
|
|
|
|
case ps of
|
|
|
|
[] -> pure (Left err)
|
|
|
|
q : qs -> go (q :| qs)
|
|
|
|
Right (p, s) -> do
|
|
|
|
pure (Right (p, s))
|
|
|
|
|
|
|
|
tryOpenAny
|
|
|
|
:: HasLogFunc e => String -> RIO e (Either IOError (W.Port, Net.Socket))
|
|
|
|
tryOpenAny hos = do
|
2020-05-13 21:29:50 +03:00
|
|
|
let ctx = ["EYRE", "SERV", "tryOpenAny"]
|
2020-06-11 05:02:09 +03:00
|
|
|
logDbg ctx "Asking the OS for any free port."
|
2020-05-09 00:27:53 +03:00
|
|
|
io (openFreePort hos) >>= \case
|
2020-05-13 21:29:50 +03:00
|
|
|
Left exn -> pure (Left exn)
|
|
|
|
Right (p, s) -> do
|
|
|
|
pure (Right (p, s))
|
|
|
|
|
2020-06-11 05:02:09 +03:00
|
|
|
logDbg :: (HasLogFunc e, Show a) => [Text] -> a -> RIO e ()
|
|
|
|
logDbg ctx msg = logDebug (prefix <> suffix)
|
2020-05-13 21:29:50 +03:00
|
|
|
where
|
|
|
|
prefix = display (concat $ fmap (<> ": ") ctx)
|
|
|
|
suffix = displayShow msg
|
2020-05-09 00:27:53 +03:00
|
|
|
|
|
|
|
forceOpenSocket
|
|
|
|
:: forall e
|
|
|
|
. HasLogFunc e
|
|
|
|
=> ServHost
|
|
|
|
-> ServPort
|
|
|
|
-> RAcquire e (W.Port, Net.Socket)
|
|
|
|
forceOpenSocket hos por = mkRAcquire opn kil
|
|
|
|
where
|
|
|
|
kil = io . Net.close . snd
|
|
|
|
|
|
|
|
opn = do
|
2020-05-13 21:29:50 +03:00
|
|
|
let ctx = ["EYRE", "SERV", "forceOpenSocket"]
|
2020-06-11 05:02:09 +03:00
|
|
|
logDbg ctx (hos, por)
|
2020-05-09 00:27:53 +03:00
|
|
|
(p, s) <- retry $ case por of
|
|
|
|
SPAnyPort -> tryOpenAny bind
|
|
|
|
SPChoices ps -> tryOpenChoices bind ps
|
2020-06-11 05:02:09 +03:00
|
|
|
logDbg ctx ("Opened port.", p)
|
2020-05-09 00:27:53 +03:00
|
|
|
pure (p, s)
|
|
|
|
|
|
|
|
bind = case hos of
|
|
|
|
SHLocalhost -> "127.0.0.1"
|
|
|
|
SHAnyHostOk -> "0.0.0.0"
|
|
|
|
|
|
|
|
|
|
|
|
-- Starting WAI ----------------------------------------------------------------
|
|
|
|
|
|
|
|
hostShip :: Maybe ByteString -> IO Ship
|
|
|
|
hostShip Nothing = error "Request must contain HOST header."
|
|
|
|
hostShip (Just bs) = byteShip (hedLabel bs) & \case
|
|
|
|
Left err -> error ("Bad host prefix. Must be a ship name: " <> unpack err)
|
|
|
|
Right sp -> pure sp
|
|
|
|
where
|
|
|
|
byteShip = fmap (fromIntegral . Ob.fromPatp) . bytePatp
|
|
|
|
bytePatp = Ob.parsePatp . decodeUtf8Lenient
|
|
|
|
hedLabel = fst . break (== fromIntegral (C.ord '.'))
|
|
|
|
|
2020-05-13 03:07:30 +03:00
|
|
|
onSniHdr
|
|
|
|
:: HasLogFunc e => e -> MultiTlsConfig -> Maybe String -> IO Credentials
|
|
|
|
onSniHdr env (MTC mtls) mHos = do
|
2020-05-09 00:27:53 +03:00
|
|
|
tabl <- atomically (readTVar mtls)
|
2020-06-11 05:02:09 +03:00
|
|
|
runRIO env $ logDbg ctx (tabl, mHos)
|
2020-05-13 03:07:30 +03:00
|
|
|
ship <- hostShip (encodeUtf8 . pack <$> mHos)
|
2020-06-11 05:02:09 +03:00
|
|
|
runRIO env $ logDbg ctx ship
|
2020-05-13 03:07:30 +03:00
|
|
|
tcfg <- lookup ship tabl & maybe (notRunning ship) (pure . snd)
|
2020-06-11 05:02:09 +03:00
|
|
|
runRIO env $ logDbg ctx tcfg
|
2020-05-09 00:27:53 +03:00
|
|
|
pure (Credentials [tcfg])
|
|
|
|
where
|
|
|
|
notRunning ship = error ("Ship not running: ~" <> show ship)
|
2020-05-13 21:29:50 +03:00
|
|
|
ctx = ["EYRE", "HTTPS", "SNI"]
|
2020-05-09 00:27:53 +03:00
|
|
|
|
|
|
|
startServer
|
|
|
|
:: HasLogFunc e
|
|
|
|
=> ServType
|
|
|
|
-> ServHost
|
|
|
|
-> W.Port
|
|
|
|
-> Net.Socket
|
|
|
|
-> Maybe W.Port
|
2020-05-11 01:27:02 +03:00
|
|
|
-> TVar E.LiveReqs
|
2020-05-09 00:27:53 +03:00
|
|
|
-> RIO e ()
|
2020-05-11 01:27:02 +03:00
|
|
|
startServer typ hos por sok red vLive = do
|
2020-05-09 00:27:53 +03:00
|
|
|
envir <- ask
|
|
|
|
|
|
|
|
let host = case hos of
|
|
|
|
SHLocalhost -> "127.0.0.1"
|
|
|
|
SHAnyHostOk -> "*"
|
|
|
|
|
|
|
|
let opts =
|
|
|
|
W.defaultSettings
|
|
|
|
& W.setHost host
|
|
|
|
& W.setPort (fromIntegral por)
|
|
|
|
& W.setTimeout (5 * 60)
|
|
|
|
|
2020-05-11 22:51:51 +03:00
|
|
|
let runAppl who = E.app envir who vLive
|
2020-05-09 00:27:53 +03:00
|
|
|
reqShip = hostShip . W.requestHeaderHost
|
|
|
|
|
|
|
|
case typ of
|
2020-05-11 22:51:51 +03:00
|
|
|
STHttp who api -> do
|
|
|
|
let app = runAppl who (rcReq api who) (rcKil api who)
|
2020-05-09 00:27:53 +03:00
|
|
|
io (W.runSettingsSocket opts sok app)
|
|
|
|
|
2020-05-11 22:51:51 +03:00
|
|
|
STHttps who TlsConfig {..} api -> do
|
2020-05-09 00:27:53 +03:00
|
|
|
let tls = W.tlsSettingsChainMemory tcCerti tcChain tcPrKey
|
2020-05-11 22:51:51 +03:00
|
|
|
let app = runAppl who (rcReq api who) (rcKil api who)
|
2020-05-09 00:27:53 +03:00
|
|
|
io (W.runTLSSocket tls opts sok app)
|
|
|
|
|
|
|
|
STMultiHttp api -> do
|
|
|
|
let app req resp = do
|
2020-05-11 22:51:51 +03:00
|
|
|
who <- reqShip req
|
|
|
|
runAppl who (rcReq api who) (rcKil api who) req resp
|
2020-05-09 00:27:53 +03:00
|
|
|
io (W.runSettingsSocket opts sok app)
|
|
|
|
|
|
|
|
STMultiHttps mtls api -> do
|
2020-05-13 03:07:30 +03:00
|
|
|
TlsConfig {..} <- atomically (getFirstTlsConfig mtls)
|
|
|
|
|
|
|
|
let sni = def { onServerNameIndication = onSniHdr envir mtls }
|
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
let tlsSing = (W.tlsSettingsChainMemory tcCerti tcChain tcPrKey)
|
|
|
|
let tlsMany = tlsSing { W.tlsServerHooks = sni }
|
2020-05-13 03:07:30 +03:00
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
let ctx = ["EYRE", "HTTPS", "REQ"]
|
2020-05-09 00:27:53 +03:00
|
|
|
|
2020-05-13 21:29:50 +03:00
|
|
|
let
|
|
|
|
app = \req resp -> do
|
2020-06-11 05:02:09 +03:00
|
|
|
runRIO envir $ logDbg ctx "Got request"
|
2020-05-13 21:29:50 +03:00
|
|
|
who <- reqShip req
|
2020-06-11 05:02:09 +03:00
|
|
|
runRIO envir $ logDbg ctx ("Parsed HOST", who)
|
2020-05-13 21:29:50 +03:00
|
|
|
runAppl who (rcReq api who) (rcKil api who) req resp
|
|
|
|
|
|
|
|
io (W.runTLSSocket tlsMany opts sok app)
|
2020-05-09 00:27:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
configCreds :: TlsConfig -> Either Text Credential
|
|
|
|
configCreds TlsConfig {..} =
|
|
|
|
credentialLoadX509ChainFromMemory tcCerti tcChain tcPrKey & \case
|
|
|
|
Left str -> Left (pack str)
|
|
|
|
Right rs -> Right rs
|
|
|
|
|
2020-05-13 02:55:49 +03:00
|
|
|
fakeServ :: HasLogFunc e => ServConf -> RIO e ServApi
|
|
|
|
fakeServ conf = do
|
|
|
|
let por = fakePort (scPort conf)
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug (displayShow ("EYRE", "SERV", "Running Fake Server", por))
|
2020-05-13 02:55:49 +03:00
|
|
|
pure $ ServApi
|
|
|
|
{ saKil = pure ()
|
|
|
|
, saPor = pure por
|
|
|
|
}
|
|
|
|
where
|
|
|
|
fakePort :: ServPort -> W.Port
|
|
|
|
fakePort SPAnyPort = 55555
|
|
|
|
fakePort (SPChoices (x :| _)) = x
|
|
|
|
|
2020-05-13 03:07:30 +03:00
|
|
|
getFirstTlsConfig :: MultiTlsConfig -> STM TlsConfig
|
|
|
|
getFirstTlsConfig (MTC var) = do
|
|
|
|
map <- readTVar var
|
|
|
|
case toList map of
|
|
|
|
[] -> STM.retry
|
|
|
|
x:_ -> pure (fst x)
|
|
|
|
|
2020-05-13 02:55:49 +03:00
|
|
|
realServ :: HasLogFunc e => TVar E.LiveReqs -> ServConf -> RIO e ServApi
|
|
|
|
realServ vLive conf@ServConf {..} = do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug (displayShow ("EYRE", "SERV", "Running Real Server"))
|
2020-05-11 01:27:02 +03:00
|
|
|
kil <- newEmptyTMVarIO
|
|
|
|
por <- newEmptyTMVarIO
|
|
|
|
|
2020-05-12 22:45:39 +03:00
|
|
|
tid <- async (runServ por)
|
|
|
|
_ <- async (atomically (takeTMVar kil) >> cancel tid)
|
2020-05-11 01:27:02 +03:00
|
|
|
|
|
|
|
pure $ ServApi
|
|
|
|
{ saKil = void (tryPutTMVar kil ())
|
|
|
|
, saPor = readTMVar por
|
|
|
|
}
|
2020-05-09 00:27:53 +03:00
|
|
|
where
|
2020-05-11 01:27:02 +03:00
|
|
|
runServ vPort = do
|
2020-06-11 05:02:09 +03:00
|
|
|
logDebug (displayShow ("EYRE", "SERV", "runServ"))
|
2020-05-11 01:27:02 +03:00
|
|
|
rwith (forceOpenSocket scHost scPort) $ \(por, sok) -> do
|
|
|
|
atomically (putTMVar vPort por)
|
|
|
|
startServer scType scHost por sok scRedi vLive
|
2020-05-13 02:55:49 +03:00
|
|
|
|
|
|
|
serv :: HasLogFunc e => TVar E.LiveReqs -> ServConf -> RIO e ServApi
|
|
|
|
serv vLive conf = do
|
|
|
|
if scFake conf
|
|
|
|
then fakeServ conf
|
|
|
|
else realServ vLive conf
|