2 {-# LANGUAGE OverloadedStrings #-}
6 import Prelude hiding (lookup, readFile)
7 import System.Environment
8 import System.Console.GetOpt
9 import System.IO hiding (readFile)
10 import System.IO.Error
11 import Data.Time.Clock.POSIX
12 import Control.Exception
13 import Text.Show.Pretty hiding (String)
15 import Control.Monad.State
16 import Data.ByteString.Lazy hiding (reverse, putStr, putStrLn, head, empty)
17 import qualified Data.ByteString.Char8 as C (pack)
18 import qualified Data.ByteString.Lazy.Char8 as L (pack)
19 import qualified Data.Text as T (pack)
20 import Data.List.Split
21 import Data.Map.Lazy (Map, empty, lookup, insert, delete, size)
22 import Data.Aeson (encode, decode, Value(..))
23 import Network.HTTP.Types
24 import Network.HTTP.Client
25 #if MIN_VERSION_http_client(0,3,0)
26 import Data.Default.Class (def)
28 import Data.Default (def)
38 confFile = "/etc/namecoin.conf"
40 -- HTTP/JsonRpc interface
42 qReq :: Config -> String -> Int -> Request
43 qReq cf q id = applyBasicAuth (C.pack (rpcuser cf)) (C.pack (rpcpassword cf))
44 $ def { host = (C.pack (rpchost cf))
47 , requestHeaders = [ (hAccept, "application/json")
48 , (hContentType, "application/json")
49 , (hConnection, "Keep-Alive")
51 , requestBody = RequestBodyLBS $ encode $
52 JsonRpcRequest JsonRpcV1
55 (String (T.pack (show id)))
56 , checkStatus = \_ _ _ -> Nothing
59 qRsp :: Response ByteString -> Either String ByteString
61 case parseJsonRpc (responseBody rsp) :: Either JsonRpcError NmcRes of
63 case (jrpcErrCode jerr) of
64 (-4) -> Right "{}" -- this is how non-existent entry is returned
65 _ -> Left $ "JsonRpc error response: " ++ (show jerr)
66 Right jrsp -> Right $ resValue jrsp
70 queryOpNmc cfg mgr qid key =
71 httpLbs (qReq cfg key qid) mgr >>= return . qRsp
73 queryOpFile key = catch (readFile key >>= return . Right)
74 (\e -> return (Left (show (e :: IOException))))
76 queryDom queryOp fqdn =
77 case reverse (splitOn "." fqdn) of
78 "bit":dn:xs -> descendNmcDom queryOp xs $ seedNmcDom dn
79 _ -> return $ Left "Only \".bit\" domain is supported"
81 -- Number of ten minute intervals elapsed since creation of Namecoin
82 -- on April 18, 2011. Another option would be to use blockcount
83 -- but that would require another lookup, and we are cheap.
84 -- Yet another - to use (const - expires_in) from the lookup.
85 nmcAge = fmap (\x -> floor ((x - 1303070400) / 600)) getPOSIXTime
87 -- run a PowerDNS coprocess. Negotiate ABI version and execute requests.
91 cfg <- readConfig confFile
93 hSetBuffering stdin LineBuffering
94 hSetBuffering stdout LineBuffering
98 loopErr e = forever $ do
99 putStrLn $ "FAIL\t" ++ e
104 ["HELO", "1"] -> return 1
105 ["HELO", "2"] -> return 2
106 ["HELO", "3"] -> return 3
107 ["HELO", x ] -> loopErr $ "unsupported ABI version " ++ (show x)
108 _ -> loopErr $ "bad HELO " ++ (show s)
110 putStrLn $ "OK\tDnsNmc ready to serve, protocol v." ++ (show ver)
112 mgr <- newManager defaultManagerSettings
116 -- Save the name under current count, increment count for the next run
117 -- so the name is saved under the count that was put into the response.
118 stow name (count, cache) =
119 (if count >= 99 then 0 else count + 1
121 $ delete (if count >= 10 then count - 10 else count + 90) cache
125 mainloop = forever $ do
128 (count, cache) <- get
129 case pdnsParse ver l of
130 Left e -> io $ putStr $ pdnsReport e
133 PdnsRequestQ qname qtype id _ _ _ -> do
134 io $ queryDom (queryOpNmc cfg mgr id) qname
135 >>= putStr . (pdnsOutQ ver count gen qname qtype)
138 io $ putStrLn $ "LOG\tRequest number " ++ (show count)
139 ++ " id: " ++ (show id)
140 ++ " qname: " ++ qname
141 ++ " qtype: " ++ (show qtype)
142 ++ " cache size: " ++ (show (size cache))
145 put $ stow qname (count, cache)
146 PdnsRequestAXFR xrq ->
147 case fetch xrq cache of
150 pdnsReport ("AXFR for unknown id: " ++ (show xrq))
152 io $ queryDom (queryOpNmc cfg mgr xrq) qname
153 >>= putStr . (pdnsOutXfr ver count gen qname)
154 PdnsRequestPing -> io $ putStrLn "END"
156 runStateT mainloop (0, empty) >> return ()
158 -- helper for command-line tools
160 pdnsOut gen key qt dom =
162 "AXFR" -> pdnsOutXfr 1 (-1) gen key dom
163 _ -> pdnsOutQ 1 (-1) gen key (rrType qt) dom
165 -- run one query by key from Namecoin, print domain object and answer
167 mainOne gen key qt = do
168 cfg <- readConfig confFile
169 mgr <- newManager defaultManagerSettings
170 dom <- queryDom (queryOpNmc cfg mgr (-1)) key
171 putStrLn $ ppShow dom
172 putStr $ pdnsOut gen key qt dom
174 -- get data from the file, print domain object and answer
176 mainFile gen key qt = do
177 dom <- queryDom queryOpFile key
178 putStrLn $ ppShow dom
179 putStr $ pdnsOut gen key qt dom
187 with f xs = case xs of
188 [qfqdn, qtype] -> f gen qfqdn qtype
189 _ -> error $ "usage: empty args, or \"[-f] <fqdn> {<TYPE>|ANY|AXFR}\""
193 "-f":xs -> with mainFile xs
194 _ -> with mainOne args