packages feed

hs-hath-1.4.0: src/Hath/Server.hs

-- | The web server: the application it serves, and the client's life around
-- it, from client_start to client_stop.
module Hath.Server
  ( startServer
  , ServerOptions(..)
  , makeApplication
  , ServerEnv(..)
  , ServerAction(..)
  , needsRenewal
  ) where

import           Control.Concurrent                   ( threadDelay )

import           Data.Hourglass                       ( Elapsed(..), Seconds(..), timeGetElapsed )
import           Data.String.Interpolate              ( i )
import           Data.X509                            ( CertificateChain(..)
                                                      , PrivKey
                                                      , certValidity
                                                      )
import qualified Data.X509                            as X509

import           Database.SQLite.Simple               ( withConnection )

import           Hath.Cache                           ( CacheRunner(..), deleteFile )
import           Hath.Cache.Filesystem                ( cachedBytes
                                                      , cachedFiles
                                                      , openFsCache
                                                      , runCacheFilesystem
                                                      , runEvictor
                                                      , saveState
                                                      )
import           Hath.Cache.R2                        ( mkR2Connection, runCacheR2 )
import           Hath.Cache.SQLite                    ( initializeDB, isEmpty, runCacheSQLite )
import           Hath.Config                          ( CacheBackend(..), ClientConfig(..) )
import           Hath.DiskSpace                       ( availableBytes )
import           Hath.Error                           ( HathError )
import           Hath.Gallery.Downloader              ( runGalleryDownloader )
import           Hath.Log                             ( logWarning )
import           Hath.RPC                             ( StartFailure(..)
                                                      , clientSettings
                                                      , clientStart
                                                      , clientStop
                                                      , getBlacklist
                                                      , getCertificate
                                                      , overload
                                                      , runRPCIO
                                                      , startFailure
                                                      , Stop(..)
                                                      , forgetRPCServerFailure
                                                      , stillAlive
                                                      , toldToStop
                                                      )
import           Hath.Server.App                      ( ServerAction(..), ServerEnv(..) )
import           Hath.Server.ConnectionLimit          ( connectionLimit )
import           Hath.Server.FloodControl             ( isRPCServer, newFloodControl )
import           Hath.Server.Handlers                 ( application )
import           Hath.Server.LegacyCiphers
import           Hath.Server.Middleware
import           Hath.ServerTime                      ( serverNow )
import           Hath.Settings                        ( HathSettings(..) )
import           Hath.Stats                           ( newStatsEnv )

import           Network.TLS                          ( Credentials(Credentials)
                                                      , TLSException(HandshakeFailed)
                                                      , Version(..)
                                                      )
import           Network.TLS.Extra                    ( ciphersuite_all )
import qualified Network.TLS.SessionManager           as SM
import           Network.Wai                          ( Application, Request )
import           Network.Wai.Handler.Warp             ( defaultOnException
                                                      , defaultSettings
                                                      , setBeforeMainLoop
                                                      , setGracefulShutdownTimeout
                                                      , setInstallShutdownHandler
                                                      , setOnClose
                                                      , setOnException
                                                      , setOnOpen
                                                      , setPort
                                                      )
import           Network.Wai.Handler.WarpTLS          ( OnInsecure(AllowInsecure)
                                                      , TLSSettings(..)
                                                      , WarpTLSException(ClientClosedConnectionPrematurely)
                                                      , runTLS
                                                      , tlsSettingsSni
                                                      )
import           Network.Wai.Middleware.RealIp        ( realIpHeader )
import           Network.Wai.Middleware.RequestLogger ( logStdout )

import           Polysemy.Error                       ( catch )

import           Relude

import           System.Exit                          ( ExitCode(..) )

import           UnliftIO                             ( TChan
                                                      , finally
                                                      , mapConcurrently_
                                                      , race
                                                      , readTChan
                                                      , tryAny
                                                      , waitCatch
                                                      , withAsync
                                                      , writeTChan
                                                      )

makeApplication :: ServerEnv -> Application
makeApplication env = middleware (application env)
  where
    middleware = realIp . floodControl . tracingConnections stats . tracingTimeUsage stats

    stats = envStats env

    realIp
      | envTrustProxyHeaders env = realIpHeader "X-Forwarded-For"
      | otherwise = id

    floodControl = maybe id (`rateLimitMiddleware` fromRPCServer) (envFloodControl env)

    fromRPCServer ip = (`isRPCServer` ip) . rpcServerIPs <$> readTVarIO (envSettings env)

data ServerOptions
  = ServerOptions { disableRateLimit  :: !Bool
                    -- ^ no flood control, for when every reader appears to come from one address
                  , trustProxyHeaders :: !Bool
                    -- ^ take the reader's address from @X-Forwarded-For@
                  , skipStartupVerify :: !Bool
                    -- ^ ignore @verify_cache@
                  }

-- | Serve until told to shut down, then return how to exit; exit if the RPC
-- server will not have us. The settings and certificate are the ones it gave us at login.
startServer :: ClientConfig
            -> ServerOptions
            -> HathSettings
            -> ( CertificateChain, PrivKey )
            -> TChan ServerAction
            -> IO ExitCode
startServer config options settings certs actions = do
  settingsVar <- newTVarIO settings
  downloadRequest <- newEmptyMVar
  stats <- newStatsEnv
  floodControl <- if disableRateLimit options
    then pure Nothing
    else Just <$> newFloodControl
  -- an empty token would let in anyone who sends "Bearer "
  metricsToken <- mfilter (not . null) <$> lookupEnv "HATH_METRICS_TOKEN"
  runGalleryDownloader config stats settingsVar downloadRequest
    `alongside` withCacheBackend
      config
      settings
      (skipStartupVerify options)
      settingsVar
      (\cache -> serverLoop
         ServerEnv { envConfig = config
                   , envSettings = settingsVar
                   , envActions = actions
                   , envDownloadRequest = downloadRequest
                   , envStats = stats
                   , envCache = cache
                   , envFloodControl = floodControl
                   , envTrustProxyHeaders = trustProxyHeaders options
                   , envMetricsToken = encodeUtf8 <$> metricsToken
                   }
         certs)

alongside :: IO a -> IO b -> IO b
alongside background action = withAsync background (const action)

-- | Open the cache the config asks for, and refuse to go on with one the
-- network would lose trust in (official CacheHandler.java:140-157). The
-- Filesystem backend also gets its evictor, which follows the disk limit the
-- server assigns, and its state saved at the end.
withCacheBackend
  :: ClientConfig -> HathSettings -> Bool -> TVar HathSettings -> (CacheRunner -> IO a) -> IO a
withCacheBackend config settings skipVerify settingsVar use = case cacheBackend config of
  CacheBackendSQLite     -> withConnection (toString $ cachePath config) $ \conn -> do
    initializeDB conn
    refuseEmpty =<< isEmpty conn
    use CacheRunner { runCacheWith = runCacheSQLite conn }
  CacheBackendR2         -> case r2Config config of
    Nothing -> die "cacheBackend=r2 but r2Config is missing"
    Just r2 -> mkR2Connection r2 >>= \case
      Left err   -> die $ "Failed to initialize R2: " <> toString err
      Right conn -> use CacheRunner { runCacheWith = runCacheR2 conn }
  CacheBackendFilesystem -> do
    let root = toString (cachePath config)
        -- what the disk must keep free (CacheHandler.hasFreeDiskSpace)
        reserve = max (100 * mib) . diskRemainingBytes <$> readTVarIO settingsVar
    fs <- openFsCache root reserve (rescanCache settings) (verifyCache settings && not skipVerify)
    flip finally (saveState fs) $ do
      refuseEmpty . (== 0) =<< cachedFiles fs
      used <- cachedBytes fs
      available <- availableBytes root
      whenJust available $ \free -> when (free < diskLimitBytes settings - used) $ die
        [i|The storage device does not have enough space available for the set maximum cache size: #{diskLimitBytes settings `div` gib} GiB is set, and #{(free + used) `div` gib} GiB is available for the cache. Free up space for H@H, or reduce the cache size on #{settingsPage}|]
      runEvictor fs (diskLimitBytes <$> readTVarIO settingsVar)
        `alongside` use CacheRunner { runCacheWith = runCacheFilesystem fs }
  where
    refuseEmpty noFiles = when (noFiles && staticRangeCount settings > 20) $ die
      [i|This client has static ranges assigned to it, but the cache is empty. Check file permissions and file system integrity. If the cache has been deleted or is otherwise lost, reset your static ranges on #{settingsPage}|]

    settingsPage = [i|https://e-hentai.org/hentaiathome.php?cid=#{clientId config}|] :: String

    mib = 1048576 :: Int64

    gib = 1024 * mib

-- | Bind the port, announce ourselves with client_start, then serve and
-- handle commands until one ends the client (official HentaiAtHomeClient.run):
--
-- * 'Cert' fetches a new certificate and swaps it in for the next handshakes;
--   the listener and open transfers carry on.
-- * 'GracefulShutdown' ends the client as 'shutDown' says.
serverLoop :: ServerEnv -> ( CertificateChain, PrivKey ) -> IO ExitCode
serverLoop env certs = do
  let config = envConfig env
  credentials <- newIORef certs
  port <- clientPort <$> readTVarIO (envSettings env)
  listening <- newEmptyMVar
  closeListener <- newEmptyMVar
  ( admit, release ) <- connectionLimit (envSettings env) (notifyOverload config)
  let warpSettings
        = setBeforeMainLoop (putMVar listening ())
        $ setInstallShutdownHandler (putMVar closeListener)
        $ setOnOpen admit
        $ setOnClose release
        $ setGracefulShutdownTimeout (Just 25)
        $ setOnException quietHandshakes
        $ setPort port defaultSettings
  withAsync (runTLS (tlsSettings credentials) warpSettings $ logStdout (makeApplication env))
    $ \web -> do
      -- the server tests our port before it answers client_start
      race (waitCatch web) (takeMVar listening) >>= \case
        Left outcome -> die [i|Could not listen on port #{port}: #{outcome}|]
        Right ()     -> pass
      started <- announce config port
      let untilShutdown
            | started = housekeeping env credentials `alongside` commands env credentials
            | otherwise = commands env credentials
      race (waitCatch web) untilShutdown >>= \case
        Left outcome -> die [i|The web server stopped: #{outcome}|]
        Right exit
          | started -> exit <$ shutDown config (join $ readMVar closeListener) (void $ waitCatch web)
          | otherwise -> pure exit
  where
    tlsSettings credentials
      -- asked on every handshake (SNI or not), so a swapped certificate applies at once
      = (tlsSettingsSni $ \_ -> Credentials . pure <$> readIORef credentials)
        { onInsecure = AllowInsecure
        , tlsCiphers = ciphersuite_all
            ++ [ cipher_ECDHE_ECDSA_AES128CBC_SHA
               , cipher_ECDHE_RSA_AES128CBC_SHA
               , cipher_ECDHE_RSA_AES256CBC_SHA
               ]
          -- tls >= 2 speaks nothing older; it rejects SSL3/TLS1.0/TLS1.1 hellos before looking at this
        , tlsAllowedVersions = [ TLS12, TLS13 ]
          -- resumed handshakes skip the certificate and its RSA signature (several ms on a small VM)
        , tlsSessionManagerConfig = Just SM.defaultConfig
        }

-- | Warp exception handler suppressing pre-app TLS handshake noise and empty disconnects.
quietHandshakes :: Maybe Request -> SomeException -> IO ()
quietHandshakes req e
  | Just (HandshakeFailed _) <- fromException e = pass
  | Just ClientClosedConnectionPrematurely <- fromException e = pass
  | otherwise = defaultOnException req e

notifyOverload :: ClientConfig -> IO ()
notifyOverload config = runRPCIO config overload >>= \case
  Left failure -> putStrLn [i|Overload notification failed: #{failure}|]
  Right ()     -> pass

-- | Send client_start, once. True if the server took us; if it refuses us for
-- good, exit.
announce :: ClientConfig -> Int -> IO Bool
announce config port = runRPCIO config clientStart >>= \case
  Right ()     -> True <$ putStrLn "Start notification successful."
  Left failure -> case startFailure port failure of
    Refused why     -> die (toString why)
    Unannounced why -> False <$ putStrLn (toString why)

commands :: ServerEnv -> IORef ( CertificateChain, PrivKey ) -> IO ExitCode
commands env credentials = atomically (readTChan (envActions env)) >>= \case
  GracefulShutdown -> pure ExitSuccess
  SessionLost -> pure (ExitFailure 1)
  Cert -> do
    renewed <- refreshCerts (envConfig env)
    now <- serverNow
    case renewed of
      Just new@( chain, _ )
        | not (needsRenewal now chain) -> writeIORef credentials new >> commands env credentials
        -- the official client refuses such a certificate too
        | otherwise -> ExitSuccess
          <$ putStrLn "The new certificate expires within a day as well; shutting down"
      Nothing -> ExitSuccess <$ putStrLn "Could not get a new certificate; shutting down"

-- | Post-start periodic tasks: initial settings + 3d blacklist, then
-- still_alive (110s), 12h blacklist (6h), cert expiry check (5m), and letting
-- a failed RPC server be picked again (4h).
housekeeping :: ServerEnv -> IORef ( CertificateChain, PrivKey ) -> IO ()
housekeeping env credentials = do
  logFailure refreshSettings
  logFailure $ applyBlacklist config (envCache env) 259200
  mapConcurrently_
    (\( delay, task ) -> infinitely $ threadDelay delay >> logFailure task)
    [ ( seconds 110, heartbeat )
    , ( hours 6, applyBlacklist config (envCache env) 43200 )
    , ( minutes 5, checkCertificate )
    , ( hours 4, forgetRPCServerFailure )
    ]
  where
    config = envConfig env

    refreshSettings = readTVarIO (envSettings env) >>= runRPCIO config . clientSettings >>= \case
      Right settings -> atomically $ writeTVar (envSettings env) settings
      Left failure   -> putStrLn [i|Failed to refresh settings: #{failure}|]

    heartbeat = runRPCIO config stillAlive >>= \case
      Right ()     -> pass
      Left failure -> case toldToStop failure of
        Just BadNetwork -> do
          putStrLn
            "The client is shutting down since the network is misconfigured; correct firewall/forwarding settings then restart the client."
          send GracefulShutdown
        Just NotLoggedIn -> do
          putStrLn "The server no longer knows this client's session; shutting down to log in again."
          send SessionLost
        Nothing -> putStrLn [i|Failed stillAlive test: #{failure}; will retry later|]

    -- the official client gives up a day before the certificate expires; we fetch another first
    checkCertificate = do
      now <- serverNow
      ( chain, _ ) <- readIORef credentials
      when (needsRenewal now chain) $ do
        putStrLn "The certificate expires within a day; fetching a new one"
        send Cert

    send = atomically . writeTChan (envActions env)

    -- an exception one task did not expect must not stop the heartbeat
    logFailure task = tryAny task >>= \case
      Left e   -> putStrLn [i|Housekeeping task failed: #{e}|]
      Right () -> pass

    seconds n = n * 1000000

    minutes n = seconds (n * 60)

    hours n = minutes (n * 60)

-- | Delete the files blacklisted in the last so many seconds (official
-- CacheHandler.processBlacklist). The list can be long: only its length is logged.
applyBlacklist :: ClientConfig -> CacheRunner -> Int -> IO ()
applyBlacklist config cache seconds = do
  outcome <- runRPCIO config $ do
    files <- getBlacklist seconds
    -- one file that cannot be deleted must not keep the others
    runCacheWith cache $ for_ files $ \uri -> deleteFile uri `catch` \(failure :: HathError)
      -> logWarning [i|Could not delete blacklisted file #{uri}: #{failure}|]
    pure (length files)
  putStrLn $ case outcome of
    Left failure -> [i|Failed to retrieve the file blacklist, will try again later: #{failure}|]
    Right count  -> [i|Blacklist: #{count} files, deleted from the cache where present|]

-- | Graceful shutdown (HentaiAtHomeClient.shutdown / httpServerShutdown):
-- send client_stop, keep serving 5s, stop listening, drain up to 25s.
shutDown :: ClientConfig -> IO () -> IO () -> IO ()
shutDown config closeListener drained = do
  runRPCIO config clientStop >>= \case
    Left failure -> putStrLn [i|Shutdown notification failed: #{failure}|]
    Right ()     -> pass
  putStrLn "Shutdown in progress - please wait up to 30 seconds"
  threadDelay 5000000
  closeListener
  drained

-- | Whether a certificate must be replaced at this Unix time: with less than a
-- day left, the official client will not serve it (HTTPServer.isCertExpired).
needsRenewal :: Int64 -> CertificateChain -> Bool
needsRenewal now = \case
  CertificateChain (leaf : _) -> notAfter leaf < now + 86400
  CertificateChain []         -> True
  where
    notAfter leaf = case timeGetElapsed (snd $ certValidity $ X509.getCertificate leaf) of
      Elapsed (Seconds unixTime) -> unixTime

-- | Fetch a new certificate, retrying three times with exponential backoff
-- (1, 2, 4 seconds). Nothing if none comes.
refreshCerts :: ClientConfig -> IO (Maybe ( CertificateChain, PrivKey ))
refreshCerts config = go (0 :: Int)
  where
    go retries = runRPCIO config getCertificate >>= \case
      Right newCerts -> pure (Just newCerts)
      Left e
        | retries >= 3 -> do
          putStrLn $ "Failed to refresh certs: " <> show e
          putStrLn "Giving up after 3 retries"
          pure Nothing
        | otherwise -> do
          let delaySeconds = 2 ^ retries :: Int
          putStrLn $ "Failed to refresh certs: " <> show e
          putStrLn
            $ "Retrying in "
            <> show delaySeconds
            <> " seconds (attempt "
            <> show (retries + 1)
            <> "/3)"
          threadDelay (1000000 * delaySeconds)
          go (retries + 1)