2020-10-12 10:33:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rubygems/package'
|
2023-05-24 05:55:40 -04:00
|
|
|
require_relative 'base'
|
2020-10-12 10:33:49 -04:00
|
|
|
|
2023-05-23 10:08:26 -04:00
|
|
|
module Mastodon::CLI
|
2023-05-24 05:55:40 -04:00
|
|
|
class IpBlocks < Base
|
2023-05-09 08:46:00 -04:00
|
|
|
option :severity, required: true, enum: %w(no_access sign_up_requires_approval sign_up_block), desc: 'Severity of the block'
|
2020-10-12 10:33:49 -04:00
|
|
|
option :comment, aliases: [:c], desc: 'Optional comment'
|
|
|
|
option :duration, aliases: [:d], type: :numeric, desc: 'Duration of the block in seconds'
|
|
|
|
option :force, type: :boolean, aliases: [:f], desc: 'Overwrite existing blocks'
|
|
|
|
desc 'add IP...', 'Add one or more IP blocks'
|
|
|
|
long_desc <<-LONG_DESC
|
|
|
|
Add one or more IP blocks. You can use CIDR syntax to
|
|
|
|
block IP ranges. You must specify --severity of the block. All
|
|
|
|
options will be copied for each IP block you create in one command.
|
|
|
|
|
|
|
|
You can add a --comment. If an IP block already exists for one of
|
|
|
|
the provided IPs, it will be skipped unless you use the --force
|
|
|
|
option to overwrite it.
|
|
|
|
LONG_DESC
|
|
|
|
def add(*addresses)
|
2024-01-26 03:53:44 -05:00
|
|
|
fail_with_message 'No IP(s) given' if addresses.empty?
|
2020-10-12 10:33:49 -04:00
|
|
|
|
|
|
|
skipped = 0
|
|
|
|
processed = 0
|
|
|
|
failed = 0
|
|
|
|
|
|
|
|
addresses.each do |address|
|
2023-05-09 08:45:47 -04:00
|
|
|
unless valid_ip_address?(address)
|
|
|
|
say("#{address} is invalid", :red)
|
|
|
|
failed += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2020-10-12 10:33:49 -04:00
|
|
|
ip_block = IpBlock.find_by(ip: address)
|
|
|
|
|
|
|
|
if ip_block.present? && !options[:force]
|
|
|
|
say("#{address} is already blocked", :yellow)
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
ip_block ||= IpBlock.new(ip: address)
|
|
|
|
|
|
|
|
ip_block.severity = options[:severity]
|
2020-12-02 15:19:41 -05:00
|
|
|
ip_block.comment = options[:comment] if options[:comment].present?
|
2020-10-12 10:33:49 -04:00
|
|
|
ip_block.expires_in = options[:duration]
|
|
|
|
|
|
|
|
if ip_block.save
|
|
|
|
processed += 1
|
|
|
|
else
|
|
|
|
say("#{address} could not be saved", :red)
|
|
|
|
failed += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
say("Added #{processed}, skipped #{skipped}, failed #{failed}", color(processed, failed))
|
|
|
|
end
|
|
|
|
|
|
|
|
option :force, type: :boolean, aliases: [:f], desc: 'Remove blocks for ranges that cover given IP(s)'
|
|
|
|
desc 'remove IP...', 'Remove one or more IP blocks'
|
|
|
|
long_desc <<-LONG_DESC
|
|
|
|
Remove one or more IP blocks. Normally, only exact matches are removed. If
|
|
|
|
you want to ensure that all of the given IP addresses are unblocked, you
|
|
|
|
can use --force which will also remove any blocks for IP ranges that would
|
|
|
|
cover the given IP(s).
|
|
|
|
LONG_DESC
|
|
|
|
def remove(*addresses)
|
2024-01-26 03:53:44 -05:00
|
|
|
fail_with_message 'No IP(s) given' if addresses.empty?
|
2020-10-12 10:33:49 -04:00
|
|
|
|
|
|
|
processed = 0
|
|
|
|
skipped = 0
|
|
|
|
|
|
|
|
addresses.each do |address|
|
2023-05-09 08:45:47 -04:00
|
|
|
unless valid_ip_address?(address)
|
|
|
|
say("#{address} is invalid", :yellow)
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2023-02-18 17:09:40 -05:00
|
|
|
ip_blocks = if options[:force]
|
|
|
|
IpBlock.where('ip >>= ?', address)
|
|
|
|
else
|
|
|
|
IpBlock.where('ip <<= ?', address)
|
|
|
|
end
|
2020-10-12 10:33:49 -04:00
|
|
|
|
|
|
|
if ip_blocks.empty?
|
|
|
|
say("#{address} is not yet blocked", :yellow)
|
|
|
|
skipped += 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
ip_blocks.in_batches.destroy_all
|
|
|
|
processed += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
say("Removed #{processed}, skipped #{skipped}", color(processed, 0))
|
|
|
|
end
|
|
|
|
|
|
|
|
option :format, aliases: [:f], enum: %w(plain nginx), desc: 'Format of the output'
|
|
|
|
desc 'export', 'Export blocked IPs'
|
|
|
|
long_desc <<-LONG_DESC
|
|
|
|
Export blocked IPs. Different formats are supported for usage with other
|
|
|
|
tools. Only blocks with no_access severity are returned.
|
|
|
|
LONG_DESC
|
|
|
|
def export
|
|
|
|
IpBlock.where(severity: :no_access).find_each do |ip_block|
|
|
|
|
case options[:format]
|
|
|
|
when 'nginx'
|
2023-04-30 00:48:16 -04:00
|
|
|
say "deny #{ip_block.ip}/#{ip_block.ip.prefix};"
|
2020-10-12 10:33:49 -04:00
|
|
|
else
|
2023-04-30 00:48:16 -04:00
|
|
|
say "#{ip_block.ip}/#{ip_block.ip.prefix}"
|
2020-10-12 10:33:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def color(processed, failed)
|
|
|
|
if !processed.zero? && failed.zero?
|
|
|
|
:green
|
|
|
|
elsif failed.zero?
|
|
|
|
:yellow
|
|
|
|
else
|
|
|
|
:red
|
|
|
|
end
|
|
|
|
end
|
2023-05-09 08:45:47 -04:00
|
|
|
|
|
|
|
def valid_ip_address?(ip_address)
|
|
|
|
IPAddr.new(ip_address)
|
|
|
|
true
|
|
|
|
rescue IPAddr::InvalidAddressError
|
|
|
|
false
|
|
|
|
end
|
2020-10-12 10:33:49 -04:00
|
|
|
end
|
|
|
|
end
|