Commit 5730a2f0 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

Replace netcat with socat

Replace netcat with socat
parents b0840002 fea76ed7
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ RUN apt-get update && apt-get -y upgrade && \
	wget \
	fdisk \
	unzip \
	socat \	
	procps \
	dnsmasq \
	xz-utils \
+2 −18
Original line number Diff line number Diff line
@@ -228,24 +228,8 @@ if [[ "${DHCP}" == [Yy1]* ]]; then

  { pkill -f server.sh || true; } 2>/dev/null

  SH_SCRIPT="/run/ipinfo.sh"

  { echo "#!/bin/bash"
    echo "INFO=\$(curl -s -m 5 -S http://127.0.0.1:2210/read?command=10 2>/dev/null)"
    echo "rest=\${INFO#*http_port}; rest=\${rest#*:}; rest=\${rest%%,*}; PORT=\${rest%%\\\"*}"
    echo "rest=\${INFO#*eth0}; rest=\${rest#*ip}; rest=\${rest#*:}; rest=\${rest#*\\\"}; IP=\${rest%%\\\"*}"
    echo "BODY=\"The location of DSM is <a href=\"http://\${IP}:\${PORT}\">http://\${IP}:\${PORT}</a><script>\\"
    echo "setTimeout(function(){ window.location.assign('http://\${IP}:\${PORT}'); }, 3000);</script>\""
    echo "HTML=\"<!DOCTYPE html><HTML><HEAD><TITLE>VirtualDSM</TITLE><STYLE>body { color: white; background-color: #125bdb; font-family: Verdana,\\"
    echo "Arial,sans-serif; } a, a:hover, a:active, a:visited { color: white; }</STYLE></HEAD><BODY><BR><BR><H1><CENTER>\$BODY</CENTER></H1></BODY></HTML>\""
    echo "LENGTH=\"\${#HTML}\"; RESPONSE=\"HTTP/1.1 200 OK\\nContent-Length: \${LENGTH}\\nConnection: close\\n\\n\$HTML\""
    echo "echo -e \"\$RESPONSE\""
  } > "$SH_SCRIPT"

  chmod +x "$SH_SCRIPT"

  /run/server.sh 80 "$SH_SCRIPT" &
  /run/server.sh 5000 "$SH_SCRIPT" &
  /run/server.sh 80 /run/ip.sh &
  /run/server.sh 5000 /run/ip.sh &

else

+23 −6
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -eu

TMP_FILE=""
TMP_FILE=$(mktemp -q /tmp/server.XXXXXX)

stop() {
  trap - SIGINT EXIT
@@ -13,17 +13,34 @@ trap 'stop' EXIT SIGINT SIGTERM SIGHUP

if [[ "$2" == "/"* ]]; then

  socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"$2" 2> /dev/null & wait $!
  if [[ "$2" == "/run/ip.sh" ]]; then

    { echo "#!/bin/bash"
      echo "INFO=\$(curl -s -m 5 -S http://127.0.0.1:2210/read?command=10 2>/dev/null)"
      echo "rest=\${INFO#*http_port}; rest=\${rest#*:}; rest=\${rest%%,*}; PORT=\${rest%%\\\"*}"
      echo "rest=\${INFO#*eth0}; rest=\${rest#*ip}; rest=\${rest#*:}; rest=\${rest#*\\\"}; IP=\${rest%%\\\"*}"
      echo "BODY=\"The location of DSM is <a href=\"http://\${IP}:\${PORT}\">http://\${IP}:\${PORT}</a><script>\\"
      echo "setTimeout(function(){ window.location.assign('http://\${IP}:\${PORT}'); }, 3000);</script>\""
      echo "HTML=\"<!DOCTYPE html><HTML><HEAD><TITLE>VirtualDSM</TITLE><STYLE>body { color: white; background-color: #125bdb; font-family: Verdana,\\"
      echo "Arial,sans-serif; } a, a:hover, a:active, a:visited { color: white; }</STYLE></HEAD><BODY><BR><BR><H1><CENTER>\$BODY</CENTER></H1></BODY></HTML>\""
      echo "echo -e \"\HTTP/1.1 200 OK\\nContent-Length: \${#HTML}\\nConnection: close\\n\\n\$HTML\""
    } > "$TMP_FILE"

  else

      cp "$2" "$TMP_FILE"

  fi

  chmod +x "$TMP_FILE"
  socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"$TMP_FILE" 2> /dev/null & wait $!

else

  HTML="<!DOCTYPE html><HTML><HEAD><TITLE>VirtualDSM</TITLE><STYLE>body { color: white; background-color: #125bdb; font-family: Verdana,\
        Arial,sans-serif; } a, a:hover, a:active, a:visited { color: white; }</STYLE></HEAD><BODY><BR><BR><H1><CENTER>$2</CENTER></H1></BODY></HTML>"

  LENGTH="${#HTML}"
  TMP_FILE=$(mktemp -q /tmp/server.XXXXXX)

  echo -en "HTTP/1.1 200 OK\nContent-Length: ${LENGTH}\nConnection: close\n\n$HTML" > "$TMP_FILE"
  echo -en "HTTP/1.1 200 OK\nContent-Length: ${#HTML}\nConnection: close\n\n$HTML" > "$TMP_FILE"
  socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"cat ${TMP_FILE}" 2> /dev/null & wait $!

fi