├── request_worker.sh ├── README.md ├── queue_server.sh ├── LICENSE └── http_server.sh /request_worker.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | tail -n 0 -f mask.db | \ 4 | while read line; do 5 | 6 | echo $line 7 | IFS=: read COMMAND REPLY_CHANNEL USERID <<< "$line" 8 | 9 | # TODO: implement a database to handle transaction 10 | 11 | echo "$USERID:SUCCESS" > proc.$REPLY_CHANNEL.reply.fifo 12 | done 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # shell-based-mask-registration 2 | 3 | A shell-based web service written in Bash. Just for fun :-D 4 | 5 | # Usage 6 | 7 | ```shell 8 | ./http_server.sh & 9 | ./queue_server.sh & 10 | ./request_handler.sh & 11 | ``` 12 | 13 | ## License 14 | 15 | Licensed under the MIT License 16 | 17 | ## Authors 18 | 19 | Copyright(c) 2020 Fred Chien <> 20 | -------------------------------------------------------------------------------- /queue_server.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | PORT=8788 4 | REPLICA=2 5 | 6 | trap 'pkill -P $$ &> /dev/null' INT 7 | trap 'pkill -P $$ &> /dev/null' EXIT 8 | 9 | # Start mulitiple processes to handle connections 10 | echo "Replica is $REPLICA" 11 | for i in $(seq 1 $REPLICA); do 12 | nc -l -k $PORT >> mask.db & 13 | done 14 | 15 | echo "Listening on port $PORT" 16 | 17 | wait 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Fred Chien(錢逢祥) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /http_server.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | PORT=8787 4 | QUEUE_SERVER=localhost 5 | QUEUE_PORT=8788 6 | REPLICA=2 7 | 8 | trap 'pkill -P $$ &> /dev/null' INT 9 | trap 'pkill -P $$ &> /dev/null' EXIT 10 | 11 | function connection_handler() { 12 | 13 | FIFO_FILE=proc.$1.reply.fifo 14 | mkfifo $FIFO_FILE &> /dev/null 15 | 16 | USERID="iamuser" 17 | COUNT=0 18 | while IFS=$'\r\n' read line; do 19 | 20 | if [ ${#line} == 0 ]; then 21 | COUNT=0 22 | 23 | # command : reply cahnnel : user id 24 | echo "ORDER:$1:$USERID" > /dev/tcp/$QUEUE_SERVER/$QUEUE_PORT 25 | 26 | # Waiting for result from reply channel 27 | IFS=: read ID STATUS < $FIFO_FILE 28 | 29 | if [ "$STATUS" == "SUCCESS" ]; then 30 | echo -en "HTTP/1.1 200 OK\r\n" 31 | echo -en "Content-Length: ${#STATUS}\r\n" 32 | echo -en "\r\n" 33 | echo -en $STATUS 34 | echo -en "\r\n" 35 | else 36 | echo -en "HTTP/1.1 400 Bad Request\r\n" 37 | echo -en "Content-Length: ${#STATUS}\r\n" 38 | echo -en "\r\n" 39 | echo -en $STATUS 40 | echo -en "\r\n" 41 | fi 42 | 43 | continue 44 | fi 45 | 46 | COUNT=$[COUNT+1] 47 | 48 | # Check token 49 | if [ "${line%: *}" == "Authentication" ]; then 50 | TOKEN=${line#*: } 51 | 52 | # TODO: Authentication mechanism 53 | 54 | # PASS ALWAYS 55 | USERID="blah" 56 | continue 57 | fi 58 | 59 | echo ">>> ${line}" >&2 60 | 61 | done < "${2:-/dev/stdin}" 62 | } 63 | 64 | function createProcess() { 65 | 66 | FIFO_FILE=proc.$1.fifo 67 | mkfifo $FIFO_FILE &> /dev/null 68 | 69 | nc -l -k $PORT < $FIFO_FILE | connection_handler "$1" > $FIFO_FILE & 70 | } 71 | 72 | # Start mulitiple processes to handle connections 73 | echo "Replica is $REPLICA" 74 | for i in $(seq 1 $REPLICA); do 75 | echo "Starting process $i ..." 76 | createProcess $i 77 | done 78 | 79 | echo "Listening on port $PORT" 80 | 81 | wait 82 | --------------------------------------------------------------------------------