You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Windows搭建SSH可访问的Git服务器时遭遇推送失败的问题求助

在Windows搭建SSH可访问的Git服务器时遭遇推送失败的问题求助

我正在做一个编程挑战,需要搭建支持SSH访问的Git服务器,创建空仓库并读取推送上来的文件。目前我在Windows上运行了OpenSSH服务器,也已经转发了22端口,操作流程大致如下:

  • 接收挑战的JSON数据,创建与用户名同名的用户和目录
  • 在新目录下,将公钥写入.ssh/authorized_keys文件
  • 按照repo_path指定的路径初始化裸Git仓库,完整路径类似C:/Users/username/repopath1/repopath2.git

之后我向挑战平台发送POST请求告知仓库已准备好,但每次都会收到这样的错误反馈:

{
  error: "couldn't push to hax@(my ip):soft/scene.git - sorry, but we have no further details, check your server logs :(",
  debug: { attempted_repo_url: 'hax@(my ip):soft/scene.git' }
}

奇怪的是,我本地用ssh localhost连接完全正常,公钥验证没有任何问题;但用公网IP尝试SSH连接却连不上——虽然我已经确认22端口在公网可访问,ping公网IP也能通。

我怀疑是权限配置或者Git与SSH的联动出了问题,有没有大佬能给我一些下一步排查的建议?

下面是我的相关代码,供参考:

export const solve = async (problem: TProblem): Promise<TSolution> => {
    try {
        const { push_token, username, repo_path } = problem;
        const url = `${BASEURL}/_/git/${push_token}`;

        const repo_host = PUBLIC_IP;
        const repoDir = await prepareDir(problem);
        await prepareGit({ repoDir, repo_host, username, repo_path });
        await setPermissions({ repoDir, username });
        const response = await axios.post(url, {
            repo_host,
        });
        console.log(response.data);
        const secret = (await fs.readFile(path.join(repoDir, "solution.txt"))).toString();
        return {
            secret,
        };
    } catch (e) {
        console.error(e);
        throw e;
    }
};

const setPermissions = async ({ repoDir, username }: PermissionParams) => {
    try {
        const ownership = await exec(`icacls "${repoDir}" /setowner "${username}"`);
        await exec(`icacls "${repoDir}" /reset`);
        await exec(`icacls "${repoDir}" /grant "${username}:(F)"`);
        await exec(`icacls "${repoDir}" /inheritance:r`);

        return ownership.stdout;
    } catch (e) {
        console.error(e);
        throw e;
    }
};

const prepareGit = async ({ repoDir, repo_host, username, repo_path }: GitParams) => {
    try {
        const remote = `${username}@${repo_host}:${repo_path}`;
        const options: Partial<SimpleGitOptions> = {
            baseDir: repoDir,
            binary: "git",
            maxConcurrentProcesses: 6,
            trimmed: true,
        };
        const git: SimpleGit = simpleGit(options);
        const init = await git.init(true);
        return init;
    } catch (e) {
        console.error(e);
        throw e;
    }
};

const prepareDir = async (problem: TProblem) => {
    try {
        const { repo_path, ssh_key, username } = problem;
        const userDir = path.join(ROOT_DIR, username);
        const sshDir = path.join(userDir, ".ssh");
        const repoDir = path.join(userDir, repo_path);
        const authKeyPath = path.join(sshDir, "authorized_keys");
        await fs.mkdir(repoDir, { recursive: true });
        await fs.writeFile(authKeyPath, ssh_key);

        return repoDir;
    } catch (e) {
        console.error(e);
        throw e;
    }
};

备注:内容来源于stack exchange,提问作者Chan Kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 15:04:39