Skip to content

Commit

Permalink
updater & find_redundant: rewrite in ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
felixonmars committed Oct 30, 2022
1 parent a11f555 commit b3e5feb
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 161 deletions.
2 changes: 1 addition & 1 deletion autoupdater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

if v.check_domain_verbose(domain, enable_cdnlist: false, show_green: true)
Filelock '.git.lock' do
puts `./updater.py -a #{domain}`
puts `./updater.rb -a #{domain}`
puts `git commit -S -am "accelerated-domains: add #{domain}"` if $?.success?
puts `./update-local` if $?.success?
end
Expand Down
77 changes: 0 additions & 77 deletions find_redundant.py

This file was deleted.

81 changes: 81 additions & 0 deletions find_redundant.rb
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
83 changes: 0 additions & 83 deletions updater.py

This file was deleted.

87 changes: 87 additions & 0 deletions updater.rb
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)

0 comments on commit b3e5feb

Please sign in to comment.