forked from felixonmars/dnsmasq-china-list
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updater & find_redundant: rewrite in ruby
- Loading branch information
1 parent
a11f555
commit b3e5feb
Showing
5 changed files
with
169 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/ruby | ||
|
||
""" Find redundant items in accelerated-domains.china.conf. | ||
e.g. 'bar.foo.com' is redundant for 'foo.com'. | ||
""" | ||
|
||
def load(conf_file) | ||
""" Parse conf file & Prepare data structure | ||
Returns: [ ['abc', 'com'], | ||
['bar', 'foo', 'com'], | ||
... ] | ||
""" | ||
|
||
results = [] | ||
if conf_file.is_a? String | ||
lines = File.readlines(conf_file) | ||
elsif conf_file.is_a? Array | ||
lines = conf_file | ||
end | ||
lines.map do |line| | ||
line = line.chomp | ||
next if line.empty? or line.start_with?('#') | ||
# A domain name is case-insensitive and | ||
# consists of several labels, separated by a full stop | ||
domain_name = line.split('/')[1] | ||
domain_name = domain_name.downcase | ||
domain_labels = domain_name.split('.') | ||
results << domain_labels | ||
end | ||
|
||
# Sort results by domain labels' length | ||
results.sort_by(&:length) | ||
end | ||
|
||
|
||
LEAF = 1 | ||
def find(labelses) | ||
""" Find redundant items by a tree of top-level domain label to sub-level. | ||
`tree` is like { 'com': { 'foo: { 'bar': LEAF }, | ||
'abc': LEAF }, | ||
'org': ... } | ||
""" | ||
redundant = [] | ||
tree = {} | ||
labelses.each do |labels| | ||
domain = labels.join('.') | ||
# Init root node as current node | ||
node = tree | ||
until labels.empty? | ||
label = labels.pop | ||
if node.include? label | ||
# If child node is a LEAF node, | ||
# current domain must be an existed domain or a subdomain of an existed. | ||
if node[label] == LEAF | ||
puts "Redundant found: #{domain} at #{labels.join('.')}" | ||
redundant << domain | ||
break | ||
end | ||
else | ||
# Create a leaf node if current label is last one | ||
if labels.empty? | ||
node[label] = LEAF | ||
# Create a branch node | ||
else | ||
node[label] = {} | ||
end | ||
end | ||
# Iterate to child node | ||
node = node[label] | ||
end | ||
end | ||
redundant | ||
end | ||
|
||
def find_redundant(conf_file) | ||
return find(load(conf_file)) | ||
end | ||
|
||
if __FILE__ == $0 | ||
find_redundant('accelerated-domains.china.conf') | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/ruby | ||
require 'domain_name' | ||
require 'optparse' | ||
require 'ostruct' | ||
|
||
options = OpenStruct.new | ||
options.sort = true | ||
options.file = "accelerated-domains.china.conf" | ||
options.add = [] | ||
options.delete = [] | ||
OptionParser.new do |opts| | ||
opts.banner = "dnsmasq-china-list updater" | ||
|
||
opts.on("-s", "--[no-]sort", "Sort the list (default action)") do |s| | ||
options.sort = s | ||
end | ||
|
||
opts.on("-f", "--file FILE", "Specify the file to update (accelerated-domains.china.conf by default)") do |f| | ||
options.file = f | ||
end | ||
|
||
opts.on("-a", "--add domain1,domain2", Array, "Add domain(s) to the list (implies -s)") do |a| | ||
options.add = a | ||
options.sort = true | ||
end | ||
|
||
opts.on("-d", "--delete domain1,domain2", Array, "Remove domain(s) from the list (implies -s)") do |d| | ||
options.delete = d | ||
options.sort = true | ||
end | ||
end.parse! | ||
|
||
lines = File.readlines(options.file).filter { |line| !line.empty? } | ||
disabled_lines = lines.filter { |line| line.start_with?("#") } | ||
|
||
changed = false | ||
|
||
options.add.each do |domain| | ||
domain = DomainName.normalize(domain) | ||
new_line = "server=/#{domain}/114.114.114.114\n" | ||
disabled_line = "#server=/#{domain}/114.114.114.114" | ||
if lines.include? new_line | ||
puts "Domain already exists: #{domain}" | ||
else | ||
if disabled_lines.any? { |line| line.start_with? disabled_line } | ||
puts "Domain already disabled: #{domain}" | ||
else | ||
# Check for duplicates | ||
test_domain = domain | ||
while test_domain.include? '.' | ||
test_domain = test_domain.partition('.').last | ||
_new_line = "server=/#{test_domain}/114.114.114.114\n" | ||
_disabled_line = "#server=/#{test_domain}/114.114.114.114" | ||
if lines.include? _new_line | ||
puts "Redundant domain already exists: #{test_domain}" | ||
break | ||
elsif disabled_lines.any? { |line| line.start_with? _disabled_line } | ||
puts "Redundant domain already disabled: #{test_domain}" | ||
break | ||
end | ||
end | ||
next if test_domain.include? '.' | ||
|
||
puts "New domain added: #{domain}" | ||
lines << new_line | ||
changed = true | ||
end | ||
end | ||
end | ||
|
||
options.delete.each do |domain| | ||
domain = DomainName.normalize(domain) | ||
target_line = "server=/#{domain}/114.114.114.114\n" | ||
unless lines.include? target_line | ||
puts "Failed to remove domain #{domain}: not found." | ||
else | ||
puts "Domain removed: #{domain}" | ||
lines.delete(target_line) | ||
changed = true | ||
end | ||
end | ||
|
||
fail "No changes made." if (options.add.length || options.delete.length) && !changed | ||
|
||
lines.sort_by! { |x| x.delete_prefix("#") } if options.sort | ||
|
||
File.write(options.file, lines.join) |