Piping Server:实现设备间通过纯 HTTP 无限传输数据

开源前哨

共 2994字,需浏览 6分钟

 · 2021-01-20

【导读】:Piping Server 可以使用户通过 HTTP/HTTPS 协议传输流数据(Stream),完成屏幕共享、视频聊天、实时文字聊天、SSH 协议、远程桌面控制等一系列功能。

开源地址:

https://github.com/nwtgck/piping-server

为什么要使用 HTTP 协议

HTTP 协议是目前最成熟、使用最广泛的协议之一,目前所有的桌面系统、移动智能设备甚至是物联网设备基本都会通过 HTTP 协议进行数据传输,除此之外,HTTP 协议也在向 HTTP/2, HTTP/3 演化,传输速度进一步加快。

HTTP 协议的广泛使用,意味着 Piping Server 可以以极低的代价完成不同设备的跨平台传输。

Piping Server

Piping Server 使用非常简单,发送方和接收方针对同一路径发起请求即可以进行数据传输,如下:

# Send
echo 'hello, world' | curl -T - https://ppng.io/hello
# Get
curl https://ppng.io/hello > hello.txt
# => "hello, world"

上述示例中,Piping Server 将数据从 POST /helloPUT /hello 传输到 GET /hello。在Piping Server 中不区分 POST 和 PUT 方法。传输路径 /hello 可以随意指定,并且发送方和接收方都可以率先发起传输。

在 JavaScript 中,可以使用 fetch() 进行传输:

// Send
fetch("https://ppng.io/hello", {
  method"POST",
  body"hello, world"
});
// Get
const res = await fetch("https://ppng.io/hello");
console.log(await res.text());
// => "hello, world"

流数据(Stream)

Piping Server 采用的是 HTTP 流式传输,这意味着可以 无限传输任何数据,下面的示例展示了如何传输文件夹:

# Send folder
tar c ./mydir | curl -T - https://ppng.io/mypath
# Get folder
curl https://ppng.io/mypath | tar xv

上述例子中,文件夹在上传过程中被打包并且在下载过程中被解压,通过流式传输并不需要其他创建临时文件。

采用如下方式可以轻松进行端对端加密传输:

  • send:... | openssl aes-256-cbc | curl -T ...
  • get:curl ... | openssl aes-256-cbc -d

同时也可以通过压缩减小传输数据大小:

  • send:... | gzip | curl -T ...
  • get:curl ... | zcat

你可以通过管道(pipe)组合完成任何数据的传输,比如 gpgzip 或未来新产生的工具,这样不管在时间上和空间上都有传输效率提升,Piping Server 的名字就来源于此,因此也可以直译为 管道传输服务器

长时间传输大量数据

在开发者实验中,Piping Server 在一个 HTTP 请求中传输了 1,110TB (≈ 1PB) 的数据,耗时超过64天2小时,这意味着它可以传输巨量数据并且保持一个 HTTP 请求超过2个月,以下为作者提供的实验截图。

实验

帮助文档

目前 能通过 curl 获取帮助和版本信息:

curl https://ppng.io/help
curl https://ppng.io/version

指定多个接收方

传输可以指定多个接收方,下面的例子中,通过增加请求参数 ?n=3 可以指定3个接收方,示例:

# Send
seq 1000 | curl -T - https://piping.ml/mypath?n=3

#
 Get
curl https://piping.ml/mypath?n=3

公用传输服务器

  • https://ppng.io (目前可用: https://ppng.ml, https://piping.ml)
  • https://piping.glitch.me
  • https://piping-47q675ro2guv.runkit.sh
  • https://piping.nwtgck.repl.co
  • https://ppng.herokuapp.com (注意: Heroku 不支持流式传输)

使用 Docker 自建传输服务器

通过以下命令可以在 http://localhost:8080 运行 Pipe Server。

docker run -p 8080:8080 nwtgck/piping-server

后台运行并且自动重启:

docker run -p 8080:8080 -d --restart=always nwtgck/piping-server

开源使用示例

以下为官方开源使用示例,大部分均使用 Vue 框架,前端学习成本较低,有兴趣的可以尝试。

  • 文字聊天[1]

  • 屏幕共享[2]

  • 共享绘图[3]

  • 点对点加密文档传输[4]

  • 浏览器 ssh 工具[5]

  • 浏览器远程桌面[6]

参考资料

[1]

文字聊天: https://github.com/nwtgck/piping-server-streaming-upload-htmls/blob/a107dd1fb1bbee9991a9278b10d9eaf88b52c395/text_stream.html

[2]

屏幕共享: https://github.com/nwtgck/piping-server-streaming-upload-htmls/blob/a107dd1fb1bbee9991a9278b10d9eaf88b52c395/screen_share.html

[3]

共享绘图: https://github.com/nwtgck/piping-draw-web

[4]

点对点加密文档传输: https://github.com/nwtgck/piping-draw-web

[5]

浏览器 ssh 工具: https://github.com/nwtgck/piping-ssh-web

[6]

浏览器远程桌面: https://github.com/nwtgck/piping-vnc-web



- EOF - 


更多优秀开源项目(点击下方图片可跳转)





开源前哨

日常分享热门、有趣和实用的开源项目。参与维护10万+star的开源技术资源库,包括:Python, Java, C/C++, Go, JS, CSS, Node.js, PHP, .NET 等

关注后获取

回复 资源 获取10万+star开源资源



分享、点赞和在看

支持我们分享更多优秀开源项目,谢谢!

浏览 48
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报