intrusion 0.1.6 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/intrusion.rb +39 -11
  3. metadata +10 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6151639617a3b7284f48de675227ab59c8150b42b8134f697ebc8328b997984
4
- data.tar.gz: 7ca22d37ccf3c3bf0acbea7d18515a4933c173b458d950c9e5cb975a19765529
3
+ metadata.gz: 943e04b12f074446b6d07fecd57c158945586b81f96fcab783747530564ef905
4
+ data.tar.gz: 631bbc1a142fa86c1f0e42c43199add90dea0e9aee934c342c8d96ef746be028
5
5
  SHA512:
6
- metadata.gz: d9fe80409942f12b85c4f2234550568b7d6a83b8b69ac4f8a60ec48dab59c98f8f58b7beb68cd40ef6b7dd3be804e0efb1aa659debde26a0e2dea5dc417f046f
7
- data.tar.gz: 874aecd912cf2217d4c776393eae257c9e9debf75b24bbec577b51e6dfe8aec3186ce569c338717ff06cd5aad252c95669e60e1b3868b1fbd37162a320347a7c
6
+ metadata.gz: 89d672e112dd6bcb025c1ab21eb5e47baa6dd301094922b9c2e2dbaa092491039f9ecd44b803043e1a2af840c555458fdd9f0e5b78e7cd8947f6b01edb22ed25
7
+ data.tar.gz: de59a116b9f48b3e1373f537658f557f236e91a5683fe743b0e8b360e2a19ea6703430c37e2fff1516fc71488f412db3802019141a830a974b7b8f7b7ce2df5f
data/lib/intrusion.rb CHANGED
@@ -1,9 +1,32 @@
1
1
  # Intrusion main module
2
+
2
3
  module Intrusion
4
+
5
+ DefaultConfig = Struct.new(:threshold) do
6
+ def initialize
7
+ self.threshold = 10
8
+ end
9
+ end
10
+
11
+ def self.configure
12
+ @config = DefaultConfig.new
13
+ yield(@config) if block_given?
14
+ @config
15
+ end
16
+
17
+ def self.config
18
+ @config || configure
19
+ end
20
+
21
+ def self.reset
22
+ @config = nil
23
+ @cache = nil
24
+ end
25
+
3
26
  # check if ip is blocked
4
27
  def ids_is_blocked?(address)
5
28
  ids_load.each do |d|
6
- return true if d[:ip] == address && d[:counter] > 9
29
+ return true if d[:ip] == address && d[:counter] >= Intrusion.config.threshold
7
30
  end
8
31
  false
9
32
  end
@@ -20,13 +43,11 @@ module Intrusion
20
43
  found = nil
21
44
  dt.each { |d| found = d if d[:ip] == address }
22
45
  if found
23
- block ? found[:counter] = 10 : found[:counter] += 1
46
+ block ? found[:counter] = Intrusion.config.threshold : found[:counter] += 1
24
47
  else
25
- dt << { ip: address, counter: block ? 10 : 1 }
48
+ dt << { ip: address, counter: block ? Intrusion.config.threshold : 1 }
26
49
  end
27
-
28
- # update record
29
- update_attributes(ids: dt.to_yaml)
50
+ ids_save!(dt)
30
51
  end
31
52
 
32
53
  # reset counter and stay
@@ -37,16 +58,23 @@ module Intrusion
37
58
 
38
59
  if found
39
60
  dt.delete(found)
40
- # update
41
- return update_attributes(ids: dt.to_yaml)
61
+ return ids_save!(dt)
42
62
  end
43
63
  false
44
64
  end
45
65
 
46
66
  # convert yaml string helper
47
67
  def ids_load
48
- dt = ids.blank? ? [] : YAML.load(ids, Intrusion) rescue []
49
- dt = [] unless dt.class == Array
50
- dt
68
+ data = ids.blank? ? [] : YAML.safe_load(ids, [Symbol])
69
+ raise 'invalid data in ids field' unless data.is_a?(Array)
70
+ data
71
+ rescue RuntimeError
72
+ []
73
+ end
74
+
75
+ # save current state to object or file
76
+ def ids_save!(dt)
77
+ update(ids: dt.to_yaml)
51
78
  end
79
+
52
80
  end
metadata CHANGED
@@ -1,26 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intrusion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Duncombe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-15 00:00:00.000000000 Z
11
+ date: 2022-07-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Intrusion is a gem helping you to block objects for IP addresses within
14
14
  your Ruby on Rails Application.
15
- email: sd@netsense.ch
15
+ email: simon@duncom.be
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/intrusion.rb
21
- homepage: http://github.com/symontech/intrusion
22
- licenses: []
23
- metadata: {}
21
+ homepage: https://github.com/symontech/intrusion
22
+ licenses:
23
+ - MIT
24
+ metadata:
25
+ source_code_uri: https://github.com/symontech/intrusion
26
+ documentation_uri: https://github.com/symontech/intrusion#readme
24
27
  post_install_message:
25
28
  rdoc_options: []
26
29
  require_paths:
@@ -36,8 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
39
  - !ruby/object:Gem::Version
37
40
  version: '0'
38
41
  requirements: []
39
- rubyforge_project:
40
- rubygems_version: 2.7.3
42
+ rubygems_version: 3.2.29
41
43
  signing_key:
42
44
  specification_version: 4
43
45
  summary: intrusion detection and prevention for rails applications