加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 使用netcat nc创建最小的REST Web服务器

发布时间:2020-12-16 01:50:26 所属栏目:安全 来源:网络整理
导读:我正在寻找使用nc作为Docker容器的单行的最小REST Web服务器.例如: * http://localhost/echo/marcello: prints marcello* http://localhost/date: prints the server's date* http://localhost/...: others 我正在查看问题“Minimal web server using netca
我正在寻找使用nc作为Docker容器的单行的最小REST Web服务器.例如:

* http://localhost/echo/marcello: prints marcello
* http://localhost/date: prints the server's date
* http://localhost/...: others

我正在查看问题“Minimal web server using netcat”,但它代理了对shell脚本的调用…我只需要一个像这样的内衬

while true ; do nc -l -p 1500 -c 'echo -e "HTTP/1.1 200 OKnn $(date)"'; done

发布的另一个解决方案很长并且从文件中加载…

解决方法

我破解了@syme在“ https://stackoverflow.com/a/24342101/433814”给出的例子并创建了单行REST服务器.缺少某些标头,但它正确处理HTTP GET和404未实现的资源.

rm -f out ; mkfifo out ; trap "rm -f out" EXIT ; while true ; do cat out | nc -l 1500 > >(export REQUEST= ; while read line ; do line=$(echo "$line" | tr -d '[rn]') ; if echo "$line" | grep -qE '^GET /' ; then REQUEST=$(echo "$line" | cut -d ' ' -f2) ; elif [ "x$line" = x ] ; then HTTP_200="HTTP/1.1 200 OK" ; HTTP_LOCATION="Location:" ; HTTP_404="HTTP/1.1 404 Not Found" ; if echo $REQUEST | grep -qE '^/echo/' ; then printf "%sn%s %snn%sn" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} > out ; elif echo $REQUEST | grep -qE '^/date' ; then date > out ; elif echo $REQUEST | grep -qE '^/stats' ; then vmstat -S M > out ; elif echo $REQUEST | grep -qE '^/net' ; then ifconfig > out ; else printf "%sn%s %snn%sn" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > out ; fi ; fi ; done) ; done

格式化版本位于https://gist.github.com/marcellodesales/9e4288f35ac2cc3e1b83#file-formatted

上面的API实现了以下内容:

> / echo / {name}

返回给定的{name}

$curl -i http://localhost:1500/echo/marcello
HTTP/1.1 200 OK
Location: /echo/marcello

marcello

> /日期

返回服务器的日期

$curl -i http://localhost:1500/date
Sun Oct 19 14:12:27 PDT 2014

> / stats

返回服务器的统计信息

$curl -i http://localhost:1500/stats
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0     11    374    383   2198    0    0     6    22   33    8  2  2 97  0  0

> / net

打印服务器的网络

$curl -i http://localhost:1500/net
docker0   Link encap:Ethernet  HWaddr 56:84:7a:fe:97:99  
          inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::5484:7aff:fefe:9799/64 Scope:Link
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:120694 errors:0 dropped:0 overruns:0 frame:0
          TX packets:141757 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:272911625 (272.9 MB)  TX bytes:289945068 (289.9 MB)

eth0      Link encap:Ethernet  HWaddr 00:0c:29:1f:d3:b5  
          inet addr:192.168.248.206  Bcast:192.168.248.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe1f:d3b5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2322493 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1098965 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2367412677 (2.3 GB)  TX bytes:700548644 (700.5 MB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:151566 errors:0 dropped:0 overruns:0 frame:0
          TX packets:151566 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:305833574 (305.8 MB)  TX bytes:305833574 (305.8 MB)

> / ANTHTHING / NOT / IMPLEMENTED

对于服务器未实现的任何内容,它会打印404消息.

$curl -i http://localhost:1500/wrong
HTTP/1.1 404 Not Found
Location: /wrong

Resource /wrong NOT FOUND!

这是上面GIST的格式化解决方案.您可以将其保存为“web.sh”并运行:)

rm -f out
mkfifo out
trap "rm -f out" EXIT
while true
do
  cat out | nc -l 1500 > >( # parse the netcat output,to build the answer redirected to the pipe "out".
    export REQUEST=
    while read line
    do
      line=$(echo "$line" | tr -d '[rn]')

      if echo "$line" | grep -qE '^GET /' # if line starts with "GET /"
      then
        REQUEST=$(echo "$line" | cut -d ' ' -f2) # extract the request
      elif [ "x$line" = x ] # empty line / end of request
      then
        HTTP_200="HTTP/1.1 200 OK"
        HTTP_LOCATION="Location:"
        HTTP_404="HTTP/1.1 404 Not Found"
        # call a script here
        # Note: REQUEST is exported,so the script can parse it (to answer 200/403/404 status code + content)
        if echo $REQUEST | grep -qE '^/echo/'
        then
            printf "%sn%s %snn%sn" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} > out
        elif echo $REQUEST | grep -qE '^/date'
        then
            date > out
        elif echo $REQUEST | grep -qE '^/stats'
        then
            vmstat -S M > out
        elif echo $REQUEST | grep -qE '^/net'
        then
            ifconfig > out
        else
            printf "%sn%s %snn%sn" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > out
        fi
      fi
    done
  )
done

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读