HTTP プロキシとして機能する簡単なスクリプトを bash で作成しました。
#!/usr/bin/env bash
trap "kill 0" SIGINT SIGTERM EXIT  # kill all subshells on exit
port="6000"
rm -f client_output client_output_for_request_forming server_output
mkfifo client_output client_output_for_request_forming server_output  # create named pipes
# creating subshell
(
    cat <server_output |
    nc -lp $port |  # awaiting connection from the client of the port specified
    tee client_output |  # sending copy of ouput to client_output pipe
    tee client_output_for_request_forming # sending copy of ouput to client_output_for_request_forming pipe
) &   # starting subshell in a separate process
echo "OK!"
# creating another subshell (to feed client_output_for_request_forming to it)
(
    while read line;  # read input from client_output_for_request_forming line by line
    do
        echo "line read: $line"
        if [[ $line =~ ^Host:[[:space:]]([[:alnum:].-_]*)(:([[:digit:]]+))?[[:space:]]*$ ]]
        then
            echo "match: $line"
            server_port=${BASH_REMATCH[3]}  # extracting server port from regular expression
            if [[ "$server_port" -eq "" ]]
            then
                server_port="80"
            fi
            host=${BASH_REMATCH[1]}  # extracting host from regular expression
            nc $host $server_port <client_output |  # connect to the server
            tee server_output  # send copy to server_output pipe
            break
        fi
    done
) <client_output_for_request_forming
echo "OK2!"
rm -f client_output client_output_for_request_forming server_output
最初のターミナルで起動します。そして、それは出力しますOK!
2番目に次のように入力します。
netcat localhost 6000
次に、サイクルがあるため、最初の端末ウィンドウに表示されることを期待してテキスト行の入力を開始しますwhile read line。しかし、何も表示されません。
私が間違っているのは何ですか?どうすればそれを機能させることができますか?