Class: ForestLiana::IpWhitelistChecker
- Inherits:
-
Object
- Object
- ForestLiana::IpWhitelistChecker
- Defined in:
- app/services/forest_liana/ip_whitelist_checker.rb
Defined Under Namespace
Modules: RuleType
Class Method Summary collapse
- .ip_version(ip) ⇒ Object
- .is_both_loopback(ip1, ip2) ⇒ Object
- .is_ip_match_ip(ip1, ip2) ⇒ Object
- .is_ip_match_range(ip, rule) ⇒ Object
- .is_ip_match_subnet(ip, subnet) ⇒ Object
- .is_ip_matches_any_rule(ip, rules) ⇒ Object
- .is_ip_matches_rule(ip, rule) ⇒ Object
- .is_same_ip_version(ip1, ip2) ⇒ Object
Class Method Details
.ip_version(ip) ⇒ Object
27 28 29 |
# File 'app/services/forest_liana/ip_whitelist_checker.rb', line 27 def self.ip_version(ip) (IPAddress ip).is_a?(IPAddress::IPv4) ? :ip_v4 : :ip_v6 end |
.is_both_loopback(ip1, ip2) ⇒ Object
38 39 40 |
# File 'app/services/forest_liana/ip_whitelist_checker.rb', line 38 def self.is_both_loopback(ip1, ip2) IPAddress(ip1).loopback? && IPAddress(ip2).loopback? end |
.is_ip_match_ip(ip1, ip2) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/services/forest_liana/ip_whitelist_checker.rb', line 42 def self.is_ip_match_ip(ip1, ip2) if !IpWhitelistChecker.is_same_ip_version(ip1, ip2) return IpWhitelistChecker.is_both_loopback(ip1, ip2) end if IPAddress(ip1) == IPAddress(ip2) true else IpWhitelistChecker.is_both_loopback(ip1, ip2) end end |
.is_ip_match_range(ip, rule) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'app/services/forest_liana/ip_whitelist_checker.rb', line 54 def self.is_ip_match_range(ip, rule) return false if !IpWhitelistChecker.is_same_ip_version(ip, rule['ip_minimum']) ip_range_minimum = (IPAddress rule['ip_minimum']).to_i ip_range_maximum = (IPAddress rule['ip_maximum']).to_i ip_value = (IPAddress ip).to_i return ip_value >= ip_range_minimum && ip_value <= ip_range_maximum end |
.is_ip_match_subnet(ip, subnet) ⇒ Object
64 65 66 67 68 |
# File 'app/services/forest_liana/ip_whitelist_checker.rb', line 64 def self.is_ip_match_subnet(ip, subnet) return false if !IpWhitelistChecker.is_same_ip_version(ip, subnet) IPAddress(subnet).include?(IPAddress(ip)) end |
.is_ip_matches_any_rule(ip, rules) ⇒ Object
11 12 13 |
# File 'app/services/forest_liana/ip_whitelist_checker.rb', line 11 def self.is_ip_matches_any_rule(ip, rules) rules.any? { |rule| IpWhitelistChecker.is_ip_matches_rule(ip, rule) } end |
.is_ip_matches_rule(ip, rule) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/services/forest_liana/ip_whitelist_checker.rb', line 15 def self.is_ip_matches_rule(ip, rule) if rule['type'] == RuleType::IP return IpWhitelistChecker.is_ip_match_ip(ip, rule['ip']) elsif rule['type'] == RuleType::RANGE return IpWhitelistChecker.is_ip_match_range(ip, rule) elsif rule['type'] == RuleType::SUBNET return IpWhitelistChecker.is_ip_match_subnet(ip, rule['range']) end raise 'Invalid rule type' end |
.is_same_ip_version(ip1, ip2) ⇒ Object
31 32 33 34 35 36 |
# File 'app/services/forest_liana/ip_whitelist_checker.rb', line 31 def self.is_same_ip_version(ip1, ip2) ip1_version = IpWhitelistChecker.ip_version(ip1) ip2_version = IpWhitelistChecker.ip_version(ip2) ip1_version == ip2_version end |