tshield 0.2.0.0 → 0.3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/tshield.rb +1 -0
- data/lib/tshield/configuration.rb +7 -0
- data/lib/tshield/controllers/requests.rb +99 -0
- data/lib/tshield/controllers/sessions.rb +27 -0
- data/lib/tshield/counter.rb +11 -14
- data/lib/tshield/request.rb +21 -12
- data/lib/tshield/server.rb +6 -83
- data/lib/tshield/sessions.rb +24 -0
- data/lib/tshield/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d38191a1b4051da47c94d62f65e23a908c2ff8b3
|
4
|
+
data.tar.gz: 96b0176cb11b3f635b3671406c024d803c480b96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6480b0e7408e523f498d71503ac505daed9bf9a0acb55a24ea5154f42d3b1100e47af69a50e43de8e62b484627970520f13f5cf45d2044af921246d8c13c4676
|
7
|
+
data.tar.gz: 9cffbac477c63f2d84b64d1c3ebc65f25a373fc34f6dd38995cae22c7306fb1f570140751d4c8d2333e11237fb08585192686260266be90d796e68eeef36a27f
|
data/lib/tshield.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
1
3
|
module TShield
|
2
4
|
class Configuration
|
3
5
|
|
4
6
|
attr_accessor :domains
|
7
|
+
attr_writer :session_path
|
5
8
|
|
6
9
|
def initialize(attributes)
|
7
10
|
attributes.each do |key, value|
|
@@ -24,6 +27,10 @@ module TShield
|
|
24
27
|
domains[domain]['headers'] || {}
|
25
28
|
end
|
26
29
|
|
30
|
+
def session_path
|
31
|
+
@session_path ||= '/sessions'
|
32
|
+
end
|
33
|
+
|
27
34
|
private
|
28
35
|
def self.load_configuration
|
29
36
|
config_path = File.join('config', 'tshield.yml')
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'sinatra'
|
4
|
+
|
5
|
+
require 'byebug'
|
6
|
+
|
7
|
+
require 'tshield/options'
|
8
|
+
require 'tshield/configuration'
|
9
|
+
require 'tshield/request'
|
10
|
+
|
11
|
+
module TShield
|
12
|
+
module Controllers
|
13
|
+
module Requests
|
14
|
+
PATHP = /([a-zA-Z\/\.-_]+)/
|
15
|
+
|
16
|
+
def self.registered(app)
|
17
|
+
app.configure :production, :development do
|
18
|
+
app.enable :logging
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
app.get (PATHP) do
|
23
|
+
treat(params, request)
|
24
|
+
end
|
25
|
+
|
26
|
+
app.post (PATHP) do
|
27
|
+
treat(params, request)
|
28
|
+
end
|
29
|
+
|
30
|
+
app.put (PATHP) do
|
31
|
+
treat(params, request)
|
32
|
+
end
|
33
|
+
|
34
|
+
app.patch (PATHP) do
|
35
|
+
treat(params, request)
|
36
|
+
end
|
37
|
+
|
38
|
+
app.head (PATHP) do
|
39
|
+
treat(params, request)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module Helpers
|
44
|
+
def treat(params, request)
|
45
|
+
path = params.fetch('captures', [])[0]
|
46
|
+
|
47
|
+
debugger if TShield::Options.instance.break?(path: path, moment: :before)
|
48
|
+
|
49
|
+
method = request.request_method
|
50
|
+
request_content_type = request.content_type
|
51
|
+
|
52
|
+
headers = {
|
53
|
+
'Content-Type' => request.content_type || 'application/json'
|
54
|
+
}
|
55
|
+
|
56
|
+
add_headers(headers, path)
|
57
|
+
|
58
|
+
options = {
|
59
|
+
method: method,
|
60
|
+
headers: headers,
|
61
|
+
raw_query: request.env['QUERY_STRING'],
|
62
|
+
ip: request.ip
|
63
|
+
}
|
64
|
+
|
65
|
+
if ['POST', 'PUT', 'PATCH'].include? method
|
66
|
+
result = request.body.read.encode('UTF-8', {
|
67
|
+
:invalid => :replace,
|
68
|
+
:undef => :replace,
|
69
|
+
:replace => ''
|
70
|
+
})
|
71
|
+
options[:body] = result
|
72
|
+
end
|
73
|
+
|
74
|
+
set_content_type content_type
|
75
|
+
|
76
|
+
response = TShield::Request.new(path, options).response
|
77
|
+
|
78
|
+
logger.info(
|
79
|
+
"original=#{response.original} method=#{method} path=#{path} content-type=#{request_content_type}")
|
80
|
+
|
81
|
+
response.body
|
82
|
+
end
|
83
|
+
|
84
|
+
def set_content_type(request_content_type)
|
85
|
+
content_type :json
|
86
|
+
end
|
87
|
+
|
88
|
+
def add_headers(headers, path)
|
89
|
+
@configuration ||= TShield::Configuration.singleton
|
90
|
+
domain = @configuration.get_domain_for(path)
|
91
|
+
@configuration.get_headers(domain).each do |source, destiny|
|
92
|
+
headers[destiny] = request.env[source] unless request.env[source].nil?
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
require 'tshield/configuration'
|
4
|
+
require 'tshield/sessions'
|
5
|
+
|
6
|
+
module TShield
|
7
|
+
module Controllers
|
8
|
+
module Sessions
|
9
|
+
def self.registered(app)
|
10
|
+
|
11
|
+
app.get TShield::Configuration.singleton.session_path do
|
12
|
+
TShield::Sessions.current(request.ip).to_json
|
13
|
+
end
|
14
|
+
|
15
|
+
app.post TShield::Configuration.singleton.session_path do
|
16
|
+
TShield::Sessions.start(request.ip, params[:name]).to_json
|
17
|
+
end
|
18
|
+
|
19
|
+
app.delete TShield::Configuration.singleton.session_path do
|
20
|
+
TShield::Sessions.stop(request.ip).to_json
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/lib/tshield/counter.rb
CHANGED
@@ -1,27 +1,24 @@
|
|
1
1
|
module TShield
|
2
2
|
class Counter
|
3
3
|
|
4
|
-
attr_reader :sessions
|
5
|
-
|
6
4
|
def initialize
|
7
|
-
@
|
5
|
+
@requests = {}
|
8
6
|
end
|
9
7
|
|
10
|
-
def add(
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
def add(path, method)
|
9
|
+
requests_to_path = @requests.fetch(path, {})
|
10
|
+
requests_to_method = requests_to_path.fetch(method, 0)
|
11
|
+
|
12
|
+
requests_to_path[method] = requests_to_method += 1
|
13
|
+
@requests[path] = requests_to_path
|
15
14
|
end
|
16
15
|
|
17
|
-
def
|
18
|
-
|
16
|
+
def current(path, method)
|
17
|
+
@requests.fetch(path, {}).fetch(method, 0)
|
19
18
|
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
counter = Counter.new
|
24
|
-
counter
|
20
|
+
def to_json(options = {})
|
21
|
+
@requests.to_json
|
25
22
|
end
|
26
23
|
|
27
24
|
end
|
data/lib/tshield/request.rb
CHANGED
@@ -3,9 +3,9 @@ require 'json'
|
|
3
3
|
require 'byebug'
|
4
4
|
|
5
5
|
require 'tshield/configuration'
|
6
|
-
require 'tshield/counter'
|
7
6
|
require 'tshield/options'
|
8
7
|
require 'tshield/response'
|
8
|
+
require 'tshield/sessions'
|
9
9
|
|
10
10
|
module TShield
|
11
11
|
|
@@ -17,7 +17,6 @@ module TShield
|
|
17
17
|
@path = path
|
18
18
|
@options = options
|
19
19
|
@configuration = TShield::Configuration.singleton
|
20
|
-
@counter = TShield::Counter.singleton
|
21
20
|
request
|
22
21
|
end
|
23
22
|
|
@@ -37,6 +36,7 @@ module TShield
|
|
37
36
|
@response = TShield::Response.new(raw.body, raw.header)
|
38
37
|
@response.original = true
|
39
38
|
end
|
39
|
+
current_session[:counter].add(@path, method) if current_session
|
40
40
|
debugger if TShield::Options.instance.break?(path: @path, moment: :after)
|
41
41
|
@response
|
42
42
|
end
|
@@ -53,29 +53,33 @@ module TShield
|
|
53
53
|
def save(raw_response)
|
54
54
|
content << {body: raw_response.body}
|
55
55
|
write(content)
|
56
|
-
|
57
56
|
content
|
58
57
|
end
|
59
58
|
|
59
|
+
def current_session
|
60
|
+
TShield::Sessions.current(@options[:ip])
|
61
|
+
end
|
62
|
+
|
60
63
|
def content
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
@content
|
64
|
+
@content ||= file_exists ? JSON.parse(File.open(destiny).read) : []
|
65
|
+
end
|
66
|
+
|
67
|
+
def file_exists
|
68
|
+
File.exists?(destiny)
|
67
69
|
end
|
68
70
|
|
69
71
|
def exists
|
70
|
-
|
72
|
+
file_exists && include_current_response?
|
71
73
|
end
|
72
74
|
|
73
75
|
def include_current_response?
|
74
|
-
|
76
|
+
session = current_session
|
77
|
+
@content_idx = session ? session[:counter].current(@path, method) : 0
|
78
|
+
not content[@content_idx].nil?
|
75
79
|
end
|
76
80
|
|
77
81
|
def get_current_response
|
78
|
-
current = content[0]
|
82
|
+
current = content[@content_idx || 0]
|
79
83
|
TShield::Response.new(current['body'], current['header'])
|
80
84
|
end
|
81
85
|
|
@@ -89,6 +93,11 @@ module TShield
|
|
89
93
|
request_path = File.join('requests')
|
90
94
|
Dir.mkdir(request_path) unless File.exists?(request_path)
|
91
95
|
|
96
|
+
if session = current_session
|
97
|
+
request_path = File.join(request_path, session[:name])
|
98
|
+
Dir.mkdir(request_path) unless File.exists?(request_path)
|
99
|
+
end
|
100
|
+
|
92
101
|
domain_path = File.join(request_path, domain.gsub(/.*:\/\//, ''))
|
93
102
|
Dir.mkdir(domain_path) unless File.exists?(domain_path)
|
94
103
|
|
data/lib/tshield/server.rb
CHANGED
@@ -1,94 +1,17 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require 'sinatra'
|
4
2
|
|
5
|
-
require '
|
6
|
-
|
7
|
-
require 'tshield/options'
|
8
|
-
require 'tshield/configuration'
|
9
|
-
require 'tshield/request'
|
3
|
+
require 'tshield/controllers/requests'
|
4
|
+
require 'tshield/controllers/sessions'
|
10
5
|
|
11
6
|
module TShield
|
12
7
|
class Server < Sinatra::Base
|
13
8
|
|
14
|
-
|
15
|
-
enable :logging
|
16
|
-
end
|
17
|
-
|
18
|
-
PATHP = /([a-zA-Z\/\.-_]+)/
|
19
|
-
|
20
|
-
get (PATHP) do
|
21
|
-
treat(params, request)
|
22
|
-
end
|
23
|
-
|
24
|
-
post (PATHP) do
|
25
|
-
treat(params, request)
|
26
|
-
end
|
27
|
-
|
28
|
-
put (PATHP) do
|
29
|
-
treat(params, request)
|
30
|
-
end
|
31
|
-
|
32
|
-
patch (PATHP) do
|
33
|
-
treat(params, request)
|
34
|
-
end
|
35
|
-
|
36
|
-
head (PATHP) do
|
37
|
-
treat(params, request)
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
def treat(params, request)
|
42
|
-
path = params.fetch('captures', [])[0]
|
43
|
-
|
44
|
-
debugger if TShield::Options.instance.break?(path: path, moment: :before)
|
45
|
-
|
46
|
-
method = request.request_method
|
47
|
-
request_content_type = request.content_type
|
9
|
+
include TShield::Controllers::Requests::Helpers
|
48
10
|
|
49
|
-
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
add_headers(headers, path)
|
54
|
-
|
55
|
-
options = {
|
56
|
-
method: method,
|
57
|
-
headers: headers,
|
58
|
-
raw_query: request.env['QUERY_STRING']
|
59
|
-
}
|
60
|
-
|
61
|
-
if ['POST', 'PUT', 'PATCH'].include? method
|
62
|
-
result = request.body.read.encode('UTF-8', {
|
63
|
-
:invalid => :replace,
|
64
|
-
:undef => :replace,
|
65
|
-
:replace => ''
|
66
|
-
})
|
67
|
-
options[:body] = result
|
68
|
-
end
|
69
|
-
|
70
|
-
set_content_type content_type
|
71
|
-
|
72
|
-
response = TShield::Request.new(path, options).response
|
73
|
-
|
74
|
-
logger.info(
|
75
|
-
"original=#{response.original} method=#{method} path=#{path} content-type=#{request_content_type}")
|
76
|
-
|
77
|
-
response.body
|
78
|
-
end
|
79
|
-
|
80
|
-
def set_content_type(request_content_type)
|
81
|
-
content_type :json
|
82
|
-
end
|
83
|
-
|
84
|
-
def add_headers(headers, path)
|
85
|
-
@configuration ||= TShield::Configuration.singleton
|
86
|
-
domain = @configuration.get_domain_for(path)
|
87
|
-
@configuration.get_headers(domain).each do |source, destiny|
|
88
|
-
headers[destiny] = request.env[source] unless request.env[source].nil?
|
89
|
-
end
|
90
|
-
end
|
11
|
+
register TShield::Controllers::Sessions
|
12
|
+
register TShield::Controllers::Requests
|
91
13
|
|
92
14
|
end
|
93
15
|
end
|
94
16
|
|
17
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
require 'tshield/counter'
|
3
|
+
|
4
|
+
module TShield
|
5
|
+
module Sessions
|
6
|
+
def self.start(ip, name)
|
7
|
+
sessions[ip] = {name: name, counter: TShield::Counter.new}
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.stop(ip)
|
11
|
+
sessions[ip] = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.current(ip)
|
15
|
+
sessions[ip]
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
def self.sessions
|
20
|
+
@sessions ||= {}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
data/lib/tshield/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tshield
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diego Rubin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -163,11 +163,14 @@ files:
|
|
163
163
|
- bin/tshield
|
164
164
|
- lib/tshield.rb
|
165
165
|
- lib/tshield/configuration.rb
|
166
|
+
- lib/tshield/controllers/requests.rb
|
167
|
+
- lib/tshield/controllers/sessions.rb
|
166
168
|
- lib/tshield/counter.rb
|
167
169
|
- lib/tshield/options.rb
|
168
170
|
- lib/tshield/request.rb
|
169
171
|
- lib/tshield/response.rb
|
170
172
|
- lib/tshield/server.rb
|
173
|
+
- lib/tshield/sessions.rb
|
171
174
|
- lib/tshield/version.rb
|
172
175
|
- spec/spec_helper.rb
|
173
176
|
- spec/tshield/configuration_spec.rb
|