sniffer 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a791b41dd917e6bd37ad0a30f52edfb29316305
4
- data.tar.gz: '08393e568d23b31a060406b390b85ddee72c20a5'
3
+ metadata.gz: 1a09ca069eb76f2fc329bf27351cc2499138ca6d
4
+ data.tar.gz: cb8e52bd4c2ad6ffe509ea85c7a1df80d5d79489
5
5
  SHA512:
6
- metadata.gz: 70eea24423a67f79778531924b3957d87f64d73ef62198bd7ad76dc05f24ae363706e5a29f69b99a961f2e4dc86509b2e6a35fa5c6d65ca69fc7b585ab678b33
7
- data.tar.gz: d0178703da107888ebd02bd115f64ebed1421fc72618708b649b7d711e3f0ac5da58e3ffcb07c7da30318ee2f8146b7802b4c33eab1a22a8bd2eff21a2104054
6
+ metadata.gz: c34b3b15837f64f6a7c9e45c71fa5f2392854122eb6f462656553a27aa12c875be6146cb0b0aacbe182c229ba2516ea41c8b0431dab33bf16e6f6a88b7f5f9e5
7
+ data.tar.gz: 0dac7a4e9d90125899d8100d168e5659c37d872b514f57cc94867ac0750b53f5ce2accc2d7527a38d48efa6cd20cf1e91d72b1f8e2c37b6f29fa91232668237f
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+ .ruby-version
data/.travis.yml CHANGED
@@ -3,3 +3,4 @@ language: ruby
3
3
  rvm:
4
4
  - 2.2.2
5
5
  - 2.4.2
6
+ - 2.5.0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,5 @@
1
+ ## 0.3.0 (January 12, 2018) ##
2
+
1
3
  ## 0.2.0 (January 8, 2018) ##
2
4
 
3
5
  * Added Excon adapter
data/README.md CHANGED
@@ -43,30 +43,6 @@ Or install it yourself as:
43
43
 
44
44
  $ gem install sniffer
45
45
 
46
- ## Configuration
47
-
48
- Sniffer default options:
49
-
50
- ```ruby
51
- Sniffer.config do |c|
52
- c.logger = Logger.new($stdout),
53
- c.severity = Logger::Severity::DEBUG,
54
- # HTTP options to log
55
- c.log = {
56
- request_url: true,
57
- request_headers: true,
58
- request_body: true,
59
- request_method: true,
60
- response_status: true,
61
- response_headers: true,
62
- response_body: true,
63
- timing: true
64
- },
65
- c.store = true, # save requests/responses to Sniffer.data
66
- c.enabled = false # Sniffer disabled by default
67
- end
68
- ```
69
-
70
46
  ## Usage
71
47
 
72
48
  Here's some simple examples to get you started:
@@ -143,6 +119,63 @@ D, [2017-10-26T16:47:14.007152 #59511] DEBUG -- : {"port":80,"host":"example.com
143
119
  ```
144
120
  where `rq_xxx` is request header and `rs_xxx` - response header
145
121
 
122
+ ## Configuration
123
+
124
+ Sniffer default options:
125
+
126
+ ```ruby
127
+ Sniffer.config do |c|
128
+ c.logger = Logger.new($stdout),
129
+ c.severity = Logger::Severity::DEBUG,
130
+ # HTTP options to log
131
+ c.log = {
132
+ request_url: true,
133
+ request_headers: true,
134
+ request_body: true,
135
+ request_method: true,
136
+ response_status: true,
137
+ response_headers: true,
138
+ response_body: true,
139
+ timing: true
140
+ },
141
+ c.store = true, # save requests/responses to Sniffer.data
142
+ c.enabled = false # Sniffer disabled by default
143
+ c.url_whitelist = nil
144
+ c.url_blacklist = nil
145
+ end
146
+ ```
147
+
148
+ ### Whitelist
149
+
150
+ You can add specific host url to whitelist as regexp or string. Sniffer will store only requests that matched.
151
+
152
+ ```ruby
153
+ Sniffer.config.url_whitelist = /whitelisted.com/
154
+
155
+ HTTP.get('http://example.com')
156
+ Sniffer.data[0].to_h
157
+ # => {}
158
+
159
+ HTTP.get('http://whitelisted.com/')
160
+ Sniffer.data[0].to_h
161
+ # => {{:request=>{:host=>"whitelisted.com", ....}}
162
+ ```
163
+
164
+ ### Blacklist
165
+
166
+ You can add specific host url to blacklist as regexp or string. Sniffer will ignore all matched requests.
167
+
168
+ ```ruby
169
+ Sniffer.config.url_blacklist = /blacklisted.com/
170
+
171
+ HTTP.get('http://blacklisted.com')
172
+ Sniffer.data[0].to_h
173
+ # => {}
174
+
175
+ HTTP.get('http://example.com')
176
+ Sniffer.data[0].to_h
177
+ # => {{:request=>{:host=>"example.com", ...}}
178
+ ```
146
179
 
147
180
  ## Development
148
181
 
@@ -160,6 +193,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/aderya
160
193
  * [Salahutdinov Dmitry](https://github.com/dsalahutdinov)
161
194
  * [Stanislav Chereshkevich](https://github.com/dissident)
162
195
  * [Anatoliy Kurichev](https://github.com/russo-matrosso)
196
+ * [Dmitriy Ivliev](https://github.com/moofkit)
163
197
 
164
198
  ## License
165
199
 
data/lib/sniffer.rb CHANGED
@@ -48,7 +48,7 @@ module Sniffer
48
48
  end
49
49
 
50
50
  def store(data_item)
51
- data.store(data_item)
51
+ data.store(data_item) if config.store
52
52
  end
53
53
 
54
54
  def logger
@@ -20,7 +20,9 @@ module Sniffer
20
20
  timing: true
21
21
  },
22
22
  store: true,
23
- enabled: false
23
+ enabled: false,
24
+ url_whitelist: nil,
25
+ url_blacklist: nil
24
26
 
25
27
  def capacity?
26
28
  store.is_a?(Hash) && store.key?(:capacity)
data/lib/sniffer/data.rb CHANGED
@@ -4,7 +4,7 @@ module Sniffer
4
4
  # Data class stores the data and controls capacity
5
5
  class Data < Array
6
6
  def store(data_item)
7
- return unless config.store
7
+ return unless data_item.allowed_to_sniff?
8
8
 
9
9
  if config.rotate?
10
10
  rotate(data_item)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'active_attr'
4
4
  require 'json'
5
+ require_relative 'request_policy'
5
6
 
6
7
  module Sniffer
7
8
  # Sniffer data item stores a request info
@@ -17,6 +18,7 @@ module Sniffer
17
18
  end
18
19
 
19
20
  def log
21
+ return unless allowed_to_sniff?
20
22
  Sniffer.logger.log(Sniffer.config.severity, to_json)
21
23
  end
22
24
 
@@ -29,6 +31,11 @@ module Sniffer
29
31
  to_log.to_json
30
32
  end
31
33
 
34
+ def allowed_to_sniff?
35
+ return true unless request
36
+ RequestPolicy.call(request)
37
+ end
38
+
32
39
  # Basic object for request and response objects
33
40
  class HttpObject
34
41
  include ActiveAttr::MassAssignment
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sniffer
4
+ # match request with white and black lists
5
+ module RequestPolicy
6
+ class << self
7
+ def call(request)
8
+ url = "#{request.host}:#{request.port}"
9
+ if config.url_whitelist
10
+ whitelist_url?(url)
11
+ elsif config.url_blacklist
12
+ !blacklist_url?(url)
13
+ else
14
+ true
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def whitelist_url?(url)
21
+ !url.match(config.url_whitelist).nil?
22
+ end
23
+
24
+ def blacklist_url?(url)
25
+ !url.match(config.url_blacklist).nil?
26
+ end
27
+
28
+ def config
29
+ Sniffer.config
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sniffer
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sniffer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Deryabin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-08 00:00:00.000000000 Z
11
+ date: 2018-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anyway_config
@@ -258,7 +258,6 @@ files:
258
258
  - ".gitignore"
259
259
  - ".rspec"
260
260
  - ".rubocop.yml"
261
- - ".ruby-version"
262
261
  - ".travis.yml"
263
262
  - CHANGELOG.md
264
263
  - CODE_OF_CONDUCT.md
@@ -281,6 +280,7 @@ files:
281
280
  - lib/sniffer/config.rb
282
281
  - lib/sniffer/data.rb
283
282
  - lib/sniffer/data_item.rb
283
+ - lib/sniffer/request_policy.rb
284
284
  - lib/sniffer/version.rb
285
285
  - sniffer.gemspec
286
286
  homepage: http://github.com/aderyabin/sniffer
@@ -303,7 +303,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
303
303
  version: '0'
304
304
  requirements: []
305
305
  rubyforge_project:
306
- rubygems_version: 2.6.13
306
+ rubygems_version: 2.6.14
307
307
  signing_key:
308
308
  specification_version: 4
309
309
  summary: Analyze HTTP Requests
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.4.2