This article does not have too much source code, mainly about the implementation of ideas and technical principles

When USING Xshell or SecureCRT terminal tools, I do all of my file transfers through LRZSZ, mainly because it’s so easy, you don’t need to open up something like SFTP, you can just command it, and after using WebSSH, I was thinking, Is WebSSH capable of this convenience?

The answer is yes, it can! Thanks to this ancient file transfer protocol: Zmodem

Zmodem is an improved version of Xmodem and Ymodem protocols. It is one of the most popular file transfer protocols and supported by many terminals, such as Xshell, SecureCRT, Item2, and so on. Zmodem is an improved version of Xmodem and Ymodem protocols

In addition to its advantages, zmodem also has certain limitations, one of which is that it can only reliably transfer files of a size less than 4GB. However, this is sufficient for most scenarios, and other transmission methods are generally used to transfer large files

LRZSZ is a file transfer system based on the Zmodem protocol. It is very convenient to use in Linux. You only need to run a simple command to install LRZSZ

yum install lrzsz
Copy the code

After the installation is complete, you can use the rz command to upload files or the sz command to download files. In the Zmodem protocol, it is more accurate to use receive and send. Both receive and send are initiated by the server

Rz stands for recevie zmodem, and the server receives the data, or in the case of the client, uploads

Sz stands for send zmodem. The server sends data, and for the client, it’s a download

The server supports the Zmodem protocol by installing LRZSZ. Xshell and SecureCRT also support the Zmodem protocol, so they can upload and download files using rZ or SZ commands. So how do Web browsers support the Zmodem protocol?

The terminal tool we used, Xterm.js, provided the Zmodem extension in 3.x, but unfortunately it stopped supporting it in 4.x. Fortunately, the author of the Zmodem extension for Xterm.js has opened source a project: Zmodemjs, used to provide browser Web support for zmodem protocol, and can be well combined with xterm.js tools, with Zmodemjs we can interact with the terminal through the browser, call system RZSZ command to achieve file upload and download

Note that zmodem is a binary protocol and only supports binary streams, so data sent through webSockets must be binary. Djangos channel lets you send binary data through webSockets by specifying the message type bytes_data. This is the core of the back-end implementation

websocket.send(bytes_data=data)
Copy the code

With the front-end implementation of ZmodemJS, the final effect is as follows

When I finished using RZSZ to upload and download files on the Web, I encountered a thorny problem. No matter monitoring or video, the downloaded files will be displayed in binary on the screen during SZ download. This is mainly because zmodemJS can parse binary file streams in WebSSH into files, while monitoring and video cannot be parsed. This requires filtering out the binary stream of files during monitoring and video recording. At first, I tried to determine whether the stream was a file or text by analyzing the binary stream, but in the end, I found it was not accurate. A more reliable method is to decode the binary stream, but it is not 100% accurate

How to realize the recognition of Zmodem protocol is studied deeply, and the realization principle of Zmodem is found

After the sz command is executed on the server, the first output is **B00000000000000, indicating the start of the file download. When the file download is complete, the output is OO. The binary stream between these two special flags is combined into a file, which is the complete file to download

** 0100000023BE50. Knowing this rule, you just need to remove the content between the SZ flags in the data sent to the monitor and video. The problem is solved, which is the end of the WebSSH series


Related articles recommended reading:

  • The core weapon of fortress machine: WebSSH video realization
  • Real-time monitoring and forced shutdown of fortress WebSSH advances