Commit 7c23df47 by Ivan Lan

Init

parents
source 'https://gems.ruby-china.com'
# Specify your gem's dependencies in shotengai.gemspec
gemspec
class CreateIpWhitelistList < ActiveRecord::Migration[5.1]
def change
create_table :ip_whitelist_list do |t|
t.string :descr
t.string :ips
end
end
end
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'shotengai/version'
Gem::Specification.new do |spec|
spec.name = 'shotengai'
spec.version = Shotengai::VERSION
spec.authors = ['ivan Lan']
spec.email = ['mumumumushu@gmail.com']
spec.summary = %q{}
spec.description = %q{}
spec.homepage = 'https://git.tallty.com/open-source/ip-whitelist'
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.add_dependency 'rails'
spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'mysql2'
end
require 'rails/generators/migration'
module IpWhitelist
module Generators
class MigrationsGenerator < Rails::Generators::Base
include Rails::Generators::Migration
desc 'Copy shotengai migrations to your application.'
def self.next_migration_number(_dir)
sleep 1
Time.now.utc.strftime('%Y%m%d%H%M%S')
end
source_root File.expand_path('../../../../db/migrate', __dir__)
def copy_migrations
# Use sort() to order the migrations by seq
# Use [2..-1] to delete the seq
Dir[ File.join(self.class.source_root, '*.rb') ].sort.each { |f|
copy_migration File.basename(f, '.rb')
}
end
protected
def copy_migration(filename)
if self.class.migration_exists?('db/migrate', filename[2..-1])
say_status('skipped', "Migration #{filename[2..-1]} already exists")
else
migration_template(
"#{filename}.rb", "db/migrate/#{filename[2..-1]}.rb"
)
end
end
end
end
end
module IpWhitelist
VERSION = '0.1.0'
end
require "ip_whitelist/version"
require 'rails'
require 'active_record'
module Shotengai
def self.redis
@redis ||= Redic.new
end
def self.redis=(redis)
@redis = redis
end
end
module IpWhitelist
class Error < RuntimeError
attr_reader :status, :code, :message
def initialize(status:, code: -1, message:)
super()
@status = status
@code = code
@message = message
end
end
end
\ No newline at end of file
module IpWhitelist
module Interceptor
extend ActiveSupport::Concern
included do
private :ip_intercept
rescue_from IpWhitelist::Error do |e|
render json: { code: e.code, message: e.message }, status: e.status
end
end
def ip_intercept
unless IpWhitelist::List.ips.includes(request.ip)
raise IpWhitelist::Error.new(status: 403, message: 'Invaild Request')
end
end
end
end
module IpWhitelist
class List < ActiveRecord::Base
REDIS_KEY = 'ip-whitelist-redis'
validates_presence_of :ips
class << self
def ips
IpWhitelist.redis.get(REDIS_KEY)&.split(',') || load
end
def load
ips_ary = all.pluck(:ips).map { |ips_str| ips_str.split(',')}.reduce(:+)
IpWhitelist.redis.set(REDIS_KEY, ips_ary.join(','))
ips_ary
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment