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

基于Golang的Git代理无法提取Git元数据的技术求助

问题:通过Golang SSH代理提取Git请求元数据

我正在使用Golang构建一个SSH代理服务器,用于将客户端的Git请求转发至后端版本控制系统(VCS),核心目的是执行认证与授权操作。目前遇到的问题是:无法从请求负载中获取Git元数据,不确定Git是否会发送Git操作类型、分支/标签名这类信息,也不知道该如何提取这些内容。

核心代码片段

主逻辑代码

downstreamConnection, err := connection.ConnectDownstream(request)

if err != nil {
    fmt.Println(err)
    panic(1)
}
chans := downstreamConnection.IncomingRequestChannel
for newChannel := range chans {
    fmt.Println("channel type : " + newChannel.ChannelType())
    channel, requests, err := newChannel.Accept()
    if err != nil {
        fmt.Println(err)
        panic(1)
    }
    go func(in <-chan *ssh.Request) {
        for req := range in {
            if req.Type == "exec" {
                err := req.Reply(req.Type == "exec", nil)
                fmt.Println(err)
            }
            fmt.Println(string(req.Payload))
        }
    }(requests)
}

下游连接实现代码

func (c Connection) ConnectDownstream(downstreamRequest DownstreamRequest) (*DownstreamConnection, error) {
    serverConfig := &ssh.ServerConfig{
        PublicKeyCallback: downstreamRequest.PublicKeyAuthFunction,
    }
    key, err := getHostKey()

    if err != nil {
        return nil, err
    }

    serverConfig.AddHostKey(key)
    authenticatedConnection, incomingRequestChannel, request, err := newSSHServerConn(downstreamRequest.Conn, serverConfig)

    if err != nil {
        return nil, err
    }

    return &DownstreamConnection{
        Request:                 request,
        IncomingRequestChannel:  incomingRequestChannel,
        AuthenticatedConnection: authenticatedConnection,
    }, nil
}

当前输出结果

local addr: [::1]:2222
channel type : session
LC_TERMINAL_VERSION3.4.19                                                                                                                                                                                              LC_CTYPEUTF-8

LC_TERMINALiTerm2
<nil>
git-receive-pack '/abcd'

目前打印负载只能得到带命令的URL路径(git-receive-pack '/abcd')。我可以通过读取channel来转储Git blob,但这种方法不够优雅,毕竟我的需求只是代理而非实现Git服务器。

需求

希望得到优雅的解决方案,提取以下数据:

  • Git操作(push、pull)
  • 分支/标签信息

内容的提问来源于stack exchange,提问作者Praveen Kamath

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 13:23:16