Commit 17c7f7cd by Ivan Lan

Version 1

parent 7c23df47
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
/mysql.yml
/.rspec_status
\ No newline at end of file
IpWhitelist.redis = Redic.new
rails g ip_whitelist:migrations
include IpWhitelist::Interceptor
\ No newline at end of file
class CreateIpWhitelistList < ActiveRecord::Migration[5.1] class CreateIpWhitelistList < ActiveRecord::Migration[5.1]
def change def change
create_table :ip_whitelist_list do |t| create_table :ip_whitelist_lists do |t|
t.string :descr t.string :descr
t.string :ips t.string :ips
end end
......
# coding: utf-8 # coding: utf-8
lib = File.expand_path('../lib', __FILE__) lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'shotengai/version' require 'ip_whitelist/version'
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = 'shotengai' spec.name = 'ip_whitelist'
spec.version = Shotengai::VERSION spec.version = IpWhitelist::VERSION
spec.authors = ['ivan Lan'] spec.authors = ['ivan Lan']
spec.email = ['mumumumushu@gmail.com'] spec.email = ['mumumumushu@gmail.com']
...@@ -22,6 +22,4 @@ Gem::Specification.new do |spec| ...@@ -22,6 +22,4 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.14' spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'mysql2' spec.add_development_dependency 'mysql2'
end end
...@@ -12,7 +12,7 @@ module IpWhitelist ...@@ -12,7 +12,7 @@ module IpWhitelist
Time.now.utc.strftime('%Y%m%d%H%M%S') Time.now.utc.strftime('%Y%m%d%H%M%S')
end end
source_root File.expand_path('../../../../db/migrate', __dir__) source_root File.expand_path('../../../../db/migrate', __FILE__)
def copy_migrations def copy_migrations
# Use sort() to order the migrations by seq # Use sort() to order the migrations by seq
......
require "ip_whitelist/version" require 'ip_whitelist/version'
require 'rails' require 'rails'
require 'active_record' require 'active_record'
module Shotengai module IpWhitelist
autoload :List, 'ip_whitelist/list'
autoload :Interceptor, 'ip_whitelist/interceptor'
autoload :Error, 'ip_whitelist/error'
def self.redis def self.redis
@redis ||= Redic.new @redis ||= Redis.new
end end
def self.redis=(redis) def self.redis=(redis)
......
...@@ -3,16 +3,16 @@ module IpWhitelist ...@@ -3,16 +3,16 @@ module IpWhitelist
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
private :ip_intercept before_action :ip_intercept
rescue_from IpWhitelist::Error do |e| rescue_from IpWhitelist::Error do |e|
render json: { code: e.code, message: e.message }, status: e.status render json: { code: e.code, message: e.message }, status: e.status
end end
end
def ip_intercept def ip_intercept
unless IpWhitelist::List.ips.includes(request.ip) unless IpWhitelist::List.ips.include?(request.ip)
raise IpWhitelist::Error.new(status: 403, message: 'Invaild Request') raise IpWhitelist::Error.new(status: 403, message: 'Invaild Request')
end
end end
end end
end end
......
module IpWhitelist module IpWhitelist
class List < ActiveRecord::Base class List < ActiveRecord::Base
self.table_name = 'ip_whitelist_lists'
REDIS_KEY = 'ip-whitelist-redis' REDIS_KEY = 'ip-whitelist-redis'
validates_presence_of :ips validates_presence_of :ips
...@@ -10,11 +12,10 @@ module IpWhitelist ...@@ -10,11 +12,10 @@ module IpWhitelist
end end
def load def load
ips_ary = all.pluck(:ips).map { |ips_str| ips_str.split(',')}.reduce(:+) ips_ary = all.pluck(:ips).map { |ips_str| ips_str.split(',') }.reduce(:+) || []
IpWhitelist.redis.set(REDIS_KEY, ips_ary.join(',')) IpWhitelist.redis.set(REDIS_KEY, ips_ary.join(','))
ips_ary ips_ary
end end
end end
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