From 81fe44ad07c40c52cb40ad5eeca02ef1e2d15539 Mon Sep 17 00:00:00 2001 From: Beh Date: Sat, 25 Jan 2020 20:53:21 +0100 Subject: [PATCH 01/21] Add uname variable to distinguish operating systems --- variables.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/variables.sh b/variables.sh index 25b451b..47b896d 100644 --- a/variables.sh +++ b/variables.sh @@ -36,6 +36,7 @@ fi export config_dir="$HOME/.config/proxyman" export default_config="default" export SHELLRC="$HOME/.bashrc" +export os=`uname` case "$SHELL" in "$(which bash)") SHELLRC="$HOME/.bashrc" From ad32394f2c8519e38abe37a041b0e5154deb5636 Mon Sep 17 00:00:00 2001 From: Beh Date: Sat, 25 Jan 2020 20:57:36 +0100 Subject: [PATCH 02/21] Add support for macOS in main.sh Create some kind of interface for different OSes: Main functions for setting proxies are split into different files for each OS which contain identical function names. The corresponding file gets sourced at the beginning of the file. --- main.sh | 84 ++++++------------------------------------------- main_linux.sh | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ main_macos.sh | 60 +++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 74 deletions(-) create mode 100644 main_linux.sh create mode 100644 main_macos.sh diff --git a/main.sh b/main.sh index 02184a3..f4fed25 100755 --- a/main.sh +++ b/main.sh @@ -11,62 +11,16 @@ source "./variables.sh" source "./configs.sh" - -function _do_it_for_all() { - # Argument - # $1 : what to do (set/unset/list) - - local what_to_do="$1" - if [[ -z "$targets" || "$targets" = "1" ]]; then - bash "bash-zsh.sh" "$what_to_do" - # avoiding /etc/environment in all because it requires logout after unset - # bashrc / zshrc is sufficient, and sudo -E might suffice - if [[ "$what_to_do" = "list" ]]; then - sudo -E bash "environment.sh" "$what_to_do" - fi - bash "gsettings.sh" "$what_to_do" - bash "kde5.sh" "$what_to_do" - bash "npm.sh" "$what_to_do" - bash "dropbox.sh" "$what_to_do" - bash "git.sh" "$what_to_do" - - # isn't required, but still checked to avoid sudo in main all the time - SUDO_CMDS="apt dnf docker" - for cmd in $SUDO_CMDS; do - command -v $cmd > /dev/null && sudo -E bash "${cmd}.sh" "$what_to_do" || : - done - else - for t in "${targets[@]}" - do - case "$t" in - # THIS stmt WILL CAUSE INFINITE RECURSION. DO NOT USE THIS. - # COMMENTED FOR WARNING - # 1) _do_it_for_all "$what_to_do" - # ;; - 2) bash "bash-zsh.sh" "$what_to_do" - ;; - 3) sudo -E bash "environment.sh" "$what_to_do" - ;; - 4) sudo -E bash "apt.sh" "$what_to_do" - sudo -E bash "dnf.sh" "$what_to_do" - ;; - 5) bash "gsettings.sh" "$what_to_do" - bash "kde5.sh" "$what_to_do" - ;; - 6) bash "npm.sh" "$what_to_do" - ;; - 7) bash "dropbox.sh" "$what_to_do" - ;; - 8) bash "git.sh" "$what_to_do" - ;; - 9) sudo -E bash "docker.sh" "$what_to_do" - ;; - *) ;; - esac - done - - fi -} +# Load main functions for current operating system +case $os in + "Linux") source "./main_linux.sh" + ;; + "Darwin") source "./main_macos.sh" + ;; + *) echo "Operating system not supported. Exiting." + exit 1 + ;; +esac function _dump_it_all() { echo "HTTP > $http_host $http_port" @@ -140,24 +94,6 @@ function prompt_for_proxy_values() { echo } -function prompt_for_proxy_targets() { - echo "${bold}${blue} Select targets to modify ${normal}" - - echo "|${bold}${red} 1 ${normal}| All of them ... Don't bother me" - echo "|${bold}${red} 2 ${normal}| Terminal / bash / zsh (current user) " - echo "|${bold}${red} 3 ${normal}| /etc/environment" - echo "|${bold}${red} 4 ${normal}| apt/dnf (Package manager)" - echo "|${bold}${red} 5 ${normal}| Desktop settings (GNOME/Ubuntu/KDE)" - echo "|${bold}${red} 6 ${normal}| npm & yarn" - echo "|${bold}${red} 7 ${normal}| Dropbox" - echo "|${bold}${red} 8 ${normal}| Git" - echo "|${bold}${red} 9 ${normal}| Docker" - echo - echo "Separate multiple choices with space" - echo -ne "\e[5m ? \e[0m" ; read targets - export targets=(`echo ${targets}`) -} - function main() { case "$1" in "configs" ) list_configs diff --git a/main_linux.sh b/main_linux.sh new file mode 100644 index 0000000..5cbe917 --- /dev/null +++ b/main_linux.sh @@ -0,0 +1,86 @@ +#!/bin/bash +# +# If you have found some issues, or some feature request : +# Raise them here : https://github.com/himanshub16/ProxyMan/issues +# Author : Himanshu Shekhar (@himanshub16) +# + +# This is the script that provides main script functions for Linux. + +function _do_it_for_selection() { + local what_to_do="$1" + for t in "${targets[@]}" + do + case "$t" in + # THIS stmt WILL CAUSE INFINITE RECURSION. DO NOT USE THIS. + # COMMENTED FOR WARNING + # 1) _do_it_for_all "$what_to_do" + # ;; + 2) bash "bash-zsh.sh" "$what_to_do" + ;; + 3) sudo -E bash "environment.sh" "$what_to_do" + ;; + 4) sudo -E bash "apt.sh" "$what_to_do" + sudo -E bash "dnf.sh" "$what_to_do" + ;; + 5) bash "gsettings.sh" "$what_to_do" + bash "kde5.sh" "$what_to_do" + ;; + 6) bash "npm.sh" "$what_to_do" + ;; + 7) bash "dropbox.sh" "$what_to_do" + ;; + 8) bash "git.sh" "$what_to_do" + ;; + 9) sudo -E bash "docker.sh" "$what_to_do" + ;; + *) ;; + esac + done +} + +function _do_it_for_all() { + # Argument + # $1 : what to do (set/unset/list) + + local what_to_do="$1" + if [[ -z "$targets" || "$targets" = "1" ]]; then + bash "bash-zsh.sh" "$what_to_do" + # avoiding /etc/environment in all because it requires logout after unset + # bashrc / zshrc is sufficient, and sudo -E might suffice + if [[ "$what_to_do" = "list" ]]; then + sudo -E bash "environment.sh" "$what_to_do" + fi + bash "gsettings.sh" "$what_to_do" + bash "kde5.sh" "$what_to_do" + bash "npm.sh" "$what_to_do" + bash "dropbox.sh" "$what_to_do" + bash "git.sh" "$what_to_do" + + # isn't required, but still checked to avoid sudo in main all the time + SUDO_CMDS="apt dnf docker" + for cmd in $SUDO_CMDS; do + command -v $cmd > /dev/null && sudo -E bash "${cmd}.sh" "$what_to_do" || : + done + else + _do_it_for_selection "$what_to_do" + fi +} + +function prompt_for_proxy_targets() { + echo "${bold}${blue} Select targets to modify ${normal}" + + echo "|${bold}${red} 1 ${normal}| All of them ... Don't bother me" + echo "|${bold}${red} 2 ${normal}| Terminal / bash / zsh (current user) " + echo "|${bold}${red} 3 ${normal}| /etc/environment" + echo "|${bold}${red} 4 ${normal}| apt/dnf (Package manager)" + echo "|${bold}${red} 5 ${normal}| Desktop settings (GNOME/Ubuntu/KDE)" + echo "|${bold}${red} 6 ${normal}| npm & yarn" + echo "|${bold}${red} 7 ${normal}| Dropbox" + echo "|${bold}${red} 8 ${normal}| Git" + echo "|${bold}${red} 9 ${normal}| Docker" + echo + echo "Separate multiple choices with space" + echo -n "${bold} ? ${normal}" ; read targets # + export targets=(`echo ${targets}`) +} \ No newline at end of file diff --git a/main_macos.sh b/main_macos.sh new file mode 100644 index 0000000..5548910 --- /dev/null +++ b/main_macos.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# +# If you have found some issues, or some feature request : +# Raise them here : https://github.com/himanshub16/ProxyMan/issues +# Author : Benjamin Gericke (@deg0nz) +# + +# This is the script that provides main script functions for macOS. + +function _do_it_for_selection() { + local what_to_do="$1" + for t in "${targets[@]}" + do + case "$t" in + # THIS stmt WILL CAUSE INFINITE RECURSION. DO NOT USE THIS. + # COMMENTED FOR WARNING + # 1) _do_it_for_all "$what_to_do" + # ;; + 2) bash "bash-zsh.sh" "$what_to_do" + ;; + 3) bash "npm.sh" "$what_to_do" + ;; + 3) echo "networksetup not yet implemented. skipping." #bash "networksetup.sh" "$what_to_do" + ;; + 4) bash "git.sh" "$what_to_do" + ;; + *) ;; + esac + done +} + +function _do_it_for_all() { + # Argument + # $1 : what to do (set/unset/list) + + local what_to_do="$1" + if [[ -z "$targets" || "$targets" = "1" ]]; then + + bash "bash-zsh.sh" "$what_to_do" + bash "npm.sh" "$what_to_do" + bash "git.sh" "$what_to_do" + + else + _do_it_for_selection "$what_to_do" + fi +} + +function prompt_for_proxy_targets() { + echo "${bold}${blue} Select targets to modify ${normal}" + + echo "|${bold}${red} 1 ${normal}| All of them ... Don't bother me" + echo "|${bold}${red} 2 ${normal}| Terminal / bash / zsh (current user) " + echo "|${bold}${red} 3 ${normal}| Desktop settings (networksetup)" + echo "|${bold}${red} 4 ${normal}| npm & yarn" + echo "|${bold}${red} 5 ${normal}| Git" + echo + echo "Separate multiple choices with space" + echo -n "${bold} ? ${normal}" ; read targets # + export targets=(`echo ${targets}`) +} \ No newline at end of file From c776b8845608a4a6fcd6aaf14fc28cb3bb8a0725 Mon Sep 17 00:00:00 2001 From: Beh Date: Sat, 25 Jan 2020 20:59:23 +0100 Subject: [PATCH 03/21] Add support for macOS in bash-zsh.sh BSD readlink has a different functionality than GNU's readlink. We need to use realpath on macOS. --- bash-zsh.sh | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/bash-zsh.sh b/bash-zsh.sh index 3f3a6e9..de6e41d 100644 --- a/bash-zsh.sh +++ b/bash-zsh.sh @@ -1,7 +1,18 @@ #!/bin/bash -BASHRC=`readlink -f $HOME/.bashrc` -ZSHRC=`readlink -f $HOME/.zshrc` +case $os in + "Linux") + BASHRC=`readlink -f $HOME/.bashrc` + ZSHRC=`readlink -f $HOME/.zshrc` + ;; + "Darwin") + BASHRC=`realpath $HOME/.zshrc` + ZSHRC=`realpath $HOME/.zshrc` + ;; + *) echo "Operating system not supported. Exiting." + exit 1 + ;; +esac which bash &> /dev/null first="$?" From c681c8316063d79403a98213d4470b78633bf51b Mon Sep 17 00:00:00 2001 From: Beh Date: Sat, 25 Jan 2020 21:01:11 +0100 Subject: [PATCH 04/21] Add support for macOS to shellrc.sh BSD sed's syntax is different from the GNU one. We check for the OS in the unset function and call the corresponding sed syntax --- shellrc.sh | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/shellrc.sh b/shellrc.sh index 8cc1865..eff683b 100644 --- a/shellrc.sh +++ b/shellrc.sh @@ -25,14 +25,33 @@ unset_proxy() { if [ ! -e "$SHELLRC" ]; then return fi - # extra effort required to avoid removing custom environment variables set - # by the user for personal use - for proxytype in "http" "https" "ftp" "rsync" "no"; do - sed -i "/export ${proxytype}_proxy\=/d" "$SHELLRC" - done - for PROTOTYPE in "HTTP" "HTTPS" "FTP" "RSYNC" "NO"; do - sed -i "/export ${PROTOTYPE}_PROXY\=/d" "$SHELLRC" - done + + # macOS ships with BSD sed, which has different syntax from GNU sed + case $os in + "Linux") + # extra effort required to avoid removing custom environment variables set + # by the user for personal use + for proxytype in "http" "https" "ftp" "rsync" "no"; do + sed -i "/export ${proxytype}_proxy\=/d" "$SHELLRC" + done + for PROXYTYPE in "HTTP" "HTTPS" "FTP" "RSYNC" "NO"; do + sed -i "/export ${PROXYTYPE}_PROXY\=/d" "$SHELLRC" + done + ;; + "Darwin") + # extra effort required to avoid removing custom environment variables set + # by the user for personal use + for proxytype in "http" "https" "ftp" "rsync" "no"; do + sed -i "" "/export ${proxytype}_proxy\=/d" "$SHELLRC" + done + for PROXYTYPE in "HTTP" "HTTPS" "FTP" "RSYNC" "NO"; do + sed -i "" "/export ${PROXYTYPE}_PROXY\=/d" "$SHELLRC" + done + ;; + *) echo "Operating system not supported. Exiting." + exit 1 + ;; + esac } set_proxy() { From 3c946a222de350d380b6754db9dd4e9b80c2b3e4 Mon Sep 17 00:00:00 2001 From: Beh Date: Sat, 25 Jan 2020 23:12:02 +0100 Subject: [PATCH 05/21] Fix indentation in main_linux --- main_linux.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main_linux.sh b/main_linux.sh index 5cbe917..d82b51f 100644 --- a/main_linux.sh +++ b/main_linux.sh @@ -21,10 +21,10 @@ function _do_it_for_selection() { 3) sudo -E bash "environment.sh" "$what_to_do" ;; 4) sudo -E bash "apt.sh" "$what_to_do" - sudo -E bash "dnf.sh" "$what_to_do" + sudo -E bash "dnf.sh" "$what_to_do" ;; 5) bash "gsettings.sh" "$what_to_do" - bash "kde5.sh" "$what_to_do" + bash "kde5.sh" "$what_to_do" ;; 6) bash "npm.sh" "$what_to_do" ;; From fbe423a954844d62820356ac6bd97325c599c735 Mon Sep 17 00:00:00 2001 From: Beh Date: Sat, 25 Jan 2020 23:12:38 +0100 Subject: [PATCH 06/21] Remove unnecessary hashtag --- main_linux.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main_linux.sh b/main_linux.sh index d82b51f..778f0c6 100644 --- a/main_linux.sh +++ b/main_linux.sh @@ -81,6 +81,6 @@ function prompt_for_proxy_targets() { echo "|${bold}${red} 9 ${normal}| Docker" echo echo "Separate multiple choices with space" - echo -n "${bold} ? ${normal}" ; read targets # + echo -n "${bold} ? ${normal}" ; read targets export targets=(`echo ${targets}`) } \ No newline at end of file From 43fc5deee81d6e911ad57423c9fa9a22459e55e9 Mon Sep 17 00:00:00 2001 From: Beh Date: Sat, 25 Jan 2020 23:13:57 +0100 Subject: [PATCH 07/21] Fixed wrong service order in macos functions also removed unnecessary hashtag also added networksetup entry to do_all function --- main_macos.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/main_macos.sh b/main_macos.sh index 5548910..d7ae662 100644 --- a/main_macos.sh +++ b/main_macos.sh @@ -9,7 +9,7 @@ function _do_it_for_selection() { local what_to_do="$1" - for t in "${targets[@]}" + for t in ${targets[@]} do case "$t" in # THIS stmt WILL CAUSE INFINITE RECURSION. DO NOT USE THIS. @@ -18,11 +18,11 @@ function _do_it_for_selection() { # ;; 2) bash "bash-zsh.sh" "$what_to_do" ;; - 3) bash "npm.sh" "$what_to_do" + 3) bash "networksetup.sh" "$what_to_do" ;; - 3) echo "networksetup not yet implemented. skipping." #bash "networksetup.sh" "$what_to_do" + 4) bash "npm.sh" "$what_to_do" ;; - 4) bash "git.sh" "$what_to_do" + 5) bash "git.sh" "$what_to_do" ;; *) ;; esac @@ -37,6 +37,7 @@ function _do_it_for_all() { if [[ -z "$targets" || "$targets" = "1" ]]; then bash "bash-zsh.sh" "$what_to_do" + # bash "networksetup.sh" "$what_to_do" bash "npm.sh" "$what_to_do" bash "git.sh" "$what_to_do" @@ -55,6 +56,6 @@ function prompt_for_proxy_targets() { echo "|${bold}${red} 5 ${normal}| Git" echo echo "Separate multiple choices with space" - echo -n "${bold} ? ${normal}" ; read targets # + echo -n "${bold} ? ${normal}" ; read targets export targets=(`echo ${targets}`) } \ No newline at end of file From 280b935e21480517b23f63bc125483eb5a9356b3 Mon Sep 17 00:00:00 2001 From: Beh Date: Sun, 26 Jan 2020 01:36:12 +0100 Subject: [PATCH 08/21] Rearrange service order for macOS --- main_macos.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main_macos.sh b/main_macos.sh index d7ae662..7a345ef 100644 --- a/main_macos.sh +++ b/main_macos.sh @@ -16,9 +16,9 @@ function _do_it_for_selection() { # COMMENTED FOR WARNING # 1) _do_it_for_all "$what_to_do" # ;; - 2) bash "bash-zsh.sh" "$what_to_do" + 2) bash "networksetup.sh" "$what_to_do" ;; - 3) bash "networksetup.sh" "$what_to_do" + 3) bash "bash-zsh.sh" "$what_to_do" ;; 4) bash "npm.sh" "$what_to_do" ;; @@ -37,7 +37,7 @@ function _do_it_for_all() { if [[ -z "$targets" || "$targets" = "1" ]]; then bash "bash-zsh.sh" "$what_to_do" - # bash "networksetup.sh" "$what_to_do" + bash "networksetup.sh" "$what_to_do" bash "npm.sh" "$what_to_do" bash "git.sh" "$what_to_do" @@ -50,8 +50,8 @@ function prompt_for_proxy_targets() { echo "${bold}${blue} Select targets to modify ${normal}" echo "|${bold}${red} 1 ${normal}| All of them ... Don't bother me" - echo "|${bold}${red} 2 ${normal}| Terminal / bash / zsh (current user) " - echo "|${bold}${red} 3 ${normal}| Desktop settings (networksetup)" + echo "|${bold}${red} 2 ${normal}| System-wide settings (networksetup)" + echo "|${bold}${red} 3 ${normal}| Terminal / bash / zsh (current user)" echo "|${bold}${red} 4 ${normal}| npm & yarn" echo "|${bold}${red} 5 ${normal}| Git" echo From a74a54d196d3c1fc7648d73835b3c9ee82901271 Mon Sep 17 00:00:00 2001 From: Beh Date: Sun, 26 Jan 2020 01:36:53 +0100 Subject: [PATCH 09/21] Added support for setting system-wide proxy on macOS via networksetup --- networksetup.sh | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 networksetup.sh diff --git a/networksetup.sh b/networksetup.sh new file mode 100644 index 0000000..eeae982 --- /dev/null +++ b/networksetup.sh @@ -0,0 +1,117 @@ +#!/bin/bash +# +# If you have found some issues, or some feature request : +# Raise them here : https://github.com/himanshub16/ProxyMan/issues +# Author : Benjamin Gericke (@deg0nz) +# + +# This is the script that provides functions for macOS's networksetup tool. + +function _prompt_networkservices_targets() { + echo "${bold}${blue} Select network interfaces to modify ${normal}" + + echo "|${bold}${red} 1 ${normal}| All of them ... Don't bother me" + local IFS=$'\n' + local counter=2 + for line in $(networksetup -listallnetworkservices); do + # TODO: Handle disabled services + if [[ "$line" == "An asterisk (*) denotes that a network service is disabled." ]]; then + continue + fi + echo "|${bold}${red} ${counter} ${normal}| ${line} " + # We write the array from 0, although 1 is "all devices" + # loop for selected services must be adjusted + arraypos="(($counter-2))" + networkservices_array[$arraypos]="$line" + ((counter++)) + done + echo + echo "Separate multiple choices with space" + echo -n "${bold} ? ${normal}" ; read targets_networkservices + export targets_networkservices=(`echo ${targets_networkservices}`) +} + +function _set_proxy_for_networkservice() { + local networkservice="$1" + # Parameters: [-setwebproxy networkservice domain portnumber authenticated username password] + if [[ "$use_auth" ]]; then + networksetup -setwebproxy "$networkservice" "$http_host" "$http_port" on "$username" "$password" + networksetup -setsecurewebproxy "$networkservice" "$https_host" "$https_port" on "$username" "$password" + networksetup -setftpproxy "$networkservice" "$http_host" "$http_port" on "$username" "$password" + else + networksetup -setwebproxy "$networkservice" "$http_host" "$http_port" off + networksetup -setsecurewebproxy "$networkservice" "$https_host" "$https_port" off + networksetup -setftpproxy "$networkservice" "$http_host" "$http_port" off + fi + + networksetup -setproxybypassdomains "$networkservice" "$no_proxy" +} + +function _unset_proxy_for_networkservice() { + local networkservice="$1" + + networksetup -setwebproxystate "$networkservice" off + networksetup -setsecurewebproxystate "$networkservice" off + networksetup -setftpproxystate "$networkservice" off + networksetup -setproxyautodiscovery "$networkservice" off +} + +function list_proxy() { + # TODO + echo "Listing proxies for system on macOS is not yet implemented" +} + +function unset_proxy() { + echo "Additional information for ${bold}unsetting${normal} system-wide proxy is needed." + + _prompt_networkservices_targets + + if [[ -z "$targets_networkservices" || "$targets_networkservices" = "1" ]]; then + # Unset for all services + local IFS=$'\n' + for networkservice in ${networkservices_array[*]}; do + _unset_proxy_for_networkservice "$networkservice" + done + else + # Unset for selection + for arrayindex in ${targets_networkservices[@]}; do + # We need to correct the index, because 1 is "all devices", so all the indexes are increased by 1 + corrected_index="(($arrayindex-2))" + _unset_proxy_for_networkservice "${networkservices_array[$corrected_index]}" + done + fi +} + +function set_proxy() { + echo "Additional information for ${bold}setting${normal} system-wide proxy is needed." + + _prompt_networkservices_targets + + if [[ -z "$targets_networkservices" || "$targets_networkservices" = "1" ]]; then + # Set for all services + local IFS=$'\n' + for networkservice in ${networkservices_array[*]}; do + echo "$networkservice" + _set_proxy_for_networkservice "$networkservice" + done + else + # Set for selection of services + for arrayindex in ${targets_networkservices[@]}; do + # We need to correct the index, because 1 is "all devices", so all the indexes are increased by 1 + corrected_index="(($arrayindex-2))" + _set_proxy_for_networkservice "${networkservices_array[$corrected_index]}" + done + fi +} + +what_to_do=$1 +case $what_to_do in + set) set_proxy + ;; + unset) unset_proxy + ;; + list) list_proxy + ;; + *) + ;; +esac \ No newline at end of file From 75a5fc891357ad8f86e9197613ef1e6510e6f008 Mon Sep 17 00:00:00 2001 From: Beh Date: Sun, 26 Jan 2020 01:44:34 +0100 Subject: [PATCH 10/21] Added explanation for checking proxies in system settings ...because this function is not implemented yet. --- networksetup.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/networksetup.sh b/networksetup.sh index eeae982..f14dac3 100644 --- a/networksetup.sh +++ b/networksetup.sh @@ -58,7 +58,8 @@ function _unset_proxy_for_networkservice() { function list_proxy() { # TODO - echo "Listing proxies for system on macOS is not yet implemented" + echo "Listing proxies for system-wide settings on macOS is not yet implemented." + echo "Please look into System Preferences -> Network -> -> Advanced -> Proxies for now." } function unset_proxy() { From 3a4bc8c3515c1c8de89e90099b203ce73a9c88a4 Mon Sep 17 00:00:00 2001 From: Beh Date: Sun, 26 Jan 2020 02:21:47 +0100 Subject: [PATCH 11/21] Removed echo that was present for debugging purposes in networksetup.sh -> set_proxy() --- networksetup.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/networksetup.sh b/networksetup.sh index f14dac3..3490943 100644 --- a/networksetup.sh +++ b/networksetup.sh @@ -92,7 +92,6 @@ function set_proxy() { # Set for all services local IFS=$'\n' for networkservice in ${networkservices_array[*]}; do - echo "$networkservice" _set_proxy_for_networkservice "$networkservice" done else From 52e63a14a69c41a3484bf45a6430c5bbb6ddd63a Mon Sep 17 00:00:00 2001 From: Beh Date: Sun, 26 Jan 2020 12:49:55 +0100 Subject: [PATCH 12/21] Changed file names of mac and linux specific files --- main_linux.sh => linux_functions.sh | 0 main_macos.sh => macos_functions.sh | 0 main.sh | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename main_linux.sh => linux_functions.sh (100%) rename main_macos.sh => macos_functions.sh (100%) diff --git a/main_linux.sh b/linux_functions.sh similarity index 100% rename from main_linux.sh rename to linux_functions.sh diff --git a/main_macos.sh b/macos_functions.sh similarity index 100% rename from main_macos.sh rename to macos_functions.sh diff --git a/main.sh b/main.sh index f4fed25..1cf2f16 100755 --- a/main.sh +++ b/main.sh @@ -13,9 +13,9 @@ source "./configs.sh" # Load main functions for current operating system case $os in - "Linux") source "./main_linux.sh" + "Linux") source "./linux_functions.sh" ;; - "Darwin") source "./main_macos.sh" + "Darwin") source "./macos_functions.sh" ;; *) echo "Operating system not supported. Exiting." exit 1 From 17a4e00982eb260723f1815d2d651b746917ff0c Mon Sep 17 00:00:00 2001 From: Beh Date: Mon, 27 Jan 2020 23:08:14 +0100 Subject: [PATCH 13/21] Fix wrong file for path .bashrc This was a copy and paste error --- bash-zsh.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash-zsh.sh b/bash-zsh.sh index de6e41d..5db1e97 100644 --- a/bash-zsh.sh +++ b/bash-zsh.sh @@ -6,7 +6,7 @@ case $os in ZSHRC=`readlink -f $HOME/.zshrc` ;; "Darwin") - BASHRC=`realpath $HOME/.zshrc` + BASHRC=`realpath $HOME/.bashrc` ZSHRC=`realpath $HOME/.zshrc` ;; *) echo "Operating system not supported. Exiting." From c500cabbd82df1e91e5b6d0bb6119e2b06a45ab9 Mon Sep 17 00:00:00 2001 From: Beh Date: Mon, 27 Jan 2020 23:09:16 +0100 Subject: [PATCH 14/21] Added portable realpath substitute macOS does not come with realpath per default, so we need a portable substitute for this --- bash-zsh.sh | 3 ++ util/realpath.sh | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 util/realpath.sh diff --git a/bash-zsh.sh b/bash-zsh.sh index 5db1e97..1ae1d33 100644 --- a/bash-zsh.sh +++ b/bash-zsh.sh @@ -1,4 +1,7 @@ #!/bin/bash +set -x +# macOS does not come with readlink or realpath, so we need to take a portable substitute +source "./util/realpath.sh" case $os in "Linux") diff --git a/util/realpath.sh b/util/realpath.sh new file mode 100644 index 0000000..9946cdf --- /dev/null +++ b/util/realpath.sh @@ -0,0 +1,71 @@ +#!/bin/sh +# Pure shell implementation of realpath +# Taken from https://github.com/mkropat/sh-realpath + +realpath() { + canonicalize_path "$(resolve_symlinks "$1")" +} + +resolve_symlinks() { + _resolve_symlinks "$1" +} + +_resolve_symlinks() { + _assert_no_path_cycles "$@" || return + + local dir_context path + path=$(readlink -- "$1") + if [ $? -eq 0 ]; then + dir_context=$(dirname -- "$1") + _resolve_symlinks "$(_prepend_dir_context_if_necessary "$dir_context" "$path")" "$@" + else + printf '%s\n' "$1" + fi +} + +_prepend_dir_context_if_necessary() { + if [ "$1" = . ]; then + printf '%s\n' "$2" + else + _prepend_path_if_relative "$1" "$2" + fi +} + +_prepend_path_if_relative() { + case "$2" in + /* ) printf '%s\n' "$2" ;; + * ) printf '%s\n' "$1/$2" ;; + esac +} + +_assert_no_path_cycles() { + local target path + + target=$1 + shift + + for path in "$@"; do + if [ "$path" = "$target" ]; then + return 1 + fi + done +} + +canonicalize_path() { + if [ -d "$1" ]; then + _canonicalize_dir_path "$1" + else + _canonicalize_file_path "$1" + fi +} + +_canonicalize_dir_path() { + (cd "$1" 2>/dev/null && pwd -P) +} + +_canonicalize_file_path() { + local dir file + dir=$(dirname -- "$1") + file=$(basename -- "$1") + (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file") +} From d4ec07fac0d87e434d171d4d0306d0be6097d34e Mon Sep 17 00:00:00 2001 From: Beh Date: Mon, 27 Jan 2020 23:14:18 +0100 Subject: [PATCH 15/21] Remove set -x that was still there for debugging purposes --- bash-zsh.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/bash-zsh.sh b/bash-zsh.sh index 1ae1d33..f143fa3 100644 --- a/bash-zsh.sh +++ b/bash-zsh.sh @@ -1,5 +1,4 @@ #!/bin/bash -set -x # macOS does not come with readlink or realpath, so we need to take a portable substitute source "./util/realpath.sh" From 8904a238881840cd113cb0cee2ee935bcf0844d2 Mon Sep 17 00:00:00 2001 From: Beh Date: Mon, 27 Jan 2020 23:25:24 +0100 Subject: [PATCH 16/21] Make copy of install script recursive to include util folder --- install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install b/install index 6624496..6053004 100755 --- a/install +++ b/install @@ -22,7 +22,7 @@ echo "Press any key to continue, or Ctrl+C to break." read mkdir -p $TARGET_DIR -cp * $TARGET_DIR +cp -r * $TARGET_DIR # add to path mkdir -p $HOME/.local/bin From e7bda24a0849222adfe97e1b6c7f5af72c247b32 Mon Sep 17 00:00:00 2001 From: Beh Date: Tue, 25 Feb 2020 07:23:08 +0100 Subject: [PATCH 17/21] Fix wrong check for use_auth in networksetup --- networksetup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/networksetup.sh b/networksetup.sh index 3490943..90d1e15 100644 --- a/networksetup.sh +++ b/networksetup.sh @@ -34,7 +34,7 @@ function _prompt_networkservices_targets() { function _set_proxy_for_networkservice() { local networkservice="$1" # Parameters: [-setwebproxy networkservice domain portnumber authenticated username password] - if [[ "$use_auth" ]]; then + if [[ "$use_auth" = "y" ]]; then networksetup -setwebproxy "$networkservice" "$http_host" "$http_port" on "$username" "$password" networksetup -setsecurewebproxy "$networkservice" "$https_host" "$https_port" on "$username" "$password" networksetup -setftpproxy "$networkservice" "$http_host" "$http_port" on "$username" "$password" From ca0d85473651bbc6cd5b779a6ed23b991eed0eed Mon Sep 17 00:00:00 2001 From: Beh Date: Thu, 9 Apr 2020 19:15:56 +0200 Subject: [PATCH 18/21] Add support for fish shell ... on basis of already supported macOS --- bash-zsh.sh => bash-zsh-fish.sh | 21 +++++++++++++++++---- linux_functions.sh | 6 +++--- macos_functions.sh | 6 +++--- 3 files changed, 23 insertions(+), 10 deletions(-) rename bash-zsh.sh => bash-zsh-fish.sh (64%) diff --git a/bash-zsh.sh b/bash-zsh-fish.sh similarity index 64% rename from bash-zsh.sh rename to bash-zsh-fish.sh index f143fa3..f181498 100644 --- a/bash-zsh.sh +++ b/bash-zsh-fish.sh @@ -6,10 +6,12 @@ case $os in "Linux") BASHRC=`readlink -f $HOME/.bashrc` ZSHRC=`readlink -f $HOME/.zshrc` + FISHRC=`readlink -f $HOME/.config/fish/config.fish` ;; "Darwin") BASHRC=`realpath $HOME/.bashrc` ZSHRC=`realpath $HOME/.zshrc` + FISHRC=`realpath $HOME/.config/fish/config.fish` ;; *) echo "Operating system not supported. Exiting." exit 1 @@ -17,16 +19,19 @@ case $os in esac which bash &> /dev/null -first="$?" +check_bash="$?" which zsh &> /dev/null -second="$?" +check_zsh="$?" + +which fish &> /dev/null +check_fish="$?" if [ "$#" = 0 ]; then exit fi -if [ "$first" = 0 ]; then +if [ "$check_bash" = 0 ]; then bash shellrc.sh "$1" "$BASHRC" if [ ! "$1" = "list" ]; then echo "To activate in current terminal window" @@ -34,10 +39,18 @@ if [ "$first" = 0 ]; then fi fi -if [ "$second" = 0 ]; then +if [ "$check_zsh" = 0 ]; then bash shellrc.sh "$1" "$ZSHRC" if [ ! "$1" = "list" ]; then echo "To activate in current terminal window" echo "run ${bold}source ~/.zshrc${normal}" fi fi + +if [ "$check_fish" = 0 ]; then + bash shellrc.sh "$1" "$FISHRC" + if [ ! "$1" = "list" ]; then + echo "To activate in current terminal window" + echo "run ${bold}source ~/.config/fish/config.fish${normal}" + fi +fi diff --git a/linux_functions.sh b/linux_functions.sh index 778f0c6..8ce7aa0 100644 --- a/linux_functions.sh +++ b/linux_functions.sh @@ -16,7 +16,7 @@ function _do_it_for_selection() { # COMMENTED FOR WARNING # 1) _do_it_for_all "$what_to_do" # ;; - 2) bash "bash-zsh.sh" "$what_to_do" + 2) bash "bash-zsh-fish.sh" "$what_to_do" ;; 3) sudo -E bash "environment.sh" "$what_to_do" ;; @@ -45,7 +45,7 @@ function _do_it_for_all() { local what_to_do="$1" if [[ -z "$targets" || "$targets" = "1" ]]; then - bash "bash-zsh.sh" "$what_to_do" + bash "bash-zsh-fish.sh" "$what_to_do" # avoiding /etc/environment in all because it requires logout after unset # bashrc / zshrc is sufficient, and sudo -E might suffice if [[ "$what_to_do" = "list" ]]; then @@ -71,7 +71,7 @@ function prompt_for_proxy_targets() { echo "${bold}${blue} Select targets to modify ${normal}" echo "|${bold}${red} 1 ${normal}| All of them ... Don't bother me" - echo "|${bold}${red} 2 ${normal}| Terminal / bash / zsh (current user) " + echo "|${bold}${red} 2 ${normal}| Terminal (bash/zsh/fish for current user) " echo "|${bold}${red} 3 ${normal}| /etc/environment" echo "|${bold}${red} 4 ${normal}| apt/dnf (Package manager)" echo "|${bold}${red} 5 ${normal}| Desktop settings (GNOME/Ubuntu/KDE)" diff --git a/macos_functions.sh b/macos_functions.sh index 7a345ef..dffba5d 100644 --- a/macos_functions.sh +++ b/macos_functions.sh @@ -18,7 +18,7 @@ function _do_it_for_selection() { # ;; 2) bash "networksetup.sh" "$what_to_do" ;; - 3) bash "bash-zsh.sh" "$what_to_do" + 3) bash "bash-zsh-fish.sh" "$what_to_do" ;; 4) bash "npm.sh" "$what_to_do" ;; @@ -36,7 +36,7 @@ function _do_it_for_all() { local what_to_do="$1" if [[ -z "$targets" || "$targets" = "1" ]]; then - bash "bash-zsh.sh" "$what_to_do" + bash "bash-zsh-fish.sh" "$what_to_do" bash "networksetup.sh" "$what_to_do" bash "npm.sh" "$what_to_do" bash "git.sh" "$what_to_do" @@ -51,7 +51,7 @@ function prompt_for_proxy_targets() { echo "|${bold}${red} 1 ${normal}| All of them ... Don't bother me" echo "|${bold}${red} 2 ${normal}| System-wide settings (networksetup)" - echo "|${bold}${red} 3 ${normal}| Terminal / bash / zsh (current user)" + echo "|${bold}${red} 3 ${normal}| Terminal (bash/zsh/fish for current user) " echo "|${bold}${red} 4 ${normal}| npm & yarn" echo "|${bold}${red} 5 ${normal}| Git" echo From 0eff8c98691dbd6f4e26134b86e4443d4f9a89d5 Mon Sep 17 00:00:00 2001 From: Beh Date: Thu, 9 Apr 2020 19:22:17 +0200 Subject: [PATCH 19/21] Change proxyman executable header to env by using /usr/bin/env, we can just execute this with fish shell as well --- proxyman | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxyman b/proxyman index 37e0a82..5c2a1c1 100644 --- a/proxyman +++ b/proxyman @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash cd $HOME/.proxyman ./main.sh $@ From c6ba57a32ab2ded210b63f02e2d04a7a196a2ce2 Mon Sep 17 00:00:00 2001 From: Beh Date: Tue, 22 Sep 2020 23:08:55 +0200 Subject: [PATCH 20/21] Adding a space for formatting --- shellrc.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shellrc.sh b/shellrc.sh index eff683b..5ac6d3d 100644 --- a/shellrc.sh +++ b/shellrc.sh @@ -6,7 +6,7 @@ # This is required for Elementary OS which contains ~/.bashrc without \n _fix_new_line() { if [[ $(tail -c 1 "$SHELLRC" | wc --lines) = 0 ]]; then - echo >> "$1" + echo >>"$1" fi } From 75e62e6cd6b0e15d8964156a96f73615b689ca78 Mon Sep 17 00:00:00 2001 From: Beh Date: Tue, 22 Sep 2020 23:13:43 +0200 Subject: [PATCH 21/21] Fix ineffective unsetting env vars for Fish shell + make sed command OS independent + use only 1 for-loop for unsetting variables and modify variable to uppercase + make code more readable by splitting variable removal into own function --- shellrc.sh | 58 +++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/shellrc.sh b/shellrc.sh index 5ac6d3d..21f8026 100644 --- a/shellrc.sh +++ b/shellrc.sh @@ -21,41 +21,45 @@ list_proxy() { fi } +_modify_shellrc_variable_line() { + PROXY_VARIABLE_NAME="$1" + DELETE="$2" + + # Fish shell needs the variables to be explicitely unset, so we replace, if on Fish + # If $DELETE is empty, we will delete the whole line, even when we are on Fish + # This is needed for cleaning the $SHELLRC file before setting a new proxy + if [[ ("$SHELLRC" == *"fish"*) && (-z "$DELETE") ]]; then + SED_STRING="s/.*${PROXY_VARIABLE_NAME}.*/set -e ${PROXY_VARIABLE_NAME}/g" + else + SED_STRING="/.*${PROXY_VARIABLE_NAME}.*/d" + fi + + # We use -i.bak and delete the backup file afterwards to be compatible with GNU sed and BSD sed at the same time + # (as in https://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-bsd-and-linux) + sed -i.bak "$SED_STRING" "$SHELLRC" && rm ${SHELLRC}.bak +} + unset_proxy() { if [ ! -e "$SHELLRC" ]; then return fi - # macOS ships with BSD sed, which has different syntax from GNU sed - case $os in - "Linux") - # extra effort required to avoid removing custom environment variables set - # by the user for personal use - for proxytype in "http" "https" "ftp" "rsync" "no"; do - sed -i "/export ${proxytype}_proxy\=/d" "$SHELLRC" - done - for PROXYTYPE in "HTTP" "HTTPS" "FTP" "RSYNC" "NO"; do - sed -i "/export ${PROXYTYPE}_PROXY\=/d" "$SHELLRC" - done - ;; - "Darwin") - # extra effort required to avoid removing custom environment variables set - # by the user for personal use - for proxytype in "http" "https" "ftp" "rsync" "no"; do - sed -i "" "/export ${proxytype}_proxy\=/d" "$SHELLRC" - done - for PROXYTYPE in "HTTP" "HTTPS" "FTP" "RSYNC" "NO"; do - sed -i "" "/export ${PROXYTYPE}_PROXY\=/d" "$SHELLRC" - done - ;; - *) echo "Operating system not supported. Exiting." - exit 1 - ;; - esac + # Option to definitely delete the whole line + DELETE="$1" + + for proxytype in "http" "https" "ftp" "rsync" "no"; do + # Handle lower case entries + PROXY_VARIABLE_NAME="${proxytype}_proxy" + _modify_shellrc_variable_line $PROXY_VARIABLE_NAME "$DELETE" + + # Handle upper case entries + PROXY_VARIABLE_NAME=$(echo "$PROXY_VARIABLE_NAME" | awk '{print toupper($0)}') + _modify_shellrc_variable_line $PROXY_VARIABLE_NAME "$DELETE" + done } set_proxy() { - unset_proxy + unset_proxy "DELETE" if [ ! -e "$SHELLRC" ]; then touch "$SHELLRC" fi