Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discussion] libssh2 support #155

Open
xywf221 opened this issue Jan 17, 2023 · 8 comments
Open

[discussion] libssh2 support #155

xywf221 opened this issue Jan 17, 2023 · 8 comments
Labels
good first issue Good for newcomers

Comments

@xywf221
Copy link

xywf221 commented Jan 17, 2023

是否可以添加libssh2支持我看libssh2文档是可以设置收包跟发包函数

@xywf221 xywf221 added the discussion Discuss things in this issue label Jan 17, 2023
@xywf221
Copy link
Author

xywf221 commented Jan 17, 2023

或者提供个示例让人自己写个扩展支持swow协程的

@twose
Copy link
Member

twose commented Jan 17, 2023

随手搜到一个很厉害的repo:https://github.com/phpseclib/phpseclib

image

<?php

require __DIR__ . '/../vendor/autoload.php';

use phpseclib3\Net\SSH2;

$host = 'your host';
$port = 22;
echo "Login to {$host}:{$port} and exec 'uname -a'...\n";

$ssh = new SSH2($host, $port);

echo 'Username: ';
$username = trim(fgets(STDIN));
echo 'Password: ';
system('stty cbreak -echo <&2');
$password = trim(fgets(STDIN));
system('stty cbreak echo <&2');
echo "\n";

$ssh->login($username, $password);
var_dump($ssh->exec('uname -a'));

你甚至可以通过 Swow 看到系统调用和数据流量日志来确认它是协程化的(用 swow-builder 编译时加上 --debug 参数,并在运行时设置环境变量 CAT_DEBUG=1):

image

image

@twose twose added good first issue Good for newcomers and removed discussion Discuss things in this issue labels Jan 17, 2023
@xywf221
Copy link
Author

xywf221 commented Jan 17, 2023

挺厉害的项目不过我目前的一个思路是改写ssh2扩展让他使用php stream函数这个应该好改

@xywf221
Copy link
Author

xywf221 commented Jan 17, 2023

成功了把ssh2扩展底层连接函数以及libssh2收包跟发包回调都使用php stream就成功了

@xywf221
Copy link
Author

xywf221 commented Jan 17, 2023

<?php
use Swow\Coroutine;
use Swow\Sync\WaitReference;


$wr1 = new WaitReference();
Coroutine::run(static function () use($wr1): void {
    $wr = new WaitReference();
    echo "start connect\r\n";
    $session = ssh2_connect("x", 22);
    echo "connect done!\r\n";

    Coroutine::run(static function () use ($session, $wr): void {
        echo "start auth\r\n";
        $flag = ssh2_auth_password($session, 'x', 'x');
        echo "auth done\r\n";

        Coroutine::run(static function () use ($session,$wr): void {
            // exec command
            echo "Exec Command : sleep 5 && echo 'done 1!' \r\n";
            ssh2_exec($session, "sleep 5 && echo 'done 1!'");
    
            echo "Exec Command : sleep 5 && echo 'done 2!' \r\n";
            ssh2_exec($session, "sleep 5 && echo 'done 2!'");
        });
    });
    echo "auth 挂起\r\n";
    WaitReference::wait($wr);
    var_dump('done');
});

echo "connect 挂起\r\n";

WaitReference::wait($wr1);

echo "整体结束\r\n";

image

@dixyes
Copy link
Member

dixyes commented Jan 18, 2023

考虑下ffi?直接dlsym/GetProcAddress到libcat相关的函数地址喂给libssh2?
(施放一个大魔法
(然后炸到妈都不认

@xywf221
Copy link
Author

xywf221 commented Jan 18, 2023

哈哈我还真玩过ffi设置回调函数不过没搞得那么激进
image
这里握手 授权什么都没问题但是如果开channel就会卡主找不出来原因就想着用纯c试试了

@xywf221
Copy link
Author

xywf221 commented Jan 18, 2023

突发奇想如果我跟上面的扩展一样统统使用php stream函数怎么样,等验证年后再说吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants