forked from felixonmars/dnsmasq-china-list
updater: improve redundant find logic and add ci for it
This commit is contained in:
parent
15d2c90170
commit
840fc72b3c
|
@ -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
|
|
@ -0,0 +1,7 @@
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "colorize"
|
||||||
|
gem "concurrent-ruby"
|
||||||
|
gem "ipaddr"
|
||||||
|
gem "public_suffix"
|
||||||
|
gem "sus"
|
|
@ -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
updater.rb
34
updater.rb
|
@ -2,6 +2,7 @@
|
||||||
require 'domain_name'
|
require 'domain_name'
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
require 'ostruct'
|
require 'ostruct'
|
||||||
|
require_relative 'verify'
|
||||||
|
|
||||||
options = OpenStruct.new
|
options = OpenStruct.new
|
||||||
options.sort = true
|
options.sort = true
|
||||||
|
@ -37,34 +38,11 @@ changed = false
|
||||||
|
|
||||||
options.add.each do |domain|
|
options.add.each do |domain|
|
||||||
domain = DomainName.normalize(domain)
|
domain = DomainName.normalize(domain)
|
||||||
new_line = "server=/#{domain}/114.114.114.114\n"
|
new_line = CheckRedundant(lines, disabled_lines, domain)
|
||||||
disabled_line = "#server=/#{domain}/114.114.114.114"
|
if new_line != false
|
||||||
if lines.include? new_line
|
puts "New domain added: #{domain}"
|
||||||
puts "Domain already exists: #{domain}"
|
lines << new_line
|
||||||
else
|
changed = true
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
30
verify.rb
30
verify.rb
|
@ -6,7 +6,6 @@ require 'ipaddr'
|
||||||
require 'public_suffix'
|
require 'public_suffix'
|
||||||
require 'resolv'
|
require 'resolv'
|
||||||
|
|
||||||
|
|
||||||
class ChinaListVerify
|
class ChinaListVerify
|
||||||
def initialize(
|
def initialize(
|
||||||
dns=nil,
|
dns=nil,
|
||||||
|
@ -224,6 +223,35 @@ class ChinaListVerify
|
||||||
end
|
end
|
||||||
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
|
if __FILE__ == $0
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
require 'ostruct'
|
require 'ostruct'
|
||||||
|
|
Loading…
Reference in New Issue