vipnet_parser 0.0.5

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/vipnet_parser.rb +148 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 412976a93107392b94f27890186cc98f02930536
4
+ data.tar.gz: 1717777e8beb43cd21e4853011b3901dffd38e48
5
+ SHA512:
6
+ metadata.gz: f242154edba17fce0881e5a9647ead0282ab7be9847f7c0544e3e98cf7b5406035b2a1c2beade5fac983c530e53b0980194c2eee528fea28c524a9e74126b396
7
+ data.tar.gz: 1b90c4dacb6b683cb2944d146f85ca89833e794d571f0d8e7212e0557d37b66cca632b1c15be9d6cec8583af324e19fc93d98a6914346cb05cc1ccdedc1fe8ef
@@ -0,0 +1,148 @@
1
+ class VipnetParser
2
+ def ==(other)
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(string)
11
+ if string =~ /(.*)0x([0-9a-f]{1,8})-0x([0-9a-f]{1,8})(.*)/
12
+ interval_begin = Regexp.last_match(2).to_i(16)
13
+ interval_end = Regexp.last_match(3).to_i(16)
14
+ return [] if interval_end < interval_begin
15
+ vipnet_ids = Array.new
16
+ (interval_end - interval_begin + 1).times do |n|
17
+ vipnet_ids.push("0x#{(interval_begin + n).to_s(16).rjust(8, '0')}")
18
+ end
19
+ return vipnet_ids
20
+ end
21
+ if string =~ /(.*)([0-9A-F]{8})(.*)/
22
+ return ["0x" + Regexp.last_match(2).downcase]
23
+ end
24
+ if string =~ /(.*)0x([0-9a-f]{1,8})(.*)/
25
+ return ["0x" + Regexp.last_match(2).to_s.rjust(8, "0")]
26
+ end
27
+ false
28
+ end
29
+
30
+ def self.network(id)
31
+ normal_ids = id(id)
32
+ if normal_ids
33
+ normal_id = normal_ids[0]
34
+ return id[2..5].to_i(16).to_s(10)
35
+ end
36
+ false
37
+ end
38
+ end
39
+
40
+ class Iplirconf < VipnetParser
41
+ PROPS = [:content, :id, :sections]
42
+ attr_accessor *PROPS, :last_error
43
+ private_constant :PROPS
44
+
45
+ def initialize(*args)
46
+ @props = PROPS
47
+ unless args.size == 1
48
+ return false
49
+ end
50
+ @content = args[0]
51
+ # remove comments
52
+ content_nc = @content.gsub(/^#.*\n/, "")
53
+ # remove ending
54
+ @sections = Hash.new
55
+ adapter_position = content_nc.index("[adapter]")
56
+ unless adapter_position
57
+ @last_error = "unable to parse iplirconf (no [adapter] section)"
58
+ return false
59
+ end
60
+ # prepare for split
61
+ content_nc = content_nc[0..(adapter_position - 2)]
62
+ content_nc = "\n" + content_nc
63
+ content_nc.split("\n[id]\n").reject{ |t| t.empty? }.each_with_index do |section_content, i|
64
+ tmp_section = Hash.new
65
+ props = {
66
+ :single => [
67
+ :id, :name, :filterdefault, :tunnel,
68
+ :firewallip, :port, :proxyid, :dynamic_timeout, :accessip,
69
+ :usefirewall, :fixfirewall, :virtualip, :version,
70
+ ],
71
+ :multi => [:ip, :filterudp, :filtertcp]
72
+ }
73
+ props.each do |type, props|
74
+ props.each do |prop|
75
+ get_section_param({ prop: prop, section: tmp_section, content: section_content, type: type })
76
+ end
77
+ end
78
+ # self section id
79
+ @id = tmp_section[:id] if i == 0
80
+ @sections[tmp_section[:id]] = tmp_section
81
+ end
82
+ true
83
+ end
84
+
85
+ def get_section_param(args)
86
+ value_regexp = Regexp.new("^#{args[:prop].to_s}=\s(.*)$")
87
+ if args[:type] == :multi
88
+ tmp_array = Array.new
89
+ args[:content].each_line do |line|
90
+ value = line[value_regexp, 1]
91
+ tmp_array.push(value) if value
92
+ end
93
+ args[:section][args[:prop]] = tmp_array unless tmp_array.empty?
94
+ elsif args[:type] == :single
95
+ value = args[:content][value_regexp, 1]
96
+ args[:section][args[:prop]] = value if value
97
+ end
98
+ end
99
+
100
+ private :get_section_param
101
+ end
102
+
103
+ class Nodename < VipnetParser
104
+ PROPS = [:content, :records]
105
+ attr_accessor *PROPS, :last_error
106
+ private_constant :PROPS
107
+
108
+ def initialize(*args)
109
+ @props = PROPS
110
+ unless args.size == 1
111
+ return false
112
+ end
113
+ @content = args[0]
114
+ lines = content.force_encoding("cp866").encode("utf-8", replace: nil).split("\r\n")
115
+ if lines.size == 0
116
+ @last_error = "error parsing nodename"
117
+ return false
118
+ end
119
+ @records = Hash.new
120
+ lines.each do |line|
121
+ tmp_record = Hash.new
122
+ tmp_record = get_record_params(line)
123
+ tmp_record[:name].rstrip!
124
+ tmp_record[:enabled] = { "1" => true, "0" => false }[tmp_record[:enabled]]
125
+ tmp_record[:category] = { "A" => :client, "S" => :server, "G" => :group }[tmp_record[:category]]
126
+ @records[tmp_record[:id]] = tmp_record
127
+ end
128
+ true
129
+ end
130
+
131
+ def get_record_params(line)
132
+ record_regexp = /^
133
+ (?<name>.{50})\s
134
+ (?<enabled>[01])\s
135
+ (?<category>[ASG])\s
136
+ [0-9A-F]{8}
137
+ (?<server_number>[0-9A-F]{4})
138
+ (?<abonent_number>[0-9A-F]{4})\s
139
+ (?<id>[0-9A-F]{8})
140
+ $/x
141
+ match = record_regexp.match(line)
142
+ names = match.names.map { |name| name.to_sym }
143
+ # https://gist.github.com/flarnie/6221219
144
+ return Hash[names.zip(match.captures)]
145
+ end
146
+
147
+ private :get_record_params
148
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vipnet_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Morozov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Parses ViPNet strings like 'something 1A0EABCD something' and returns
14
+ 0x1a0eabcd (and stuff).
15
+ email: ntcomp12@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/vipnet_parser.rb
21
+ homepage: https://github.com/kengho/vipnet_parser
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: ViPNet strings parser
45
+ test_files: []