Shifu高级功能:命令行中间件之HTTP 到 PowerShell 的中间件
创始人
2025-05-29 06:45:42
0

简介

为了让你的设备接入Shifu。我们用Go编写了一个简单的HTTP到 PowerShell 的中间件,供开发者使用。

设计

这个HTTP到PowerShell的中间件是这样设计的:

  • 中间件在主机上暴露了一个HTTP接口
  • 该HTTP接口用于转发来自外部的请求到 Windows 主机
  • 中间件将代理结果和执行状态返回给请求者

功能

将HTTP请求体代理到PowerShell shell并执行

接受HTTP请求体中的一切内容,并在规定超时时间内执行。

构建中间件

操作

386

GOOS=windows GOARCH=386 go build -a -o http2powershell.exe cmd/httpstub/powershellstub/powershellstub.go

amd64

GOOS=windows GOARCH=amd64 go build -a -o http2powershell.exe cmd/httpstub/powershellstub/powershellstub.go

使用方法

该可执行文件需要配置以下环境变量:

  • EDGEDEVICE_DRIVER_HTTP_PORT (可选)
    • 驱动程序容器的HTTP服务器端口,默认为11112
  • EDGEDEVICE_DRIVER_EXEC_TIMEOUT_SECOND (可选)
    • 执行指令的超时时间,可以通过在命令前面加上timeout 来实现。

对于Windows主机的操作:

如果要运行中间件,请双击http2powershell.exe,默认情况下,中间件会在0.0.0.0上监听11112端口。

对于Shifu的操作:

使用/examples/simple-powershell-stub中提供的样本部署文件。

shifu的根目录下发布:

kubectl apply -f driver_util/http-to-powershell-stub/examples/Simple-powershell-stub

代理命令

使用curlWindows主机发布请求:

root@nginx:/# curl "edgedevice-powershell/issue_cmd?flags_no_parameter=ls,C:"Directory: C:\
Mode                 LastWriteTime         Length Name                                                   
----                 -------------         ------ ----                                                   
d-----          6/5/2021   8:10 PM                PerfLogs                                               
d-r---          6/9/2022   2:48 PM                Program Files                                          
d-r---         4/29/2022   8:02 PM                Program Files (x86)                                    
d-r---         4/16/2022   1:46 AM                Users                                                  
d-----          6/9/2022   2:48 PM                Windows                                                
d-----         4/17/2022   5:23 PM                xampp                                                     root@nginx:/# curl "edgedevice-powershell/issue_cmd?flags_no_parameter=ping,8.8.8.8"Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=64ms TTL=114
Reply from 8.8.8.8: bytes=32 time=56ms TTL=114
Reply from 8.8.8.8: bytes=32 time=57ms TTL=114
Reply from 8.8.8.8: bytes=32 time=59ms TTL=114Ping statistics for 8.8.8.8:Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:Minimum = 56ms, Maximum = 64ms, Average = 59ms

示例

当使用curl向一个给定的URL发布请求时,命令如下:

curl "example.com/issue_cmd?flags_no_parameter=ping,8.8.8.8

然后请求将从HTTP的中间件传到Windows主机的PowerShell

> powershell.exe ping 8.8.8.8

请注意,默认的定时EDGEDEVICE_DRIVER_EXEC_TIMEOUT_SECOND可以被URL中的timeout标志所覆盖,例如:

  • 如果没有timeout标志(命令超时,输出不完整):
root@nginx:/# curl "example.com/issue_cmd?flags_no_parameter=ping,-n,6,8.8.8.8"   Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=58ms TTL=114
Reply from 8.8.8.8: bytes=32 time=51ms TTL=114
Reply from 8.8.8.8: bytes=32 time=59ms TTL=114
Reply from 8.8.8.8: bytes=32 time=45ms TTL=114
Reply from 8.8.8.8: bytes=32 time=59ms TTL=114
  • 使用timeout标志(输出完整):
root@nginx:/# curl "example.com/issue_cmd?timeout=10&flags_no_parameter=ping,-n,6,8.8.8.8" Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=60ms TTL=114
Reply from 8.8.8.8: bytes=32 time=60ms TTL=114
Reply from 8.8.8.8: bytes=32 time=59ms TTL=114
Reply from 8.8.8.8: bytes=32 time=59ms TTL=114
Reply from 8.8.8.8: bytes=32 time=59ms TTL=114
Reply from 8.8.8.8: bytes=32 time=60ms TTL=114Ping statistics for 8.8.8.8:Packets: Sent = 6, Received = 6, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:Minimum = 59ms, Maximum = 60ms, Average = 59ms

我们还添加了一个参数stub_toleration来处理deviceShifu和中间件之间的延迟问题。默认情况下,它被设置为1秒,你可以用以下方法覆盖这个时间:

root@nginx:/# curl "example.com/issue_cmd?timeout=10&flags_no_parameter=ping,-n,6,8.8.8.8&stub_toleration=0" 

本文由边无际授权发布

相关内容

热门资讯

数据库缓存 许多web停用都将数据保存到RDBMS(关系型数据库)中,...
各地政策持续加力 城市更新提速 人民网北京5月31日电(记者许维娜)近日,中共中央办公厅、国务院办公厅印发《关于持续推进城市更新行动...
河南胜蓝律师事务所开展《中华人... 在“六一国际儿童节”来临之际,河南胜蓝律师事务所刘红平主任带领律师志愿者走进驻马店市博爱幼儿园,开展...
Java每日一练(202303... 目录 1. 出现次数最多的字符  🌟 2. 最后一个单词的长度  🌟...
基于卷积神经网络CNN的图像分... 目录 背影 卷积神经网络CNN的原理 卷积神经网络CNN的定义 卷积神经网络CNN的神经元 卷积神经...
每周股票复盘:ST金鸿(000... 截至2025年5月30日收盘,ST金鸿(000669)报收于2.26元,较上周的2.22元上涨1.8...
每周股票复盘:洁特生物(688... 截至2025年5月30日收盘,洁特生物(688026)报收于15.68元,较上周的15.08元上涨3...
莱伯泰科股价小幅下跌 专利侵权... 截至2025年5月30日收盘,莱伯泰科股价报31.81元,较前一交易日下跌0.21元,跌幅0.66%...
南微医学股价微跌 专利诉讼进展... 南微医学股价报67.32元,较前一交易日下跌0.27%。盘中最高触及67.97元,最低下探66.70...
Unity学习笔记——unit... 2019-8-26 视频1: 巴拉巴拉巴拉 视频2: unity的介绍 ...