Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added osx resolver manager

  • Loading branch information...
commit 8063c7b8c8b02a0d2d49afd78fbb71ee1e738347 1 parent 8c7200c
@mattinsler authored
View
2  index.coffee
@@ -8,3 +8,5 @@ exports.Client = require './lib/client'
exports.Server = require './lib/server'
exports.Service = require './lib/service'
exports.Dns = require './lib/dns'
+
+exports.OsxResolverManager = require './lib/osx_resolver_manager'
View
3  lib/cli.coffee
@@ -4,6 +4,7 @@
# - proxy
# - web server for status
+os = require 'os'
axle = require '../index'
walkabout = require 'walkabout'
exec = require('child_process').exec
@@ -93,6 +94,8 @@ exports.run_server = ->
servers.push(new axle.Service(instance)) if instance.config.service.enabled
servers.push(new axle.Dns(instance)) if instance.config.dns.enabled
+ servers.push(new axle.OsxResolverManager(instance)) if os.platform() is 'darwin'
+
servers.forEach (s) -> s.start()
exports.daemon = ->
View
71 lib/dns.coffee
@@ -107,37 +107,6 @@ class Dns
parse: (msg) ->
DnsRequest.unpack(msg)
-
- # read_string = (offset) ->
- # length = msg.readUInt8(offset)
- # msg.slice(offset + 1, offset + 1 + length).toString('ascii')
- #
- # parsed = {}
- #
- # parsed.qid = msg.readUInt16BE(0)
- # parsed.flags = msg.readUInt16BE(2)
- # parsed.qcount = msg.readUInt16BE(4)
- # parsed.acount = msg.readUInt16BE(6)
- # parsed.auth_count = msg.readUInt16BE(8)
- # parsed.addl_count = msg.readUInt16BE(10)
- #
- # offset = 12
- #
- # parsed.domain = []
- # while msg.readUInt8(offset) isnt 0
- # d = read_string(offset)
- # parsed.domain.push(d)
- # offset += 1 + d.length
- #
- # ++offset
- #
- # parsed.domain = parsed.domain.join('.')
- #
- # parsed.qtype = msg.readUInt16BE(offset)
- # parsed.qclass = msg.readUInt16BE(offset + 2)
- # parsed.qtype_readable = REQUEST_TYPES[parsed.qtype] ? 'UNKNOWN'
- #
- # parsed
adjust_response_ttl: (response, ttl) ->
questions = response.readUInt16BE(4)
@@ -196,46 +165,6 @@ class Dns
@server.send(res, 0, res.length, remote_info.port, remote_info.address)
- # packet = new Buffer(1024)
- #
- # write_string = (str, offset) ->
- # packet.writeUInt8(str.length, offset)
- # new Buffer(str, 'ascii').copy(packet, offset + 1, 0, str.length)
- #
- # write_ip = (ip, offset) ->
- # i = ip.split('.')
- # packet.writeUInt8(parseInt(i[0]), offset)
- # packet.writeUInt8(parseInt(i[1]), offset + 1)
- # packet.writeUInt8(parseInt(i[2]), offset + 2)
- # packet.writeUInt8(parseInt(i[3]), offset + 3)
- #
- # packet.writeUInt16BE(parsed.qid, 0)
- # packet.writeUInt16BE(0x8180, 2)
- # packet.writeUInt16BE(1, 4) # question count
- # packet.writeUInt16BE(1, 6) # answer count
- # packet.writeUInt16BE(0, 8)
- # packet.writeUInt16BE(0, 10)
- #
- # offset = 12
- #
- # for d in parsed.domain.split('.')
- # write_string(d, offset)
- # offset += 1 + d.length
- #
- # packet.writeUInt8(0, offset++)
- # packet.writeUInt16BE(parsed.qtype, offset)
- # packet.writeUInt16BE(1, offset + 2)
- #
- # packet.writeUInt8(0xc0, offset + 4)
- # packet.writeUInt8(0x0c, offset + 5)
- # packet.writeUInt16BE(parsed.qtype, offset + 6)
- # packet.writeUInt16BE(1, offset + 8)
- # packet.writeUInt32BE(1, offset + 10) # TTL
- # packet.writeUInt16BE(4, offset + 14) # IP Length
- # write_ip(ip_address, offset + 16)
- #
- # @server.send(packet, 0, offset + 20, remote_info.port, remote_info.address)
-
on_message: (msg, remote_info) ->
req = @parse(msg)
console.log "#{REQUEST_TYPES[req.question.qtype]} #{req.question.domain}"
View
55 lib/osx_resolver_manager.coffee
@@ -0,0 +1,55 @@
+walkabout = require 'walkabout'
+Logger = require './logger'
+Configuration = require './configuration'
+
+AXLE_RESOLVER = """
+# Resolver for axle
+nameserver 127.0.0.1
+port #{Configuration.dns.port}
+"""
+
+class OsxResolverManager
+ constructor: (@axle) ->
+ @domains = {}
+ @axle.routes.forEach (d) => @add_domain(d)
+
+ @axle.on 'route:add', (route) =>
+ @add_domain(route.host)
+
+ @axle.on 'route:remove', (route) =>
+ @remove_domain(route.host)
+
+ add_domain: (domain) ->
+ domain = domain.split('.').slice(-1)[0]
+ return @domains[domain] if domain is '*'
+ @domains[domain] ?= 0
+ @create_resolver(domain) if ++@domains[domain] > 0
+ @domains[domain]
+
+ remove_domain: (domain) ->
+ domain = domain.split('.').slice(-1)[0]
+ return @domains[domain] if domain is '*'
+ @domains[domain] ?= 0
+ @remove_resolver(domain) if --@domains[domain] is 0
+ @domains[domain]
+
+ create_resolver: (domain) ->
+ return unless @running is true
+
+ Logger.info "write /etc/resolver/#{domain}"
+ walkabout('/etc/resolver').mkdirp_sync()
+ walkabout("/etc/resolver/#{domain}").write_file_sync(AXLE_RESOLVER)
+
+ remove_resolver: (domain) ->
+ return unless @running is true
+
+ Logger.info "rm /etc/resolver/#{domain}"
+ walkabout("/etc/resolver/#{domain}").unlink_sync() if walkabout("/etc/resolver/#{domain}").exists_sync()
+
+ start: ->
+ @running = true
+
+ stop: ->
+ @running = false
+
+module.exports = OsxResolverManager
Please sign in to comment.
Something went wrong with that request. Please try again.