如何执行简单的SSH x11转发

I'm trying to implement ssh x11 forwarding in Go, referring to Paramiko's source, but it does not work well.

The x11-req request seems to be successful, but fails with OpenChannel. Isn't there any better way?

https://tools.ietf.org/html/rfc4254#section-6.3.2

The full code is here.

https://gist.github.com/blacknon/6e2e6e2c0ebcd64c381925f0e3e86e42

package main

(omit)

func main() {
    // Create sshClientConfig
    sshConfig := &ssh.ClientConfig{
        User: user,
        Auth: []ssh.AuthMethod{
            ssh.Password(pass),
        },
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }

    // SSH connect.
    client, err := ssh.Dial("tcp", host+":"+port, sshConfig)

    // Create Session
    session, err := client.NewSession()
    defer session.Close()

    // NOTE:
    // x11-req Payload
    payload := x11request{
        SingleConnection: false,
        AuthProtocol:     string("MIT-MAGIC-COOKIE-1"),
        AuthCookie:       string("d92c30482cc3d2de61888961deb74c08"),
        ScreenNumber:     uint32(0),
    }

    // NOTE:
    // send x11-req Request
    ok, err := session.SendRequest("x11-req", true, ssh.Marshal(payload))
    if err == nil && !ok {
        fmt.Println(errors.New("ssh: x11-req failed"))
    }
    fmt.Printf("x11-req: %v
", ok)
    fmt.Println("-----")

    // x11 OpenChannel (Not working...)
    x11Data := x11channel{
        Host: "localhost",
        Port: uint32(6000),
    }

    sshChan, req, x11err := client.OpenChannel("x11", ssh.Marshal(x11Data))
    fmt.Println(sshChan) // DEBUG
    fmt.Println(req)     // DEBUG
    fmt.Println(x11err)  // DEBUG

    (omit)
}

I added sshd side debug log.

sshd[1811]: debug1: server_input_channel_req: channel 0 request x11-req reply 1
sshd[1811]: debug1: session_by_channel: session 0 channel 0
sshd[1811]: debug1: session_input_channel_req: session 0 req x11-req
sshd[1811]: debug1: channel 1: new [X11 inet listener]
sshd[1811]: debug1: channel 2: new [X11 inet listener]
sshd[1811]: debug1: server_input_channel_open: ctype x11 rchan 1 win 2097152 max 32768
sshd[1811]: debug1: server_input_channel_open: failure x11

Thanks guys! Thanks to it, I was able to implement it safely. Have a working code.

https://gist.github.com/blacknon/9eca2e2b5462f71474e1101179847d2a

doucang6739
doucang6739 抱歉。我添加了sshd端调试日志。
一年多之前 回复
dragonsun2005
dragonsun2005 “...但是使用OpenChannel失败。”这不是非常有用。那到底发生了什么?服务器是否记录了任何有用的错误消息?
一年多之前 回复

1个回答



  // x11 OpenChannel(不起作用...)
x11Data:= x11channel {
主机:“ localhost”,

端口:uint32(6000),
}

sshChan,要求,x11err:= client.OpenChannel(“ x11”,ssh.Marshal(x11Data))
</ code> </ pre>
\ n

此处的根本问题是X11转发通道是从SSH服务器启动到SSH客户端的。 您正在尝试打开从客户端到服务器的X11通道。 您的服务器不支持此功能,也不是使用X转发的常用方法。</ p>

我不是一名Go程序员。 但是在查看文档之后,在发送x11-req之后,它看起来像 您将调用 client.HandleChannelOpen()来接收X11频道 请求来自服务器。</ p>

更多背景:为清楚起见,请从术语开始。 您的程序是 ssh客户端</ em>,它连接到 ssh服务器</ em>。 对于X,服务器</ em>是用于控制显示器,键盘和鼠标的程序。 X客户端</ em>是xterm和xeyes之类的程序,它们连接到服务器以显示窗口并执行类似的操作。</ p>

当您想通过SSH转发X11时,ssh 客户端将向SSH服务器发送X11请求。 这告诉服务器客户端希望X11转发该连接。 服务器将执行一些设置并打开TCP侦听端口以接收来自X客户端的连接。</ p>

当X客户端连接到ssh服务器的X11侦听端口时,ssh服务器将打开一个通道 回到ssh客户端。 ssh客户端将连接到本地X服务器,并且ssh客户端和ssh服务器将在X服务器(对ssh客户端主机本地)和X客户端(对ssh服务器主机本地)之间中继数据。 每个通道都处理一个X客户端。</ p>

因此,您这样的程序必须向服务器发送请求,以表明您的程序希望通过ssh连接转发X11。 当X客户端尝试使用转发的X11服务时,实际的x11通道将根据需要从ssh服务器打开到ssh客户端。</ p>
</ div>

展开原文

原文

// x11 OpenChannel (Not working...)
x11Data := x11channel{
    Host: "localhost",
    Port: uint32(6000),
}

sshChan, req, x11err := client.OpenChannel("x11", ssh.Marshal(x11Data))

The fundamental issue here is that X11 forwarding channels are initiated from the SSH server to the SSH client. You're trying to open an X11 channel from the client to the server. This isn't supported by your server, and it isn't the usual way to use X forwarding.

I'm not a go programmer. But after looking the documentation, after your send your x11-req it looks like you would call client.HandleChannelOpen() to receive X11 channel requests coming from the server.

More background: To be clear, start with the terminology. Your program is an ssh client and it connects to an ssh server. With X, the server is the program which controls a display, keyboard, and mouse. X clients are programs like xterm and xeyes which connect to a server to display windows and do similar things.

When you want to forward X11 through SSH, the ssh client will send an X11 request to the ssh server. This tells the server that the client wants X11 forwarding for the connection. The server will perform some setup and open a TCP listening port to receive connections from X clients.

When an X client connects to the ssh server's X11 listening port, the ssh server will open a channel back to the ssh client. The ssh client will connect to the local X server, and the ssh client and ssh server will relay data between the X server (local to the ssh client host) and the X client (local to the ssh server host). Each channel handles a single X client.

So a program such as yours has to send a request to the server, signalling that your program wants to forward X11 through the ssh connection. The actual x11 channels will be opened on demand from the ssh server to the ssh client when an X client tries to use the forwarded X11 service.

doudeng1870
doudeng1870 非常感谢你! 看来我误会了! 可以根据您所教的内容实施!
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐