2018-10-27 21:56:16 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-15 08:24:35 +00:00
|
|
|
require 'concurrent'
|
2018-10-27 21:56:16 +01:00
|
|
|
require_relative '../../config/boot'
|
|
|
|
require_relative '../../config/environment'
|
|
|
|
require_relative 'cli_helper'
|
|
|
|
|
|
|
|
module Mastodon
|
|
|
|
class DomainsCLI < Thor
|
|
|
|
def self.exit_on_failure?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
option :dry_run, type: :boolean
|
2019-07-30 10:10:46 +01:00
|
|
|
option :whitelist_mode, type: :boolean
|
|
|
|
desc 'purge [DOMAIN]', 'Remove accounts from a DOMAIN without a trace'
|
2018-10-27 21:56:16 +01:00
|
|
|
long_desc <<-LONG_DESC
|
|
|
|
Remove all accounts from a given DOMAIN without leaving behind any
|
|
|
|
records. Unlike a suspension, if the DOMAIN still exists in the wild,
|
|
|
|
it means the accounts could return if they are resolved again.
|
2019-07-30 10:10:46 +01:00
|
|
|
|
|
|
|
When the --whitelist-mode option is given, instead of purging accounts
|
|
|
|
from a single domain, all accounts from domains that are not whitelisted
|
|
|
|
are removed from the database.
|
2018-10-27 21:56:16 +01:00
|
|
|
LONG_DESC
|
2019-07-30 10:10:46 +01:00
|
|
|
def purge(domain = nil)
|
2018-10-27 21:56:16 +01:00
|
|
|
removed = 0
|
|
|
|
dry_run = options[:dry_run] ? ' (DRY RUN)' : ''
|
|
|
|
|
2019-07-30 10:10:46 +01:00
|
|
|
scope = begin
|
|
|
|
if options[:whitelist_mode]
|
|
|
|
Account.remote.where.not(domain: DomainAllow.pluck(:domain))
|
|
|
|
elsif domain.present?
|
|
|
|
Account.remote.where(domain: domain)
|
|
|
|
else
|
|
|
|
say('No domain given', :red)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
scope.find_each do |account|
|
2018-12-03 00:32:08 +00:00
|
|
|
SuspendAccountService.new.call(account, destroy: true) unless options[:dry_run]
|
2018-10-27 21:56:16 +01:00
|
|
|
removed += 1
|
|
|
|
say('.', :green, false)
|
|
|
|
end
|
|
|
|
|
2019-05-09 21:03:21 +01:00
|
|
|
DomainBlock.where(domain: domain).destroy_all unless options[:dry_run]
|
2018-10-27 21:56:16 +01:00
|
|
|
|
|
|
|
say
|
|
|
|
say("Removed #{removed} accounts#{dry_run}", :green)
|
2019-05-09 21:03:21 +01:00
|
|
|
|
|
|
|
custom_emojis = CustomEmoji.where(domain: domain)
|
|
|
|
custom_emojis_count = custom_emojis.count
|
|
|
|
custom_emojis.destroy_all unless options[:dry_run]
|
|
|
|
say("Removed #{custom_emojis_count} custom emojis", :green)
|
2018-10-27 21:56:16 +01:00
|
|
|
end
|
2019-01-15 08:24:35 +00:00
|
|
|
|
|
|
|
option :concurrency, type: :numeric, default: 50, aliases: [:c]
|
|
|
|
option :silent, type: :boolean, default: false, aliases: [:s]
|
|
|
|
option :format, type: :string, default: 'summary', aliases: [:f]
|
2019-08-03 18:11:09 +01:00
|
|
|
option :exclude_suspended, type: :boolean, default: false, aliases: [:x]
|
2019-01-15 08:24:35 +00:00
|
|
|
desc 'crawl [START]', 'Crawl all known peers, optionally beginning at START'
|
|
|
|
long_desc <<-LONG_DESC
|
|
|
|
Crawl the fediverse by using the Mastodon REST API endpoints that expose
|
|
|
|
all known peers, and collect statistics from those peers, as long as those
|
|
|
|
peers support those API endpoints. When no START is given, the command uses
|
|
|
|
this server's own database of known peers to seed the crawl.
|
|
|
|
|
|
|
|
The --concurrency (-c) option controls the number of threads performing HTTP
|
|
|
|
requests at the same time. More threads means the crawl may complete faster.
|
|
|
|
|
|
|
|
The --silent (-s) option controls progress output.
|
|
|
|
|
|
|
|
The --format (-f) option controls how the data is displayed at the end. By
|
|
|
|
default (`summary`), a summary of the statistics is returned. The other options
|
|
|
|
are `domains`, which returns a newline-delimited list of all discovered peers,
|
|
|
|
and `json`, which dumps all the aggregated data raw.
|
2019-08-03 18:11:09 +01:00
|
|
|
|
|
|
|
The --exclude-suspended (-x) option means that domains that are suspended
|
|
|
|
instance-wide do not appear in the output and are not included in summaries.
|
|
|
|
This also excludes subdomains of any of those domains.
|
2019-01-15 08:24:35 +00:00
|
|
|
LONG_DESC
|
|
|
|
def crawl(start = nil)
|
2019-08-03 18:11:09 +01:00
|
|
|
stats = Concurrent::Hash.new
|
|
|
|
processed = Concurrent::AtomicFixnum.new(0)
|
|
|
|
failed = Concurrent::AtomicFixnum.new(0)
|
|
|
|
start_at = Time.now.to_f
|
|
|
|
seed = start ? [start] : Account.remote.domains
|
|
|
|
blocked_domains = Regexp.new('\\.?' + DomainBlock.where(severity: 1).pluck(:domain).join('|') + '$')
|
2019-01-15 08:24:35 +00:00
|
|
|
|
|
|
|
pool = Concurrent::ThreadPoolExecutor.new(min_threads: 0, max_threads: options[:concurrency], idletime: 10, auto_terminate: true, max_queue: 0)
|
|
|
|
|
|
|
|
work_unit = ->(domain) do
|
|
|
|
next if stats.key?(domain)
|
2019-08-03 18:11:09 +01:00
|
|
|
next if options[:exclude_suspended] && domain.match(blocked_domains)
|
|
|
|
|
2019-01-15 08:24:35 +00:00
|
|
|
stats[domain] = nil
|
|
|
|
processed.increment
|
|
|
|
|
|
|
|
begin
|
|
|
|
Request.new(:get, "https://#{domain}/api/v1/instance").perform do |res|
|
|
|
|
next unless res.code == 200
|
|
|
|
stats[domain] = Oj.load(res.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
Request.new(:get, "https://#{domain}/api/v1/instance/peers").perform do |res|
|
|
|
|
next unless res.code == 200
|
|
|
|
|
|
|
|
Oj.load(res.to_s).reject { |peer| stats.key?(peer) }.each do |peer|
|
|
|
|
pool.post(peer, &work_unit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Request.new(:get, "https://#{domain}/api/v1/instance/activity").perform do |res|
|
|
|
|
next unless res.code == 200
|
|
|
|
stats[domain]['activity'] = Oj.load(res.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
say('.', :green, false) unless options[:silent]
|
|
|
|
rescue StandardError
|
|
|
|
failed.increment
|
|
|
|
say('.', :red, false) unless options[:silent]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
seed.each do |domain|
|
|
|
|
pool.post(domain, &work_unit)
|
|
|
|
end
|
|
|
|
|
|
|
|
sleep 20
|
|
|
|
sleep 20 until pool.queue_length.zero?
|
|
|
|
|
|
|
|
pool.shutdown
|
|
|
|
pool.wait_for_termination(20)
|
|
|
|
ensure
|
|
|
|
pool.shutdown
|
|
|
|
|
|
|
|
say unless options[:silent]
|
|
|
|
|
|
|
|
case options[:format]
|
|
|
|
when 'summary'
|
|
|
|
stats_to_summary(stats, processed, failed, start_at)
|
|
|
|
when 'domains'
|
|
|
|
stats_to_domains(stats)
|
|
|
|
when 'json'
|
|
|
|
stats_to_json(stats)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def stats_to_summary(stats, processed, failed, start_at)
|
|
|
|
stats.compact!
|
|
|
|
|
|
|
|
total_domains = stats.size
|
|
|
|
total_users = stats.reduce(0) { |sum, (_key, val)| val.is_a?(Hash) && val['stats'].is_a?(Hash) ? sum + val['stats']['user_count'].to_i : sum }
|
|
|
|
total_active = stats.reduce(0) { |sum, (_key, val)| val.is_a?(Hash) && val['activity'].is_a?(Array) && val['activity'].size > 2 && val['activity'][1].is_a?(Hash) ? sum + val['activity'][1]['logins'].to_i : sum }
|
|
|
|
total_joined = stats.reduce(0) { |sum, (_key, val)| val.is_a?(Hash) && val['activity'].is_a?(Array) && val['activity'].size > 2 && val['activity'][1].is_a?(Hash) ? sum + val['activity'][1]['registrations'].to_i : sum }
|
|
|
|
|
|
|
|
say("Visited #{processed.value} domains, #{failed.value} failed (#{(Time.now.to_f - start_at).round}s elapsed)", :green)
|
|
|
|
say("Total servers: #{total_domains}", :green)
|
|
|
|
say("Total registered: #{total_users}", :green)
|
|
|
|
say("Total active last week: #{total_active}", :green)
|
|
|
|
say("Total joined last week: #{total_joined}", :green)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stats_to_domains(stats)
|
|
|
|
say(stats.keys.join("\n"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def stats_to_json(stats)
|
2019-01-16 14:47:55 +00:00
|
|
|
stats.compact!
|
|
|
|
say(Oj.dump(stats))
|
2019-01-15 08:24:35 +00:00
|
|
|
end
|
2018-10-27 21:56:16 +01:00
|
|
|
end
|
|
|
|
end
|