Forked from blacknon/notworking_ssh_term_x11forwarding.go
Created
November 4, 2021 02:46
-
-
Save wahello/ffde4e00384881b766ccf5b996209f2e to your computer and use it in GitHub Desktop.
notworking_ssh_term_x11forwarding.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"os" | |
"os/signal" | |
"syscall" | |
"golang.org/x/crypto/ssh" | |
"golang.org/x/crypto/ssh/terminal" | |
) | |
var ( | |
host = "targethost" | |
port = "22" | |
user = "user" | |
pass = "password" | |
) | |
type x11request struct { | |
SingleConnection bool | |
AuthProtocol string | |
AuthCookie string | |
ScreenNumber uint32 | |
} | |
type x11channel struct { | |
Host string | |
Port uint32 | |
} | |
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\n", ok) | |
fmt.Println("-----") | |
// x11 OpenChannel (Not working...) | |
x11Data := x11channel{ | |
Host: "localhost", | |
Port: uint32(6010), | |
} | |
x11Marshal := ssh.Marshal(x11Data) // DEBUG | |
fmt.Println(x11Marshal) // DEBUG | |
fmt.Printf("%s\n", ssh.Marshal(x11Data)) // DEBUG | |
sshChan, req, x11err := client.OpenChannel("x11", ssh.Marshal(x11Data)) | |
fmt.Println(sshChan) // DEBUG | |
fmt.Println(req) // DEBUG | |
fmt.Println(x11err) // DEBUG | |
fd := int(os.Stdin.Fd()) | |
state, err := terminal.MakeRaw(fd) | |
if err != nil { | |
fmt.Println(err) | |
} | |
defer terminal.Restore(fd, state) | |
// Get terminal size | |
w, h, err := terminal.GetSize(fd) | |
if err != nil { | |
fmt.Println(err) | |
} | |
modes := ssh.TerminalModes{ | |
ssh.ECHO: 1, | |
ssh.TTY_OP_ISPEED: 14400, | |
ssh.TTY_OP_OSPEED: 14400, | |
} | |
err = session.RequestPty("xterm", h, w, modes) | |
if err != nil { | |
fmt.Println(err) | |
} | |
session.Stdout = os.Stdout | |
session.Stderr = os.Stderr | |
session.Stdin = os.Stdin | |
// Start shell | |
err = session.Shell() | |
if err != nil { | |
fmt.Println(err) | |
} | |
// sigwinch | |
signal_chan := make(chan os.Signal, 1) | |
signal.Notify(signal_chan, syscall.SIGWINCH) | |
go func() { | |
for { | |
s := <-signal_chan | |
switch s { | |
case syscall.SIGWINCH: | |
fd := int(os.Stdout.Fd()) | |
w, h, _ = terminal.GetSize(fd) | |
session.WindowChange(h, w) | |
} | |
} | |
}() | |
err = session.Wait() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment