X-Git-Url: http://www.average.org/gitweb/?p=pdns-pipe-nmc.git;a=blobdiff_plain;f=NmcDom.hs;h=11b77ac4a01905140f19161f0451d9f6501f7ecc;hp=c94e8af8bdd50970f9428ce5fd8a2dc8da4bed95;hb=355038bc44ff6a9dbbc5a09739fba3fe4b073b32;hpb=2ad29ac97637f25aea0a7ac0ee31afa72221a938 diff --git a/NmcDom.hs b/NmcDom.hs index c94e8af..11b77ac 100644 --- a/NmcDom.hs +++ b/NmcDom.hs @@ -9,20 +9,36 @@ module NmcDom ( NmcDom(..) import Prelude hiding (length) import Data.ByteString.Lazy (ByteString) -import qualified Data.Text as T (unpack) +import Data.Text (Text, unpack) import Data.List as L (union) import Data.List.Split import Data.Char -import Data.Map as M (Map, lookup, delete, size, union) -import Data.Vector (toList,(!),length) -import Control.Applicative ((<$>), (<*>), empty) +import Data.Map as M (Map, lookup, delete, size, unionWith) +import Data.Vector (toList,(!),length, singleton) +import Control.Applicative ((<$>), (<*>), empty, pure) import Data.Aeson +import qualified Data.HashMap.Strict as H +import Data.Aeson.Types + +-- Variant of Aeson's `.:?` that interprets a String as a +-- single-element list, so it is possible to have either +-- "ip":["1.2.3.4"] +-- or +-- "ip":"1.2.3.4" +-- with the same result. +(.:/) :: (FromJSON a) => Object -> Text -> Parser (Maybe a) +obj .:/ key = case H.lookup key obj of + Nothing -> pure Nothing + Just v -> case v of + String s -> parseJSON $ Array (singleton v) + _ -> parseJSON v + class Mergeable a where merge :: a -> a -> a -- bias towads second arg -instance Ord k => Mergeable (Map k a) where - merge mx my = M.union my mx +instance (Ord k, Mergeable a) => Mergeable (Map k a) where + merge mx my = M.unionWith merge my mx -- instance Mergeable String where -- merge _ b = b @@ -79,6 +95,44 @@ instance FromJSON NmcRRI2p where instance Mergeable NmcRRI2p where merge _ b = b +data NmcRRTls = NmcRRTls + { tlsMatchType :: Int -- 0:exact 1:sha256 2:sha512 + , tlsMatchValue :: String + , tlsIncSubdoms :: Int -- 1:enforce on subdoms 0:no + } deriving (Show, Eq) + +instance FromJSON NmcRRTls where + parseJSON (Array a) = + if length a == 3 then NmcRRTls + <$> parseJSON (a ! 0) + <*> parseJSON (a ! 1) + <*> parseJSON (a ! 2) + else empty + parseJSON _ = empty + +instance Mergeable NmcRRTls where + merge _ b = b + +data NmcRRDs = NmcRRDs + { dsKeyTag :: Int + , dsAlgo :: Int + , dsHashType :: Int + , dsHashValue :: String + } deriving (Show, Eq) + +instance FromJSON NmcRRDs where + parseJSON (Array a) = + if length a == 4 then NmcRRDs + <$> parseJSON (a ! 0) + <*> parseJSON (a ! 1) + <*> parseJSON (a ! 2) + <*> parseJSON (a ! 3) + else empty + parseJSON _ = empty + +instance Mergeable NmcRRDs where + merge _ b = b + data NmcDom = NmcDom { domService :: Maybe [NmcRRService] , domIp :: Maybe [String] , domIp6 :: Maybe [String] @@ -91,13 +145,13 @@ data NmcDom = NmcDom { domService :: Maybe [NmcRRService] , domLoc :: Maybe String , domInfo :: Maybe Value , domNs :: Maybe [String] - , domDelegate :: Maybe [String] + , domDelegate :: Maybe String , domImport :: Maybe String , domMap :: Maybe (Map String NmcDom) , domFingerprint :: Maybe [String] , domTls :: Maybe (Map String - (Map String [[String]])) - , domDs :: Maybe [[String]] + (Map String [NmcRRTls])) + , domDs :: Maybe [NmcRRDs] , domMx :: Maybe [String] -- Synthetic } deriving (Show, Eq) @@ -110,15 +164,15 @@ instance FromJSON NmcDom where then emptyNmcDom { domIp = Just [s'] } else emptyNmcDom where - s' = T.unpack s + s' = unpack s isIPv4 x = all isNibble $ splitOn "." x isNibble x = if all isDigit x then (read x :: Int) < 256 else False parseJSON (Object o) = NmcDom <$> o .:? "service" - <*> o .:? "ip" - <*> o .:? "ip6" + <*> o .:/ "ip" + <*> o .:/ "ip6" <*> o .:? "tor" <*> o .:? "i2p" <*> o .:? "freenet" @@ -127,11 +181,11 @@ instance FromJSON NmcDom where <*> o .:? "email" <*> o .:? "loc" <*> o .:? "info" - <*> o .:? "ns" + <*> o .:/ "ns" <*> o .:? "delegate" <*> o .:? "import" <*> o .:? "map" - <*> o .:? "fingerprint" + <*> o .:/ "fingerprint" <*> o .:? "tls" <*> o .:? "ds" <*> return Nothing -- domMx not parsed