dnschain
Version:
A blockchain-based DNS + HTTPS server that fixes HTTPS security, and more!
82 lines (62 loc) • 2.91 kB
text/coffeescript
###
dnschain
http://dnschain.net
Copyright (c) 2014 okTurtles Foundation
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
###
request = require 'superagent'
module.exports = (dnschain) ->
# expose these into our namespace
for k of dnschain.globals
eval "var #{k} = dnschain.globals.#{k};"
BlockchainResolver = require('../blockchain.coffee')(dnschain)
ResolverStream = require('../resolver-stream')(dnschain)
getAsync = Promise.promisify request.get
QTYPE_NAME = dns2.consts.QTYPE_TO_NAME
NAME_QTYPE = dns2.consts.NAME_TO_QTYPE
NAME_RCODE = dns2.consts.NAME_TO_RCODE
RCODE_NAME = dns2.consts.RCODE_TO_NAME
BLOCKS2SEC = 60
class NxtResolver extends BlockchainResolver
constructor: () ->
= gNewLogger 'NXT'
= 'nxt'
= 'nxt'
.dnsInfo = (data) -> data.aliasURI
gFillWithRunningChecks @
config: ->
.debug "Loading #{@name} resolver"
= _.transform ["port", "connect"], (o,v) =>
o[v] = gConf.get 'nxt:'+v
, {}
unless _().values().every()
missing = _.transform , ((o,v,k)->if !v then o.push 'nxt:'+k), []
.info "Disabled. Missing params:", missing
return
= "http://#{@params.connect}:#{@params.port}/nxt?requestType=getAlias&aliasName="
.info "Nxt API on:",
@
resources:
key: (property, operation, fmt, args, cb) ->
result =
if S(property).endsWith(".#{@tld}")
property = S(property).chompRight(".#{@tld}").s
if (dotIdx = property.lastIndexOf('.')) != -1
property = property.slice(dotIdx+1) #rm subdomain
.debug gLineInfo("#{@name} resolve"), {property:property}
getAsync( + encodeURIComponent(property)).then (res) =>
try
json = JSON.parse res.text
if json.aliasURI?
try json.aliasURI = JSON.parse json.aliasURI
result.data = json
.debug gLineInfo('resolved OK!'), {result:result}
cb null, result
catch e
.warn gLineInfo('server did not respond with valid json!'), {err:e.message, url:res.request.url, response:res.text}
cb e
.catch (e) =>
.error gLineInfo('error contacting NXT server!'), {err:e.message}
cb e