欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django3基于WebSocket實(shí)現(xiàn)WebShell的詳細(xì)過程

 更新時(shí)間:2021年08月26日 08:37:49   作者:從零開始的程序員生活  
最近工作中需要開發(fā)前端操作遠(yuǎn)程虛擬機(jī)的功能,簡(jiǎn)稱WebShell,普通應(yīng)用大部分用的都是wsgi.py配合nginx部署線上服務(wù). 這次主要使用asgi.py,具體實(shí)現(xiàn)過程跟隨小編一起看看吧

前言

最近工作中需要開發(fā)前端操作遠(yuǎn)程虛擬機(jī)的功能,簡(jiǎn)稱WebShell. 基于當(dāng)前的技術(shù)棧為react+django,調(diào)研了一會(huì)發(fā)現(xiàn)大部分的后端實(shí)現(xiàn)都是django+channels來實(shí)現(xiàn)websocket服務(wù).
大致看了下覺得這不夠有趣,翻了翻django的官方文檔發(fā)現(xiàn)django原生是不支持websocket的,但django3之后支持了asgi協(xié)議可以自己實(shí)現(xiàn)websocket服務(wù). 于是選定
gunicorn+uvicorn+asgi+websocket+django3.2+paramiko來實(shí)現(xiàn)WebShell.

實(shí)現(xiàn)websocket服務(wù)

使用django自帶的腳手架生成的項(xiàng)目會(huì)自動(dòng)生成asgi.py和wsgi.py兩個(gè)文件,普通應(yīng)用大部分用的都是wsgi.py配合nginx部署線上服務(wù). 這次主要使用asgi.py
實(shí)現(xiàn)websocket服務(wù)的思路大致網(wǎng)上搜一下就能找到,主要就是實(shí)現(xiàn) connect/send/receive/disconnect這個(gè)幾個(gè)動(dòng)作的處理方法.
這里 How to Add Websockets to a Django App without Extra Dependencies 就是一個(gè)很好的實(shí)例
, 但過于簡(jiǎn)單........:

思路

# asgi.py 
import os

from django.core.asgi import get_asgi_application
from websocket_app.websocket import websocket_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'websocket_app.settings')

django_application = get_asgi_application()


async def application(scope, receive, send):
    if scope['type'] == 'http':
        await django_application(scope, receive, send)
    elif scope['type'] == 'websocket':
        await websocket_application(scope, receive, send)
    else:
        raise NotImplementedError(f"Unknown scope type {scope['type']}")


# websocket.py
async def websocket_application(scope, receive, send):
    pass
# websocket.py
async def websocket_application(scope, receive, send):
    while True:
        event = await receive()

        if event['type'] == 'websocket.connect':
            await send({
                'type': 'websocket.accept'
            })

        if event['type'] == 'websocket.disconnect':
            break

        if event['type'] == 'websocket.receive':
            if event['text'] == 'ping':
                await send({
                    'type': 'websocket.send',
                    'text': 'pong!'
                })

實(shí)現(xiàn)

上面的代碼提供了思路,比較完整的可以參考這里 websockets-in-django-3-1 基本可以復(fù)用了
其中最核心的實(shí)現(xiàn)部分我放下面:

class WebSocket:
    def __init__(self, scope, receive, send):
        self._scope = scope
        self._receive = receive
        self._send = send
        self._client_state = State.CONNECTING
        self._app_state = State.CONNECTING

    @property
    def headers(self):
        return Headers(self._scope)

    @property
    def scheme(self):
        return self._scope["scheme"]

    @property
    def path(self):
        return self._scope["path"]

    @property
    def query_params(self):
        return QueryParams(self._scope["query_string"].decode())

    @property
    def query_string(self) -> str:
        return self._scope["query_string"]

    @property
    def scope(self):
        return self._scope

    async def accept(self, subprotocol: str = None):
        """Accept connection.
        :param subprotocol: The subprotocol the server wishes to accept.
        :type subprotocol: str, optional
        """
        if self._client_state == State.CONNECTING:
            await self.receive()
        await self.send({"type": SendEvent.ACCEPT, "subprotocol": subprotocol})

    async def close(self, code: int = 1000):
        await self.send({"type": SendEvent.CLOSE, "code": code})

    async def send(self, message: t.Mapping):
        if self._app_state == State.DISCONNECTED:
            raise RuntimeError("WebSocket is disconnected.")

        if self._app_state == State.CONNECTING:
            assert message["type"] in {SendEvent.ACCEPT, SendEvent.CLOSE}, (
                    'Could not write event "%s" into socket in connecting state.'
                    % message["type"]
            )
            if message["type"] == SendEvent.CLOSE:
                self._app_state = State.DISCONNECTED
            else:
                self._app_state = State.CONNECTED

        elif self._app_state == State.CONNECTED:
            assert message["type"] in {SendEvent.SEND, SendEvent.CLOSE}, (
                    'Connected socket can send "%s" and "%s" events, not "%s"'
                    % (SendEvent.SEND, SendEvent.CLOSE, message["type"])
            )
            if message["type"] == SendEvent.CLOSE:
                self._app_state = State.DISCONNECTED

        await self._send(message)

    async def receive(self):
        if self._client_state == State.DISCONNECTED:
            raise RuntimeError("WebSocket is disconnected.")

        message = await self._receive()

        if self._client_state == State.CONNECTING:
            assert message["type"] == ReceiveEvent.CONNECT, (
                    'WebSocket is in connecting state but received "%s" event'
                    % message["type"]
            )
            self._client_state = State.CONNECTED

        elif self._client_state == State.CONNECTED:
            assert message["type"] in {ReceiveEvent.RECEIVE, ReceiveEvent.DISCONNECT}, (
                    'WebSocket is connected but received invalid event "%s".'
                    % message["type"]
            )
            if message["type"] == ReceiveEvent.DISCONNECT:
                self._client_state = State.DISCONNECTED

        return message

縫合怪

做為合格的代碼搬運(yùn)工,為了提高搬運(yùn)效率還是要造點(diǎn)輪子填點(diǎn)坑的,如何將上面的WebSocket類與paramiko結(jié)合起來實(shí)現(xiàn)從前端接受字符傳遞給遠(yuǎn)程主機(jī)并同時(shí)接受返回呢?

import asyncio
import traceback
import paramiko
from webshell.ssh import Base, RemoteSSH
from webshell.connection import WebSocket


class WebShell:
    """整理 WebSocket 和 paramiko.Channel,實(shí)現(xiàn)兩者的數(shù)據(jù)互通"""

    def __init__(self, ws_session: WebSocket,
                 ssh_session: paramiko.SSHClient = None,
                 chanel_session: paramiko.Channel = None
                 ):
        self.ws_session = ws_session
        self.ssh_session = ssh_session
        self.chanel_session = chanel_session

    def init_ssh(self, host=None, port=22, user="admin", passwd="admin@123"):
        self.ssh_session, self.chanel_session = RemoteSSH(host, port, user, passwd).session()

    def set_ssh(self, ssh_session, chanel_session):
        self.ssh_session = ssh_session
        self.chanel_session = chanel_session

    async def ready(self):
        await self.ws_session.accept()

    async def welcome(self):
        # 展示Linux歡迎相關(guān)內(nèi)容
        for i in range(2):
            if self.chanel_session.send_ready():
                message = self.chanel_session.recv(2048).decode('utf-8')
                if not message:
                    return
                await self.ws_session.send_text(message)

    async def web_to_ssh(self):
        # print('--------web_to_ssh------->')
        while True:
            # print('--------------->')
            if not self.chanel_session.active or not self.ws_session.status:
                return
            await asyncio.sleep(0.01)
            shell = await self.ws_session.receive_text()
            # print('-------shell-------->', shell)
            if self.chanel_session.active and self.chanel_session.send_ready():
                self.chanel_session.send(bytes(shell, 'utf-8'))
            # print('--------------->', "end")

    async def ssh_to_web(self):
        # print('<--------ssh_to_web-----------')
        while True:
            # print('<-------------------')
            if not self.chanel_session.active:
                await self.ws_session.send_text('ssh closed')
                return
            if not self.ws_session.status:
                return
            await asyncio.sleep(0.01)
            if self.chanel_session.recv_ready():
                message = self.chanel_session.recv(2048).decode('utf-8')
                # print('<---------message----------', message)
                if not len(message):
                    continue
                await self.ws_session.send_text(message)
            # print('<-------------------', "end")

    async def run(self):
        if not self.ssh_session:
            raise Exception("ssh not init!")
        await self.ready()
        await asyncio.gather(
            self.web_to_ssh(),
            self.ssh_to_web()
        )

    def clear(self):
        try:
            self.ws_session.close()
        except Exception:
            traceback.print_stack()
        try:
            self.ssh_session.close()
        except Exception:
            traceback.print_stack()

前端

xterm.js 完全滿足,搜索下找個(gè)看著簡(jiǎn)單的就行.

export class Term extends React.Component {
    private terminal!: HTMLDivElement;
    private fitAddon = new FitAddon();

    componentDidMount() {
        const xterm = new Terminal();
        xterm.loadAddon(this.fitAddon);
        xterm.loadAddon(new WebLinksAddon());

        // using wss for https
        //         const socket = new WebSocket("ws://" + window.location.host + "/api/v1/ws");
        const socket = new WebSocket("ws://localhost:8000/webshell/");
        // socket.onclose = (event) => {
        //     this.props.onClose();
        // }
        socket.onopen = (event) => {
            xterm.loadAddon(new AttachAddon(socket));
            this.fitAddon.fit();
            xterm.focus();
        }

        xterm.open(this.terminal);
        xterm.onResize(({ cols, rows }) => {
            socket.send("<RESIZE>" + cols + "," + rows)
        });

        window.addEventListener('resize', this.onResize);
    }

    componentWillUnmount() {
        window.removeEventListener('resize', this.onResize);
    }

    onResize = () => {
        this.fitAddon.fit();
    }

    render() {
        return <div className="Terminal" ref={(ref) => this.terminal = ref as HTMLDivElement}></div>;
    }
}

好了,廢話不多少了,代碼我放這里了webshell 歡迎star/fork!

參考資料

webshell

django文檔

graphene-django文檔

django 異步視圖

websockets-in-django-3-1

How to Add Websockets to a Django App without Extra Dependencies

到此這篇關(guān)于Django3使用WebSocket實(shí)現(xiàn)WebShell的文章就介紹到這了,更多相關(guān)Django3實(shí)現(xiàn)WebShell內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中dir函數(shù)用法分析

    python中dir函數(shù)用法分析

    這篇文章主要介紹了python中dir函數(shù)用法,實(shí)例分析了dir函數(shù)的功能及相應(yīng)的使用技巧,需要的朋友可以參考下
    2015-04-04
  • 詳解使用python繪制混淆矩陣(confusion_matrix)

    詳解使用python繪制混淆矩陣(confusion_matrix)

    這篇文章主要介紹了詳解使用python繪制混淆矩陣(confusion_matrix),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 使用numpy轉(zhuǎn)換成cupy利用GPU執(zhí)行錯(cuò)誤

    使用numpy轉(zhuǎn)換成cupy利用GPU執(zhí)行錯(cuò)誤

    在使用PyInstaller打包Python程序時(shí),可能會(huì)遇到缺少模塊的錯(cuò)誤,尤其是在將Numpy轉(zhuǎn)換為CuPy以利用GPU加速時(shí),如果遇到ModuleNotFoundError,表明PyInstaller沒有包含一些隱式導(dǎo)入的包,解決方法是手動(dòng)將缺失的包添加到打包目錄中
    2024-09-09
  • 簡(jiǎn)單介紹Python的第三方庫(kù)yaml

    簡(jiǎn)單介紹Python的第三方庫(kù)yaml

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識(shí),文章圍繞著Python的第三方庫(kù)yaml展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 通過 Python 和 OpenCV 實(shí)現(xiàn)目標(biāo)數(shù)量監(jiān)控

    通過 Python 和 OpenCV 實(shí)現(xiàn)目標(biāo)數(shù)量監(jiān)控

    這篇文章主要介紹了如何通過 Python 和 OpenCV 實(shí)現(xiàn)目標(biāo)數(shù)量監(jiān)控,本文通過實(shí)例代碼圖文的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • python如何對(duì)圖片或文件的操作

    python如何對(duì)圖片或文件的操作

    在日常編程中,我們經(jīng)常會(huì)遇到文件處理的需求,如base64與圖片的相互轉(zhuǎn)換、圖片與像素點(diǎn)的操作、本地文件與二進(jìn)制的互相轉(zhuǎn)換、計(jì)算文件的md5以及下載網(wǎng)絡(luò)文件等,這些操作對(duì)于處理多媒體數(shù)據(jù)、實(shí)現(xiàn)數(shù)據(jù)的持久化存儲(chǔ)、保證數(shù)據(jù)的完整性和安全性等方面都至關(guān)重要
    2024-09-09
  • 如何將Pycharm中調(diào)整字體大小的方式設(shè)置為

    如何將Pycharm中調(diào)整字體大小的方式設(shè)置為"ctrl+鼠標(biāo)滾輪上下滑"

    這篇文章主要介紹了如何將Pycharm中調(diào)整字體大小的方式設(shè)置為"ctrl+鼠標(biāo)滾輪上下滑",本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 使用python測(cè)試prometheus的實(shí)現(xiàn)

    使用python測(cè)試prometheus的實(shí)現(xiàn)

    本文主要介紹了使用python測(cè)試prometheus的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 代碼解析python標(biāo)準(zhǔn)庫(kù)logging模塊

    代碼解析python標(biāo)準(zhǔn)庫(kù)logging模塊

    這篇文章主要為大家介紹了代碼解析python標(biāo)準(zhǔn)庫(kù)logging模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python?中賦值,深拷貝,淺拷貝的區(qū)別

    python?中賦值,深拷貝,淺拷貝的區(qū)別

    這篇文章主要介紹了python?中賦值,深拷貝,淺拷貝的區(qū)別,下文利用實(shí)例對(duì)三者進(jìn)行詳細(xì)的解析,具有一的的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-03-03

最新評(píng)論