<commit_before>#!/bin/bash

function displayUsage()
{
    local scriptName="$(basename ${0})"

    echo -e "\033[1;33m"
    echo    "SYNOPSIS :"
    echo    "    ${scriptName}"
    echo    "        --help"
    echo    "        --configure"
    echo    "        --local-port <LOCAL_PORT>"
    echo    "        --remote-user <REMOTE_USER>"
    echo    "        --remote-host <REMOTE_HOST>"
    echo    "        --remote-port <REMOTE_PORT>"
    echo -e "\033[1;35m"
    echo    "DESCRIPTION :"
    echo    "    --help           Help page"
    echo    "    --configure      Config remote server to support forwarding (optional)"
    echo    "                     This option will require arguments '--remote-user' and '--remote-host'"
    echo    "    --local-port     Local port number (require)"
    echo    "    --remote-user    Remote user (require)"
    echo    "    --remote-host    Remote host (require)"
    echo    "    --remote-port    Remote port number (require)"
    echo -e "\033[1;36m"
    echo    "EXAMPLES :"
    echo    "    ./${scriptName} --help"
    echo    "    ./${scriptName}"
    echo    "        --configure"
    echo    "        --remote-user 'root'"
    echo    "        --remote-host 'my-server.com'"
    echo    "    ./${scriptName}"
    echo    "        --local-port 8080"
    echo    "        --remote-user 'root'"
    echo    "        --remote-host 'my-server.com'"
    echo    "        --remote-port 9090"
    echo -e "\033[0m"

    exit ${1}
}

function configure()
{
    local remoteUser="${1}"
    local remoteHost="${2}"

    local commands="$(cat "${utilPath}")
                    checkRequireRootUser
                    appendToFileIfNotFound "${sshdConfigFile}" "${tcpForwardConfigPattern}" '\nAllowTcpForwarding yes'
                    appendToFileIfNotFound "${sshdConfigFile}" "${gatewayConfigPattern}" 'GatewayPorts yes'
                    service ssh restart"

    ssh -n "${remoteUser}@${remoteHost}" "${commands}"
}

function tunnel()
{
    local localPort="${1}"
    local remoteUser="${2}"
    local remoteHost="${3}"
    local remotePort="${4}"

    local localProcess="$(lsof -Pi | grep -Fi ":${localPort} (LISTEN)" | head -1)"
    local remoteProcess="$(ssh -n "${remoteUser}@${remoteHost}" lsof -Pi | grep -Fi ":${remotePort} (LISTEN)" | head -1)"

    if [[ "$(isEmptyString "${localProcess}")" = 'true' ]]
    then
        fatal "\nERROR: local port ${localPort} is not running at local machine. Please pick another local running port!\n"
    fi

    if [[ "$(isEmptyString "${remoteProcess}")" = 'false' ]]
    then
        fatal "\nERROR: remote port ${remotePort} is already taken at remote host '${remoteHost}'. Please pick another remote port!\n"
    fi

    echo -e "\033[1;35m${remoteHost}:${remotePort} \033[1;36mforwards to \033[1;32mlocalhost:${localPort}\033[0m\n"

    ssh -C -N -g -v \
        -p 22 \
        -c '3des-cbc' \
        -R "${remotePort}:localhost:${localPort}" \
        "${remoteUser}@${remoteHost}"
}

function main()
{
    local appPath="$(cd "$(dirname "${0}")" && pwd)"
    local optCount=${#}
    utilPath="${appPath}/lib/util.bash"

    source "${utilPath}" || exit 1

    while [[ ${#} -gt 0 ]]<commit_msg>bug fixes<commit_after>#!/bin/bash

function displayUsage()
{
    local scriptName="$(basename ${0})"

    echo -e "\033[1;33m"
    echo    "SYNOPSIS :"
    echo    "    ${scriptName}"
    echo    "        --help"
    echo    "        --configure"
    echo    "        --local-port <LOCAL_PORT>"
    echo    "        --remote-user <REMOTE_USER>"
    echo    "        --remote-host <REMOTE_HOST>"
    echo    "        --remote-port <REMOTE_PORT>"
    echo -e "\033[1;35m"
    echo    "DESCRIPTION :"
    echo    "    --help           Help page"
    echo    "    --configure      Config remote server to support forwarding (optional)"
    echo    "                     This option will require arguments '--remote-user' and '--remote-host'"
    echo    "    --local-port     Local port number (require)"
    echo    "    --remote-user    Remote user (require)"
    echo    "    --remote-host    Remote host (require)"
    echo    "    --remote-port    Remote port number (require)"
    echo -e "\033[1;36m"
    echo    "EXAMPLES :"
    echo    "    ./${scriptName} --help"
    echo    "    ./${scriptName}"
    echo    "        --configure"
    echo    "        --remote-user 'root'"
    echo    "        --remote-host 'my-server.com'"
    echo    "    ./${scriptName}"
    echo    "        --local-port 8080"
    echo    "        --remote-user 'root'"
    echo    "        --remote-host 'my-server.com'"
    echo    "        --remote-port 9090"
    echo -e "\033[0m"

    exit ${1}
}

function configure()
{
    local remoteUser="${1}"
    local remoteHost="${2}"

    local commands="$(cat "${utilPath}")
                    checkRequireRootUser
                    appendToFileIfNotFound '${sshdConfigFile}' '${tcpForwardConfigPattern}' '\nAllowTcpForwarding yes'
                    appendToFileIfNotFound '${sshdConfigFile}' '${gatewayConfigPattern}' 'GatewayPorts yes'
                    service ssh restart"

    ssh -n "${remoteUser}@${remoteHost}" "${commands}"
}

function tunnel()
{
    local localPort="${1}"
    local remoteUser="${2}"
    local remoteHost="${3}"
    local remotePort="${4}"

    # Verify Ports

    local localProcess="$(lsof -Pi | grep -Fi ":${localPort} (LISTEN)" | head -1)"
    local remoteProcess="$(ssh -n "${remoteUser}@${remoteHost}" lsof -Pi | grep -Fi ":${remotePort} (LISTEN)" | head -1)"

    if [[ "$(isEmptyString "${localProcess}")" = 'true' ]]
    then
        fatal "\nERROR: local port ${localPort} is not running at local machine. Please pick another local running port!\n"
    fi

    if [[ "$(isEmptyString "${remoteProcess}")" = 'false' ]]
    then
        fatal "\nERROR: remote port ${remotePort} is already taken at remote host '${remoteHost}'. Please pick another remote port!\n"
    fi

    # Verify Remote Config

    local tcpForwardConfigFound="$(ssh -n "${remoteUser}@${remoteHost}" grep -Eo "'${tcpForwardConfigPattern}'" "'${sshdConfigFile}'")"
    local gatewayConfigFound="$(ssh -n "${remoteUser}@${remoteHost}" grep -Eo "'${gatewayConfigPattern}'" "'${sshdConfigFile}'")"

    if [[ "$(isEmptyString "${tcpForwardConfigFound}")" = 'true' || "$(isEmptyString "${gatewayConfigFound}")" = 'true' ]]
    then
       error "\nWARNING :"
       error "    - Your remote host '${remoteHost}' is NOT yet configured for tunneling. Run '--configure' to set it up!"
       error "    - Will continue tunneling but it might not work for you!\n"
       sleep 5
    fi

    # Start Forwarding

    echo -e "\033[1;35m${remoteHost}:${remotePort} \033[1;36mforwards to \033[1;32mlocalhost:${localPort}\033[0m\n"

    ssh -C -N -g -v \
        -p 22 \
        -c '3des-cbc' \
        -R "${remotePort}:localhost:${localPort}" \
        "${remoteUser}@${remoteHost}"
}

function main()
{
    local appPath="$(cd "$(dirname "${0}")" && pwd)"
    local optCount=${#}
    utilPath="${appPath}/lib/util.bash"

    source "${utilPath}" || exit 1

    while [[ ${#} -gt 0 ]]
{'fraction_non_alphanumeric': 0.16502670789991566, 'fraction_numerical': 0.01925780151813326, 'mean_word_length': 2.174921909861669, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '167658', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2176, 'n_words': 573}