Skip to content

Commit 5e1a57a

Browse files
author
Brian Wiborg
committed
🚑️ Don't use Zellij session-name for rw access
1 parent 941686d commit 5e1a57a

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

cmd/ziina/main.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ var (
5454
// An empty string denotes that the host has not yet initiaed a session.
5555
sessionName = ""
5656

57+
// rwUser contains the username for full read-write acess
58+
rwUser = ""
59+
5760
// roUser contains the username for read-only access
5861
roUser = ""
5962
)
@@ -138,6 +141,13 @@ var App = &cli.App{
138141
return err
139142
}
140143

144+
// Generate a random username for full read-write access.
145+
// Generate a random username for read-only access.
146+
rwUser, err = randomString(7)
147+
if err != nil {
148+
return err
149+
}
150+
141151
// Generate a random username for read-only access.
142152
roUser, err = randomString(7)
143153
if err != nil {
@@ -165,7 +175,7 @@ var App = &cli.App{
165175

166176
// Start the SSH server
167177
go func() {
168-
if err := runServer(chGuard, port, ctx.String("listen"), sessionName, ctx.String("host-key"), server); err != nil {
178+
if err := runServer(chGuard, port, ctx.String("listen"), ctx.String("host-key"), server); err != nil {
169179
log.Fatalf("SSH server error: %v", err)
170180
}
171181
}()
@@ -175,7 +185,7 @@ var App = &cli.App{
175185
fmt.Println("")
176186
if server != "" {
177187
fmt.Println("Join via:")
178-
fmt.Printf(" ssh -p %d %s@%s # read-write\n", port, sessionName, server)
188+
fmt.Printf(" ssh -p %d %s@%s # read-write\n", port, rwUser, server)
179189
fmt.Printf(" ssh -p %d %s@%s # read-only\n", port, roUser, server)
180190
}
181191
if listenHost != "127.0.0.1" {
@@ -184,7 +194,7 @@ var App = &cli.App{
184194
displayHost = "<local-addr>"
185195
}
186196
fmt.Println("Join via:")
187-
fmt.Printf(" ssh -p %d %s@%s # read-write\n", port, sessionName, displayHost)
197+
fmt.Printf(" ssh -p %d %s@%s # read-write\n", port, rwUser, displayHost)
188198
fmt.Printf(" ssh -p %d %s@%s # read-only\n", port, roUser, displayHost)
189199
}
190200
fmt.Println("\nPress Enter to continue...")
@@ -198,7 +208,7 @@ var App = &cli.App{
198208
},
199209
}
200210

201-
func runServer(chGuard chan struct{}, port int, listenAddr, sessionName, hostKeyFile, entrypoint string) error {
211+
func runServer(chGuard chan struct{}, port int, listenAddr, hostKeyFile, entrypoint string) error {
202212
// Define the SSH server
203213
server := &ssh.Server{
204214
Addr: listenAddr,
@@ -207,7 +217,7 @@ func runServer(chGuard chan struct{}, port int, listenAddr, sessionName, hostKey
207217
fmt.Println(username, roUser)
208218

209219
// Disallow clients connecting with the wrong username.
210-
if !(username == sessionName || username == roUser) {
220+
if !(username == rwUser || username == roUser) {
211221
return
212222
}
213223

@@ -228,7 +238,7 @@ func runServer(chGuard chan struct{}, port int, listenAddr, sessionName, hostKey
228238
// Set TERM environment variable
229239
cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
230240
cmd.Env = append(cmd.Env, fmt.Sprintf("SHELL=%s", os.Getenv("SHELL")))
231-
cmd.Env = append(cmd.Env, fmt.Sprintf("ZIINA_CONNECTION_INFO=%s", fmt.Sprintf("ssh -p %d %s@%s", port, sessionName, entrypoint)))
241+
cmd.Env = append(cmd.Env, fmt.Sprintf("ZIINA_CONNECTION_INFO=%s", fmt.Sprintf("ssh -p %d %s@%s", port, rwUser, entrypoint)))
232242
cmd.Env = append(cmd.Env, fmt.Sprintf("ZIINA_CONNECTION_INFO_RO=%s", fmt.Sprintf("ssh -p %d %s@%s", port, roUser, entrypoint)))
233243

234244
// Start Zellij in a new PTY
@@ -280,7 +290,7 @@ func runServer(chGuard chan struct{}, port int, listenAddr, sessionName, hostKey
280290
return server.ListenAndServe()
281291
}
282292

283-
func runReverseTunnel(chGuard chan struct{}, bindAddr, remoteHost, user string, port int) error {
293+
func runReverseTunnel(chGuard chan struct{}, bindAddr, remoteHost, username string, port int) error {
284294
log.Println("Starting SSH reverse port-forwarding...")
285295

286296
// Connect to the running SSH agent
@@ -301,7 +311,7 @@ func runReverseTunnel(chGuard chan struct{}, bindAddr, remoteHost, user string,
301311

302312
// SSH client configuration
303313
config := &sshcrypto.ClientConfig{
304-
User: user, // Replace with your SSH username
314+
User: username, // Replace with your SSH username
305315
Auth: []sshcrypto.AuthMethod{
306316
// Use the SSH agent to retrieve keys for authentication
307317
sshcrypto.PublicKeysCallback(agentClient.Signers),
@@ -377,7 +387,7 @@ func runZellij(server, sessionName string, port int) error {
377387

378388
// SSH config
379389
config := &sshcrypto.ClientConfig{
380-
User: sessionName,
390+
User: rwUser,
381391
Auth: []sshcrypto.AuthMethod{
382392
sshcrypto.PublicKeysCallback(ag.Signers),
383393
},

0 commit comments

Comments
 (0)