Skip to content

Instantly share code, notes, and snippets.

@elct9620
Created November 26, 2019 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elct9620/bac5213bd9e09c6367c6996f4cdcfe2e to your computer and use it in GitHub Desktop.
Save elct9620/bac5213bd9e09c6367c6996f4cdcfe2e to your computer and use it in GitHub Desktop.
TWNIC 凍結域名偵測
# frozen_string_literal: true
require 'net/http'
require 'date'
require 'optparse'
require 'json'
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'hirb'
end
# Hirb.enable :pager
module TWNIC
# :nodoc:
class Frozen
class << self
def load
new Net::HTTP.get(URI)
end
def find(&block)
load.find(&block)
end
end
include Enumerable
URI = URI('https://www.twnic.net.tw/dnservice_announce_frozen.php')
TABLE_RULE = %r{<table[^>]*>(.*?)</table>}m.freeze
CELL_RULE = %r{<td[^>]*>(.*?)</td>}m.freeze
DOMAIN_FILTER = /.*\.tw/.freeze
HTML_TAG = /<[^>]*>/.freeze
def initialize(doc)
@items =
doc
.scan(TABLE_RULE)
.join
.scan(CELL_RULE)
.flatten
.map { |text| text.gsub(HTML_TAG, '') }
.each_slice(2)
.select { |domain, _| domain.match(DOMAIN_FILTER) }
.map { |domain, date| { domain: domain.strip, date: Date.parse(date) } }
end
def each(&block)
@items.each(&block)
end
end
end
module Discord
# :nodoc:
class Notify
def initialize(url)
@uri = URI(url)
end
def send(message)
req = Net::HTTP::Post.new(@uri)
req.content_type = 'application/json'
req.body = { text: message }.to_json
Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
http.request req
end
end
end
end
# :nodoc:
class Command < OptionParser
class << self
def parse!
new.parse!
end
end
def initialize
super
on('-f', '--find=DOMAIN', 'Find the domain is frozen') do |domain|
@domain = domain
end
on('-w', '--webhook=URL', 'The notification slack webhook') do |webhook|
@webhook = webhook
@notify = Discord::Notify.new(@webhook)
end
end
def parse!
super
return find if @domain
display
end
private
def find
res = TWNIC::Frozen.find { |item| item[:domain] == @domain }
return not_found if res.nil?
return notify(res) if @webhook
puts "#{@domain} is frozen at #{res[:date]}"
end
def not_found
puts 'Frozen domain not found!'
exit 1
end
def notify(item)
@notify.send("#{item[:domain]} is frozen at #{item[:date]}")
end
def display
Hirb.enable pager: true
extend Hirb::Console
table TWNIC::Frozen.load
end
end
Command.parse!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment