212 lines
5.8 KiB
Go
212 lines
5.8 KiB
Go
// `wsbx code --desktop`: an SSH server on stdin/stdout for VS Code Remote-SSH (or plain ssh), one per
|
|
// connection, started by the host's ProxyCommand: `wsbx exec NAME -- /.wsbx/host/guest -ssh-stdio`.
|
|
// The exec channel is the authentication (only the host user can open it), so the client gets in with no
|
|
// auth against an ephemeral host key, and runs as the user exec started us as (the image user).
|
|
// Supported: sessions (exec, shell, pty, env, window-change) and host -> guest forwarding (direct-tcpip,
|
|
// direct-streamlocal). Guest -> host forwarding (tcpip-forward) and agent forwarding are refused.
|
|
package main
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"strconv"
|
|
"syscall"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
func serveSSH() error {
|
|
_, key, err := ed25519.GenerateKey(nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
signer, err := ssh.NewSignerFromKey(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cfg := &ssh.ServerConfig{NoClientAuth: true}
|
|
cfg.AddHostKey(signer)
|
|
_, chans, reqs, err := ssh.NewServerConn(stdio{}, cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
go ssh.DiscardRequests(reqs) // global requests: tcpip-forward etc. are refused
|
|
for nc := range chans {
|
|
switch nc.ChannelType() {
|
|
case "session":
|
|
go sshSession(nc)
|
|
case "direct-tcpip":
|
|
var p struct {
|
|
Host string
|
|
Port uint32
|
|
OrigHost string
|
|
OrigPort uint32
|
|
}
|
|
if ssh.Unmarshal(nc.ExtraData(), &p) != nil {
|
|
nc.Reject(ssh.ConnectionFailed, "bad request")
|
|
continue
|
|
}
|
|
go sshForward(nc, "tcp", net.JoinHostPort(p.Host, strconv.Itoa(int(p.Port))))
|
|
case "direct-streamlocal@openssh.com":
|
|
var p struct {
|
|
Path string
|
|
Reserved0 string
|
|
Reserved1 uint32
|
|
}
|
|
if ssh.Unmarshal(nc.ExtraData(), &p) != nil {
|
|
nc.Reject(ssh.ConnectionFailed, "bad request")
|
|
continue
|
|
}
|
|
go sshForward(nc, "unix", p.Path)
|
|
default:
|
|
nc.Reject(ssh.UnknownChannelType, "not supported by wsbx")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func sshForward(nc ssh.NewChannel, network, addr string) {
|
|
c, err := net.Dial(network, addr)
|
|
if err != nil {
|
|
nc.Reject(ssh.ConnectionFailed, err.Error())
|
|
return
|
|
}
|
|
defer c.Close()
|
|
ch, reqs, err := nc.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer ch.Close()
|
|
go ssh.DiscardRequests(reqs)
|
|
done := make(chan struct{})
|
|
go func() {
|
|
io.Copy(ch, c)
|
|
ch.CloseWrite()
|
|
close(done)
|
|
}()
|
|
io.Copy(c, ch)
|
|
if cw, ok := c.(interface{ CloseWrite() error }); ok {
|
|
cw.CloseWrite()
|
|
}
|
|
<-done
|
|
}
|
|
|
|
func sshSession(nc ssh.NewChannel) {
|
|
ch, reqs, err := nc.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer ch.Close()
|
|
var env []string
|
|
var pty, tty *os.File
|
|
defer func() {
|
|
if pty != nil {
|
|
pty.Close()
|
|
}
|
|
}()
|
|
started, command := false, ""
|
|
for req := range reqs {
|
|
ok, start := false, false
|
|
switch req.Type {
|
|
case "env":
|
|
var kv struct{ K, V string }
|
|
if ssh.Unmarshal(req.Payload, &kv) == nil {
|
|
env, ok = append(env, kv.K+"="+kv.V), true
|
|
}
|
|
case "pty-req":
|
|
var p struct {
|
|
Term string
|
|
Cols, Rows, W, H uint32
|
|
Modes string
|
|
}
|
|
if pty == nil && !started && ssh.Unmarshal(req.Payload, &p) == nil {
|
|
if m, s, err := openPty(); err == nil {
|
|
pty, tty, ok = m, s, true
|
|
setWinsize(m, uint16(p.Rows), uint16(p.Cols))
|
|
env = append(env, "TERM="+p.Term)
|
|
}
|
|
}
|
|
case "window-change":
|
|
var p struct{ Cols, Rows, W, H uint32 }
|
|
if pty != nil && ssh.Unmarshal(req.Payload, &p) == nil {
|
|
setWinsize(pty, uint16(p.Rows), uint16(p.Cols))
|
|
ok = true
|
|
}
|
|
case "shell", "exec":
|
|
var p struct{ Cmd string }
|
|
if !started && (req.Type == "shell" || ssh.Unmarshal(req.Payload, &p) == nil) {
|
|
started, ok, start, command = true, true, true, p.Cmd
|
|
}
|
|
}
|
|
if req.WantReply {
|
|
req.Reply(ok, nil)
|
|
}
|
|
if start {
|
|
go func(env []string) {
|
|
code := sshRun(ch, command, env, pty, tty)
|
|
ch.SendRequest("exit-status", false, ssh.Marshal(struct{ C uint32 }{code}))
|
|
ch.Close()
|
|
}(env)
|
|
}
|
|
}
|
|
}
|
|
|
|
// sshRun runs command (empty: a login shell) like sshd: `$SHELL -c command`, in run.sh's env and dir.
|
|
func sshRun(ch ssh.Channel, command string, env []string, pty, tty *os.File) uint32 {
|
|
shell := "/bin/sh"
|
|
if _, err := os.Stat("/bin/bash"); err == nil {
|
|
shell = "/bin/bash"
|
|
}
|
|
cmd := exec.Command(shell, "-c", command)
|
|
if command == "" {
|
|
cmd = exec.Command(shell, "-l")
|
|
}
|
|
cmd.Env = append(os.Environ(), env...)
|
|
if pty != nil {
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = tty, tty, tty
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
|
|
} else {
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = ch, ch, ch.Stderr()
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
|
cmd.WaitDelay = time.Second // don't hang on daemons (the VS Code server) that keep stdout open
|
|
}
|
|
if err := cmd.Start(); err != nil {
|
|
fmt.Fprintln(ch.Stderr(), "wsbx:", err)
|
|
return 127
|
|
}
|
|
if pty == nil {
|
|
cmd.Wait()
|
|
return exitCode(cmd.ProcessState)
|
|
}
|
|
tty.Close() // our copy: the master reads EIO once the session's are gone
|
|
go io.Copy(pty, ch)
|
|
done := make(chan struct{})
|
|
go func() {
|
|
io.Copy(ch, pty)
|
|
close(done)
|
|
}()
|
|
cmd.Wait()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(500 * time.Millisecond): // a background job still holds the pty
|
|
}
|
|
return exitCode(cmd.ProcessState)
|
|
}
|
|
|
|
// stdio is our stdin/stdout as the net.Conn the SSH server runs on.
|
|
type stdio struct{}
|
|
|
|
func (stdio) Read(b []byte) (int, error) { return os.Stdin.Read(b) }
|
|
func (stdio) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
|
|
func (stdio) Close() error { os.Stdin.Close(); return os.Stdout.Close() }
|
|
func (stdio) LocalAddr() net.Addr { return &net.UnixAddr{Name: "stdio", Net: "unix"} }
|
|
func (stdio) RemoteAddr() net.Addr { return &net.UnixAddr{Name: "wsbx", Net: "unix"} }
|
|
func (stdio) SetDeadline(time.Time) error { return nil }
|
|
func (stdio) SetReadDeadline(time.Time) error { return nil }
|
|
func (stdio) SetWriteDeadline(time.Time) error { return nil }
|