59 lines
1.6 KiB
Plaintext
59 lines
1.6 KiB
Plaintext
module Main where
|
|
|
|
import Prelude
|
|
|
|
import Effect (Effect)
|
|
import Effect.Aff (Aff, launchAff)
|
|
import Effect.Class (liftEffect)
|
|
import Effect.Console (log)
|
|
import Fetch (fetch)
|
|
import Data.Binary.Base64 (decode)
|
|
import Data.Either (Either, hush, note)
|
|
import Data.Maybe (Maybe(..))
|
|
import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
|
|
import Data.String (drop, indexOf', indexOf, splitAt)
|
|
import Data.String.Pattern (Pattern(..))
|
|
|
|
import Crypto.Subtle.Constants.AES (aesGCM)
|
|
import Crypto.Subtle.Key.Import (aes)
|
|
import Crypto.Subtle.Key.Import (importKey) as SC
|
|
import Crypto.Subtle.Key.Types (CryptoKey, decrypt, raw)
|
|
|
|
data AviaryError
|
|
= MalformedKey
|
|
| MalformedUrl
|
|
|
|
instance showAviaryError :: Show AviaryError where
|
|
show MalformedKey = "Malformed key!!"
|
|
show MalformedUrl = "Malformed url!!"
|
|
|
|
type UrlInfo =
|
|
{ server :: String
|
|
, fileId :: String
|
|
, key :: String
|
|
}
|
|
|
|
parseUrl :: String -> Either AviaryError UrlInfo
|
|
parseUrl url = note MalformedUrl do
|
|
slashIndex <- indexOf' (Pattern "/") 8 url
|
|
let { after, before: server } = splitAt (1 + slashIndex) url
|
|
poundIndex <- indexOf (Pattern "#") after
|
|
let { after: key, before: fileId } = splitAt poundIndex after
|
|
Just { server, fileId, key: drop 1 key }
|
|
|
|
decodeKey :: String -> Either AviaryError Uint8Array
|
|
decodeKey key = note MalformedKey $ hush $ decode key
|
|
|
|
importKey :: ArrayBuffer -> Aff CryptoKey
|
|
importKey key =
|
|
SC.importKey raw key (aes aesGCM) false [decrypt]
|
|
|
|
main_aff :: Aff Unit
|
|
main_aff = do
|
|
{ status, text } <- fetch "https://envs.sh/Q_V.txt" {}
|
|
responseText <- text
|
|
liftEffect $ log responseText
|
|
|
|
main :: Effect Unit
|
|
main = launchAff main_aff $> unit
|