<?php

namespace App\Services\TG\Hook;

class System extends Base {
    
    public function AddSshHostGithub()
    {

        // 定义要添加的主机
        $host = 'github.com';

        // 执行 ssh-keyscan 命令
        $key = shell_exec("ssh-keyscan -t ed25519 $host");

        // 检查是否成功获取到公钥
        if ($key) {
            // 获取 known_hosts 文件的路径
            $knownHostsFile = getenv('HOME') . '/.ssh/known_hosts';

            // 检查 known_hosts 文件中是否已经存在该主机的公钥
            if (strpos(file_get_contents($knownHostsFile), $host) === false) {
                // 将公钥写入 known_hosts 文件
                file_put_contents($knownHostsFile, $key, FILE_APPEND);
                $sMsg = "公钥已成功添加到 $knownHostsFile\n";
            } else {
                $sMsg = "公钥已存在于 $knownHostsFile\n";
            }
        } else {
            $sMsg = "无法获取 $host 的公钥\n";
        }
        
        $this->oTGHttp->sendToTG($sMsg);
        
        $this->oHttp::response(200, 'ok');
        
    }
    
}
