Repository navigation
Expand file tree
/
Copy pathlocal.rb
More file actions
195 lines (175 loc) · 6.01 KB
/
Copy pathlocal.rb
File metadata and controls
195 lines (175 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require 'browserstack/localbinary'
require 'browserstack/localexception'
require 'json'
require 'fileutils'
module BrowserStack
class Local
attr_reader :pid
def initialize(key = ENV["BROWSERSTACK_ACCESS_KEY"])
@key = key
@user_arguments = []
@logfile = File.join(Dir.pwd, "local.log")
@is_windows = RbConfig::CONFIG['host_os'].match(/mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/)
@exec = @is_windows ? "call" : "exec";
end
def add_args(key, value=nil)
if key == "key"
@key = value
elsif key == "v" && value.to_s != "false"
@verbose_flag = "-vvv"
elsif key == "force" && value.to_s != "false"
@force_flag = "-force"
elsif key == "only" && value.to_s != "false"
@only_flag = "-only"
elsif key == "onlyAutomate" && value.to_s != "false"
@only_automate_flag = "-onlyAutomate"
elsif key == "forcelocal" && value.to_s != "false"
@force_local_flag = "-forcelocal"
elsif key == "localIdentifier"
@local_identifier_flag = value
elsif key == "f"
@folder_flag = "-f"
@folder_path = value
elsif key == "proxyHost"
@proxy_host = value
elsif key == "proxyPort"
@proxy_port = value
elsif key == "proxyUser"
@proxy_user = value
elsif key == "proxyPass"
@proxy_pass = value
elsif key == "hosts"
@hosts = value
elsif key == "logfile"
@logfile = value
elsif key == "binarypath"
@binary_path = value
elsif key == "forceproxy" && value.to_s != "false"
@force_proxy_flag = "-forceproxy"
else
if value.to_s.downcase.eql?("true")
@user_arguments << "-#{key}"
else
@user_arguments += ["-#{key}", value]
end
end
end
def start(options = {})
options.each_pair do |key, value|
self.add_args(key, value)
end
@binary_path = if @binary_path.nil?
BrowserStack::LocalBinary.new(
auth_token: @key,
proxy_host: @proxy_host,
proxy_port: @proxy_port
).binary_path
else
@binary_path
end
# Create/truncate the logfile without a shell. The previous
# `system("echo ... > #{@logfile}")` passed @logfile to /bin/sh (or cmd.exe),
# so shell metacharacters in a caller-supplied logfile path executed as commands
# (CWE-78). File.write treats the path purely as a filename.
logfile_dir = File.dirname(@logfile)
FileUtils.mkdir_p(logfile_dir) unless File.directory?(logfile_dir)
begin
File.write(@logfile, "")
rescue SystemCallError => e
raise BrowserStack::LocalException.new("Unable to open logfile: #{e.message}")
end
if defined? spawn
@process = IO.popen(start_command_args)
else
@process = IO.popen(start_command)
end
while true
begin
line = @process.readline
rescue EOFError => e
sleep 1
next
end
data = JSON.parse(line) rescue {"message" => "Unable to parse daemon mode JSON output"}
if data['state'].to_s != "connected"
@process.close
raise BrowserStack::LocalException.new(data["message"]["message"])
return
else
@pid = data["pid"]
break
end
end
end
def isRunning
return true if (!@pid.nil? && Process.kill(0, @pid)) rescue false
end
def stop
return if @pid.nil?
@process.close
if defined? spawn
@process = IO.popen(stop_command_args)
else
@process = IO.popen(stop_command)
end
@process.close
@pid = nil
end
# Public accessor used by callers for debugging/logging. Return the command
# with the access key masked so it is never written to logs, CI artifacts or
# error trackers (CWE-312). The real key is still used for execution via
# start_command_args / start_command(false).
def command
start_command(true)
end
# Prevent Ruby's default #inspect from dumping @key when a Local instance is
# logged or included in an exception payload (CWE-312).
def inspect
redacted = instance_variables.map do |var|
value = var == :@key && !@key.to_s.empty? ? "[REDACTED]" : instance_variable_get(var)
"#{var}=#{value.inspect}"
end.join(", ")
"#<#{self.class}:0x#{format('%016x', object_id << 1)} #{redacted}>"
end
def start_command(redact = false)
key = redact && !@key.to_s.empty? ? "[REDACTED]" : @key
cmd = "#{@binary_path} -d start -logFile '#{@logfile}' #{@folder_flag} #{key} #{@folder_path} #{@force_local_flag}"
cmd += " -localIdentifier #{@local_identifier_flag}" if @local_identifier_flag
cmd += " #{@only_flag} #{@only_automate_flag}"
cmd += " -proxyHost #{@proxy_host}" if @proxy_host
cmd += " -proxyPort #{@proxy_port}" if @proxy_port
cmd += " -proxyUser #{@proxy_user}" if @proxy_user
cmd += " -proxyPass #{@proxy_pass}" if @proxy_pass
cmd += " #{@force_proxy_flag} #{@force_flag} #{@verbose_flag} #{@hosts} #{@user_arguments.join(" ")} 2>&1"
cmd.strip
end
def start_command_args
args = [@binary_path, "-d", "start", "-logFile", @logfile, @key, @folder_flag, @folder_path, @force_local_flag]
args += ["-localIdentifier", @local_identifier_flag] if @local_identifier_flag
args += [@only_flag, @only_automate_flag]
args += ["-proxyHost", @proxy_host] if @proxy_host
args += ["-proxyPort", @proxy_port] if @proxy_port
args += ["-proxyUser", @proxy_user] if @proxy_user
args += ["-proxyPass", @proxy_pass] if @proxy_pass
args += [@force_proxy_flag, @force_flag, @verbose_flag, @hosts]
args += @user_arguments
args = args.select {|a| a.to_s != "" }
args.push(:err => [:child, :out])
args
end
def stop_command
if @local_identifier_flag
return "#{@binary_path} -d stop -localIdentifier #{@local_identifier_flag}".strip
else
return "#{@binary_path} -d stop".strip
end
end
def stop_command_args
args = ["#{@binary_path}", "-d", "stop"]
args += ["-localIdentifier", "#{@local_identifier_flag}"] if @local_identifier_flag
args = args.select {|a| a.to_s != "" }
args.push(:err => [:child, :out])
args
end
end
end