Skip to content

Instantly share code, notes, and snippets.

@alexolinux
Last active June 6, 2024 10:13
Show Gist options
  • Save alexolinux/8ebb71c4de9240172d693942fea9a008 to your computer and use it in GitHub Desktop.
Save alexolinux/8ebb71c4de9240172d693942fea9a008 to your computer and use it in GitHub Desktop.
Function shell to handle with proxy under cntlm

cntlm-proxy


Usage:

  • To set the proxy, use: proxy set
  • To unset the proxy, use: proxy unset

RHEL

#!/bin/zsh

# Function to set and unset HTTP and HTTPS proxy
function proxy() {
  local YUM_CONF="/etc/yum.conf"
  local DNF_CONF="/etc/dnf/dnf.conf"
  local PORT=3128

  # Check if cntlmd service is running
  service_name="cntlmd"
  service_status=$(systemctl is-active $service_name)

  if [[ $1 == "set" ]]; then

    if [ "$service_status" != "active" ]; then
      sudo systemctl start $service_name
    fi

    export http_proxy="http://127.0.0.1:${PORT}"
    export https_proxy="http://127.0.0.1:${PORT}"
    export ftp_proxy="http://127.0.0.1:${PORT}"
    echo "Proxy is enabled."

    # Add proxy to repo confs if not already present
    if ! grep -q "^proxy=http://127.0.0.1:${PORT}" "${YUM_CONF}"; then
      echo "proxy=http://127.0.0.1:${PORT}" | sudo tee -a "${YUM_CONF}" > /dev/null
      echo "Proxy line added to ${YUM_CONF}"
    else
      echo "Proxy line already exists in ${YUM_CONF}"
    fi

    if ! grep -q "^proxy=http://127.0.0.1:${PORT}" "${DNF_CONF}"; then
      echo "proxy=http://127.0.0.1:${PORT}" | sudo tee -a "${DNF_CONF}" > /dev/null
      echo "Proxy line added to ${DNF_CONF}"
    else
      echo "Proxy line already exists in ${DNF_CONF}"
    fi

  elif [[ $1 == "unset" ]]; then

    if [ "$service_status" = "active" ]; then
      sudo systemctl stop $service_name
    fi

    unset http_proxy
    unset https_proxy
    unset ftp_proxy
    echo "Proxy is disabled."

    # Remove proxy from repo confs if present
    if grep -q "^proxy=http://127.0.0.1:${PORT}" "${YUM_CONF}"; then
      sudo sed -i "/^proxy=http:\/\/127.0.0.1:${PORT}/d" "${YUM_CONF}"
      echo "Proxy line removed from ${YUM_CONF}"
    else
      echo "Proxy line not found in ${YUM_CONF}"
    fi

    if grep -q "^proxy=http://127.0.0.1:${PORT}" "${DNF_CONF}"; then
      sudo sed -i "/^proxy=http:\/\/127.0.0.1:${PORT}/d" "${DNF_CONF}"
      echo "Proxy line removed from ${DNF_CONF}"
    else
      echo "Proxy line not found in ${DNF_CONF}"
    fi

  else
    echo "Usage: proxy {set|unset}"
  fi
}

DEBIAN

#!/bin/zsh

# Function to set and unset HTTP and HTTPS proxy
function proxy() {

  APT_CONF="/etc/apt/apt.conf.d"
  PORT=3129

  if [[ $1 == "set" ]]; then

    #-- Check Service
    if $(sudo service cntlm status | grep -q "is running"); then
      echo "CNTLM is already running."
    else
      /usr/bin/sudo service cntlm start
    fi

    export http_proxy="http://127.0.0.1:${PORT}"
    export https_proxy="http://127.0.0.1:${PORT}"
    export ftp_proxy="http://127.0.0.1:${PORT}"
    echo "Proxy is enabled."

    # Add proxy to ${APT_CONF}/02proxy.conf if not already present
    if ! grep -q "Acquire::http::Proxy \"http://127.0.0.1:${PORT}\";" ${APT_CONF}/02proxy.conf; then
      echo "Acquire::http::Proxy \"http://127.0.0.1:${PORT}\";" | sudo tee -a ${APT_CONF}/02proxy.conf > /dev/null
      echo "Acquire::https::Proxy \"http://127.0.0.1:${PORT}\";" | sudo tee -a ${APT_CONF}/02proxy.conf > /dev/null
      echo "Proxy lines added to $APT_CONF/02proxy.conf"
    else
      echo "Proxy lines already exist in APT Configuration"
    fi

  elif [[ $1 == "unset" ]]; then

    #-- Check Service
    if $(sudo service cntlm status | grep -q "is running"); then
      /usr/bin/sudo service cntlm stop
    else
      echo "CNTLM is already stopped."
    fi

    unset http_proxy
    unset https_proxy
    unset ftp_proxy
    echo "Proxy is disabled."

    # Remove proxy from ${APT_CONF}/02proxy.conf if present
    if grep -q "Acquire::http::Proxy \"http://127.0.0.1:${PORT}\";" ${APT_CONF}/02proxy.conf; then
      sudo sed -i "/Acquire::http::Proxy \"http:\/\/127.0.0.1:${PORT}\";/d" ${APT_CONF}/02proxy.conf
      sudo sed -i "/Acquire::https::Proxy \"http:\/\/127.0.0.1:${PORT}\";/d" ${APT_CONF}/02proxy.conf
      echo "Proxy lines removed from $APT_CONF/02proxy.conf"
    else
      echo "Proxy lines not found in $APT_CONF/02proxy.conf"
    fi
  else
    echo "Usage: proxy {set|unset}"
  fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment