Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/proxy.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
class Proxy
23
TYPES = {
24
direct: 'DIRECT', # Direct connection, no proxy (default on Windows).
25
manual: 'MANUAL', # Manual proxy settings (e.g., for httpProxy).
26
pac: 'PAC', # Proxy autoconfiguration from URL.
27
auto_detect: 'AUTODETECT', # Proxy autodetection (presumably with WPAD).
28
system: 'SYSTEM' # Use system settings (default on Linux).
29
}.freeze
30
31
ALLOWED = {type: 'proxyType',
32
ftp: 'ftpProxy',
33
http: 'httpProxy',
34
no_proxy: 'noProxy',
35
pac: 'proxyAutoconfigUrl',
36
ssl: 'sslProxy',
37
auto_detect: 'autodetect',
38
socks: 'socksProxy',
39
socks_username: 'socksUsername',
40
socks_password: 'socksPassword',
41
socks_version: 'socksVersion'}.freeze
42
43
ALLOWED.each_key { |t| attr_reader t }
44
45
def self.json_create(data)
46
data['proxyType'] = data['proxyType'].downcase.to_sym
47
return if data['proxyType'] == :unspecified
48
49
proxy = new
50
51
ALLOWED.each do |k, v|
52
proxy.send(:"#{k}=", data[v]) if data.key?(v)
53
end
54
55
proxy
56
end
57
58
def initialize(opts = {})
59
not_allowed = []
60
61
opts.each do |k, v|
62
if ALLOWED.key?(k)
63
send(:"#{k}=", v)
64
else
65
not_allowed << k
66
end
67
end
68
69
return if not_allowed.empty?
70
71
raise ArgumentError, "unknown option#{'s' if not_allowed.size != 1}: #{not_allowed.inspect}"
72
end
73
74
def ==(other)
75
other.is_a?(self.class) && as_json == other.as_json
76
end
77
alias eql? ==
78
79
def ftp=(value)
80
WebDriver.logger.deprecate('FTP proxy support', nil, id: :ftp_proxy)
81
self.type = :manual
82
@ftp = value
83
end
84
85
def http=(value)
86
self.type = :manual
87
@http = value
88
end
89
90
def no_proxy=(value)
91
self.type = :manual
92
@no_proxy = value
93
end
94
95
def ssl=(value)
96
self.type = :manual
97
@ssl = value
98
end
99
100
def pac=(url)
101
self.type = :pac
102
@pac = url
103
end
104
105
def auto_detect=(bool)
106
self.type = :auto_detect
107
@auto_detect = bool
108
end
109
110
def socks=(value)
111
self.type = :manual
112
@socks = value
113
end
114
115
def socks_username=(value)
116
self.type = :manual
117
@socks_username = value
118
end
119
120
def socks_password=(value)
121
self.type = :manual
122
@socks_password = value
123
end
124
125
def socks_version=(value)
126
self.type = :manual
127
@socks_version = value
128
end
129
130
def type=(type)
131
unless TYPES.key? type
132
raise ArgumentError,
133
"invalid proxy type: #{type.inspect}, expected one of #{TYPES.keys.inspect}"
134
end
135
136
if defined?(@type) && type != @type
137
raise ArgumentError, "incompatible proxy type #{type.inspect} (already set to #{@type.inspect})"
138
end
139
140
@type = type
141
end
142
143
def as_json(*)
144
json_result = {
145
'proxyType' => TYPES[type].downcase,
146
'ftpProxy' => ftp,
147
'httpProxy' => http,
148
'noProxy' => no_proxy.is_a?(String) ? no_proxy.split(', ') : no_proxy,
149
'proxyAutoconfigUrl' => pac,
150
'sslProxy' => ssl,
151
'autodetect' => auto_detect,
152
'socksProxy' => socks,
153
'socksUsername' => socks_username,
154
'socksPassword' => socks_password,
155
'socksVersion' => socks_version
156
}.compact
157
158
json_result if json_result.length > 1
159
end
160
161
def to_json(*)
162
JSON.generate as_json
163
end
164
end # Proxy
165
end # WebDriver
166
end # Selenium
167
168