vipnet_parser 0.1 → 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/vipnet_parser.rb +113 -112
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c12c7a620a2e80a808e81ef380e3c84ef4498a4
|
4
|
+
data.tar.gz: b5c9c4dd56f9cc43619ea7109705e628cc3fb513
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6c2300da35ea7e80b52899f17a5ca24063ba09db10625677e22c7d198b19e4cf8bc9c729fe103ca98ff01c28313fab6c32b5db9e6d39c600bd03adb34817697
|
7
|
+
data.tar.gz: 83d219af3d3cd9395db168783b08f9b14b70479849b41be6a4a3ce737bb29b04a913b626920130026bf117ebbc1582e6afdfab0675737c03d2fbb2c45d615c2b
|
data/lib/vipnet_parser.rb
CHANGED
@@ -1,13 +1,5 @@
|
|
1
|
-
|
2
|
-
def
|
3
|
-
res = true
|
4
|
-
@props.each do |prop|
|
5
|
-
res = res && self.send(prop.to_s) == other.send(prop.to_s)
|
6
|
-
end
|
7
|
-
res
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.id(args)
|
1
|
+
module VipnetParser
|
2
|
+
def id(args)
|
11
3
|
if args.class == String
|
12
4
|
string = args
|
13
5
|
array = Array.new
|
@@ -27,11 +19,10 @@ class VipnetParser
|
|
27
19
|
if string =~ regexp && !string_matches_anything
|
28
20
|
string_matches_anything = true
|
29
21
|
array += callback.call({ string: Regexp.last_match(2), threshold: threshold })
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
array += VipnetParser::id({ string: Regexp.last_match(3), array: array, threshold: threshold })
|
22
|
+
[Regexp.last_match(1), Regexp.last_match(3)].each do |side_match|
|
23
|
+
unless side_match.empty?
|
24
|
+
array += id({ string: side_match, array: array, threshold: threshold })
|
25
|
+
end
|
35
26
|
end
|
36
27
|
end
|
37
28
|
end
|
@@ -69,7 +60,7 @@ class VipnetParser
|
|
69
60
|
["0x" + string.rjust(8, "0")]
|
70
61
|
end
|
71
62
|
|
72
|
-
def
|
63
|
+
def network(id)
|
73
64
|
normal_ids = id(id)
|
74
65
|
if normal_ids
|
75
66
|
normal_id = normal_ids[0]
|
@@ -78,115 +69,125 @@ class VipnetParser
|
|
78
69
|
false
|
79
70
|
end
|
80
71
|
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
class Iplirconf < VipnetParser
|
85
|
-
PROPS = [:content, :id, :sections]
|
86
|
-
attr_accessor *PROPS, :last_error
|
87
|
-
private_constant :PROPS
|
72
|
+
module_function :id, :network
|
88
73
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
content_nc = @content.gsub(/^#.*\n/, "")
|
97
|
-
# remove ending
|
98
|
-
@sections = Hash.new
|
99
|
-
adapter_position = content_nc.index("[adapter]")
|
100
|
-
unless adapter_position
|
101
|
-
@last_error = "unable to parse iplirconf (no [adapter] section)"
|
102
|
-
return false
|
74
|
+
class VipnetParser
|
75
|
+
def ==(other)
|
76
|
+
res = true
|
77
|
+
@props.each do |prop|
|
78
|
+
res = res && self.send(prop.to_s) == other.send(prop.to_s)
|
79
|
+
end
|
80
|
+
res
|
103
81
|
end
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
82
|
+
end
|
83
|
+
|
84
|
+
class Iplirconf < VipnetParser
|
85
|
+
PROPS = [:content, :id, :sections]
|
86
|
+
attr_accessor *PROPS, :last_error
|
87
|
+
private_constant :PROPS
|
88
|
+
|
89
|
+
def initialize(*args)
|
90
|
+
@props = PROPS
|
91
|
+
unless args.size == 1
|
92
|
+
return false
|
93
|
+
end
|
94
|
+
@content = args[0]
|
95
|
+
# remove comments
|
96
|
+
content_nc = @content.gsub(/^#.*\n/, "")
|
97
|
+
# remove ending
|
98
|
+
@sections = Hash.new
|
99
|
+
adapter_position = content_nc.index("[adapter]")
|
100
|
+
unless adapter_position
|
101
|
+
@last_error = "unable to parse iplirconf (no [adapter] section)"
|
102
|
+
return false
|
103
|
+
end
|
104
|
+
# prepare for split
|
105
|
+
content_nc = content_nc[0..(adapter_position - 2)]
|
106
|
+
content_nc = "\n" + content_nc
|
107
|
+
content_nc.split("\n[id]\n").reject{ |t| t.empty? }.each_with_index do |section_content, i|
|
108
|
+
tmp_section = Hash.new
|
109
|
+
props = {
|
110
|
+
:single => [
|
111
|
+
:id, :name, :filterdefault, :tunnel,
|
112
|
+
:firewallip, :port, :proxyid, :dynamic_timeout, :accessip,
|
113
|
+
:usefirewall, :fixfirewall, :virtualip, :version,
|
114
|
+
],
|
115
|
+
:multi => [:ip, :filterudp, :filtertcp]
|
116
|
+
}
|
117
|
+
props.each do |type, props|
|
118
|
+
props.each do |prop|
|
119
|
+
get_section_param({ prop: prop, section: tmp_section, content: section_content, type: type })
|
120
|
+
end
|
120
121
|
end
|
122
|
+
# self section id
|
123
|
+
@id = tmp_section[:id] if i == 0
|
124
|
+
@sections[tmp_section[:id]] = tmp_section
|
121
125
|
end
|
122
|
-
|
123
|
-
@id = tmp_section[:id] if i == 0
|
124
|
-
@sections[tmp_section[:id]] = tmp_section
|
126
|
+
true
|
125
127
|
end
|
126
|
-
true
|
127
|
-
end
|
128
128
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
129
|
+
def get_section_param(args)
|
130
|
+
value_regexp = Regexp.new("^#{args[:prop].to_s}=\s(.*)$")
|
131
|
+
if args[:type] == :multi
|
132
|
+
tmp_array = Array.new
|
133
|
+
args[:content].each_line do |line|
|
134
|
+
value = line[value_regexp, 1]
|
135
|
+
tmp_array.push(value) if value
|
136
|
+
end
|
137
|
+
args[:section][args[:prop]] = tmp_array unless tmp_array.empty?
|
138
|
+
elsif args[:type] == :single
|
139
|
+
value = args[:content][value_regexp, 1]
|
140
|
+
args[:section][args[:prop]] = value if value
|
136
141
|
end
|
137
|
-
args[:section][args[:prop]] = tmp_array unless tmp_array.empty?
|
138
|
-
elsif args[:type] == :single
|
139
|
-
value = args[:content][value_regexp, 1]
|
140
|
-
args[:section][args[:prop]] = value if value
|
141
142
|
end
|
142
|
-
end
|
143
143
|
|
144
|
-
|
145
|
-
end
|
144
|
+
private :get_section_param
|
145
|
+
end
|
146
146
|
|
147
|
-
class Nodename < VipnetParser
|
148
|
-
|
149
|
-
|
150
|
-
|
147
|
+
class Nodename < VipnetParser
|
148
|
+
PROPS = [:content, :records]
|
149
|
+
attr_accessor *PROPS, :last_error
|
150
|
+
private_constant :PROPS
|
151
151
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
152
|
+
def initialize(*args)
|
153
|
+
@props = PROPS
|
154
|
+
unless args.size == 1
|
155
|
+
return false
|
156
|
+
end
|
157
|
+
@content = args[0]
|
158
|
+
lines = content.force_encoding("cp866").encode("utf-8", replace: nil).split("\r\n")
|
159
|
+
if lines.size == 0
|
160
|
+
@last_error = "error parsing nodename"
|
161
|
+
return false
|
162
|
+
end
|
163
|
+
@records = Hash.new
|
164
|
+
lines.each do |line|
|
165
|
+
tmp_record = Hash.new
|
166
|
+
tmp_record = get_record_params(line)
|
167
|
+
tmp_record[:name].rstrip!
|
168
|
+
tmp_record[:enabled] = { "1" => true, "0" => false }[tmp_record[:enabled]]
|
169
|
+
tmp_record[:category] = { "A" => :client, "S" => :server, "G" => :group }[tmp_record[:category]]
|
170
|
+
@records[tmp_record[:id]] = tmp_record
|
171
|
+
end
|
172
|
+
true
|
162
173
|
end
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
174
|
+
|
175
|
+
def get_record_params(line)
|
176
|
+
record_regexp = /^
|
177
|
+
(?<name>.{50})\s
|
178
|
+
(?<enabled>[01])\s
|
179
|
+
(?<category>[ASG])\s
|
180
|
+
[0-9A-F]{8}
|
181
|
+
(?<server_number>[0-9A-F]{4})
|
182
|
+
(?<abonent_number>[0-9A-F]{4})\s
|
183
|
+
(?<id>[0-9A-F]{8})
|
184
|
+
$/x
|
185
|
+
match = record_regexp.match(line)
|
186
|
+
names = match.names.map { |name| name.to_sym }
|
187
|
+
# https://gist.github.com/flarnie/6221219
|
188
|
+
return Hash[names.zip(match.captures)]
|
171
189
|
end
|
172
|
-
true
|
173
|
-
end
|
174
190
|
|
175
|
-
|
176
|
-
record_regexp = /^
|
177
|
-
(?<name>.{50})\s
|
178
|
-
(?<enabled>[01])\s
|
179
|
-
(?<category>[ASG])\s
|
180
|
-
[0-9A-F]{8}
|
181
|
-
(?<server_number>[0-9A-F]{4})
|
182
|
-
(?<abonent_number>[0-9A-F]{4})\s
|
183
|
-
(?<id>[0-9A-F]{8})
|
184
|
-
$/x
|
185
|
-
match = record_regexp.match(line)
|
186
|
-
names = match.names.map { |name| name.to_sym }
|
187
|
-
# https://gist.github.com/flarnie/6221219
|
188
|
-
return Hash[names.zip(match.captures)]
|
191
|
+
private :get_record_params
|
189
192
|
end
|
190
|
-
|
191
|
-
private :get_record_params
|
192
193
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vipnet_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Morozov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Parses ViPNet strings like 'something 1A0EABCD something'
|
14
|
-
|
13
|
+
description: Parses ViPNet strings like 'something 1A0EABCD something', iplir.conf
|
14
|
+
and other files.
|
15
15
|
email: ntcomp12@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|