-
Notifications
You must be signed in to change notification settings - Fork 4
/
Proxy.rb
519 lines (455 loc) · 15.2 KB
/
Proxy.rb
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
require "yast"
require "shellwords"
module Yast
# Configures FTP and HTTP proxies via sysconfig
# and /root/.curlrc (for YOU)
class ProxyClass < Module
# @return [Boolean] Whether the configuration should be copied to the target system
attr_accessor :to_target
def main
textdomain "proxy"
Yast.import "Summary"
Yast.import "Progress"
Yast.import "Mode"
Yast.import "Stage"
Yast.import "Popup"
@proposal_valid = false
@write_only = false
# Data was modified?
@modified = false
@enabled = false
@http = ""
@https = ""
@ftp = ""
@no = ""
@user = ""
@pass = ""
@to_target = false
end
# domains that should not be proxied; reader
# @return [String]
def no_proxy_domains
clean_up_no_proxy(@no)
end
# domains that should not be proxied; writer
# @param value [String]
def no_proxy_domains=(value)
@no = clean_up_no_proxy(value)
end
# Compatibility:
publish variable: :no, type: "string"
# we need "publish :variable" but it defines an attr_accessor
# so let's undefine it
remove_method :no
remove_method :"no="
alias_method :no, :no_proxy_domains
alias_method :"no=", :"no_proxy_domains="
# Display popup at the end of the proxy configuration
# @param [Boolean] modified true if proxy settings have been modified
def ProxyFinishPopup(modified)
return if !modified
# Popup headline
head = _("Proxy Configuration Successfully Saved")
text = _(
"It is recommended to relogin to make new proxy settings effective."
)
Popup.AnyMessage(head, text)
nil
end
# Create comment for changed file
# @param [String] modul YaST2 module changing the file
# @return comment
# @example ChangedComment("lan") -> # Changed by YaST2 module lan 1.1.2000"
def ChangedComment(modul)
ret = "\n# Changed by YaST2"
ret << " module " << modul if !modul.nil? && modul != ""
out = Convert.to_map(
SCR.Execute(path(".target.bash_output"), "/bin/date '+%x'")
)
date = Ops.get_string(out, "stdout", "")
ret = Ops.add(Ops.add(ret, " "), date) if date != ""
ret
end
# Read settings
# @return true if success
def Read
# Read /etc/sysconfig/proxy
@http = Convert.to_string(SCR.Read(path(".sysconfig.proxy.HTTP_PROXY")))
@http = "" if @http.nil?
@https = Convert.to_string(SCR.Read(path(".sysconfig.proxy.HTTPS_PROXY")))
@https = "" if @https.nil?
@ftp = Convert.to_string(SCR.Read(path(".sysconfig.proxy.FTP_PROXY")))
@ftp = "" if @ftp.nil?
self.no_proxy_domains = SCR.Read(path(".sysconfig.proxy.NO_PROXY")) || ""
@enabled = Convert.to_string(
SCR.Read(path(".sysconfig.proxy.PROXY_ENABLED"))
) == "yes"
# Read /root/.curlrc
if Ops.greater_than(SCR.Read(path(".target.size"), "/root/.curlrc"), 0)
@user = Convert.to_string(
SCR.Read(Builtins.add(path(".root.curlrc"), "--proxy-user"))
)
end
@user = "" if @user.nil?
if Builtins.issubstring(@user, ":")
@pass = Builtins.regexpsub(@user, "^.*:(.*)$", "\\1")
@user = Builtins.regexpsub(@user, "^(.*):.*$", "\\1")
end
@pass = "" if @pass.nil?
if @user.nil?
@user = ""
elsif Builtins.regexpmatch(@user, "^.*\\\\.*$")
@user = Builtins.regexpsub(@user, "^(.*)\\\\(.*)$", "\\1\\2")
end
true
end
def WriteSysconfig
SCR.Write(path(".sysconfig.proxy.PROXY_ENABLED"), @enabled ? "yes" : "no")
SCR.Write(path(".sysconfig.proxy.HTTP_PROXY"), @http)
SCR.Write(path(".sysconfig.proxy.HTTPS_PROXY"), @https)
SCR.Write(path(".sysconfig.proxy.FTP_PROXY"), @ftp)
SCR.Write(path(".sysconfig.proxy.NO_PROXY"), no_proxy_domains)
SCR.Write(path(".sysconfig.proxy"), nil)
end
# Escape backslash characters in .curlrc (bnc#331038)
# see also http://curl.haxx.se/docs/manpage.html#-K for escaping rules
def EscapeForCurlrc(s)
return nil if s.nil?
Builtins.mergestring(Builtins.splitstring(s, "\\"),
"\\\\")
end
def WriteCurlrc
proxyuser = nil
if @user && !@user.empty?
proxyuser = @user
proxyuser << ":" << @pass if @pass && !@pass.empty?
end
options = {
"--proxy-user" => proxyuser,
# bnc#305163
"--proxy" => @http,
# bsc#923788
"--noproxy" => no_proxy_domains
}
# proxy is used, write /root/.curlrc
if @enabled
write_comment = true
options.each do |option, value|
value = nil if value == ""
SCR.Write(path(".root.curlrc") + option, EscapeForCurlrc(value))
next unless !value.nil? && write_comment
SCR.Write(path(".root.curlrc") + option + "comment",
ChangedComment("proxy"))
write_comment = false
end
# proxy is not used, remove proxy-related settings
else
options.each_key do |option|
SCR.Write(path(".root.curlrc") + option, nil)
end
end
SCR.Write(path(".root.curlrc"), nil)
end
# Write proxy settings and apply changes
# @return true if success
def Write
Builtins.y2milestone("Writing configuration")
if !@modified
Builtins.y2milestone(
"No changes to proxy configuration -> nothing to write"
)
return true
end
steps = [_("Update proxy configuration")]
caption = _("Saving Proxy Configuration")
# sleep for longer time, so that progress does not disappear right afterwards
# but only when Progress is visible
sl = (Progress.status == true) ? 500 : 0
Progress.New(caption, " ", Builtins.size(steps), steps, [], "")
Progress.NextStage
Progress.Title(_("Updating proxy configuration..."))
# Update /etc/sysconfig/proxy
WriteSysconfig()
WriteCurlrc()
Builtins.sleep(sl)
Progress.NextStage
# user can't relogin in installation and update, do not show the msg then (bnc#486037, bnc#543469)
ProxyFinishPopup(true) if Mode.normal
# By now the configuration written to the inst-sys should be copied always to the target
# system, so it is set here in order to discharge others from establishing it. (bsc#1185016)
@to_target = true if Stage.initial
@modified = false
true
end
# Get all settings from a map.
# When called by <name>_auto (preparing autoinstallation data)
# the map may be empty.
# @param [Hash] settings autoinstallation settings
# @return true if success
def Import(settings)
settings = deep_copy(settings)
@enabled = Ops.get_boolean(settings, "enabled", false)
@http = Ops.get_string(settings, "http_proxy", "")
@https = Ops.get_string(settings, "https_proxy", "")
@ftp = Ops.get_string(settings, "ftp_proxy", "")
self.no_proxy_domains = Ops.get_string(settings, "no_proxy", "localhost")
@user = Ops.get_string(settings, "proxy_user", "")
@pass = Ops.get_string(settings, "proxy_password", "")
@modified = true
true
end
# Runs tests of the HTTP and FTP proxy
#
# @param [String] http_proxy such as "http://cache.example.com:3128"
# @param [String] https_proxy such as "http://cache.example.com:3128"
# @param [String] ftp_proxy such as "http://cache.example.com:3128"
# @param [String] proxy_user such as "proxy-username"
# @param [String] proxy_password such as "proxy-password"
#
# @return [Hash <String, Hash{String => Object>}] with results of the test
#
# **Structure:**
#
# return = $[
# "HTTP" : $[
# "exit" : _exit_code,
# "stdout" : _stdout,
# "stderr" : _stderr,
# ],
# "HTTPS" : $[
# "exit" : _exit_code,
# "stdout" : _stdout,
# "stderr" : _stderr,
# ],
# "FTP" : $[
# "exit" : _exit_code,
# "stdout" : _stdout,
# "stderr" : _stderr,
# ],
# ]
def RunTestProxy(http_proxy, https_proxy, ftp_proxy, proxy_user, proxy_password)
# /usr/bin/curl --verbose
# --proxy http://server_name:port_number
# --proxy-user user:password
# --url http://www.suse.com or ftp://ftp.suse.com | suggested for HTTP or FTP test
# --url https://www.suse.com --insecure
ret = {}
test_http = (http_proxy != "" && http_proxy != "http://") ? true : false
test_https = (https_proxy != "" && https_proxy != "http://") ? true : false
test_ftp = (ftp_proxy != "" && ftp_proxy != "http://") ? true : false
user_pass = ""
if proxy_user != ""
if proxy_password != ""
user_pass = " --proxy-user #{proxy_user.shellescape}" +
user_pass << ":#{proxy_password.shellescape}"
end
end
# timeout for the connection
timeout_sec = 90
# %1 = http or ftp proxy, %2 = user:password if any, %3 = URL
command = "/usr/bin/curl --verbose --proxy %1 %2 --connect-timeout %3 --url %4"
http_command = Builtins.sformat(
command,
http_proxy.shellescape,
user_pass,
timeout_sec,
"http://www.suse.com"
)
# adding option --insecure to accept the certificate without asking
https_command = Builtins.sformat(
command,
https_proxy.shellescape,
user_pass,
timeout_sec,
"https://www.suse.com --insecure"
)
ftp_command = Builtins.sformat(
command,
ftp_proxy.shellescape,
user_pass,
timeout_sec,
"ftp://ftp.suse.com"
)
Builtins.y2milestone("Running HTTP_PROXY test...")
if test_http
Builtins.y2milestone("Testing HTTP proxy %1", http_proxy)
Ops.set(
ret,
"HTTP",
Convert.convert(
SCR.Execute(path(".target.bash_output"), http_command),
from: "any",
to: "map <string, any>"
)
)
else
Builtins.y2milestone("Skipping HTTP Proxy test, no proxy used.")
Ops.set(
ret,
"HTTP",
"exit" => 0, "stderr" => "", "stdout" => "", "tested" => false
)
end
Builtins.y2milestone("Done.")
Builtins.y2milestone("Running HTTPS_PROXY test...")
if test_https
Builtins.y2milestone("Testing HTTPS proxy %1", https_proxy)
Ops.set(
ret,
"HTTPS",
Convert.convert(
SCR.Execute(path(".target.bash_output"), https_command),
from: "any",
to: "map <string, any>"
)
)
else
Builtins.y2milestone("Skipping HTTPS Proxy test, no proxy used.")
Ops.set(
ret,
"HTTPS",
"exit" => 0, "stderr" => "", "stdout" => "", "tested" => false
)
end
Builtins.y2milestone("Done.")
Builtins.y2milestone("Running FTP_PROXY test...")
if test_ftp
Builtins.y2milestone("Testing FTP proxy %1", ftp_proxy)
Ops.set(
ret,
"FTP",
Convert.convert(
SCR.Execute(path(".target.bash_output"), ftp_command),
from: "any",
to: "map <string, any>"
)
)
else
Builtins.y2milestone("Skipping FTP Proxy test, no proxy used.")
Ops.set(
ret,
"FTP",
"exit" => 0, "stderr" => "", "stdout" => "", "tested" => false
)
end
Builtins.y2milestone("Done.")
deep_copy(ret)
end
# Dump the Routing settings to a map, for autoinstallation use.
# @return autoinstallation settings
def Export
settings = if @enabled
{
"enabled" => true,
"http_proxy" => @http,
"https_proxy" => @https,
"ftp_proxy" => @ftp,
"no_proxy" => no_proxy_domains,
"proxy_user" => @user,
"proxy_password" => @pass
}
else
{ "enabled" => false }
end
deep_copy(settings)
end
# Create proxy summary
# @return summary text
def Summary
# Summary text
ret = if !@enabled
[Summary.Device(_("Proxy is disabled."), "")]
else
# Summary text
[
Summary.Device(
_("Proxy is enabled."),
Ops.add(
Ops.add(
Ops.add(
Ops.add(
"",
# Summary text
if @http != ""
Ops.add(
Builtins.sformat(_("HTTP Proxy: %1"), @http),
"<br>"
)
else
""
end
),
# Summary text
if @https != ""
Ops.add(
Builtins.sformat(_("HTTPS Proxy: %1"), @https),
"<br>"
)
else
""
end
),
# Summary text
if @ftp != ""
Ops.add(Builtins.sformat(_("FTP Proxy: %1"), @ftp), "<br>")
else
""
end
),
""
)
)
]
end
Summary.DevicesList(ret)
end
# Function which returns if the settings were modified
# @return [Boolean] settings were modified
def GetModified
@modified
end
# Function sets internal variable, which indicates, that any
# settings were modified, to "true"
def SetModified
@modified = true
nil
end
# Function returns an environment usable for curl. The proxy user/password
# are read from /root/.curlrc.
def GetEnvironment
return {} if !@enabled
Read() if !@modified
{
"http_proxy" => @http,
"HTTPS_PROXY" => @https,
"FTP_PROXY" => @ftp,
"NO_PROXY" => no_proxy_domains
}
end
publish variable: :proposal_valid, type: "boolean"
publish variable: :write_only, type: "boolean"
publish variable: :modified, type: "boolean"
publish variable: :enabled, type: "boolean"
publish variable: :http, type: "string"
publish variable: :https, type: "string"
publish variable: :ftp, type: "string"
publish variable: :user, type: "string"
publish variable: :pass, type: "string"
publish function: :Read, type: "boolean ()"
publish function: :Write, type: "boolean ()"
publish function: :Import, type: "boolean (map)"
publish function: :RunTestProxy, type: "map <string, map <string, any>> (string, string, string, string, string)"
publish function: :Export, type: "map ()"
publish function: :Summary, type: "string ()"
publish function: :GetModified, type: "boolean ()"
publish function: :SetModified, type: "void ()"
publish function: :GetEnvironment, type: "map <string, string> ()"
private
# Clean up the no_proxy value: not all clients ignore spaces (bsc#1089796)
def clean_up_no_proxy(v)
v.gsub(" ", "")
end
end
Proxy = ProxyClass.new
Proxy.main
end