You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.5 KiB
132 lines
3.5 KiB
4 years ago
|
# frozen_string_literal: true
|
||
|
|
||
|
require 'concurrent'
|
||
2 years ago
|
require_relative '../../../config/boot'
|
||
|
require_relative '../../../config/environment'
|
||
|
require_relative 'helper'
|
||
4 years ago
|
|
||
2 years ago
|
module Mastodon::CLI
|
||
|
class EmailDomainBlocks < Thor
|
||
|
include Helper
|
||
4 years ago
|
|
||
|
def self.exit_on_failure?
|
||
|
true
|
||
|
end
|
||
|
|
||
4 years ago
|
desc 'list', 'List blocked e-mail domains'
|
||
4 years ago
|
def list
|
||
|
EmailDomainBlock.where(parent_id: nil).order(id: 'DESC').find_each do |entry|
|
||
|
say(entry.domain.to_s, :white)
|
||
4 years ago
|
|
||
4 years ago
|
EmailDomainBlock.where(parent_id: entry.id).order(id: 'DESC').find_each do |child|
|
||
|
say(" #{child.domain}", :cyan)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
option :with_dns_records, type: :boolean
|
||
4 years ago
|
desc 'add DOMAIN...', 'Block e-mail domain(s)'
|
||
4 years ago
|
long_desc <<-LONG_DESC
|
||
4 years ago
|
Blocking an e-mail domain prevents users from signing up
|
||
|
with e-mail addresses from that domain. You can provide one or
|
||
|
multiple domains to the command.
|
||
|
|
||
|
When the --with-dns-records option is given, an attempt to resolve the
|
||
3 years ago
|
given domains' MX records will be made and the results will also be blocked.
|
||
|
This can be helpful if you are blocking an e-mail server that has many
|
||
|
different domains pointing to it as it allows you to essentially block
|
||
4 years ago
|
it at the root.
|
||
4 years ago
|
LONG_DESC
|
||
|
def add(*domains)
|
||
|
if domains.empty?
|
||
|
say('No domain(s) given', :red)
|
||
|
exit(1)
|
||
|
end
|
||
|
|
||
|
skipped = 0
|
||
|
processed = 0
|
||
|
|
||
|
domains.each do |domain|
|
||
|
if EmailDomainBlock.where(domain: domain).exists?
|
||
|
say("#{domain} is already blocked.", :yellow)
|
||
|
skipped += 1
|
||
|
next
|
||
|
end
|
||
|
|
||
3 years ago
|
other_domains = []
|
||
|
if options[:with_dns_records]
|
||
|
Resolv::DNS.open do |dns|
|
||
|
dns.timeouts = 5
|
||
|
other_domains = dns.getresources(@email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a
|
||
4 years ago
|
end
|
||
|
end
|
||
|
|
||
3 years ago
|
email_domain_block = EmailDomainBlock.new(domain: domain, other_domains: other_domains)
|
||
|
email_domain_block.save!
|
||
|
processed += 1
|
||
|
|
||
|
(email_domain_block.other_domains || []).uniq.each do |hostname|
|
||
4 years ago
|
another_email_domain_block = EmailDomainBlock.new(domain: hostname, parent: email_domain_block)
|
||
4 years ago
|
|
||
4 years ago
|
if EmailDomainBlock.where(domain: hostname).exists?
|
||
|
say("#{hostname} is already blocked.", :yellow)
|
||
|
skipped += 1
|
||
|
next
|
||
|
end
|
||
4 years ago
|
|
||
4 years ago
|
another_email_domain_block.save!
|
||
|
processed += 1
|
||
|
end
|
||
|
end
|
||
|
|
||
|
say("Added #{processed}, skipped #{skipped}", color(processed, 0))
|
||
|
end
|
||
|
|
||
4 years ago
|
desc 'remove DOMAIN...', 'Remove e-mail domain blocks'
|
||
4 years ago
|
def remove(*domains)
|
||
|
if domains.empty?
|
||
|
say('No domain(s) given', :red)
|
||
|
exit(1)
|
||
|
end
|
||
|
|
||
|
skipped = 0
|
||
|
processed = 0
|
||
|
failed = 0
|
||
|
|
||
|
domains.each do |domain|
|
||
|
entry = EmailDomainBlock.find_by(domain: domain)
|
||
4 years ago
|
|
||
4 years ago
|
if entry.nil?
|
||
|
say("#{domain} is not yet blocked.", :yellow)
|
||
|
skipped += 1
|
||
|
next
|
||
|
end
|
||
|
|
||
|
children_count = EmailDomainBlock.where(parent_id: entry.id).count
|
||
|
result = entry.destroy
|
||
4 years ago
|
|
||
4 years ago
|
if result
|
||
4 years ago
|
processed += children_count + 1
|
||
4 years ago
|
else
|
||
4 years ago
|
say("#{domain} could not be unblocked.", :red)
|
||
4 years ago
|
failed += 1
|
||
|
end
|
||
|
end
|
||
|
|
||
|
say("Removed #{processed}, skipped #{skipped}, failed #{failed}", color(processed, failed))
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def color(processed, failed)
|
||
|
if !processed.zero? && failed.zero?
|
||
|
:green
|
||
|
elsif failed.zero?
|
||
|
:yellow
|
||
|
else
|
||
|
:red
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|