superbot-teleport 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/superbot/teleport/cli.rb +59 -22
- data/lib/superbot/teleport/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4cf993d114e2d4d467b2b14447bc00703fec337a3a3ed31493b276b9b497c8b0
|
4
|
+
data.tar.gz: a5d01bc4e1ff057bdbabf44fe7f8442a179c51f39f1f52c1a8846a6461910ba8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f05f544ed9c7b1953f287473deb3abe8d1946431cf148ea44f3c609d831c8da8307fd7fa8ae679c3ac7ce8c15fa38590e179090fb9a3f4b8019a6de8fc2704e
|
7
|
+
data.tar.gz: 19be38e67e0bd137c11eeb887a521dd6ed5043e4087bad4ae5270c8be7055361b99ce2591578ef7b424bba4994ded339f114a9a6da01ce7ffbc4a7d7f66cf0af
|
data/Gemfile.lock
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require 'sinatra'
|
1
|
+
require 'sinatra/base'
|
2
2
|
require 'excon'
|
3
|
+
require 'json'
|
3
4
|
|
4
5
|
module Superbot
|
5
6
|
module Teleport
|
@@ -10,25 +11,13 @@ module Superbot
|
|
10
11
|
s.set :show_exceptions, false
|
11
12
|
|
12
13
|
s.before do
|
13
|
-
$
|
14
|
-
end
|
15
|
-
|
16
|
-
s.error Excon::Error::Socket do
|
17
|
-
puts "="*30
|
18
|
-
puts $__request.path_info
|
19
|
-
|
20
|
-
if $!.message.end_with? "(Errno::ECONNREFUSED)"
|
21
|
-
status 500
|
22
|
-
"upstream failed"
|
23
|
-
else
|
24
|
-
raise "unknown"
|
25
|
-
end
|
14
|
+
$__superbot_teleport_request_for_errors = request
|
26
15
|
end
|
27
16
|
|
28
17
|
s.set :connection, (
|
29
18
|
Excon.new ENV.fetch("SUPERBOT_TELEPORT_UPSTREAM_URL"), {
|
30
19
|
persistent: true,
|
31
|
-
connect_timeout:
|
20
|
+
connect_timeout: 3,
|
32
21
|
read_timeout: 5,
|
33
22
|
write_timeout: 5,
|
34
23
|
debug_request: (ENV["EXCON_DEBUG"] == "true"),
|
@@ -36,25 +25,73 @@ module Superbot
|
|
36
25
|
}
|
37
26
|
)
|
38
27
|
|
39
|
-
|
40
|
-
|
41
|
-
|
28
|
+
s.helpers do
|
29
|
+
def request_path(params)
|
30
|
+
params[:splat].join("/")
|
31
|
+
end
|
32
|
+
|
33
|
+
def safe_parse_json(string_or_io, on_error: nil)
|
34
|
+
JSON.parse (string_or_io.respond_to?(:read) ? string_or_io.read : string_or_io)
|
35
|
+
rescue
|
36
|
+
on_error
|
37
|
+
end
|
38
|
+
|
39
|
+
def proxy(method, params, opts={})
|
40
|
+
raise "DELETE may not contain body" if method == :delete && opts[:body]
|
41
|
+
opts[:headers] ||= {}
|
42
42
|
|
43
43
|
unless ENV.fetch("SUPERBOT_TELEPORT_UPSTREAM_URL").include? "localhost"
|
44
|
-
path = "wd/hub/#{
|
44
|
+
path = "wd/hub/#{request_path(params)}"
|
45
45
|
headers["Content-Type"] = "application/json"
|
46
|
+
else
|
47
|
+
path = request_path
|
46
48
|
end
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
-
})
|
50
|
+
settings.connection.request({method: method, path: path}.merge(opts))
|
51
|
+
end
|
51
52
|
|
53
|
+
def respond(upstream)
|
52
54
|
headers = upstream.headers
|
53
55
|
status upstream.status
|
54
56
|
upstream.body
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
60
|
+
s.get "/wd/hub/*" do
|
61
|
+
respond proxy(:get, params, {headers: headers, body: request.body})
|
62
|
+
end
|
63
|
+
|
64
|
+
s.post "/wd/hub/*" do
|
65
|
+
case request_path(params)
|
66
|
+
when "session"
|
67
|
+
parsed_body = safe_parse_json request.body, on_error: {}
|
68
|
+
|
69
|
+
if ENV['SUPERBOT_TELEPORT_REGION'] && parsed_body['desiredCapabilities']
|
70
|
+
parsed_body['desiredCapabilities']['superOptions'] = { "region": ENV['SUPERBOT_TELEPORT_REGION'] }
|
71
|
+
end
|
72
|
+
|
73
|
+
respond proxy(:post, params, {headers: headers, body: parsed_body.to_json, read_timeout: 500})
|
74
|
+
else
|
75
|
+
respond proxy(:post, params, {headers: headers, body: request.body})
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
s.delete "/wd/hub/*" do
|
80
|
+
respond proxy(:delete, params, {headers: headers})
|
81
|
+
end
|
82
|
+
|
83
|
+
s.error Excon::Error::Socket do
|
84
|
+
puts "="*30
|
85
|
+
puts $__superbot_teleport_request_for_errors.path_info
|
86
|
+
|
87
|
+
if $!.message.end_with? "(Errno::ECONNREFUSED)"
|
88
|
+
status 500
|
89
|
+
"upstream does not respond"
|
90
|
+
else
|
91
|
+
raise "unknown: #{$!.message}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
58
95
|
s.run!
|
59
96
|
end
|
60
97
|
end
|