Skip to content

Commit 8cb26d9

Browse files
feat: add --port and --key-file flags using cobra
Co-authored-by: aider (openrouter/qwen/qwen3-coder) <[email protected]>
1 parent 5adb7ce commit 8cb26d9

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
This serves the [NYT Connections TUI](https://github.com/jmelahman/connections) over SSH.
99

10+
```shell
11+
$ ssh connections.lahman.dev
12+
```
13+
1014
## Running
1115

1216
In one terminal, start the server,
@@ -22,13 +26,13 @@ In a separate terminal,
2226
$ ssh -p 2222 localhost
2327
```
2428

25-
This assumes the server has an [SSH key](https://wiki.archlinux.org/title/SSH_keys) at [~/.ssh/id_rsa](https://github.com/jmelahman/connections-ssh/blob/12b9ba7d3ec6059a349d23ea85e7b948b16517a1/main.go#L32).
29+
By default, the server looks for an [SSH key](https://wiki.archlinux.org/title/SSH_keys) at [~/.ssh/id_rsa](https://github.com/jmelahman/connections-ssh/blob/12b9ba7d3ec6059a349d23ea85e7b948b16517a1/main.go#L32).
30+
This can be overridden with the `--key-file` flag.
2631

27-
Moreover, if running on port `22` is desired, you'll like need elevated privileges (not recommended),
32+
Moreover, if running on port `22` is desired, you'll likely need elevated privileges (not recommended),
2833

2934
```shell
30-
$ go build .
31-
$ sudo ./connections-ssh
35+
$ sudo connections-ssh --port 22
3236
2025/08/07 22:55:16 Starting SSH server on :22
3337
```
3438

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ require (
66
github.com/gdamore/tcell/v2 v2.8.1
77
github.com/gliderlabs/ssh v0.3.8
88
github.com/jmelahman/connections v0.1.17
9+
github.com/spf13/cobra v1.9.1
910
)
1011

1112
require (
1213
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
1314
github.com/gdamore/encoding v1.0.1 // indirect
15+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1416
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
1517
github.com/mattn/go-runewidth v0.0.16 // indirect
1618
github.com/rivo/tview v0.0.0-20250625164341-a4a78f1e05cb // indirect
1719
github.com/rivo/uniseg v0.4.7 // indirect
20+
github.com/spf13/pflag v1.0.6 // indirect
1821
golang.org/x/crypto v0.41.0 // indirect
1922
golang.org/x/sys v0.35.0 // indirect
2023
golang.org/x/term v0.34.0 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
22
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
34
github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
45
github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo=
56
github.com/gdamore/tcell/v2 v2.8.1 h1:KPNxyqclpWpWQlPLx6Xui1pMk8S+7+R37h3g07997NU=
67
github.com/gdamore/tcell/v2 v2.8.1/go.mod h1:bj8ori1BG3OYMjmb3IklZVWfZUJ1UBQt9JXrOCOhGWw=
78
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
89
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
910
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
12+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
1013
github.com/jmelahman/connections v0.1.17 h1:6CCN3e3NNnSSSnFl8DJdhjYIO0rfjPBWjF1LO+YBFvw=
1114
github.com/jmelahman/connections v0.1.17/go.mod h1:mTgdz687JXTa3HlSeIsejJrXjsv0WT1waXyOw4k1PXw=
1215
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
@@ -19,6 +22,11 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
1922
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
2023
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
2124
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
25+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
26+
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
27+
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
28+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
29+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
2230
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2331
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2432
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -89,3 +97,5 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
8997
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
9098
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
9199
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
100+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
101+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,32 @@ import (
1111
"github.com/gdamore/tcell/v2/terminfo"
1212
"github.com/gliderlabs/ssh"
1313
"github.com/jmelahman/connections/game"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
var (
18+
port string
19+
keyFile string
1420
)
1521

1622
func main() {
23+
var rootCmd = &cobra.Command{
24+
Use: "ssh-game",
25+
Short: "SSH game server",
26+
Run: func(cmd *cobra.Command, args []string) {
27+
serve()
28+
},
29+
}
30+
31+
rootCmd.Flags().StringVar(&port, "port", "2222", "Port to listen on")
32+
rootCmd.Flags().StringVar(&keyFile, "key-file", "", "Path to SSH host key file")
33+
34+
if err := rootCmd.Execute(); err != nil {
35+
log.Fatal(err)
36+
}
37+
}
38+
39+
func serve() {
1740
ssh.Handle(func(s ssh.Session) {
1841
screen, err := NewSessionScreen(s)
1942
if err != nil {
@@ -25,13 +48,20 @@ func main() {
2548
}
2649
})
2750

28-
log.Println("Starting SSH server on :2222")
29-
home, err := os.UserHomeDir()
30-
if err != nil {
31-
panic(err)
51+
log.Printf("Starting SSH server on :%s", port)
52+
53+
// Use provided key file or default to ~/.ssh/id_rsa
54+
hostKeyFile := keyFile
55+
if hostKeyFile == "" {
56+
home, err := os.UserHomeDir()
57+
if err != nil {
58+
panic(err)
59+
}
60+
hostKeyFile = filepath.Join(home, ".ssh", "id_rsa")
3261
}
33-
log.Fatal(ssh.ListenAndServe(":2222", nil,
34-
ssh.HostKeyFile(filepath.Join(home, ".ssh", "id_rsa")),
62+
63+
log.Fatal(ssh.ListenAndServe(":"+port, nil,
64+
ssh.HostKeyFile(hostKeyFile),
3565
))
3666
}
3767

0 commit comments

Comments
 (0)