packages feed

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

-- | The settings the RPC server assigns to this client.
module Hath.Settings
  ( HathSettings(..)
  , ImageProxy(..)
  , imageProxy
  , defaultHathSettings
  , parseSettings
  , settingNumber
  , maxConnections
  ) where

import qualified Data.ByteString.Char8 as BS
import           Data.Char             ( toLower )

import           Relude

data HathSettings
  = HathSettings
  { clientHost :: !ByteString
  , clientPort :: !Int
  , throttleBytes :: !Int64
    -- ^ Bandwidth limit. Nothing enforces it; it sets 'maxConnections'.
  , diskLimitBytes :: !Int64
    -- ^ Cache size set on the H\@H settings page; the Filesystem backend evicts to stay under it.
  , diskRemainingBytes :: !Int64
  , rpcServerIPs :: ![ ByteString ]
    -- ^ As sent by the server, e.g. @::ffff:192.0.2.10@. These are exempt from flood control.
  , rpcServerPort :: !Int
  , maxConnectionsOverride :: !Int
    -- ^ @max_connections@: when above 0, 'maxConnections' is this.
  , maxAllowedFileSize :: !Int
    -- ^ @max_allowed_filesize@: a cache miss or download larger than this is not fetched.
  , maxFilenameLength :: !Int
    -- ^ @max_filename_length@: how long the name of a gallery's directory may get.
  , staticRangeCount :: !Int
  , verifyCache :: !Bool
  , rescanCache :: !Bool
  , imageProxyType :: !ByteString
  , imageProxyHost :: !ByteString
  , imageProxyPort :: !Int
  }
  deriving ( Show )

defaultHathSettings :: HathSettings
defaultHathSettings
  = HathSettings
  { clientHost = ""
  , clientPort = 0
  , throttleBytes = 0
  , diskLimitBytes = 0
  , diskRemainingBytes = 0
  , rpcServerIPs = []
  , rpcServerPort = 80
  , maxConnectionsOverride = 0
    -- the official client's defaults (Settings.java:58)
  , maxAllowedFileSize = 1073741824
  , maxFilenameLength = 125
  , staticRangeCount = 0
  , verifyCache = False
  , rescanCache = False
  , imageProxyType = ""
  , imageProxyHost = ""
  , imageProxyPort = 0
  }

-- | Apply @name=value@ lines onto the settings we have. @client_settings@ only
-- carries what changed, so a refresh must leave the other fields alone. Lines
-- we do not know, or whose number does not parse, are skipped.
parseSettings :: HathSettings -> [ ByteString ] -> HathSettings
parseSettings = foldl' apply
  where
    apply settings line = case ( name, readMaybe @Int64 (BS.unpack value) ) of
      ( "host", _ ) -> settings { clientHost = value }
      ( "port", Just n ) -> settings { clientPort = fromIntegral n }
      ( "throttle_bytes", Just n ) -> settings { throttleBytes = n }
      ( "disklimit_bytes", Just n ) -> settings { diskLimitBytes = n }
      ( "diskremaining_bytes", Just n ) -> settings { diskRemainingBytes = n }
      ( "rpc_server_ip", _ ) -> settings { rpcServerIPs = BS.split ';' value }
      ( "rpc_server_port", Just n ) -> settings { rpcServerPort = fromIntegral n }
      ( "max_connections", Just n ) -> settings { maxConnectionsOverride = fromIntegral n }
      ( "max_allowed_filesize", Just n ) -> settings { maxAllowedFileSize = fromIntegral n }
      ( "max_filename_length", Just n ) -> settings { maxFilenameLength = fromIntegral n }
      -- sent at login only (Settings.java:318-332)
      ( "static_ranges", _ )
        -> settings { staticRangeCount = length (filter ((== 4) . BS.length) (BS.split ';' value)) }
      ( "static_range_count", Just n ) -> settings { staticRangeCount = fromIntegral n }
      ( "verify_cache", _ ) -> settings { verifyCache = value == "true", rescanCache = value == "true" }
      ( "rescan_cache", _ ) -> settings { rescanCache = value == "true" }
      ( "image_proxy_type", _ ) -> settings { imageProxyType = BS.map toLower value }
      ( "image_proxy_host", _ ) -> settings { imageProxyHost = BS.map toLower value }
      ( "image_proxy_port", Just n ) -> settings { imageProxyPort = fromIntegral n }
      _ -> settings
      where
        ( name, value ) = nameValue line

-- | Image proxy from the H\@H settings page, used for cache misses, proxy tests,
-- and retried gallery downloads (Settings.getImageProxy; defaults: SOCKS 1080, HTTP 8080).
data ImageProxy = SOCKSProxy !String !Int | HTTPProxy !ByteString !Int
  deriving ( Eq, Show )

imageProxy :: HathSettings -> Maybe ImageProxy
imageProxy settings
  | BS.null host = Nothing
  | imageProxyType settings == "http" = Just $ HTTPProxy host (port 8080)
  | otherwise = Just $ SOCKSProxy (BS.unpack host) (port 1080)
  where
    host = imageProxyHost settings

    port standard
      | imageProxyPort settings > 0 = imageProxyPort settings
      | otherwise = standard

-- | How many connections we take at once (Settings.getMaxConnections).
maxConnections :: HathSettings -> Int
maxConnections settings
  | maxConnectionsOverride settings > 0 = maxConnectionsOverride settings
  | otherwise = 20 + fromIntegral (min 480 (throttleBytes settings `quot` 10000))

-- | The number one of a reply's @name=value@ lines gives, such as @server_time@.
settingNumber :: ByteString -> [ ByteString ] -> Maybe Int64
settingNumber wanted reply
  = listToMaybe
    [ n
    | ( name, value ) <- map nameValue reply
    , name == wanted
    , Just n <- [ readMaybe (BS.unpack value) ]
    ]

nameValue :: ByteString -> ( ByteString, ByteString )
nameValue = second (BS.drop 1) . BS.span (/= '=')