class Dnsblim::Methods

Attributes

api_host[R]
key[R]

Public Class Methods

new(options) click to toggle source
# File lib/dnsblim/methods.rb, line 13
def initialize(options)
  config_path = Pathname(options[:config]).expand_path.realdirpath.to_s
  @hl = HighLine.new(STDIN, STDOUT, 80, 1, 2, 0)
  begin
    config = ParseConfig.new(config_path)
    @api_host = config['api_host']
    @key = config['key']
    @quiet = options[:quiet]
    @table = options[:table]
    @record_type = options[:record_type]
  rescue Errno::EACCES
    @hl.say "<%= color('Error', :red, :bold) %>: #{config_path} does not exist."
    @hl.indent 1, 'Please run `dnsblim init` to initialize the gem.'
  end
end

Public Instance Methods

add(type, ip_addr, reason) click to toggle source
# File lib/dnsblim/methods.rb, line 62
def add(type, ip_addr, reason)
  path = "#{@api_host}/import"
  template = {
    key: @key,
    addresses: [
      { ip: ip_addr, type: type, reason: reason }
    ]
  }

  response = HTTParty.post(path,
                           headers: { 'Content-Type' => 'application/json' },
                           body: template.to_json)
  reply = APIResponse.parse(response.body, response.code, ip_addrs: [ip_addr])
  if @table
    reply.print_table('add')
  else
    puts reply.to_s
  end
end
check(ip_addr) click to toggle source

@param [IPAddr] ip_addr An IP to check

# File lib/dnsblim/methods.rb, line 30
def check(ip_addr)
  path = "#{@api_host}/check"
  response = HTTParty.post(path,
                           body: {
                             key: @key,
                             ip: ip_addr
                           }.to_json)

  reply = APIResponse.parse(response.body, response.code, ip_addr: ip_addr, quiet: @quiet)

  if @table
    reply.print_table('check')
  else
    puts reply.to_s
  end
end
config(software = nil) click to toggle source

@param [String] software service config to return

# File lib/dnsblim/methods.rb, line 171
def config(software = nil)
  path = "#{@api_host}/config/raw/"
  case software
  when 'insp', 'inspircd'
    path += 'inspircd'
  when 'unreal', 'unrealircd'
    path += 'unreal'
  when 'chary', 'charybdis'
    path += 'charybdis'
  when 'hopm', 'bopm'
    path += 'hopm'
  end
  response = HTTParty.get(path)
  puts response.body
end
fadd(file = nil, options) click to toggle source
# File lib/dnsblim/methods.rb, line 107
def fadd(file = nil, options)
  path = "#{@api_host}/import"
  addresses = []
  ips = File.read(Pathname(file).expand_path.realdirpath).lines.map(&:chomp)
  ips.each_slice(50) do |bit|
    hash_list = []
    bit.each do |ip|
      hash_list << {ip: ip, type: options[:type], reason: options[:reason]}
    end

    template = {
      key: @key,
      addresses: hash_list
    }

    response = HTTParty.post(path,
                             headers: { 'Content-Type' => 'application/json' },
                             body: template.to_json)
    reply = APIResponse.parse(response.body, response.code, ip_addrs: ips, quiet: @quiet)

    if @table
      reply.print_table('madd')
    else
      puts reply.to_s
    end
  end
end
init(options) click to toggle source
# File lib/dnsblim/methods.rb, line 187
def init(options)
  if FileTest.exist? File.expand_path '~/.dnsblimrc'
    @hl.say("'~/.dnsblimrc', exists, not overwriting.")
  else
    template = [
      '# DNSBL.im CLI Configuration File',
      '# API Key',
      'key = PUTYOURKEYHERE',
      "# DNSBL.im API url, don't change unless you",
      '# know what you are doing.',
      '#api_host = https://api.dnsbl.im'
    ]
    File.open(File.expand_path('~/.dnsblimrc'), 'w') do |fd|
      fd.puts template
    end
  end
end
madd(type, reason, ips) click to toggle source

@param [Integer] type Listing Type/Record @param [String] reason Reason to list IPs @param [Array] ips Array of IPs to list

# File lib/dnsblim/methods.rb, line 85
def madd(type, reason, ips)
  path = "#{@api_host}/import"
  addresses = []
  ips.each do |ip|
    addresses << { ip: ip, type: type, reason: reason }
  end
  template = {
    key: @key,
    addresses: addresses
  }

  response = HTTParty.post(path,
                           headers: { 'Content-Type' => 'application/json' },
                           body: template.to_json)
  reply = APIResponse.parse(response.body, response.code, ip_addrs: ips)
  if @table
    reply.print_table('madd')
  else
    puts reply.to_s
  end
end
mcheck(*ips) click to toggle source

@param [Array] ips A list of IPs to check

# File lib/dnsblim/methods.rb, line 48
def mcheck(*ips)
  @hl.say('10 IP limit') if ips.length > 10
  path = "#{@api_host}/mcheck/#{ips.join('+')}"
  response = HTTParty.post(path, body: {
    key: @key
  }.to_json)
  reply = APIResponse.parse(response.body, response.code, quiet: @quiet)
  if @table
    reply.print_table('mcheck')
  else
    puts reply.to_s
  end
end
stats(type = nil) click to toggle source
# File lib/dnsblim/methods.rb, line 134
def stats(type = nil)
  path = "#{@api_host}/stats"
  response = HTTParty.post(
      path,
      headers: {'Content-Type' => 'application/json'},
      body: {key: @key}.to_json
  )
  reply = APIResponse.new(response.body, response.code, record_type: @record_type)
  if @table
    case type
    when 'tor', '1'
      reply.print_table('stats')
    when 'http', '2'

    when 'socks', '3'

    when 'router', '4'

    when 'abusive', '5', 'spam'

    when 'mail', 'mta', '7'

    when '*'
      reply.print_table('stats')

    else
      @hl.say "Error: #{type} is not a valid listing type."
    end
  else
    puts reply.to_s
  end


end