sniffer 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +24 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +24 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +23 -0
- data/.github/workflows/rspec.yml +34 -0
- data/.github/workflows/rubocop.yml +23 -0
- data/.rubocop.yml +14 -20
- data/CHANGELOG.md +5 -0
- data/README.md +29 -9
- data/lib/sniffer.rb +12 -3
- data/lib/sniffer/adapters/curb_adapter.rb +2 -2
- data/lib/sniffer/adapters/ethon_adapter.rb +3 -3
- data/lib/sniffer/adapters/eventmachine_adapter.rb +1 -1
- data/lib/sniffer/adapters/excon_adapter.rb +2 -2
- data/lib/sniffer/adapters/http_adapter.rb +2 -2
- data/lib/sniffer/adapters/httpclient_adapter.rb +5 -7
- data/lib/sniffer/adapters/net_http_adapter.rb +2 -2
- data/lib/sniffer/adapters/patron_adapter.rb +2 -2
- data/lib/sniffer/config.rb +14 -0
- data/lib/sniffer/data.rb +0 -2
- data/lib/sniffer/data_item.rb +35 -10
- data/lib/sniffer/middleware/chain.rb +73 -0
- data/lib/sniffer/middleware/entry.rb +19 -0
- data/lib/sniffer/middleware/logger.rb +27 -0
- data/lib/sniffer/version.rb +1 -1
- data/sniffer.gemspec +4 -4
- metadata +23 -17
- data/.travis.yml +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50fc6c81f9d42483702b1f7c58c784bb34098d4b687bb14463cdff1a115ca9b7
|
4
|
+
data.tar.gz: e075f394c7b28b95468039faf893f5cc3cb5a548111a065f69b81fbda715bfe2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71103bf582fe917cda1c4ad52c7e238c851c2aeb5459c5375b7157394cf0893e0cc2d2d1d100f3006d0a176accf083c6bf55d90d21d4f9f8b8ba395d50eefd60
|
7
|
+
data.tar.gz: d765279e5adb997fd1021a131587a96ae6057f8ee808af9debe657c1f67fdaecd537c23d62d8663729e79ae24f81e5f4f7f6bf54326870bf3a29e939bc04af05
|
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
name: Bug report
|
3
|
+
about: Create a report to help us improve
|
4
|
+
title: ''
|
5
|
+
labels: ''
|
6
|
+
assignees: aderyabin
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
## What did you do?
|
11
|
+
|
12
|
+
## What did you expect to happen?
|
13
|
+
|
14
|
+
## What actually happened?
|
15
|
+
|
16
|
+
## Additional context
|
17
|
+
|
18
|
+
## Environment
|
19
|
+
|
20
|
+
**Ruby Version:**
|
21
|
+
|
22
|
+
**Framework Version (Rails, whatever):**
|
23
|
+
|
24
|
+
**Sniffer Version:**
|
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
name: Feature request
|
3
|
+
about: Suggest an idea for this project
|
4
|
+
title: ''
|
5
|
+
labels: enhancement
|
6
|
+
assignees: aderyabin
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
## Is your feature request related to a problem? Please describe.
|
11
|
+
|
12
|
+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
13
|
+
|
14
|
+
## Describe the solution you'd like
|
15
|
+
|
16
|
+
A clear and concise description of what you want to happen.
|
17
|
+
|
18
|
+
## Describe alternatives you've considered
|
19
|
+
|
20
|
+
A clear and concise description of any alternative solutions or features you've considered.
|
21
|
+
|
22
|
+
## Additional context
|
23
|
+
|
24
|
+
Add any other context or screenshots about the feature request here.
|
@@ -0,0 +1,23 @@
|
|
1
|
+
<!--
|
2
|
+
First of all, thanks for contributing!
|
3
|
+
|
4
|
+
If it's a typo fix or minor documentation update feel free to skip the rest of this template!
|
5
|
+
-->
|
6
|
+
|
7
|
+
## What is the purpose of this pull request?
|
8
|
+
|
9
|
+
<!--
|
10
|
+
If it's a bug fix, then link it to the issue, for example:
|
11
|
+
|
12
|
+
Fixes #xxx
|
13
|
+
-->
|
14
|
+
|
15
|
+
## What changes did you make? (overview)
|
16
|
+
|
17
|
+
## Is there anything you'd like reviewers to focus on?
|
18
|
+
|
19
|
+
## Checklist
|
20
|
+
|
21
|
+
- [ ] I've added tests for this change
|
22
|
+
- [ ] I've added a Changelog entry
|
23
|
+
- [ ] I've updated a documentation
|
@@ -0,0 +1,34 @@
|
|
1
|
+
name: Run Tests
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request:
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
rspec:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
strategy:
|
13
|
+
fail-fast: false
|
14
|
+
matrix:
|
15
|
+
ruby: ["2.5.x", "2.6.x"]
|
16
|
+
steps:
|
17
|
+
- uses: actions/checkout@v1
|
18
|
+
- name: Set up Ruby
|
19
|
+
uses: actions/setup-ruby@v1
|
20
|
+
with:
|
21
|
+
ruby-version: ${{ matrix.ruby }}
|
22
|
+
- name: Install Bundler
|
23
|
+
run: |
|
24
|
+
gem install bundler
|
25
|
+
- name: Install deps
|
26
|
+
run: |
|
27
|
+
sudo apt-get update
|
28
|
+
sudo apt-get install -y --no-install-recommends libcurl4-openssl-dev
|
29
|
+
- name: Install Ruby deps
|
30
|
+
run: |
|
31
|
+
bundle install --jobs 4 --retry 3
|
32
|
+
- name: Run RSpec
|
33
|
+
run: |
|
34
|
+
bundle exec rake spec
|
@@ -0,0 +1,23 @@
|
|
1
|
+
name: Lint Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request:
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
rubocop:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
steps:
|
13
|
+
- uses: actions/checkout@v1
|
14
|
+
- name: Set up Ruby 2.6
|
15
|
+
uses: actions/setup-ruby@v1
|
16
|
+
with:
|
17
|
+
ruby-version: 2.6.x
|
18
|
+
- name: Install RuboCop
|
19
|
+
run: |
|
20
|
+
gem install rubocop
|
21
|
+
- name: Run RuboCop
|
22
|
+
run: |
|
23
|
+
rubocop -c .rubocop.yml
|
data/.rubocop.yml
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
2
3
|
# Include gemspec and Rakefile
|
3
4
|
Include:
|
4
|
-
-
|
5
|
-
-
|
6
|
-
-
|
5
|
+
- "lib/**/*.rb"
|
6
|
+
- "lib/**/*.rake"
|
7
|
+
- "spec/**/*.rb"
|
7
8
|
Exclude:
|
8
|
-
-
|
9
|
-
-
|
10
|
-
-
|
11
|
-
-
|
9
|
+
- "bin/**/*"
|
10
|
+
- "Gemfile"
|
11
|
+
- "Rakefile"
|
12
|
+
- "*.gemspec"
|
12
13
|
DisplayCopNames: true
|
13
14
|
StyleGuideCopsOnly: false
|
14
|
-
TargetRubyVersion: 2.2
|
15
15
|
|
16
16
|
Naming/AccessorMethodName:
|
17
17
|
Enabled: false
|
@@ -24,14 +24,14 @@ Style/TrivialAccessors:
|
|
24
24
|
|
25
25
|
Style/Documentation:
|
26
26
|
Exclude:
|
27
|
-
-
|
27
|
+
- "spec/**/*.rb"
|
28
28
|
|
29
29
|
Style/StringLiterals:
|
30
30
|
Enabled: false
|
31
31
|
|
32
32
|
Style/BlockDelimiters:
|
33
33
|
Exclude:
|
34
|
-
-
|
34
|
+
- "spec/**/*.rb"
|
35
35
|
|
36
36
|
Style/DoubleNegation:
|
37
37
|
Enabled: false
|
@@ -47,22 +47,16 @@ Lint/AmbiguousBlockAssociation:
|
|
47
47
|
|
48
48
|
Metrics/MethodLength:
|
49
49
|
Exclude:
|
50
|
-
-
|
50
|
+
- "spec/**/*.rb"
|
51
51
|
|
52
|
-
|
52
|
+
Layout/LineLength:
|
53
53
|
Max: 120
|
54
54
|
Exclude:
|
55
|
-
-
|
55
|
+
- "spec/**/*.rb"
|
56
56
|
|
57
57
|
Metrics/BlockLength:
|
58
58
|
Exclude:
|
59
|
-
-
|
60
|
-
|
61
|
-
Rails/Date:
|
62
|
-
Enabled: false
|
63
|
-
|
64
|
-
Rails/TimeZone:
|
65
|
-
Enabled: false
|
59
|
+
- "spec/**/*.rb"
|
66
60
|
|
67
61
|
Security/YAMLLoad:
|
68
62
|
Enabled: false
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Sniffer
|
2
2
|
|
3
|
-
[![Build
|
3
|
+
[![Build](https://github.com/aderyabin/sniffer/workflows/Run%20Tests/badge.svg)](https://github.com/aderyabin/sniffer/actions) [![Gem Version](https://badge.fury.io/rb/sniffer.svg)](https://rubygems.org/gems/sniffer) [![Join the chat at https://gitter.im/aderyabin/sniffer](https://badges.gitter.im/aderyabin/sniffer.svg)](https://gitter.im/aderyabin/sniffer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
4
4
|
|
5
5
|
Sniffer aims to help:
|
6
6
|
|
@@ -19,10 +19,6 @@ Sniffer supports most common HTTP accessing libraries:
|
|
19
19
|
* [EM-HTTP-Request](https://github.com/igrigorik/em-http-request)
|
20
20
|
* [Excon](https://github.com/excon/excon)
|
21
21
|
|
22
|
-
|
23
|
-
<a href="https://evilmartians.com/">
|
24
|
-
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
|
25
|
-
|
26
22
|
## Demo
|
27
23
|
|
28
24
|
![demo](https://github.com/aderyabin/sniffer/blob/master/assets/demo.gif?raw=true)
|
@@ -125,8 +121,8 @@ Sniffer default options:
|
|
125
121
|
|
126
122
|
```ruby
|
127
123
|
Sniffer.config do |c|
|
128
|
-
c.logger = Logger.new($stdout)
|
129
|
-
c.severity = Logger::Severity::DEBUG
|
124
|
+
c.logger = Logger.new($stdout)
|
125
|
+
c.severity = Logger::Severity::DEBUG
|
130
126
|
# HTTP options to log
|
131
127
|
c.log = {
|
132
128
|
request_url: true,
|
@@ -137,8 +133,8 @@ Sniffer.config do |c|
|
|
137
133
|
response_headers: true,
|
138
134
|
response_body: true,
|
139
135
|
timing: true
|
140
|
-
}
|
141
|
-
c.store = true
|
136
|
+
}
|
137
|
+
c.store = true # save requests/responses to Sniffer.data
|
142
138
|
c.enabled = false # Sniffer disabled by default
|
143
139
|
c.url_whitelist = nil
|
144
140
|
c.url_blacklist = nil
|
@@ -177,6 +173,30 @@ Sniffer.data[0].to_h
|
|
177
173
|
# => {{:request=>{:host=>"example.com", ...}}
|
178
174
|
```
|
179
175
|
|
176
|
+
### Middleware
|
177
|
+
|
178
|
+
You can add the middleware to run custom code before/after the sniffed data was logged.
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
Sniffer.middleware do |chain|
|
182
|
+
chain.add MyHook
|
183
|
+
end
|
184
|
+
|
185
|
+
class MyHook
|
186
|
+
def request(data_item)
|
187
|
+
puts "Before work"
|
188
|
+
yield
|
189
|
+
puts "After work"
|
190
|
+
end
|
191
|
+
|
192
|
+
def response(data_item)
|
193
|
+
puts "Before work"
|
194
|
+
yield
|
195
|
+
puts "After work"
|
196
|
+
end
|
197
|
+
end
|
198
|
+
```
|
199
|
+
|
180
200
|
## Development
|
181
201
|
|
182
202
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/sniffer.rb
CHANGED
@@ -48,11 +48,20 @@ module Sniffer
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def store(data_item)
|
51
|
-
|
51
|
+
return unless config.store
|
52
|
+
return unless data_item.allowed_to_sniff?
|
53
|
+
|
54
|
+
config.middleware.invoke_request(data_item) do
|
55
|
+
data.store(data_item)
|
56
|
+
end
|
52
57
|
end
|
53
58
|
|
54
|
-
def
|
55
|
-
config.
|
59
|
+
def notify_response(data_item)
|
60
|
+
return unless config.store
|
61
|
+
return unless data_item.allowed_to_sniff?
|
62
|
+
|
63
|
+
config.middleware.invoke_response(data_item) do
|
64
|
+
end
|
56
65
|
end
|
57
66
|
end
|
58
67
|
end
|
@@ -76,10 +76,10 @@ module Sniffer
|
|
76
76
|
body: body_str,
|
77
77
|
timing: timing)
|
78
78
|
|
79
|
-
data_item
|
79
|
+
Sniffer.notify_response(data_item)
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
|
-
Curl::Easy.
|
85
|
+
Curl::Easy.include Sniffer::Adapters::CurlAdapter if defined?(::Curl::Easy)
|
@@ -66,7 +66,7 @@ module Sniffer
|
|
66
66
|
headers: hash_headers,
|
67
67
|
body: @response_body,
|
68
68
|
timing: bm)
|
69
|
-
@data_item
|
69
|
+
Sniffer.notify_response(@data_item)
|
70
70
|
|
71
71
|
end
|
72
72
|
|
@@ -82,6 +82,6 @@ module Sniffer
|
|
82
82
|
end
|
83
83
|
|
84
84
|
if defined?(::Ethon::Easy)
|
85
|
-
Ethon::Easy::Http.
|
86
|
-
Ethon::Easy::Operations.
|
85
|
+
Ethon::Easy::Http.include Sniffer::Adapters::EthonAdapter::Http
|
86
|
+
Ethon::Easy::Operations.include Sniffer::Adapters::EthonAdapter::Operations
|
87
87
|
end
|
@@ -36,7 +36,7 @@ module Sniffer
|
|
36
36
|
body: @response.body,
|
37
37
|
timing: bm)
|
38
38
|
|
39
|
-
data_item
|
39
|
+
Sniffer.notify_response(data_item)
|
40
40
|
end
|
41
41
|
|
42
42
|
@response
|
@@ -46,4 +46,4 @@ module Sniffer
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
::Excon::Connection.
|
49
|
+
::Excon::Connection.include Sniffer::Adapters::ExconAdapter if defined?(::Excon::Connection)
|
@@ -55,7 +55,7 @@ module Sniffer
|
|
55
55
|
body: @res.body,
|
56
56
|
timing: bm)
|
57
57
|
|
58
|
-
data_item
|
58
|
+
Sniffer.notify_response(data_item)
|
59
59
|
end
|
60
60
|
|
61
61
|
return @res unless opts.follow
|
@@ -69,4 +69,4 @@ module Sniffer
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
HTTP::Client.
|
72
|
+
HTTP::Client.include Sniffer::Adapters::HTTPAdapter if defined?(::HTTP::Client)
|
@@ -30,11 +30,9 @@ module Sniffer
|
|
30
30
|
retryable_response = nil
|
31
31
|
|
32
32
|
bm = Benchmark.realtime do
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
retryable_response = e
|
37
|
-
end
|
33
|
+
do_get_block_without_sniffer(req, proxy, conn, &block)
|
34
|
+
rescue HTTPClient::RetryableResponse => e
|
35
|
+
retryable_response = e
|
38
36
|
end
|
39
37
|
|
40
38
|
if Sniffer.enabled?
|
@@ -46,7 +44,7 @@ module Sniffer
|
|
46
44
|
|
47
45
|
conn.push(res)
|
48
46
|
|
49
|
-
data_item
|
47
|
+
Sniffer.notify_response(data_item)
|
50
48
|
end
|
51
49
|
|
52
50
|
raise retryable_response unless retryable_response.nil?
|
@@ -56,4 +54,4 @@ module Sniffer
|
|
56
54
|
end
|
57
55
|
end
|
58
56
|
|
59
|
-
HTTPClient.
|
57
|
+
HTTPClient.include Sniffer::Adapters::HTTPClientAdapter if defined?(::HTTPClient)
|
@@ -38,7 +38,7 @@ module Sniffer
|
|
38
38
|
body: @response.body.to_s,
|
39
39
|
timing: bm)
|
40
40
|
|
41
|
-
data_item
|
41
|
+
Sniffer.notify_response(data_item)
|
42
42
|
end
|
43
43
|
|
44
44
|
@response
|
@@ -47,4 +47,4 @@ module Sniffer
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
50
|
-
Net::HTTP.
|
50
|
+
Net::HTTP.include Sniffer::Adapters::NetHttpAdapter
|
@@ -36,7 +36,7 @@ module Sniffer
|
|
36
36
|
body: @res.body.to_s,
|
37
37
|
timing: bm)
|
38
38
|
|
39
|
-
data_item
|
39
|
+
Sniffer.notify_response(data_item)
|
40
40
|
end
|
41
41
|
|
42
42
|
@res
|
@@ -46,4 +46,4 @@ module Sniffer
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
::Patron::Session.
|
49
|
+
::Patron::Session.include Sniffer::Adapters::PatronAdapter if defined?(::Patron::Session)
|
data/lib/sniffer/config.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "anyway_config"
|
4
|
+
require_relative "middleware/chain"
|
5
|
+
require_relative "middleware/logger"
|
4
6
|
|
5
7
|
module Sniffer
|
6
8
|
# Sniffer configuration
|
@@ -24,6 +26,17 @@ module Sniffer
|
|
24
26
|
url_whitelist: nil,
|
25
27
|
url_blacklist: nil
|
26
28
|
|
29
|
+
def middleware
|
30
|
+
@middleware ||= begin
|
31
|
+
Middleware::Chain.new.tap do |chain|
|
32
|
+
chain.add(Sniffer::Middleware::Logger, logger, severity)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
yield @middleware if block_given?
|
37
|
+
@middleware
|
38
|
+
end
|
39
|
+
|
27
40
|
def capacity?
|
28
41
|
store.is_a?(Hash) && store.key?(:capacity)
|
29
42
|
end
|
@@ -34,6 +47,7 @@ module Sniffer
|
|
34
47
|
|
35
48
|
def rotate?
|
36
49
|
return false unless capacity?
|
50
|
+
|
37
51
|
store.fetch(:rotate, true)
|
38
52
|
end
|
39
53
|
end
|
data/lib/sniffer/data.rb
CHANGED
data/lib/sniffer/data_item.rb
CHANGED
@@ -12,27 +12,38 @@ module Sniffer
|
|
12
12
|
|
13
13
|
def to_h
|
14
14
|
{
|
15
|
-
request:
|
16
|
-
|
15
|
+
request: # frozen_string_literal: true
|
16
|
+
# Sniffer data item stores a request info
|
17
|
+
# Basic object for request and response objects
|
18
|
+
# Stores http request data
|
19
|
+
# rubocop:enable
|
20
|
+
# Stores http response data
|
21
|
+
|
22
|
+
request&.to_h,
|
23
|
+
response: # frozen_string_literal: true
|
24
|
+
# Sniffer data item stores a request info
|
25
|
+
# Basic object for request and response objects
|
26
|
+
# Stores http request data
|
27
|
+
# rubocop:enable
|
28
|
+
# Stores http response data
|
29
|
+
|
30
|
+
response&.to_h
|
17
31
|
}
|
18
32
|
end
|
19
33
|
|
20
|
-
def log
|
21
|
-
return unless Sniffer.logger && allowed_to_sniff?
|
22
|
-
Sniffer.logger.log(Sniffer.config.severity, to_json)
|
23
|
-
end
|
24
|
-
|
25
34
|
def to_log
|
26
35
|
return {} unless Sniffer.config.logger
|
36
|
+
|
27
37
|
request.to_log.merge(response.to_log)
|
28
38
|
end
|
29
39
|
|
30
|
-
def to_json
|
40
|
+
def to_json(*_args)
|
31
41
|
to_log.to_json
|
32
42
|
end
|
33
43
|
|
34
44
|
def allowed_to_sniff?
|
35
45
|
return true unless request
|
46
|
+
|
36
47
|
RequestPolicy.call(request)
|
37
48
|
end
|
38
49
|
|
@@ -59,7 +70,14 @@ module Sniffer
|
|
59
70
|
query: query,
|
60
71
|
port: port,
|
61
72
|
headers: headers,
|
62
|
-
body:
|
73
|
+
body: # frozen_string_literal: true
|
74
|
+
# Sniffer data item stores a request info
|
75
|
+
# Basic object for request and response objects
|
76
|
+
# Stores http request data
|
77
|
+
# rubocop:enable
|
78
|
+
# Stores http response data
|
79
|
+
|
80
|
+
body&.to_s,
|
63
81
|
method: method
|
64
82
|
}
|
65
83
|
end
|
@@ -94,7 +112,14 @@ module Sniffer
|
|
94
112
|
{
|
95
113
|
status: status,
|
96
114
|
headers: headers,
|
97
|
-
body:
|
115
|
+
body: # frozen_string_literal: true
|
116
|
+
# Sniffer data item stores a request info
|
117
|
+
# Basic object for request and response objects
|
118
|
+
# Stores http request data
|
119
|
+
# rubocop:enable
|
120
|
+
# Stores http response data
|
121
|
+
|
122
|
+
body&.to_s,
|
98
123
|
timing: timing
|
99
124
|
}
|
100
125
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "entry"
|
4
|
+
|
5
|
+
module Sniffer
|
6
|
+
# Middleware is code configured to run before/after
|
7
|
+
# storing sniffed request/response
|
8
|
+
# To add middleware
|
9
|
+
#
|
10
|
+
# Sniffer.middleware do |chain|
|
11
|
+
# chain.add MyHook
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# class MyHook
|
15
|
+
# def request(data_item)
|
16
|
+
# puts "Before request work"
|
17
|
+
# yield
|
18
|
+
# puts "After request work"
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# def response(data_item)
|
22
|
+
# puts "Before response work"
|
23
|
+
# yield
|
24
|
+
# puts "After response work"
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
module Middleware
|
28
|
+
# Stores all the middleware configs
|
29
|
+
class Chain
|
30
|
+
include Enumerable
|
31
|
+
|
32
|
+
def entries
|
33
|
+
@entries ||= []
|
34
|
+
end
|
35
|
+
|
36
|
+
def each(&block)
|
37
|
+
entries.each(&block)
|
38
|
+
end
|
39
|
+
|
40
|
+
def add(klass, *args)
|
41
|
+
entries.push(Entry.new(klass, *args))
|
42
|
+
end
|
43
|
+
|
44
|
+
def remove(klass)
|
45
|
+
entries.delete_if { |entry| entry.klass == klass }
|
46
|
+
end
|
47
|
+
|
48
|
+
def invoke_request(*args)
|
49
|
+
chain = map(&:make_new).dup
|
50
|
+
traverse_chain = lambda do
|
51
|
+
if chain.empty?
|
52
|
+
yield
|
53
|
+
else
|
54
|
+
chain.shift.request(*args, &traverse_chain)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
traverse_chain.call
|
58
|
+
end
|
59
|
+
|
60
|
+
def invoke_response(*args)
|
61
|
+
chain = map(&:make_new).dup
|
62
|
+
traverse_chain = lambda do
|
63
|
+
if chain.empty?
|
64
|
+
yield
|
65
|
+
else
|
66
|
+
chain.shift.response(*args, &traverse_chain)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
traverse_chain.call
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sniffer
|
4
|
+
module Middleware
|
5
|
+
# Middleware entry, represented with klass and arguments for initializer
|
6
|
+
class Entry
|
7
|
+
attr_reader :klass
|
8
|
+
|
9
|
+
def initialize(klass, *args)
|
10
|
+
@klass = klass
|
11
|
+
@args = args
|
12
|
+
end
|
13
|
+
|
14
|
+
def make_new
|
15
|
+
@klass.new(*@args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sniffer
|
4
|
+
module Middleware
|
5
|
+
# Response logging build-in middleware
|
6
|
+
class Logger
|
7
|
+
attr_reader :logger, :severity
|
8
|
+
|
9
|
+
def initialize(logger, severity)
|
10
|
+
@logger = logger
|
11
|
+
@severity = severity
|
12
|
+
end
|
13
|
+
|
14
|
+
def request(_data_item)
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
|
18
|
+
def response(data_item)
|
19
|
+
yield
|
20
|
+
|
21
|
+
return unless logger
|
22
|
+
|
23
|
+
logger.log(severity, data_item.to_json)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/sniffer/version.rb
CHANGED
data/sniffer.gemspec
CHANGED
@@ -19,13 +19,13 @@ Gem::Specification.new do |spec|
|
|
19
19
|
end
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency "anyway_config", "
|
22
|
+
spec.add_dependency "anyway_config", ">= 1.0"
|
23
23
|
spec.add_dependency "active_attr", ">= 0.10.2"
|
24
24
|
|
25
|
-
spec.add_development_dependency "bundler", "~>
|
26
|
-
spec.add_development_dependency "rake", "
|
25
|
+
spec.add_development_dependency "bundler", "~> 2"
|
26
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
27
27
|
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
-
spec.add_development_dependency "rubocop"
|
28
|
+
spec.add_development_dependency "rubocop"
|
29
29
|
spec.add_development_dependency "pry-byebug"
|
30
30
|
spec.add_development_dependency "sinatra", "~> 2.0"
|
31
31
|
spec.add_development_dependency "puma", ">= 3.10.0"
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sniffer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.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:
|
11
|
+
date: 2020-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: anyway_config
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -44,28 +44,28 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '2'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '2'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 12.3.3
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 12.3.3
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -84,16 +84,16 @@ dependencies:
|
|
84
84
|
name: rubocop
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0
|
89
|
+
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '0
|
96
|
+
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: pry-byebug
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -255,10 +255,14 @@ executables: []
|
|
255
255
|
extensions: []
|
256
256
|
extra_rdoc_files: []
|
257
257
|
files:
|
258
|
+
- ".github/ISSUE_TEMPLATE/bug_report.md"
|
259
|
+
- ".github/ISSUE_TEMPLATE/feature_request.md"
|
260
|
+
- ".github/PULL_REQUEST_TEMPLATE.md"
|
261
|
+
- ".github/workflows/rspec.yml"
|
262
|
+
- ".github/workflows/rubocop.yml"
|
258
263
|
- ".gitignore"
|
259
264
|
- ".rspec"
|
260
265
|
- ".rubocop.yml"
|
261
|
-
- ".travis.yml"
|
262
266
|
- CHANGELOG.md
|
263
267
|
- CODE_OF_CONDUCT.md
|
264
268
|
- Gemfile
|
@@ -280,6 +284,9 @@ files:
|
|
280
284
|
- lib/sniffer/config.rb
|
281
285
|
- lib/sniffer/data.rb
|
282
286
|
- lib/sniffer/data_item.rb
|
287
|
+
- lib/sniffer/middleware/chain.rb
|
288
|
+
- lib/sniffer/middleware/entry.rb
|
289
|
+
- lib/sniffer/middleware/logger.rb
|
283
290
|
- lib/sniffer/request_policy.rb
|
284
291
|
- lib/sniffer/version.rb
|
285
292
|
- sniffer.gemspec
|
@@ -302,8 +309,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
302
309
|
- !ruby/object:Gem::Version
|
303
310
|
version: '0'
|
304
311
|
requirements: []
|
305
|
-
|
306
|
-
rubygems_version: 2.7.7
|
312
|
+
rubygems_version: 3.0.1
|
307
313
|
signing_key:
|
308
314
|
specification_version: 4
|
309
315
|
summary: Analyze HTTP Requests
|