vipnet_parser 0.3.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/vipnet_parser.rb +3 -212
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f043e287cec988e0e3892e9046c588f81a1a2bde
|
4
|
+
data.tar.gz: 9eedaba5195eebc282099329486eceba82bca783
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 184ee79a3e19bcf31b990c5d5e29624c84c36fc9cb5f73b9e61c09fb28c0ea474918a2bccb5145cac848c0ad1d41c848d91bbb2a424628143f8b4fa83ee6824c
|
7
|
+
data.tar.gz: 58bc03342eb484ce873104610e87ee43559daa9a08bce1fc010504a692cde78ec4ed97dc0bc99f7cc4e9589fdbede18fcaa84d5b80e4b34233815af65e1257f8
|
data/lib/vipnet_parser.rb
CHANGED
@@ -1,212 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
string = args
|
5
|
-
array = Array.new
|
6
|
-
elsif args.class == Hash
|
7
|
-
string = args[:string]
|
8
|
-
array = args[:array]
|
9
|
-
threshold = args[:threshold]
|
10
|
-
end
|
11
|
-
string = string.downcase
|
12
|
-
cyrillic_sub = {
|
13
|
-
"а" => "a", "б" => "b", "с" => "c", "д" => "d", "е" => "e", "ф" => "f",
|
14
|
-
"А" => "a", "Б" => "b", "С" => "c", "Д" => "d", "Е" => "e", "Ф" => "f",
|
15
|
-
}
|
16
|
-
cyrillic_sub.each do |cyr, lat|
|
17
|
-
string = string.gsub(cyr, lat)
|
18
|
-
end
|
19
|
-
|
20
|
-
array = [] unless array
|
21
|
-
regexps = {
|
22
|
-
/(.*)(0x[0-9a-f]{1,8}-0x[0-9a-f]{1,8})(.*)/m => method(:id_parse_variant1),
|
23
|
-
/(.*)([0-9a-f]{8})(.*)/m => method(:id_parse_variant2),
|
24
|
-
/(.*)0x([0-9a-f]{1,8})(.*)/m => method(:id_parse_variant3),
|
25
|
-
}
|
26
|
-
string_matches_anything = false
|
27
|
-
regexps.each do |regexp, callback|
|
28
|
-
if string =~ regexp && !string_matches_anything
|
29
|
-
string_matches_anything = true
|
30
|
-
array += callback.call({ string: Regexp.last_match(2), threshold: threshold })
|
31
|
-
[Regexp.last_match(1), Regexp.last_match(3)].each do |side_match|
|
32
|
-
unless side_match.empty?
|
33
|
-
array += id({ string: side_match, array: array, threshold: threshold })
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
if string_matches_anything
|
39
|
-
return array.uniq.sort
|
40
|
-
else
|
41
|
-
return []
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.id_parse_variant1(args)
|
46
|
-
string = args[:string]
|
47
|
-
threshold = args[:threshold]
|
48
|
-
string =~ /0x([0-9a-f]{1,8})-0x([0-9a-f]{1,8})/
|
49
|
-
interval_begin = Regexp.last_match(1).to_i(16)
|
50
|
-
interval_end = Regexp.last_match(2).to_i(16)
|
51
|
-
return [] if interval_end < interval_begin
|
52
|
-
if threshold
|
53
|
-
return [] if interval_end - interval_begin + 1 > threshold
|
54
|
-
end
|
55
|
-
array = Array.new
|
56
|
-
(interval_end - interval_begin + 1).times do |n|
|
57
|
-
array.push("0x#{(interval_begin + n).to_s(16).rjust(8, '0')}")
|
58
|
-
end
|
59
|
-
array
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.id_parse_variant2(args)
|
63
|
-
string = args[:string]
|
64
|
-
["0x" + string.downcase]
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.id_parse_variant3(args)
|
68
|
-
string = args[:string]
|
69
|
-
["0x" + string.rjust(8, "0")]
|
70
|
-
end
|
71
|
-
|
72
|
-
def network(id)
|
73
|
-
normal_ids = id(id)
|
74
|
-
if normal_ids
|
75
|
-
normal_id = normal_ids[0]
|
76
|
-
return id[2..5].to_i(16).to_s(10)
|
77
|
-
end
|
78
|
-
false
|
79
|
-
end
|
80
|
-
|
81
|
-
module_function :id, :network
|
82
|
-
|
83
|
-
class VipnetConfig
|
84
|
-
def ==(other)
|
85
|
-
res = true
|
86
|
-
@props.each do |prop|
|
87
|
-
res = res && self.send(prop.to_s) == other.send(prop.to_s)
|
88
|
-
end
|
89
|
-
res
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
class Iplirconf < VipnetConfig
|
94
|
-
PROPS = [:content, :id, :sections]
|
95
|
-
attr_accessor *PROPS, :last_error
|
96
|
-
private_constant :PROPS
|
97
|
-
|
98
|
-
def initialize(*args)
|
99
|
-
@props = PROPS
|
100
|
-
unless args.size == 1
|
101
|
-
return false
|
102
|
-
end
|
103
|
-
args = args[0]
|
104
|
-
if args.class == String
|
105
|
-
@content = args
|
106
|
-
elsif args.class == Hash
|
107
|
-
@content = args[:content]
|
108
|
-
end
|
109
|
-
# remove comments
|
110
|
-
content_nc = @content.gsub(/^#.*\n/, "")
|
111
|
-
# remove ending
|
112
|
-
@sections = Hash.new
|
113
|
-
adapter_position = content_nc.index("[adapter]")
|
114
|
-
unless adapter_position
|
115
|
-
@last_error = "unable to parse iplirconf (no [adapter] section)"
|
116
|
-
return false
|
117
|
-
end
|
118
|
-
# prepare for split
|
119
|
-
content_nc = content_nc[0..(adapter_position - 2)]
|
120
|
-
content_nc = "\n" + content_nc
|
121
|
-
content_nc.split("\n[id]\n").reject{ |t| t.empty? }.each_with_index do |section_content, i|
|
122
|
-
tmp_section = Hash.new
|
123
|
-
props = {
|
124
|
-
:single => [
|
125
|
-
:id, :name, :filterdefault, :tunnel,
|
126
|
-
:firewallip, :port, :proxyid, :dynamic_timeout, :accessip,
|
127
|
-
:usefirewall, :fixfirewall, :virtualip, :version,
|
128
|
-
],
|
129
|
-
:multi => [:ip, :filterudp, :filtertcp]
|
130
|
-
}
|
131
|
-
props.each do |type, props|
|
132
|
-
props.each do |prop|
|
133
|
-
get_section_param({ prop: prop, section: tmp_section, content: section_content, type: type, opts: args })
|
134
|
-
end
|
135
|
-
end
|
136
|
-
# self section id
|
137
|
-
@id = tmp_section[:id] if i == 0
|
138
|
-
@sections[tmp_section[:id]] = tmp_section
|
139
|
-
end
|
140
|
-
true
|
141
|
-
end
|
142
|
-
|
143
|
-
def get_section_param(args)
|
144
|
-
opts = {} if args[:opts].class == String
|
145
|
-
opts = args[:opts] if args[:opts].class == Hash
|
146
|
-
value_regexp = Regexp.new("^#{args[:prop].to_s}=\s(.*)$")
|
147
|
-
if args[:type] == :multi
|
148
|
-
tmp_array = Array.new
|
149
|
-
args[:content].each_line do |line|
|
150
|
-
value = line[value_regexp, 1]
|
151
|
-
tmp_array.push(value) if value
|
152
|
-
end
|
153
|
-
unless tmp_array.empty?
|
154
|
-
tmp_array = tmp_array.to_s if opts[:arrays_to_s]
|
155
|
-
args[:section][args[:prop]] = tmp_array
|
156
|
-
end
|
157
|
-
elsif args[:type] == :single
|
158
|
-
value = args[:content][value_regexp, 1]
|
159
|
-
args[:section][args[:prop]] = value if value
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
private :get_section_param
|
164
|
-
end
|
165
|
-
|
166
|
-
class Nodename < VipnetConfig
|
167
|
-
PROPS = [:content, :records]
|
168
|
-
attr_accessor *PROPS, :last_error
|
169
|
-
private_constant :PROPS
|
170
|
-
|
171
|
-
def initialize(*args)
|
172
|
-
@props = PROPS
|
173
|
-
unless args.size == 1
|
174
|
-
return false
|
175
|
-
end
|
176
|
-
@content = args[0]
|
177
|
-
lines = content.force_encoding("cp866").encode("utf-8", replace: nil).split("\r\n")
|
178
|
-
if lines.size == 0
|
179
|
-
@last_error = "error parsing nodename"
|
180
|
-
return false
|
181
|
-
end
|
182
|
-
@records = Hash.new
|
183
|
-
lines.each do |line|
|
184
|
-
tmp_record = Hash.new
|
185
|
-
tmp_record = get_record_params(line)
|
186
|
-
tmp_record[:name].rstrip!
|
187
|
-
tmp_record[:enabled] = { "1" => true, "0" => false }[tmp_record[:enabled]]
|
188
|
-
tmp_record[:category] = { "A" => :client, "S" => :server, "G" => :group }[tmp_record[:category]]
|
189
|
-
@records[VipnetParser::id(tmp_record[:id])[0]] = tmp_record.reject { |k, _| k == :id }
|
190
|
-
end
|
191
|
-
true
|
192
|
-
end
|
193
|
-
|
194
|
-
def get_record_params(line)
|
195
|
-
record_regexp = /^
|
196
|
-
(?<name>.{50})\s
|
197
|
-
(?<enabled>[01])\s
|
198
|
-
(?<category>[ASG])\s
|
199
|
-
[0-9A-F]{8}
|
200
|
-
(?<server_number>[0-9A-F]{4})
|
201
|
-
(?<abonent_number>[0-9A-F]{4})\s
|
202
|
-
(?<id>[0-9A-F]{8})
|
203
|
-
$/x
|
204
|
-
match = record_regexp.match(line)
|
205
|
-
names = match.names.map { |name| name.to_sym }
|
206
|
-
# https://gist.github.com/flarnie/6221219
|
207
|
-
return Hash[names.zip(match.captures)]
|
208
|
-
end
|
209
|
-
|
210
|
-
private :get_record_params
|
211
|
-
end
|
212
|
-
end
|
1
|
+
require "vipnet_parser/strings"
|
2
|
+
require "vipnet_parser/iplirconf"
|
3
|
+
require "vipnet_parser/nodename"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vipnet_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
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-
|
11
|
+
date: 2016-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Parses ViPNet IDs, iplir.conf and other files
|
14
14
|
email: ntcomp12@gmail.com
|