hs-hath-1.4.0: src/Hath/Server/Handlers.hs
module Hath.Server.Handlers ( application ) where
import Control.Concurrent ( forkIO, threadDelay )
import Data.ByteString.Builder ( byteString )
import qualified Data.ByteString.Char8 as BS
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import Data.String.Interpolate ( i )
import Hath.Cache ( CacheHit(..) )
import Hath.Config ( ClientConfig )
import Hath.Download ( fetchFile )
import Hath.Error ( HathError )
import Hath.FileURI ( FileURI(fileSize), mimeType, parseFileURI )
import Hath.Locate ( Located(..), locateResource )
import Hath.Log ( logInfo, logWarning )
import Hath.Protocol ( commandTimeValid
, fetchParams
, keystampValid
, parseURLParams
, responseHeaders
, sameSecret
, serverCommandKey
, speedTestKey
)
import Hath.RPC ( clientSettings )
import Hath.Server.API ( ProxyTest(..)
, Route(..)
, ServerCommand(..)
, parseServerCommand
, route
)
import Hath.Server.App ( App, ServerAction(..), ServerEnv(..), runApp )
import Hath.Server.FloodControl ( isLocalIP, isRPCServer, sockAddrIP )
import Hath.Server.Range ( fileETag, notModified, requestedPart )
import Hath.ServerTime ( serverNow )
import Hath.Settings ( HathSettings(maxAllowedFileSize, rpcServerIPs), imageProxy )
import Hath.Stats ( addDownload, addUpload, incServed, readMetrics )
import qualified Network.HTTP.Client as HC
import Network.HTTP.Types ( Header
, Status
, hAcceptRanges
, hAuthorization
, hCacheControl
, hContentLength
, hContentRange
, hContentType
, hETag
, hLocation
, methodGet
, methodHead
, status200
, status206
, status301
, status302
, status304
, status400
, status403
, status404
, statusCode
)
import Network.Socket ( SockAddr )
import qualified Network.Wai as Wai
import Polysemy ( embed )
import Polysemy.Error ( catch, throw )
import Polysemy.Reader ( ask )
import Relude hiding ( ask )
import UnliftIO ( getMonotonicTime
, replicateConcurrently
, timeout
, writeTChan
)
-- | The client's HTTP interface. Like the official client, it reads GET and
-- HEAD requests only (HTTPSession.java:107), and answers a HEAD as the GET,
-- without the body: Warp leaves that out.
application :: ServerEnv -> Wai.Application
application env req respond
| Wai.requestMethod req `notElem` [ methodGet, methodHead ] = respond (errorPage status400)
| otherwise = runApp env (handle env req) >>= respond . either errorPage id
handle :: ServerEnv -> Wai.Request -> App Wai.Response
handle env req = case route (Wai.rawPathInfo req) of
Favicon -> pure $ redirect status301 "https://e-hentai.org/favicon.ico" []
Robots -> pure $ bytes status200 "text/plain; charset=ISO-8859-1" "User-agent: *\nDisallow: /"
File fileId options -> resource env req fileId options
ServerCmd command additional time commandKey
-> serverCommand env (Wai.remoteHost req) command additional time commandKey
Test size time testKey -> speedTest size time testKey
Metrics -> tokenHolder >> ours <$> readMetrics
AdminSettings -> tokenHolder >> ours . show <$> embed @IO (readTVarIO (envSettings env))
Refuse status -> throw status
where
-- Private endpoints; hidden (404) unless HATH_METRICS_TOKEN is set.
tokenHolder = case envMetricsToken env of
Nothing -> throw status404
Just token -> unless (sameSecret sent ("Bearer " <> token)) $ throw status403
sent = fromMaybe "" (List.lookup hAuthorization (Wai.requestHeaders req))
-- Strip default 1-year Cache-Control.
ours
= Wai.mapResponseHeaders (filter ((/= hCacheControl) . fst))
. bytes status200 "text/plain;charset=utf-8"
. encodeUtf8
-- | @\/h\/\<file id>\/\<options>@: a file for a reader (HTTPResponse.java:161-228).
-- The options carry the keystamp the H\@H server issued for this file, which
-- entitles the reader to it, and the @fileindex@ and @xres@ to fetch it by.
resource :: ServerEnv -> Wai.Request -> ByteString -> ByteString -> App Wai.Response
resource env req fileId optionsText = do
now <- embed serverNow
cfg <- ask @ClientConfig
unless (keystampValid cfg now fileId (fromMaybe "" (Map.lookup "keystamp" options)))
$ throw status403
( uri, fetchBy )
<- maybe (throw status404) pure ((,) <$> parseFileURI fileId <*> fetchParams options)
let size = fileSize uri
etag = fileETag uri
-- the browser may keep the file for good: no other file has its id
file = (<> [ ( hETag, etag ), ( hCacheControl, "public, max-age=31536000, immutable" ) ])
. filter ((/= hCacheControl) . fst)
part = requestedPart etag size (Wai.requestHeaders req)
( offset, len ) = fromMaybe ( 0, size ) part
status
| isJust part = status206
| otherwise = status200
headers = file (responseHeaders (mimeType uri) size)
served sent response
= response <$ unless (Wai.requestMethod req == methodHead) (incServed >> addUpload sent)
settings <- embed @IO (readTVarIO (envSettings env))
if notModified etag (Wai.requestHeaders req)
then pure $ Wai.responseLBS status304 (file (filter ((`notElem` [ hContentType, hContentLength ]) . fst) (responseHeaders "" 0))) ""
else locateResource settings fileId uri fetchBy >>= either throw pure >>= \case
Stored (Redirect url)
-> pure $ redirect status302 url [ ( "Cache-Control", "public, max-age=600" ) ]
-- Warp sets the length, and the Content-Range of a part
Stored (LocalFile path) -> served len
$ Wai.responseFile status (filter ((/= hContentLength) . fst) headers) path
$ Just
$ Wai.FilePart (toInteger offset) (toInteger len) (toInteger size)
Stored (InMemory content) -> served len
$ Wai.responseLBS status (ranged headers) (fromStrict (BS.take len (BS.drop offset content)))
where
ranged
= ([ ( hContentRange, [i|bytes #{offset}-#{offset + len - 1}/#{size}|] ) | isJust part ] <>)
. (( hAcceptRanges, "bytes" ) :)
. map (\h@( name, _ ) -> if name == hContentLength then ( name, show len ) else h)
-- a cache miss: stream the whole file as it arrives, like the official proxy
-- mode. Warp skips the body on HEAD, but the file is still fetched.
Streamed relay -> served size $ Wai.responseStream status200 headers $ \write flush -> relay
(\chunk -> write (byteString chunk) >> flush)
where
options = parseURLParams optionsText
-- | @\/servercmd\/\<command>\/\<additional>\/\<time>\/\<key>@ (HTTPResponse.java:230-260).
-- Requires an RPC server IP, or a local peer (tunnels/proxies) authenticated by key alone.
serverCommand :: ServerEnv -> SockAddr -> Text -> Text -> Int -> Text -> App Wai.Response
serverCommand env peer command additional time commandKey = do
settings <- embed @IO (readTVarIO (envSettings env))
now <- embed serverNow
cfg <- ask @ClientConfig
unless
(maybe False (\ip -> isLocalIP ip || isRPCServer (rpcServerIPs settings) ip) (sockAddrIP peer))
$ throw status403
unless
(commandTimeValid now (fromIntegral time)
&& sameSecret
(encodeUtf8 commandKey)
(encodeUtf8 (serverCommandKey cfg command additional time)))
$ throw status403
maybe
(pure $ text "INVALID_COMMAND")
(runCommand env)
(parseServerCommand command (parseURLParams (encodeUtf8 additional)))
runCommand :: ServerEnv -> ServerCommand -> App Wai.Response
runCommand env = \case
StillAlive -> pure $ text "I feel FANTASTIC and I'm still alive"
ThreadedProxyTest test -> text <$> runProxyTest env test
SpeedTest size -> do
addDownload size
pure $ filler size
RefreshSettings -> do
logInfo "Refreshing settings"
-- the official client answers the same whether the refresh worked
refresh `catch` \(e :: HathError) -> logWarning [i|Failed to refresh settings: #{e}|]
pure $ text ""
StartDownloader -> do
logInfo "Starting gallery downloader"
void $ embed @IO $ tryPutMVar (envDownloadRequest env) ()
pure $ text ""
RefreshCerts -> do
logInfo "Refreshing certificates"
void $ embed $ forkIO $ do
threadDelay (2 * 1000000)
atomically $ writeTChan (envActions env) Cert
pure $ text ""
where
refresh = do
refreshed <- clientSettings =<< embed @IO (readTVarIO (envSettings env))
embed @IO $ atomically $ writeTVar (envSettings env) refreshed
-- | @\/t\/\<size>\/\<time>\/\<key>@: a speed test, for the RPC server or for
-- another client it sent ('ThreadedProxyTest') (HTTPResponse.java:261-290).
speedTest :: Int -> Int64 -> ByteString -> App Wai.Response
speedTest size time testKey = do
now <- embed serverNow
cfg <- ask @ClientConfig
unless (commandTimeValid now time && sameSecret testKey (speedTestKey cfg size time))
$ throw status403
addUpload size
pure $ filler size
bytes :: Status -> ByteString -> ByteString -> Wai.Response
bytes status contentType content
= Wai.responseLBS status (responseHeaders contentType (BS.length content)) (fromStrict content)
-- | A reply of the official client's HTTPResponseProcessorText.
text :: ByteString -> Wai.Response
text = bytes status200 textType
-- | The page the official client answers an error with (HTTPResponse.getHTTPResponseProcessor).
errorPage :: Status -> Wai.Response
errorPage status = bytes status textType [i|An error has occurred. (#{statusCode status})|]
redirect :: Status -> ByteString -> [ Header ] -> Wai.Response
redirect status url headers
= Wai.responseLBS status (( hLocation, url ) : headers <> responseHeaders textType 0) ""
-- | What the official client's text replies say they are; its other replies
-- without a file type spell it in lower case ('filler').
textType :: ByteString
textType = "text/html; charset=ISO-8859-1"
-- | @size@ filler bytes, produced one TCP segment's worth at a time
-- (Settings.TCP_PACKET_SIZE), as HTTPResponseProcessorSpeedtest sends them.
filler :: Int -> Wai.Response
filler size
= Wai.responseStream status200 (responseHeaders "text/html; charset=iso-8859-1" (max 0 size))
$ \write _ -> let
go n = when (n > 0) $ write (byteString (BS.take n segment)) >> go (n - BS.length segment)
in
go size
where
segment = BS.replicate 1460 '6'
-- | Run the downloads concurrently and report, in the official format,
-- @OK:\<successful tests>-\<total milliseconds of the successful tests>@.
runProxyTest :: ServerEnv -> ProxyTest -> App ByteString
runProxyTest env test = case HC.parseRequest (proxyTestURL test) of
Nothing -> pure "INVALID_COMMAND"
Just request -> do
settings <- embed @IO (readTVarIO (envSettings env))
results <- embed
$ replicateConcurrently
(proxyTestCount test)
(timedDownload settings (proxyTestSize test) request)
let succeeded = catMaybes results
addUpload (proxyTestCount test * fromIntegral (proxyTestSize test))
pure [i|OK:#{length succeeded}-#{sum succeeded}|]
-- | A cap on one test download, its attempts included. The official client
-- hands FileDownloader 60 s (HTTPResponse.java:105), which it never enforces;
-- without a cap a target that trickles could hold the RPC server's request for minutes.
testTimeoutMicros :: Int
testTimeoutMicros = 60 * 1000000
-- | The milliseconds a test download took, from its first byte to its end, as
-- FileDownloader times it (FileDownloader.java:130-315): the first byte is the
-- first any of its attempts received. It counts if a body of the announced
-- length arrived, at least the test size (HTTPResponse.java:112).
timedDownload :: HathSettings -> Int64 -> HC.Request -> IO (Maybe Int64)
timedDownload settings testSize request = do
firstByte <- newIORef Nothing
fetched <- timeout testTimeoutMicros
$ fetchFile (maxAllowedFileSize settings) (imageProxy settings) request
$ \_ _
-> whenNothingM_ (readIORef firstByte) (writeIORef firstByte . Just =<< getMonotonicTime)
end <- getMonotonicTime
start <- readIORef firstByte
pure $ case fetched of
Just (Right size)
| fromIntegral size >= testSize -> Just $ maybe 0 (\t -> truncate ((end - t) * 1000)) start
_ -> Nothing