Skip to content

Commit

Permalink
updater: improve redundant find logic and add ci for it
Browse files Browse the repository at this point in the history
  • Loading branch information
felixonmars committed Dec 5, 2022
1 parent 15d2c90 commit 840fc72
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 29 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.0'
bundler-cache: true
- name: Run tests
run: bundle exec sus
7 changes: 7 additions & 0 deletions gems.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "https://rubygems.org"

gem "colorize"
gem "concurrent-ruby"
gem "ipaddr"
gem "public_suffix"
gem "sus"
14 changes: 14 additions & 0 deletions test/redundant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require_relative '../verify'

let (:lines) { File.readlines("accelerated-domains.china.conf").filter { |line| !line.empty? } }

it "should find redundant domains" do
expect(CheckRedundant(lines, [], 'qq.com')).to be == false
expect(CheckRedundant(lines, [], 'www.qq.com')).to be == false
expect(CheckRedundant(lines, [], 'qq.cn')).to be == false
expect(CheckRedundant(lines, [], 'www.qq.cn')).to be == false
end

it "should add new domains" do
expect(CheckRedundant(lines, [], 'what.a.wonderful.domain')).to be == "server=/what.a.wonderful.domain/114.114.114.114\n"
end
34 changes: 6 additions & 28 deletions updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'domain_name'
require 'optparse'
require 'ostruct'
require_relative 'verify'

options = OpenStruct.new
options.sort = true
Expand Down Expand Up @@ -37,34 +38,11 @@

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
new_line = CheckRedundant(lines, disabled_lines, domain)
if new_line != false
puts "New domain added: #{domain}"
lines << new_line
changed = true
end
end

Expand Down
30 changes: 29 additions & 1 deletion verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'public_suffix'
require 'resolv'


class ChinaListVerify
def initialize(
dns=nil,
Expand Down Expand Up @@ -224,6 +223,35 @@ def check_domain_list(domain_list, sample: 30, show_green: false, jobs: Concurre
end
end

# Operates on the raw file to preserve commented out lines
def CheckRedundant(lines, disabled_lines, 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}"
return false
elsif disabled_lines.any? { |line| line.start_with? disabled_line }
puts "Domain already disabled: #{domain}"
return false
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}"
return false
elsif disabled_lines.any? { |line| line.start_with? _disabled_line }
puts "Redundant domain already disabled: #{test_domain}"
return false
end
end
end
return new_line
end

if __FILE__ == $0
require 'optparse'
require 'ostruct'
Expand Down

0 comments on commit 840fc72

Please sign in to comment.