dns_one 0.4.48 → 0.4.49

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5594e70d0e8e9e4e1a48fdfd912d679708807ba
4
- data.tar.gz: f07e8e672204c721295f875542f4a5e3f41ea8da
3
+ metadata.gz: 31952ea93dc7ece574b2a235f1dca11504c4e892
4
+ data.tar.gz: 9aa0c8f73e8255825d711f94cb574ae84d15ac98
5
5
  SHA512:
6
- metadata.gz: 4d36a7402147335e9390e9486ff972648fc9ad0142581021a6fff15848470a0b13f6e589800aad62c0bf09e14a68c6d772c70f7600e89f2b1a79bb5e01a8e2d1
7
- data.tar.gz: d7beb92f9e3d1b3bbe9f244086cbed96cbaa721faaa46d28c5fd361c2a775f944ef3e8e0a1f0621040e051e3042bd368154e3d063a124e30a75b0084b56ce7f0
6
+ metadata.gz: 33bda4530037b8037b897a1ec3d2c9a86cfc20d84837b176171c531dbc15447646837848d4a0b85df41730df16d5a2a2916c1647d3bc73ad351f03604a4c1d5a
7
+ data.tar.gz: 4d12197f714963e6512dae88d456ce914cd16c2fad47f0359b0b9d373780bb858b10b6163c6e2fd192a17e103ab32e5e93c79440ad2d34a81bbeb49bdfebb579
@@ -0,0 +1,9 @@
1
+ module DnsOne; module Backend; class Base
2
+ def allow_cache
3
+ true
4
+ end
5
+
6
+ def preload_dummy?
7
+ false
8
+ end
9
+ end; end; end
@@ -1,5 +1,5 @@
1
1
 
2
- module DnsOne; module Backend; class DB
2
+ module DnsOne; module Backend; class DB < Base
3
3
 
4
4
  def initialize conf
5
5
  @query = conf.delete :query
@@ -7,12 +7,23 @@ module DnsOne; module Backend; class DB
7
7
  setup_db
8
8
  end
9
9
 
10
- def find dom_name, tries = 1
10
+ def find dom_name
11
+ query build_query(dom_name)
12
+ end
13
+
14
+ # Find a dummy zone to make AR/pg load all dependencies
15
+ def preload_dummy?
16
+ true
17
+ end
18
+
19
+ private
20
+
21
+ def query sql, tries = 1
11
22
  return if tries > 3
12
23
 
13
24
  # http://jakeyesbeck.com/2016/02/14/ruby-threads-and-active-record-connections/
14
25
  res = ActiveRecord::Base.connection_pool.with_connection do
15
- ActiveRecord::Base.connection.execute build_query(dom_name)
26
+ ActiveRecord::Base.connection.execute sql
16
27
  end
17
28
 
18
29
  first_record = res&.first
@@ -23,17 +34,11 @@ module DnsOne; module Backend; class DB
23
34
  Log.e "SQL query error. Trying to reconnect #{tries}. Details:\n#{e.desc}"
24
35
  # http://geoff.evason.name/2015/01/18/postgres-ssl-connection-has-been-closed-unexpectedly
25
36
  ActiveRecord::Base.connection.reconnect!
26
- find dom_name, (tries+1)
37
+ find sql, (tries+1)
27
38
  rescue => e
28
39
  Log.e "SQL query error. Details:\n#{e.desc}"
29
40
  end
30
41
 
31
- def allow_cache
32
- true
33
- end
34
-
35
- private
36
-
37
42
  def build_query dom_name
38
43
  @query.sub "'$domain'",
39
44
  ActiveRecord::Base.connection.quote(dom_name)
@@ -1,5 +1,5 @@
1
1
 
2
- module DnsOne; module Backend; class File
2
+ module DnsOne; module Backend; class File < Base
3
3
 
4
4
  def initialize file
5
5
  @domain_map = {}
@@ -0,0 +1,62 @@
1
+
2
+ module DnsOne; module Backend; class HTTPBell < Base
3
+
4
+ def initialize conf
5
+ @conf = conf
6
+ @domains = {}
7
+ @last_id = nil
8
+ update
9
+ listen_updater_bell
10
+ end
11
+
12
+ def find dom_name
13
+ @domains[dom_name]
14
+ end
15
+
16
+ def allow_cache
17
+ false
18
+ end
19
+
20
+ private
21
+
22
+ def update
23
+ last_id = @last_id || 0
24
+
25
+ url = @conf[:update_cache_url].sub '$id', last_id.to_s
26
+
27
+ recs = `curl #{url}`
28
+ .split("\n")
29
+ .map{ |r|
30
+ r.strip.split /\s+/
31
+ }
32
+
33
+ recs.each do |rec|
34
+ id, domain = rec
35
+ id = id.to_i
36
+ @domains[domain] = @conf[:update_cache_record_set]
37
+ if !@last_id || @last_id < id
38
+ @last_id = id
39
+ end
40
+ end
41
+
42
+ Log.d "#{recs.size} new domains added."
43
+ end
44
+
45
+ def listen_updater_bell
46
+ unless @conf[:update_cache_bell_port]
47
+ return
48
+ end
49
+ require "socket"
50
+ dts = TCPServer.new '0.0.0.0', @conf[:update_cache_bell_port]
51
+ Thread.new do
52
+ loop do
53
+ Thread.start(dts.accept) do |s|
54
+ s.close
55
+ Log.i "update`ing..."
56
+ update
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ end; end; end
data/lib/dns_one/cache.rb CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  module DnsOne; class Cache
3
- DEFAULT_MAX_SIZE = 10000
3
+ DEFAULT_MAX_SIZE = 100000
4
4
 
5
5
  def initialize max_size = nil
6
6
  @max_size = max_size || DEFAULT_MAX_SIZE
@@ -1,3 +1,3 @@
1
1
  module DnsOne
2
- VERSION = "0.4.48"
2
+ VERSION = "0.4.49"
3
3
  end
@@ -17,8 +17,9 @@ module DnsOne; class ZoneSearch
17
17
  @cache = Cache.new @conf[:cache_max]
18
18
  @ignore_subdomains_re = build_ignore_subdomains_re
19
19
 
20
- # Find a dummy zone to make AR/pg load all dependencies
21
- query 'dummy.com.br', Resolv::DNS::Resource::IN, '1.2.3.4'
20
+ if @backend.preload_dummy?
21
+ query 'dummy.com.br', Resolv::DNS::Resource::IN, '1.2.3.4'
22
+ end
22
23
 
23
24
  self
24
25
  end
@@ -38,7 +39,7 @@ module DnsOne; class ZoneSearch
38
39
 
39
40
  # use first record set if rec_set_name == ''
40
41
  rec_set_name = @conf[:record_sets].keys.first if rec_set_name == ''
41
-
42
+
42
43
  rec_set = @conf[:record_sets][rec_set_name.to_sym]
43
44
  Log.d "record set #{ rec_set ? 'found' : 'not found' }"
44
45
  return records unless rec_set
@@ -81,20 +82,20 @@ module DnsOne; class ZoneSearch
81
82
  unless ::File.exists? file
82
83
  Util.die "Domain list file #{file} not found."
83
84
  end
84
- Backend::File.new file
85
+ Backend::File.new file
86
+ elsif @conf[:backend][:update_cache_url]
87
+ unless @conf[:backend][:update_cache_record_set]
88
+ Util.die "backend.update_cache_record_set not set."
89
+ end
90
+ Backend::HTTPBell.new @conf[:backend]
85
91
  else
86
92
  Backend::DB.new @conf[:backend]
87
93
  end
88
94
  end
89
95
 
90
96
  def find_record_set dom_name
91
- use_cache = true
92
- use_cache = false if dom_name =~ /^NC/
93
- dom_name.sub! /^NC/, ''
94
-
95
- dom_name.downcase!
96
-
97
- dom_name.sub! /\.home$/i, ''
97
+ dom_name, use_cache = check_debug_tags dom_name
98
+ dom_name = normalize_domain dom_name
98
99
 
99
100
  if @ignore_subdomains_re
100
101
  dom_name.sub! @ignore_subdomains_re, ''
@@ -102,22 +103,30 @@ module DnsOne; class ZoneSearch
102
103
 
103
104
  enabled_cache = use_cache && @backend.allow_cache
104
105
 
105
- from_cache = false
106
-
107
106
  if enabled_cache and rec_set = @cache.find(dom_name)
108
107
  Log.d "found in cache (#{@cache.stat})"
109
- from_cache = true
110
- [rec_set, from_cache]
108
+ [rec_set, true]
111
109
  else
112
110
  if rec_set = @backend.find(dom_name)
113
- if enabled_cache
114
- @cache.add dom_name, rec_set
115
- end
116
- [rec_set, from_cache]
111
+ @cache.add dom_name, rec_set if enabled_cache
112
+ [rec_set, false]
117
113
  end
118
114
  end
119
115
  end
120
116
 
117
+ def normalize_domain dom_name
118
+ dom_name.downcase
119
+ .sub(/\.home$/i, '')
120
+ end
121
+
122
+ def check_debug_tags dom_name
123
+ use_cache = true
124
+ if dom_name.sub!(/^NC/, '')
125
+ use_cache = false
126
+ end
127
+ [dom_name, use_cache]
128
+ end
129
+
121
130
  def check_record_sets
122
131
  if @conf[:record_sets].blank?
123
132
  Util.die "Record sets cannot be empty. Check file."
data/util/dns_one.service CHANGED
@@ -10,3 +10,4 @@ WorkingDirectory=/var/local/dns_one
10
10
  ExecStart=/bin/bash -lc '/root/.rbenv/shims/dns_one'
11
11
  StandardOutput=syslog
12
12
  StandardError=syslog
13
+ SyslogIdentifier=dns_one
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dns_one
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.48
4
+ version: 0.4.49
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lobato
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-03 00:00:00.000000000 Z
11
+ date: 2018-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -160,8 +160,10 @@ files:
160
160
  - exe/dns_one
161
161
  - gempush
162
162
  - lib/dns_one.rb
163
+ - lib/dns_one/backend/base.rb
163
164
  - lib/dns_one/backend/db.rb
164
165
  - lib/dns_one/backend/file.rb
166
+ - lib/dns_one/backend/http_bell.rb
165
167
  - lib/dns_one/cache.rb
166
168
  - lib/dns_one/cli.rb
167
169
  - lib/dns_one/core_ext/blank.rb