smart_proxy_dhcp_dnsmasq 0.2 → 0.4
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97a5371cbd074b39c66bd23b907b1b31730beb0b
|
4
|
+
data.tar.gz: 47b63b6d25aca90f3dcf03d442efca03493084d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5495631faaeaa74b84b6123fe2fb33f7f6bee3ec3f8b1a26e6901f70eee55bd31050b68ca50eb4afb94a46b5729ac8b636923a55c85365928280327ad8f9cf4d
|
7
|
+
data.tar.gz: 54d43f53f1a2d9d6bc1026d71dd5521060161da1d4d3ed13b27c365c8d0dbad0f019e179d377a241878aecee473fc28cb77c25b14d37328639a3a9abb7cd1793
|
@@ -4,12 +4,13 @@ require 'dhcp_common/server'
|
|
4
4
|
|
5
5
|
module Proxy::DHCP::Dnsmasq
|
6
6
|
class Record < ::Proxy::DHCP::Server
|
7
|
-
attr_reader :config_files, :write_config_file, :reload_cmd
|
7
|
+
attr_reader :config_files, :write_config_file, :reload_cmd, :subnet_service
|
8
8
|
|
9
9
|
def initialize(config_files, write_config_file, reload_cmd, subnet_service)
|
10
10
|
@config_files = config_files
|
11
11
|
@write_config_file = write_config_file
|
12
12
|
@reload_cmd = reload_cmd
|
13
|
+
@subnet_service = subnet_service
|
13
14
|
|
14
15
|
subnet_service.load!
|
15
16
|
|
@@ -25,15 +26,21 @@ module Proxy::DHCP::Dnsmasq
|
|
25
26
|
file.puts "dhcp-boot=tag:#{record.mac},#{options[:filename]},#{options[:nextServer]}" if\
|
26
27
|
options[:filename] && options[:nextServer]
|
27
28
|
end
|
29
|
+
|
30
|
+
subnet_service.add_host(record)
|
31
|
+
|
28
32
|
raise Proxy::DHCP::Error, 'Failed to reload configuration' unless system(@reload_cmd)
|
29
33
|
|
30
34
|
record
|
31
35
|
end
|
32
36
|
|
33
37
|
def del_record(record)
|
38
|
+
# TODO: Removal of leases, to prevent DHCP record collisions
|
39
|
+
return record if record.is_a? ::Proxy::DHCP::Lease
|
40
|
+
|
34
41
|
found = false
|
35
42
|
tmppath = nil
|
36
|
-
|
43
|
+
Tempfile.open('reservations') do |output|
|
37
44
|
tmppath = output.path.freeze
|
38
45
|
open(@write_config_file, 'r').each_line do |line|
|
39
46
|
output.puts line unless line.start_with?("dhcp-host=#{record.mac}") || \
|
@@ -44,13 +51,15 @@ module Proxy::DHCP::Dnsmasq
|
|
44
51
|
line.start_with?("dhcp-host=set:#{record.mac}")
|
45
52
|
end
|
46
53
|
end
|
47
|
-
FileUtils.mv(
|
54
|
+
FileUtils.mv(tmppath, @write_config_file) if found
|
55
|
+
|
56
|
+
subnet_service.delete_host(record)
|
48
57
|
|
49
58
|
raise Proxy::DHCP::Error, 'Failed to reload configuration' unless system(@reload_cmd)
|
50
59
|
|
51
60
|
record
|
52
61
|
ensure
|
53
|
-
File.unlink(tmppath) if File.exists?(tmppath)
|
62
|
+
File.unlink(tmppath) if tmppath && File.exists?(tmppath)
|
54
63
|
end
|
55
64
|
end
|
56
65
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'ipaddr'
|
2
|
-
require 'rb-inotify'
|
2
|
+
#require 'rb-inotify'
|
3
3
|
require 'dhcp_common/dhcp_common'
|
4
4
|
require 'dhcp_common/subnet_service'
|
5
5
|
|
@@ -19,7 +19,7 @@ module Proxy::DHCP::Dnsmasq
|
|
19
19
|
def load!
|
20
20
|
add_subnet(parse_config_for_subnet)
|
21
21
|
load_subnet_data
|
22
|
-
add_watch
|
22
|
+
#add_watch # TODO
|
23
23
|
|
24
24
|
true
|
25
25
|
end
|
@@ -31,7 +31,7 @@ module Proxy::DHCP::Dnsmasq
|
|
31
31
|
next unless ev.absolute_name == lease_file
|
32
32
|
|
33
33
|
leases = load_leases(subnet_service)
|
34
|
-
|
34
|
+
|
35
35
|
# FIXME: Proper method for this
|
36
36
|
m.synchronize do
|
37
37
|
leases_by_ip.clear
|
@@ -56,7 +56,7 @@ module Proxy::DHCP::Dnsmasq
|
|
56
56
|
@lease_file = value
|
57
57
|
when 'dhcp-range'
|
58
58
|
data = value.split(',')
|
59
|
-
|
59
|
+
|
60
60
|
ttl = data.pop
|
61
61
|
mask = data.pop
|
62
62
|
range_to = data.pop
|
@@ -100,7 +100,7 @@ module Proxy::DHCP::Dnsmasq
|
|
100
100
|
|
101
101
|
# Expects subnet_service to have subnet data
|
102
102
|
def parse_config_for_dhcp_reservations(files = @config_files)
|
103
|
-
to_ret =
|
103
|
+
to_ret = {}
|
104
104
|
files.each do |file|
|
105
105
|
open(file, 'r').each_line do |line|
|
106
106
|
line.strip!
|
@@ -112,21 +112,35 @@ module Proxy::DHCP::Dnsmasq
|
|
112
112
|
data = value.split(',')
|
113
113
|
data.shift while data.first.start_with? 'set:'
|
114
114
|
|
115
|
-
mac, ip, hostname = data
|
115
|
+
mac, ip, hostname = data[0,3]
|
116
|
+
|
117
|
+
# TODO: Possible ttl on end
|
116
118
|
|
117
119
|
subnet = find_subnet(ip)
|
118
|
-
to_ret
|
120
|
+
to_ret[mac] = ::Proxy::DHCP::Reservation.new(
|
119
121
|
hostname,
|
120
122
|
ip,
|
121
123
|
mac,
|
122
124
|
subnet,
|
123
|
-
|
124
|
-
:source_file => file
|
125
|
+
# :source_file => file # TODO: Needs to overload the comparison
|
125
126
|
)
|
127
|
+
when 'dhcp-boot'
|
128
|
+
data = value.split(',')
|
129
|
+
if data.first.start_with? 'tag:'
|
130
|
+
mac = data.first[4..-1]
|
131
|
+
data.shift
|
132
|
+
|
133
|
+
next unless to_ret.key? mac
|
134
|
+
|
135
|
+
file, server = data
|
136
|
+
|
137
|
+
to_ret[mac].options[:nextServer] = file
|
138
|
+
to_ret[mac].options[:filename] = server
|
139
|
+
end
|
126
140
|
end
|
127
141
|
end
|
128
142
|
end
|
129
|
-
to_ret
|
143
|
+
to_ret.values
|
130
144
|
rescue Exception => e
|
131
145
|
logger.error msg = "Unable to parse reservations: #{e}"
|
132
146
|
raise Proxy::DHCP::Error, msg
|
@@ -134,10 +148,25 @@ module Proxy::DHCP::Dnsmasq
|
|
134
148
|
|
135
149
|
def load_subnet_data
|
136
150
|
reservations = parse_config_for_dhcp_reservations
|
137
|
-
reservations.each
|
138
|
-
|
139
|
-
|
140
|
-
|
151
|
+
reservations.each do |record|
|
152
|
+
if dupe = find_host_by_mac(record.subnet_address, record.mac)
|
153
|
+
delete_host(dupe)
|
154
|
+
end
|
155
|
+
|
156
|
+
add_host(record.subnet_address, record)
|
157
|
+
end
|
158
|
+
|
159
|
+
leases = [] # load_leases # FIXME: Will cause collisions if added
|
160
|
+
leases.each do |lease|
|
161
|
+
if dupe = find_lease_by_mac(lease.subnet_address, lease.mac)
|
162
|
+
delete_lease(dupe)
|
163
|
+
end
|
164
|
+
if dupe = find_lease_by_ip(lease.subnet_address, lease.ip)
|
165
|
+
delete_lease(dupe)
|
166
|
+
end
|
167
|
+
|
168
|
+
add_lease(lease.subnet_address, lease)
|
169
|
+
end
|
141
170
|
end
|
142
171
|
|
143
172
|
# Expects subnet_service to have subnet data
|
@@ -150,13 +179,15 @@ module Proxy::DHCP::Dnsmasq
|
|
150
179
|
|
151
180
|
subnet = find_subnet(ip)
|
152
181
|
leases << ::Proxy::DHCP::Lease.new(
|
153
|
-
|
182
|
+
hostname,
|
154
183
|
ip,
|
155
184
|
mac,
|
156
185
|
subnet,
|
157
186
|
timestamp - (@ttl or 24*60*60),
|
158
187
|
timestamp,
|
159
|
-
'active'
|
188
|
+
'active',
|
189
|
+
deleteable: false,
|
190
|
+
)
|
160
191
|
end
|
161
192
|
leases
|
162
193
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_proxy_dhcp_dnsmasq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Olofsson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|