Create temporary configuration file per SSH connection

This commit is contained in:
Trevor Slocum 2020-02-16 10:32:14 -08:00
parent 23edcfcdee
commit dc085770a6
4 changed files with 31 additions and 7 deletions

4
go.mod
View File

@ -10,7 +10,7 @@ require (
github.com/gliderlabs/ssh v0.2.2
github.com/mattn/go-isatty v0.0.12
gitlab.com/tslocum/cbind v0.1.1
gitlab.com/tslocum/cview v1.4.4-0.20200213233906-2a8ba3160c01
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678
gitlab.com/tslocum/cview v1.4.4-0.20200216181307-c6599f4ee261
golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6
gopkg.in/yaml.v2 v2.2.8
)

8
go.sum
View File

@ -23,11 +23,11 @@ github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
gitlab.com/tslocum/cbind v0.1.1 h1:JXXtxMWHgWLvoF+QkrvcNvOQ59juy7OE1RhT7hZfdt0=
gitlab.com/tslocum/cbind v0.1.1/go.mod h1:rX7vkl0pUSg/yy427MmD1FZAf99S7WwpUlxF/qTpPqk=
gitlab.com/tslocum/cview v1.4.4-0.20200213233906-2a8ba3160c01 h1:YdNvWO1OoGnmtjdAx84+3MWW1DAANWV8WLN9ZXE7PZc=
gitlab.com/tslocum/cview v1.4.4-0.20200213233906-2a8ba3160c01/go.mod h1:+bEf1cg6IoWvL16YHJAKwGGpQf5s/nxXAA7YJr+WOHE=
gitlab.com/tslocum/cview v1.4.4-0.20200216181307-c6599f4ee261 h1:5EAh+iLkvHBBR+jUWCRZEvOLiMCRAxvWvrA31wDTSN8=
gitlab.com/tslocum/cview v1.4.4-0.20200216181307-c6599f4ee261/go.mod h1:+bEf1cg6IoWvL16YHJAKwGGpQf5s/nxXAA7YJr+WOHE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 h1:wCWoJcFExDgyYx2m2hpHgwz8W3+FPdfldvIgzqDIhyg=
golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6 h1:Sy5bstxEqwwbYs6n0/pBuxKENqOeZUgD45Gp3Q3pqLg=
golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -17,6 +17,7 @@ builds:
goarch:
- 386
- amd64
- arm64
-
id: netris-server
binary: netris-server
@ -31,6 +32,7 @@ builds:
goarch:
- 386
- amd64
- arm64
archives:
-
id: netris

View File

@ -6,10 +6,12 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"syscall"
"time"
"unsafe"
@ -59,10 +61,16 @@ func (s *SSHServer) Host(newPlayers chan<- *game.IncomingPlayer) {
return
}
configPath, err := createTemporaryConfig()
if err != nil {
log.Printf("warning: failed to create temporary configuration file: %s", err)
return
}
cmdCtx, cancelCmd := context.WithCancel(sshSession.Context())
defer cancelCmd()
cmd := exec.CommandContext(cmdCtx, s.NetrisBinary, "--nick", game.Nickname(sshSession.User()), "--server", s.NetrisAddress)
cmd := exec.CommandContext(cmdCtx, s.NetrisBinary, "--nick", game.Nickname(sshSession.User()), "--server", s.NetrisAddress, "--config", configPath)
cmd.Env = append(sshSession.Environ(), fmt.Sprintf("TERM=%s", ptyReq.Term))
@ -88,6 +96,7 @@ func (s *SSHServer) Host(newPlayers chan<- *game.IncomingPlayer) {
f.Close()
cmd.Wait()
os.Remove(configPath)
},
PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
// TODO: Compare public key
@ -121,3 +130,16 @@ func (s *SSHServer) Host(newPlayers chan<- *game.IncomingPlayer) {
func (s *SSHServer) Shutdown(reason string) {
server.Close()
}
// Eventually this will load and save configuration based on public key hash.
func createTemporaryConfig() (string, error) {
f, err := ioutil.TempFile("", "netris-config-*.yaml")
if err != nil {
return "", err
}
log.Println(f.Stat())
f.Close()
return filepath.Clean(f.Name()), err
}