From b4dd7b1f47ba00d393205af4772490bc99b55c30 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 17:04:27 -0300 Subject: [PATCH 01/14] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 987867a..dd49e3c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## proxy_configuraion +## proxy_configuration [![Built with ❤](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com/#) [![made with &hearts in Python](https://img.shields.io/badge/made%20with%20%E2%9D%A4%20in-Python-red.svg)](http://shields.io/#your-badge) From 3f93cda2dfb9fa8e8c275550d7992c5864ebc2be Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 18:20:49 -0300 Subject: [PATCH 02/14] docs improvements --- README.md | 48 +++++++++++++++++++++--------------------------- proxy.py | 6 +++--- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index dd49e3c..52672e0 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,37 @@ -## proxy_configuration +# ProxyConfiguration: A Simple Proxy Configuration Tool -[![Built with ❤](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com/#) -[![made with &hearts in Python](https://img.shields.io/badge/made%20with%20%E2%9D%A4%20in-Python-red.svg)](http://shields.io/#your-badge) +[![made with ❤ in Python](https://img.shields.io/badge/made%20with%20❤%20in-Python-red.svg)](http://shields.io/#your-badge) [![works on Linux](https://img.shields.io/badge/works%20on-Linux-orange.svg)](http://shields.io/#your-badge) ## Screenshot ![screenshot](/img/scrshot.png) - ## Requirements -

Python3

- To install python3 go to http://python.org + +- Python 3.6+ + +To install Python 3, visit [python.org](https://www.python.org). ## About - This is a script which configures removes the hassle of configuring proxy manually by supporting system wide proxy_configuration. - It is kept as simple as possible . No extra library other than the one that comes with python3 is used. - Currently tested on Ubuntu 14.04, 16.04, 18.04. +This script removes the hassle of configuring a proxy manually by supporting system-wide proxy configuration. It is kept +as simple as possible and does not use any additional libraries other than those that come with Python 3. - There are four options - 1)Set proxy : Takes input from the user and modifies the required files. - 2)Remove proxy : Remove any proxy settings if present in the files. - 3)View Proxy : Displays the current proxy settings if there are any. - 4)Restore default : Restores to the state before running this script for the first time. +Currently tested on Ubuntu 14.04 LTS, 16.04 LTS, 18.04 LTS, 22.04 LTS. -## Run +There are four options: -``` -chmod +x proxy.py -sudo ./proxy.py -``` +1. **Set proxy:** Takes input from the user and modifies the required files. +2. **Remove proxy:** Removes any proxy settings present in the files. +3. **View Proxy:** Displays the current proxy settings, if any. +4. **Restore default:** Restores the state before running this script for the first time. -## Support - Currently it modifies the following files - 1)/etc/apt/apt.conf - 2)/etc/environment - 3)/etc/bash.bashrc +## Example run +```sh +chmod +x proxy.py +``` -## contribution - contributors are more than welcome to contribute to this project. - +``` sh +sudo ./proxy.py +``` \ No newline at end of file diff --git a/proxy.py b/proxy.py index 8028821..ed07ba6 100755 --- a/proxy.py +++ b/proxy.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 """ created by : @@ -21,10 +21,10 @@ # This files takes the location as input and writes the proxy authentication import getpass # for taking password input -import shutil # for copying file -import sys import os import os.path # for checking if file is present or not +import shutil # for copying file +import sys apt_ = r'/etc/apt/apt.conf' apt_backup = r'./.backup_proxy/apt.txt' From 7f79d3a58e1e66f677e988d0d66777488f07b531 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 18:43:31 -0300 Subject: [PATCH 03/14] docs improvements --- proxy.py | 77 ++++++++++++++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/proxy.py b/proxy.py index ed07ba6..0ac5341 100755 --- a/proxy.py +++ b/proxy.py @@ -1,42 +1,36 @@ #!/usr/bin/env python3 """ -created by : +Script to configure system-wide proxy settings on Linux distributions. + +created by: Nityananda Gohain School of Engineering, Tezpur University 27/10/17 -""" - - -# run it as sudo - -""" -Three files will be modified -1) /etc/apt/apt.conf -2) /etc/environment -3) /etc/bash.bashrc +Modified by: +Pablo Peitsch +Data Scientist, UNSAM, ARG """ # This files takes the location as input and writes the proxy authentication -import getpass # for taking password input +import getpass import os -import os.path # for checking if file is present or not -import shutil # for copying file +import shutil import sys -apt_ = r'/etc/apt/apt.conf' -apt_backup = r'./.backup_proxy/apt.txt' -bash_ = r'/etc/bash.bashrc' -bash_backup = r'./.backup_proxy/bash.txt' -env_ = r'/etc/environment' -env_backup = r'./.backup_proxy/env.txt' +APT_CONF = '/etc/apt/apt.conf' +APT_BACKUP = './.backup_proxy/apt.txt' +BASH_BASHRC = r'/etc/bash.bashrc' +BASH_BACKUP = r'./.backup_proxy/bash.txt' +ENVIRONMENT = r'/etc/environment' +ENV_BACKUP = r'./.backup_proxy/env.txt' # This function directly writes to the apt.conf file def writeToApt(proxy, port, username, password, flag): - filepointer = open(apt_, "w") + filepointer = open(APT_CONF, "w") if not flag: filepointer.write( 'Acquire::http::proxy "http://{0}:{1}@{2}:{3}/";\n'.format(username, password, proxy, port)) @@ -53,7 +47,7 @@ def writeToApt(proxy, port, username, password, flag): # Fist deletes the lines containng http:// , https://, ftp:// def writeToEnv(proxy, port, username, password, flag): # find and delete line containing http://, https://, ftp:// - with open(env_, "r+") as opened_file: + with open(ENVIRONMENT, "r+") as opened_file: lines = opened_file.readlines() opened_file.seek(0) # moves the file pointer to the beginning for line in lines: @@ -63,7 +57,7 @@ def writeToEnv(proxy, port, username, password, flag): # writing starts if not flag: - filepointer = open(env_, "a") + filepointer = open(ENVIRONMENT, "a") filepointer.write( 'http_proxy="http://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) filepointer.write( @@ -78,7 +72,7 @@ def writeToEnv(proxy, port, username, password, flag): # This function will write to the def writeToBashrc(proxy, port, username, password, flag): # find and delete http:// , https://, ftp:// - with open(bash_, "r+") as opened_file: + with open(BASH_BASHRC, "r+") as opened_file: lines = opened_file.readlines() opened_file.seek(0) for line in lines: @@ -88,7 +82,7 @@ def writeToBashrc(proxy, port, username, password, flag): # writing starts if not flag: - filepointer = open(bash_, "a") + filepointer = open(BASH_BASHRC, "a") filepointer.write( 'export http_proxy="http://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) filepointer.write( @@ -114,23 +108,24 @@ def set_proxy(flag): def view_proxy(): # finds the size of apt file - size = os.path.getsize(apt_) + size = os.path.getsize(APT_CONF) if size: - filepointer = open(apt_, "r") + filepointer = open(APT_CONF, "r") string = filepointer.readline() - print('\nHTTP Proxy: ' + string[string.rfind('@')+1 : string.rfind(':')]) - print('Port: ' + string[string.rfind(':')+1 : string.rfind('/')]) + print('\nHTTP Proxy: ' + string[string.rfind('@') + 1: string.rfind(':')]) + print('Port: ' + string[string.rfind(':') + 1: string.rfind('/')]) print('Username: ' + string.split('://')[1].split(':')[0]) - print('Password: ' + '*' * len(string[string.rfind(':',0,string.rfind('@'))+1 : string.rfind('@')])) + print('Password: ' + '*' * len(string[string.rfind(':', 0, string.rfind('@')) + 1: string.rfind('@')])) filepointer.close() else: print("No proxy is set") + def restore_default(): # copy from backup to main - shutil.copy(apt_backup, apt_) - shutil.copy(env_backup, env_) - shutil.copy(bash_backup, bash_) + shutil.copy(APT_BACKUP, APT_CONF) + shutil.copy(ENV_BACKUP, ENVIRONMENT) + shutil.copy(BASH_BACKUP, BASH_BASHRC) # The main Function Starts @@ -141,10 +136,10 @@ def restore_default(): # create backup if not present if not os.path.isdir("./.backup_proxy"): os.makedirs("./.backup_proxy") - if os.path.isfile(apt_): - shutil.copyfile(apt_, apt_backup) - shutil.copyfile(env_, env_backup) - shutil.copyfile(bash_, bash_backup) + if os.path.isfile(APT_CONF): + shutil.copyfile(APT_CONF, APT_BACKUP) + shutil.copyfile(ENVIRONMENT, ENV_BACKUP) + shutil.copyfile(BASH_BASHRC, BASH_BACKUP) # choice print("Please run this program as Super user(sudo)\n") @@ -155,13 +150,13 @@ def restore_default(): print("5:) Exit") choice = int(input("\nChoice (1/2/3/4/5) : ")) - if(choice == 1): + if choice == 1: set_proxy(flag=0) - elif(choice == 2): + elif choice == 2: set_proxy(flag=1) - elif(choice == 3): + elif choice == 3: view_proxy() - elif(choice == 4): + elif choice == 4: restore_default() else: sys.exit() From 19827488bc1387a3027ea280c526324bc420a8bc Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 18:45:23 -0300 Subject: [PATCH 04/14] write_to_apt() function improvement --- proxy.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/proxy.py b/proxy.py index 0ac5341..f9464d8 100755 --- a/proxy.py +++ b/proxy.py @@ -28,19 +28,22 @@ ENV_BACKUP = r'./.backup_proxy/env.txt' -# This function directly writes to the apt.conf file -def writeToApt(proxy, port, username, password, flag): - filepointer = open(APT_CONF, "w") - if not flag: - filepointer.write( - 'Acquire::http::proxy "http://{0}:{1}@{2}:{3}/";\n'.format(username, password, proxy, port)) - filepointer.write( - 'Acquire::https::proxy "https://{0}:{1}@{2}:{3}/";\n'.format(username, password, proxy, port)) - filepointer.write( - 'Acquire::ftp::proxy "ftp://{0}:{1}@{2}:{3}/";\n'.format(username, password, proxy, port)) - filepointer.write( - 'Acquire::socks::proxy "socks://{0}:{1}@{2}:{3}/";\n'.format(username, password, proxy, port)) - filepointer.close() +def write_to_apt(proxy, port, username, password, flag): + """ + Writes the proxy configuration to the apt.conf file. + + :param proxy: Proxy address + :param port: Proxy port + :param username: Username for authentication + :param password: Password for authentication + :param flag: Flag indicating whether to remove the configuration + """ + with open(APT_CONF, "w") as filepointer: + if not flag: + filepointer.write(f'Acquire::http::proxy "http://{username}:{password}@{proxy}:{port}/";\n') + filepointer.write(f'Acquire::https::proxy "https://{username}:{password}@{proxy}:{port}/";\n') + filepointer.write(f'Acquire::ftp::proxy "ftp://{username}:{password}@{proxy}:{port}/";\n') + filepointer.write(f'Acquire::socks::proxy "socks://{username}:{password}@{proxy}:{port}/";\n') # This function writes to the environment file @@ -101,7 +104,7 @@ def set_proxy(flag): port = input("Enter port : ") username = input("Enter username : ") password = getpass.getpass("Enter password : ") - writeToApt(proxy, port, username, password, flag) + write_to_apt(proxy, port, username, password, flag) writeToEnv(proxy, port, username, password, flag) writeToBashrc(proxy, port, username, password, flag) From 186394e8cf62d6ec9b7e6f032fabce9425247e22 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 18:46:30 -0300 Subject: [PATCH 05/14] write_to_env() function improvement --- proxy.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/proxy.py b/proxy.py index f9464d8..257ecf4 100755 --- a/proxy.py +++ b/proxy.py @@ -46,30 +46,30 @@ def write_to_apt(proxy, port, username, password, flag): filepointer.write(f'Acquire::socks::proxy "socks://{username}:{password}@{proxy}:{port}/";\n') -# This function writes to the environment file -# Fist deletes the lines containng http:// , https://, ftp:// -def writeToEnv(proxy, port, username, password, flag): - # find and delete line containing http://, https://, ftp:// +def write_to_env(proxy, port, username, password, flag): + """ + Writes the proxy configuration to the environment file. + + :param proxy: Proxy address + :param port: Proxy port + :param username: Username for authentication + :param password: Password for authentication + :param flag: Flag indicating whether to remove the configuration + """ with open(ENVIRONMENT, "r+") as opened_file: lines = opened_file.readlines() - opened_file.seek(0) # moves the file pointer to the beginning + opened_file.seek(0) for line in lines: - if r"http://" not in line and r"https://" not in line and r"ftp://" not in line and r"socks://" not in line: + if all(protocol not in line for protocol in ["http://", "https://", "ftp://", "socks://"]): opened_file.write(line) opened_file.truncate() - # writing starts if not flag: - filepointer = open(ENVIRONMENT, "a") - filepointer.write( - 'http_proxy="http://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) - filepointer.write( - 'https_proxy="https://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) - filepointer.write( - 'ftp_proxy="ftp://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) - filepointer.write( - 'socks_proxy="socks://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) - filepointer.close() + with open(ENVIRONMENT, "a") as filepointer: + filepointer.write(f'http_proxy="http://{username}:{password}@{proxy}:{port}/"\n') + filepointer.write(f'https_proxy="https://{username}:{password}@{proxy}:{port}/"\n') + filepointer.write(f'ftp_proxy="ftp://{username}:{password}@{proxy}:{port}/"\n') + filepointer.write(f'socks_proxy="socks://{username}:{password}@{proxy}:{port}/"\n') # This function will write to the @@ -105,7 +105,7 @@ def set_proxy(flag): username = input("Enter username : ") password = getpass.getpass("Enter password : ") write_to_apt(proxy, port, username, password, flag) - writeToEnv(proxy, port, username, password, flag) + write_to_env(proxy, port, username, password, flag) writeToBashrc(proxy, port, username, password, flag) From 929349b851deea035d10c9da64b4b2a9f7f30719 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 18:48:58 -0300 Subject: [PATCH 06/14] write_to_bashrc() function improvement --- proxy.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/proxy.py b/proxy.py index 257ecf4..ce128de 100755 --- a/proxy.py +++ b/proxy.py @@ -72,29 +72,30 @@ def write_to_env(proxy, port, username, password, flag): filepointer.write(f'socks_proxy="socks://{username}:{password}@{proxy}:{port}/"\n') -# This function will write to the -def writeToBashrc(proxy, port, username, password, flag): - # find and delete http:// , https://, ftp:// +def write_to_bashrc(proxy, port, username, password, flag): + """ + Writes the proxy configuration to the bash.bashrc file. + + :param proxy: Proxy address + :param port: Proxy port + :param username: Username for authentication + :param password: Password for authentication + :param flag: Flag indicating whether to remove the configuration + """ with open(BASH_BASHRC, "r+") as opened_file: lines = opened_file.readlines() opened_file.seek(0) for line in lines: - if r"http://" not in line and r'"https://' not in line and r"ftp://" not in line and r"socks://" not in line: + if all(protocol not in line for protocol in ["http://", "https://", "ftp://", "socks://"]): opened_file.write(line) opened_file.truncate() - # writing starts if not flag: - filepointer = open(BASH_BASHRC, "a") - filepointer.write( - 'export http_proxy="http://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) - filepointer.write( - 'export https_proxy="https://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) - filepointer.write( - 'export ftp_proxy="ftp://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) - filepointer.write( - 'export socks_proxy="socks://{0}:{1}@{2}:{3}/"\n'.format(username, password, proxy, port)) - filepointer.close() + with open(BASH_BASHRC, "a") as filepointer: + filepointer.write(f'export http_proxy="http://{username}:{password}@{proxy}:{port}/"\n') + filepointer.write(f'export https_proxy="https://{username}:{password}@{proxy}:{port}/"\n') + filepointer.write(f'export ftp_proxy="ftp://{username}:{password}@{proxy}:{port}/"\n') + filepointer.write(f'export socks_proxy="socks://{username}:{password}@{proxy}:{port}/"\n') def set_proxy(flag): @@ -106,7 +107,7 @@ def set_proxy(flag): password = getpass.getpass("Enter password : ") write_to_apt(proxy, port, username, password, flag) write_to_env(proxy, port, username, password, flag) - writeToBashrc(proxy, port, username, password, flag) + write_to_bashrc(proxy, port, username, password, flag) def view_proxy(): From 15e08dd967811be400f8b0fc35989b90fe49dae5 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 18:49:57 -0300 Subject: [PATCH 07/14] set_proxy() function improvement --- proxy.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/proxy.py b/proxy.py index ce128de..fd26adb 100755 --- a/proxy.py +++ b/proxy.py @@ -99,12 +99,17 @@ def write_to_bashrc(proxy, port, username, password, flag): def set_proxy(flag): + """ + Sets or removes the proxy configuration. + + :param flag: Flag indicating whether to remove the configuration + """ proxy, port, username, password = "", "", "", "" if not flag: - proxy = input("Enter proxy : ") - port = input("Enter port : ") - username = input("Enter username : ") - password = getpass.getpass("Enter password : ") + proxy = input("Enter proxy: ") + port = input("Enter port: ") + username = input("Enter username: ") + password = getpass.getpass("Enter password: ") write_to_apt(proxy, port, username, password, flag) write_to_env(proxy, port, username, password, flag) write_to_bashrc(proxy, port, username, password, flag) From 1c12a1cc7c2e0fbdf65c35e0c344807455e8d719 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 18:53:21 -0300 Subject: [PATCH 08/14] view_proxy() function improvement --- proxy.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/proxy.py b/proxy.py index fd26adb..fee439b 100755 --- a/proxy.py +++ b/proxy.py @@ -22,10 +22,10 @@ APT_CONF = '/etc/apt/apt.conf' APT_BACKUP = './.backup_proxy/apt.txt' -BASH_BASHRC = r'/etc/bash.bashrc' -BASH_BACKUP = r'./.backup_proxy/bash.txt' -ENVIRONMENT = r'/etc/environment' -ENV_BACKUP = r'./.backup_proxy/env.txt' +BASH_BASHRC = '/etc/bash.bashrc' +BASH_BACKUP = './.backup_proxy/bash.txt' +ENVIRONMENT = '/etc/environment' +ENV_BACKUP = './.backup_proxy/env.txt' def write_to_apt(proxy, port, username, password, flag): @@ -116,16 +116,16 @@ def set_proxy(flag): def view_proxy(): - # finds the size of apt file - size = os.path.getsize(APT_CONF) - if size: - filepointer = open(APT_CONF, "r") - string = filepointer.readline() - print('\nHTTP Proxy: ' + string[string.rfind('@') + 1: string.rfind(':')]) - print('Port: ' + string[string.rfind(':') + 1: string.rfind('/')]) - print('Username: ' + string.split('://')[1].split(':')[0]) - print('Password: ' + '*' * len(string[string.rfind(':', 0, string.rfind('@')) + 1: string.rfind('@')])) - filepointer.close() + """ + Displays the current proxy configuration. + """ + if os.path.getsize(APT_CONF): + with open(APT_CONF, "r") as filepointer: + line = filepointer.readline() + print('\nHTTP Proxy:', line[line.rfind('@') + 1:line.rfind(':')]) + print('Port:', line[line.rfind(':') + 1:line.rfind('/')]) + print('Username:', line.split('://')[1].split(':')[0]) + print('Password:', '*' * len(line[line.rfind(':', 0, line.rfind('@')) + 1:line.rfind('@')])) else: print("No proxy is set") From bfd8c4878afa4fc4bb99268eaf2fd01723a066c7 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 19:07:49 -0300 Subject: [PATCH 09/14] little changes in text menu --- proxy.py | 69 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/proxy.py b/proxy.py index fee439b..5c3343f 100755 --- a/proxy.py +++ b/proxy.py @@ -127,22 +127,22 @@ def view_proxy(): print('Username:', line.split('://')[1].split(':')[0]) print('Password:', '*' * len(line[line.rfind(':', 0, line.rfind('@')) + 1:line.rfind('@')])) else: - print("No proxy is set") + print("\nNo proxy is set") def restore_default(): - # copy from backup to main + """ + Restores the proxy configuration to the default state. + """ shutil.copy(APT_BACKUP, APT_CONF) shutil.copy(ENV_BACKUP, ENVIRONMENT) shutil.copy(BASH_BACKUP, BASH_BASHRC) -# The main Function Starts - - -if __name__ == "__main__": - - # create backup if not present +def main(): + """ + Main function that handles the program flow. + """ if not os.path.isdir("./.backup_proxy"): os.makedirs("./.backup_proxy") if os.path.isfile(APT_CONF): @@ -150,24 +150,37 @@ def restore_default(): shutil.copyfile(ENVIRONMENT, ENV_BACKUP) shutil.copyfile(BASH_BASHRC, BASH_BACKUP) - # choice - print("Please run this program as Super user(sudo)\n") - print("1:) Set Proxy") - print("2:) Remove Proxy") - print("3:) View Current Proxy") - print("4:) Restore Default") - print("5:) Exit") - choice = int(input("\nChoice (1/2/3/4/5) : ")) - - if choice == 1: - set_proxy(flag=0) - elif choice == 2: - set_proxy(flag=1) - elif choice == 3: - view_proxy() - elif choice == 4: - restore_default() - else: - sys.exit() + menu_options = { + 1: 'Set Proxy', + 2: 'Remove Proxy', + 3: 'View Current Proxy', + 4: 'Restore Default', + 5: 'Exit' + } + + print("\nPlease run this program as Super user (sudo)\n") + for key in sorted(menu_options): + print(f"{key}. {menu_options[key]}") + + try: + choice = int(input("\nChoose an option (1, 2, 3, 4, or 5) and then press ENTER: ")) + if choice == 1: + set_proxy(flag=0) + elif choice == 2: + set_proxy(flag=1) + elif choice == 3: + view_proxy() + elif choice == 4: + restore_default() + elif choice == 5: + sys.exit() + else: + print("\nInvalid choice. Please choose a valid option.") + except ValueError: + print("\nInvalid input. Please enter a number between 1 and 5.") + + print("\nDONE!\n") - print("DONE!") + +if __name__ == "__main__": + main() From 07b40d915823a49e9f510a42680fba88cce7dc4d Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 19:13:46 -0300 Subject: [PATCH 10/14] exit only with option 5 --- proxy.py | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/proxy.py b/proxy.py index 5c3343f..dd2907b 100755 --- a/proxy.py +++ b/proxy.py @@ -158,28 +158,31 @@ def main(): 5: 'Exit' } - print("\nPlease run this program as Super user (sudo)\n") - for key in sorted(menu_options): - print(f"{key}. {menu_options[key]}") - - try: - choice = int(input("\nChoose an option (1, 2, 3, 4, or 5) and then press ENTER: ")) - if choice == 1: - set_proxy(flag=0) - elif choice == 2: - set_proxy(flag=1) - elif choice == 3: - view_proxy() - elif choice == 4: - restore_default() - elif choice == 5: - sys.exit() - else: - print("\nInvalid choice. Please choose a valid option.") - except ValueError: - print("\nInvalid input. Please enter a number between 1 and 5.") - - print("\nDONE!\n") + actions = { + 1: lambda: set_proxy(flag=0), + 2: lambda: set_proxy(flag=1), + 3: view_proxy, + 4: restore_default, + 5: sys.exit + } + + while True: + print("\nPlease run this program as Super user (sudo)\n") + for key in sorted(menu_options): + print(f"{key}. {menu_options[key]}") + + try: + choice = int(input("\nChoose an option (1, 2, 3, 4, or 5) and then press ENTER: ")) + if choice in actions: + actions[choice]() + if choice == 5: + break + else: + print("\nInvalid choice. Please choose a valid option.") + except ValueError: + print("\nInvalid input. Please enter a number between 1 and 5.") + + print("\nDONE!\n") if __name__ == "__main__": From 6e7a0be8268b487ece91b27695c3d4bc6904248c Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 19:20:39 -0300 Subject: [PATCH 11/14] git and snap proxy setting --- proxy.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/proxy.py b/proxy.py index dd2907b..644f610 100755 --- a/proxy.py +++ b/proxy.py @@ -98,6 +98,42 @@ def write_to_bashrc(proxy, port, username, password, flag): filepointer.write(f'export socks_proxy="socks://{username}:{password}@{proxy}:{port}/"\n') +def write_to_snap(proxy, port, username, password, flag): + """ + Writes the proxy configuration to Snap. + + :param proxy: Proxy address + :param port: Proxy port + :param username: Username for authentication + :param password: Password for authentication + :param flag: Flag indicating whether to remove the configuration + """ + if flag: + os.system("snap set system proxy.http=''") + os.system("snap set system proxy.https=''") + else: + os.system(f"snap set system proxy.http=http://{username}:{password}@{proxy}:{port}") + os.system(f"snap set system proxy.https=https://{username}:{password}@{proxy}:{port}") + + +def write_to_git(proxy, port, username, password, flag): + """ + Writes the proxy configuration to Git. + + :param proxy: Proxy address + :param port: Proxy port + :param username: Username for authentication + :param password: Password for authentication + :param flag: Flag indicating whether to remove the configuration + """ + if flag: + os.system("git config --global --unset http.proxy") + os.system("git config --global --unset https.proxy") + else: + os.system(f"git config --global http.proxy http://{username}:{password}@{proxy}:{port}") + os.system(f"git config --global https.proxy https://{username}:{password}@{proxy}:{port}") + + def set_proxy(flag): """ Sets or removes the proxy configuration. @@ -110,9 +146,12 @@ def set_proxy(flag): port = input("Enter port: ") username = input("Enter username: ") password = getpass.getpass("Enter password: ") + write_to_apt(proxy, port, username, password, flag) write_to_env(proxy, port, username, password, flag) write_to_bashrc(proxy, port, username, password, flag) + write_to_snap(proxy, port, username, password, flag) + write_to_git(proxy, port, username, password, flag) def view_proxy(): From 2e9bdade6e4b6ac769e420eb4ea40db77dbe877e Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Mon, 27 May 2024 19:23:34 -0300 Subject: [PATCH 12/14] function to select which proxies to configure --- proxy.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/proxy.py b/proxy.py index 644f610..5b6f617 100755 --- a/proxy.py +++ b/proxy.py @@ -134,6 +134,37 @@ def write_to_git(proxy, port, username, password, flag): os.system(f"git config --global https.proxy https://{username}:{password}@{proxy}:{port}") +def select_proxies(): + """ + Allows the user to select which proxies to configure. + """ + options = { + 'apt': True, + 'env': True, + 'bashrc': True, + 'snap': True, + 'git': True + } + + while True: + print("\nSelect which proxies to configure (toggle selection with numbers):") + for i, (key, value) in enumerate(options.items(), 1): + print(f"{i}. {'[x]' if value else '[ ]'} {key.capitalize()} Proxy") + print(f"{len(options) + 1}. Confirm selection") + + try: + choice = int(input("\nToggle selection (1-5) or confirm (6): ")) + if 1 <= choice <= len(options): + key = list(options.keys())[choice - 1] + options[key] = not options[key] + elif choice == len(options) + 1: + return options + else: + print("\nInvalid choice. Please choose a valid option.") + except ValueError: + print("\nInvalid input. Please enter a number between 1 and 6.") + + def set_proxy(flag): """ Sets or removes the proxy configuration. @@ -194,7 +225,8 @@ def main(): 2: 'Remove Proxy', 3: 'View Current Proxy', 4: 'Restore Default', - 5: 'Exit' + 5: 'Select Proxies to Configure', + 6: 'Exit' } actions = { @@ -202,7 +234,15 @@ def main(): 2: lambda: set_proxy(flag=1), 3: view_proxy, 4: restore_default, - 5: sys.exit + 6: sys.exit + } + + selected_proxies = { + 'apt': True, + 'env': True, + 'bashrc': True, + 'snap': True, + 'git': True } while True: @@ -211,15 +251,17 @@ def main(): print(f"{key}. {menu_options[key]}") try: - choice = int(input("\nChoose an option (1, 2, 3, 4, or 5) and then press ENTER: ")) - if choice in actions: + choice = int(input("\nChoose an option (1, 2, 3, 4, 5, or 6) and then press ENTER: ")) + if choice == 5: + selected_proxies = select_proxies() + elif choice in actions: actions[choice]() - if choice == 5: + if choice == 6: break else: print("\nInvalid choice. Please choose a valid option.") except ValueError: - print("\nInvalid input. Please enter a number between 1 and 5.") + print("\nInvalid input. Please enter a number between 1 and 6.") print("\nDONE!\n") From 56b26a19dcbe2e936ec25228b885ff04d86e6c40 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Tue, 18 Jun 2024 12:01:38 -0300 Subject: [PATCH 13/14] correction if usr and pass are not included --- proxy.py | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/proxy.py b/proxy.py index 5b6f617..7a30e79 100755 --- a/proxy.py +++ b/proxy.py @@ -40,10 +40,11 @@ def write_to_apt(proxy, port, username, password, flag): """ with open(APT_CONF, "w") as filepointer: if not flag: - filepointer.write(f'Acquire::http::proxy "http://{username}:{password}@{proxy}:{port}/";\n') - filepointer.write(f'Acquire::https::proxy "https://{username}:{password}@{proxy}:{port}/";\n') - filepointer.write(f'Acquire::ftp::proxy "ftp://{username}:{password}@{proxy}:{port}/";\n') - filepointer.write(f'Acquire::socks::proxy "socks://{username}:{password}@{proxy}:{port}/";\n') + auth = f"{username}:{password}@" if username and password else "" + filepointer.write(f'Acquire::http::proxy "http://{auth}{proxy}:{port}/";\n') + filepointer.write(f'Acquire::https::proxy "https://{auth}{proxy}:{port}/";\n') + filepointer.write(f'Acquire::ftp::proxy "ftp://{auth}{proxy}:{port}/";\n') + filepointer.write(f'Acquire::socks::proxy "socks://{auth}{proxy}:{port}/";\n') def write_to_env(proxy, port, username, password, flag): @@ -66,10 +67,11 @@ def write_to_env(proxy, port, username, password, flag): if not flag: with open(ENVIRONMENT, "a") as filepointer: - filepointer.write(f'http_proxy="http://{username}:{password}@{proxy}:{port}/"\n') - filepointer.write(f'https_proxy="https://{username}:{password}@{proxy}:{port}/"\n') - filepointer.write(f'ftp_proxy="ftp://{username}:{password}@{proxy}:{port}/"\n') - filepointer.write(f'socks_proxy="socks://{username}:{password}@{proxy}:{port}/"\n') + auth = f"{username}:{password}@" if username and password else "" + filepointer.write(f'http_proxy="http://{auth}{proxy}:{port}/"\n') + filepointer.write(f'https_proxy="https://{auth}{proxy}:{port}/"\n') + filepointer.write(f'ftp_proxy="ftp://{auth}{proxy}:{port}/"\n') + filepointer.write(f'socks_proxy="socks://{auth}{proxy}:{port}/"\n') def write_to_bashrc(proxy, port, username, password, flag): @@ -92,10 +94,11 @@ def write_to_bashrc(proxy, port, username, password, flag): if not flag: with open(BASH_BASHRC, "a") as filepointer: - filepointer.write(f'export http_proxy="http://{username}:{password}@{proxy}:{port}/"\n') - filepointer.write(f'export https_proxy="https://{username}:{password}@{proxy}:{port}/"\n') - filepointer.write(f'export ftp_proxy="ftp://{username}:{password}@{proxy}:{port}/"\n') - filepointer.write(f'export socks_proxy="socks://{username}:{password}@{proxy}:{port}/"\n') + auth = f"{username}:{password}@" if username and password else "" + filepointer.write(f'export http_proxy="http://{auth}{proxy}:{port}/"\n') + filepointer.write(f'export https_proxy="https://{auth}{proxy}:{port}/"\n') + filepointer.write(f'export ftp_proxy="ftp://{auth}{proxy}:{port}/"\n') + filepointer.write(f'export socks_proxy="socks://{auth}{proxy}:{port}/"\n') def write_to_snap(proxy, port, username, password, flag): @@ -112,8 +115,9 @@ def write_to_snap(proxy, port, username, password, flag): os.system("snap set system proxy.http=''") os.system("snap set system proxy.https=''") else: - os.system(f"snap set system proxy.http=http://{username}:{password}@{proxy}:{port}") - os.system(f"snap set system proxy.https=https://{username}:{password}@{proxy}:{port}") + auth = f"{username}:{password}@" if username and password else "" + os.system(f"snap set system proxy.http=http://{auth}{proxy}:{port}") + os.system(f"snap set system proxy.https=https://{auth}{proxy}:{port}") def write_to_git(proxy, port, username, password, flag): @@ -130,8 +134,9 @@ def write_to_git(proxy, port, username, password, flag): os.system("git config --global --unset http.proxy") os.system("git config --global --unset https.proxy") else: - os.system(f"git config --global http.proxy http://{username}:{password}@{proxy}:{port}") - os.system(f"git config --global https.proxy https://{username}:{password}@{proxy}:{port}") + auth = f"{username}:{password}@" if username and password else "" + os.system(f"git config --global http.proxy http://{auth}{proxy}:{port}") + os.system(f"git config --global https.proxy https://{auth}{proxy}:{port}") def select_proxies(): @@ -192,10 +197,14 @@ def view_proxy(): if os.path.getsize(APT_CONF): with open(APT_CONF, "r") as filepointer: line = filepointer.readline() - print('\nHTTP Proxy:', line[line.rfind('@') + 1:line.rfind(':')]) - print('Port:', line[line.rfind(':') + 1:line.rfind('/')]) - print('Username:', line.split('://')[1].split(':')[0]) - print('Password:', '*' * len(line[line.rfind(':', 0, line.rfind('@')) + 1:line.rfind('@')])) + if "@" in line: + print('\nHTTP Proxy:', line[line.rfind('@') + 1:line.rfind(':')]) + print('Port:', line[line.rfind(':') + 1:line.rfind('/')]) + print('Username:', line.split('://')[1].split(':')[0]) + print('Password:', '*' * len(line[line.rfind(':', 0, line.rfind('@')) + 1:line.rfind('@')])) + else: + print('HTTP Proxy:', line[line.rfind('http'):line.rfind(':')]) + print('Port:', line[line.rfind(':') + 1:line.rfind('/')]) else: print("\nNo proxy is set") From 4e49ac6a48f1d2e21a3b1787159ba0b99745a0c8 Mon Sep 17 00:00:00 2001 From: PPeitsch Date: Tue, 18 Jun 2024 12:49:14 -0300 Subject: [PATCH 14/14] correction snap https direction --- proxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy.py b/proxy.py index 7a30e79..e52df66 100755 --- a/proxy.py +++ b/proxy.py @@ -117,7 +117,7 @@ def write_to_snap(proxy, port, username, password, flag): else: auth = f"{username}:{password}@" if username and password else "" os.system(f"snap set system proxy.http=http://{auth}{proxy}:{port}") - os.system(f"snap set system proxy.https=https://{auth}{proxy}:{port}") + os.system(f"snap set system proxy.https=http://{auth}{proxy}:{port}") def write_to_git(proxy, port, username, password, flag):