Page Menu
Home
VyOS Platform
Search
Configure Global Search
Log In
Files
F118306052
system-proxy.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
system-proxy.py
View Options
#!/usr/bin/env python3
#
# Copyright (C) 2018 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
import
sys
import
os
import
re
from
vyos
import
ConfigError
from
vyos.config
import
Config
from
vyos
import
airbag
airbag
.
enable
()
proxy_def
=
r'/etc/profile.d/vyos-system-proxy.sh'
def
get_config
():
c
=
Config
()
if
not
c
.
exists
(
'system proxy'
):
return
None
c
.
set_level
(
'system proxy'
)
cnf
=
{
'url'
:
None
,
'port'
:
None
,
'usr'
:
None
,
'passwd'
:
None
}
if
c
.
exists
(
'url'
):
cnf
[
'url'
]
=
c
.
return_value
(
'url'
)
if
c
.
exists
(
'port'
):
cnf
[
'port'
]
=
c
.
return_value
(
'port'
)
if
c
.
exists
(
'username'
):
cnf
[
'usr'
]
=
c
.
return_value
(
'username'
)
if
c
.
exists
(
'password'
):
cnf
[
'passwd'
]
=
c
.
return_value
(
'password'
)
return
cnf
def
verify
(
c
):
if
not
c
:
return
None
if
not
c
[
'url'
]
or
not
c
[
'port'
]:
raise
ConfigError
(
"proxy url and port requires a value"
)
elif
c
[
'usr'
]
and
not
c
[
'passwd'
]:
raise
ConfigError
(
"proxy password requires a value"
)
elif
not
c
[
'usr'
]
and
c
[
'passwd'
]:
raise
ConfigError
(
"proxy username requires a value"
)
def
generate
(
c
):
if
not
c
:
return
None
if
not
c
[
'usr'
]:
return
str
(
"export http_proxy={url}:{port}
\n
export https_proxy=$http_proxy
\n
export ftp_proxy=$http_proxy"
.
format
(
url
=
c
[
'url'
],
port
=
c
[
'port'
]))
else
:
return
str
(
"export http_proxy=http://{usr}:{passwd}@{url}:{port}
\n
export https_proxy=$http_proxy
\n
export ftp_proxy=$http_proxy"
.
format
(
url
=
re
.
sub
(
'http://'
,
''
,
c
[
'url'
]),
port
=
c
[
'port'
],
usr
=
c
[
'usr'
],
passwd
=
c
[
'passwd'
]))
def
apply
(
ln
):
if
not
ln
and
os
.
path
.
exists
(
proxy_def
):
os
.
remove
(
proxy_def
)
else
:
open
(
proxy_def
,
'w'
)
.
write
(
"# generated by system-proxy.py
\n
{}
\n
"
.
format
(
ln
))
if
__name__
==
'__main__'
:
try
:
c
=
get_config
()
verify
(
c
)
ln
=
generate
(
c
)
apply
(
ln
)
except
ConfigError
as
e
:
print
(
e
)
sys
.
exit
(
1
)
File Metadata
Details
Attached
Mime Type
text/x-script.python
Expires
Sat, Oct 17, 12:11 AM (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4350842
Default Alt Text
system-proxy.py (2 KB)
Attached To
Mode
rVYOSONEX vyos-1x
Attached
Detach File
Event Timeline
Log In to Comment