1 {-# LANGUAGE OverloadedStrings #-}
5 --import Control.Applicative
7 import qualified Data.ByteString.Char8 as C (pack, unpack)
8 import qualified Data.ByteString.Lazy.Char8 as L (pack, unpack)
9 import Data.ByteString.Lazy as BS hiding (reverse, putStrLn)
10 import Data.ConfigFile
11 import Data.Either.Utils
12 import Data.List.Split
13 import Data.Aeson (encode, decode, Value(..))
14 import Network.HTTP.Types
16 import Network.HTTP.Conduit
17 import Data.JsonRpcClient
21 confFile = "/etc/namecoin.conf"
23 -- Config file handling
25 data Config = Config { rpcuser :: String
26 , rpcpassword :: String
31 readConfig :: String -> IO Config
33 cp <- return . forceEither =<< readfile emptyCP f
34 return (Config { rpcuser = getSetting cp "rpcuser" ""
35 , rpcpassword = getSetting cp "rpcpassword" ""
36 , rpchost = getSetting cp "rpchost" "localhost"
37 , rpcport = getSetting cp "rpcport" 8336
40 getSetting cp x dfl = case get cp "DEFAULT" x of
44 -- HTTP/JsonRpc interface
46 qReq :: Config -> ByteString -> ByteString -> Request m
47 qReq cf q id = applyBasicAuth (C.pack (rpcuser cf)) (C.pack (rpcpassword cf))
48 $ def { host = (C.pack (rpchost cf))
51 , requestHeaders = [ (hAccept, "application/json")
52 , (hContentType, "application/json")
53 , (hConnection, "Keep-Alive")
55 , requestBody = RequestBodyLBS $ encode $
56 JsonRpcRequest JsonRpcV1
60 , checkStatus = \_ _ _ -> Nothing
63 qRsp :: Response ByteString -> Either String NmcDom
65 case parseJsonRpc (responseBody rsp) :: Either JsonRpcError NmcRes of
66 Left jerr -> Left $ "Unparseable response: " ++ (show (responseBody rsp))
68 case decode (resValue jrsp) :: Maybe NmcDom of
69 Nothing -> Left $ "Unparseable value: " ++ (show (resValue jrsp))
74 queryNmc :: Manager -> Config -> String -> RRType -> String
75 -> IO (Either String NmcDom)
76 queryNmc mgr cfg fqdn qtype qid = do
77 case reverse (splitOn "." fqdn) of
80 httpLbs (qReq cfg (L.pack ("d/" ++ dn)) (L.pack qid)) mgr
83 return $ Left "Only \".bit\" domain is supported"
89 cfg <- readConfig confFile
93 loopErr e = forever $ do
94 putStrLn $ "FAIL\t" ++ e
99 ["HELO", "1"] -> return 1
100 ["HELO", "2"] -> return 2
101 ["HELO", "3"] -> return 3
102 ["HELO", x ] -> loopErr $ "unsupported ABI version " ++ (show x)
103 _ -> loopErr $ "bad HELO " ++ (show s)
105 putStrLn $ "OK\tDnsNmc ready to serve, protocol v." ++ (show ver)
107 mgr <- newManager def
109 print $ qReq cfg "d/nosuchdomain" "query-nmc"
110 rsp <- runResourceT $ httpLbs (qReq cfg "d/nosuchdomain" "query-nmc") mgr
111 print $ (statusCode . responseStatus) rsp
112 putStrLn "===== complete response is:"
114 let rbody = responseBody rsp
115 putStrLn "===== response body is:"
117 let result = parseJsonRpc rbody :: Either JsonRpcError NmcRes
118 putStrLn "===== parsed response is:"
120 -- print $ parseJsonRpc (responseBody rsp)
122 --forever $ getLine >>= (pdnsOut uri) . (pdnsParse ver)