vipnet_parser 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f043e287cec988e0e3892e9046c588f81a1a2bde
4
- data.tar.gz: 9eedaba5195eebc282099329486eceba82bca783
3
+ metadata.gz: 226c8bad8cea85b92007b578658ddd7e7a54e50d
4
+ data.tar.gz: a8b452e88b132fc3904ebb54244c852f42f81809
5
5
  SHA512:
6
- metadata.gz: 184ee79a3e19bcf31b990c5d5e29624c84c36fc9cb5f73b9e61c09fb28c0ea474918a2bccb5145cac848c0ad1d41c848d91bbb2a424628143f8b4fa83ee6824c
7
- data.tar.gz: 58bc03342eb484ce873104610e87ee43559daa9a08bce1fc010504a692cde78ec4ed97dc0bc99f7cc4e9589fdbede18fcaa84d5b80e4b34233815af65e1257f8
6
+ metadata.gz: 34be58ee030ef22c9a618369d0ad27e6d9165c9f87a0a6829e0367d92a77dfc589a606b3437afa15601900a7d73ec78e60a064cede8399ced1d8da6ac2767604
7
+ data.tar.gz: 2b667aaad8eccd2f8c562ab63a611e3410cc8718aaed1299aa4079782ee0bb9d966bf9e8f91f48a0b1a2ce5ab4600d98d2ea3e5dfeea87208b6021dc1d690eac
@@ -0,0 +1,97 @@
1
+ require "vipnet_parser/vipnet_config"
2
+
3
+ module VipnetParser
4
+ class Iplirconf < VipnetConfig
5
+ attr_accessor :string, :hash
6
+
7
+ def initialize(iplirconf_file)
8
+ @string = iplirconf_file
9
+ end
10
+
11
+ def parse(format = :hash, encoding = "koi8-r")
12
+ # change encoding to utf8 and remove comments
13
+ string = self.string
14
+ .force_encoding(encoding)
15
+ .encode("utf-8")
16
+ .gsub(/^#.*\n/, "")
17
+ .gsub(/^;.*\n/, "")
18
+
19
+ # "[id]something[server]something".split(/(?=\[.+\])/)
20
+ # => ["[id]something", "[server]something"]
21
+ string = string.split(/(?=\[.+\])/)
22
+
23
+ # ["[id]something1", "[server]something2"]
24
+ # => [
25
+ # { name: :id, content: "something1" },
26
+ # { name: :server, content: "something2" },
27
+ # ]
28
+ sections = string.map do |section|
29
+ section =~ /\[(?<section_name>.+)\]\n(?<section_content>.*)/m
30
+ {
31
+ name: Regexp.last_match[:section_name].to_sym,
32
+ content: Regexp.last_match[:section_content],
33
+ }
34
+ end
35
+
36
+ case format
37
+ when :hash
38
+ @hash = {}
39
+ hash_keys = {
40
+ id: :id,
41
+ adapter: :name,
42
+ }
43
+
44
+ sections.each do |section|
45
+ @hash[section[:name]] ||= {}
46
+ hash_key = hash_keys[section[:name]]
47
+ if hash_key
48
+ @hash[section[:name]][:hash_key] ||= hash_key
49
+ hash, current_key = _section_hash(section[:content], hash_key)
50
+ @hash[section[:name]][current_key] = hash
51
+ else
52
+ hash, _ = _section_hash(section[:content])
53
+ @hash[section[:name]] = hash
54
+ end
55
+ end
56
+
57
+ # :servers => { :server => ["0x1a0e000a, coordinator1"] }
58
+ # => :servers => ["0x1a0e000a, coordinator1"]
59
+ @hash[:servers] = @hash[:servers][:server] || nil
60
+
61
+ return @hash
62
+ end
63
+ end
64
+
65
+ def _section_hash(section_content, hash_key = nil)
66
+ hash = {}
67
+
68
+ section_content.split("\n").each do |line|
69
+ if line =~ /(?<prop>.*)=\s(?<value>.*)/
70
+ prop = Regexp.last_match[:prop].to_sym
71
+ value = Regexp.last_match[:value]
72
+
73
+ # array-type props
74
+ if [:ip, :filterudp, :filtertcp, :server].include?(prop)
75
+ if hash[prop]
76
+ hash[prop].push(value)
77
+ else
78
+ hash[prop] = [value]
79
+ end
80
+ else
81
+ hash[prop] = value
82
+ end
83
+ end
84
+ end
85
+
86
+ if hash_key && hash[hash_key]
87
+ current_key = hash[hash_key]
88
+ hash.delete(hash_key)
89
+ return [hash, current_key]
90
+ else
91
+ return hash
92
+ end
93
+ end
94
+
95
+ private :_section_hash
96
+ end
97
+ end
@@ -0,0 +1,54 @@
1
+ require "vipnet_parser/vipnet_config"
2
+
3
+ module VipnetParser
4
+ class Nodename < VipnetConfig
5
+ attr_accessor :string, :hash
6
+
7
+ def initialize(nodename_file)
8
+ @string = nodename_file
9
+ end
10
+
11
+ def parse(format = :hash, encoding = "cp866")
12
+ # change encoding to utf8
13
+ string = self.string
14
+ .force_encoding(encoding)
15
+ .encode("utf-8", replace: nil)
16
+
17
+ case format
18
+ when :hash
19
+ @hash = {}
20
+
21
+ string.split("\r\n").each do |line|
22
+ record = _record_hash(line)
23
+ record[:name].rstrip!
24
+ record[:enabled] = { "1" => true, "0" => false }[record[:enabled]]
25
+ record[:category] = { "A" => :client, "S" => :server, "G" => :group }[record[:category]]
26
+ normal_id = VipnetParser::id(record[:id]).first
27
+ record.delete(:id)
28
+ @hash[normal_id] = record
29
+ end
30
+
31
+ return @hash
32
+ end
33
+ end
34
+
35
+ def _record_hash(line)
36
+ record_regexp = /^
37
+ (?<name>.{50})\s
38
+ (?<enabled>[01])\s
39
+ (?<category>[ASG])\s
40
+ [0-9A-F]{8}
41
+ (?<server_number>[0-9A-F]{4})
42
+ (?<abonent_number>[0-9A-F]{4})\s
43
+ (?<id>[0-9A-F]{8})
44
+ $/x
45
+ match = record_regexp.match(line)
46
+ names = match.names.map { |name| name.to_sym }
47
+
48
+ # https://gist.github.com/flarnie/6221219
49
+ return Hash[names.zip(match.captures)]
50
+ end
51
+
52
+ private :_record_hash
53
+ end
54
+ end
@@ -0,0 +1,82 @@
1
+ module VipnetParser
2
+ def id(args)
3
+ if args.class == String
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
+ end
@@ -0,0 +1,4 @@
1
+ module VipnetParser
2
+ class VipnetConfig
3
+ end
4
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vipnet_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Morozov
@@ -17,6 +17,10 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/vipnet_parser.rb
20
+ - lib/vipnet_parser/iplirconf.rb
21
+ - lib/vipnet_parser/nodename.rb
22
+ - lib/vipnet_parser/strings.rb
23
+ - lib/vipnet_parser/vipnet_config.rb
20
24
  homepage: https://github.com/kengho/vipnet_parser
21
25
  licenses:
22
26
  - MIT