Skip to content

Commit

Permalink
Retry without cookie on error
Browse files Browse the repository at this point in the history
Signed-off-by: Marwan Sulaiman <[email protected]>
Co-authored-by: Lonny Wong <[email protected]>
  • Loading branch information
marwan-at-work and lonnywong committed Nov 16, 2023
1 parent bdf94bb commit e268530
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
35 changes: 27 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client
import (
"context"
"fmt"
"io/ioutil"
"io"
"math/rand"
"net"
"net/http"
Expand All @@ -19,17 +19,36 @@ import (
"marwan.io/iterm2/api"
)

// New returns a new websocket connection
// that talks to the iTerm2 application.New
// Callers must call the Close() method when done.
// The cookie parameter is optional. If provided,
// it will bypass script authentication prompts.
// New returns a new websocket connection that talks to the iTerm2
// application.New Callers must call the Close() method when done. The cookie
// parameter is optional. If provided, it will bypass script authentication
// prompts.
func New(appName string) (*Client, error) {
// ITERM2_COOKIE is an an environment variable that's set on each terminal
// session. But it only seems to work the first time, then it gets
// invalidated. Therefore, we keep trying until it returns an error, then we
// try to generate a new cookie instead. See
// https://github.com/marwan-at-work/iterm2/issues/4
if cookie := os.Getenv("ITERM2_COOKIE"); cookie != "" {
client, err := newClient(appName, cookie)
if err == nil {
return client, nil
}
}
client, err := newClient(appName, "")
if err != nil {
return nil, err
}
return client, err
}

func newClient(appName, cookie string) (*Client, error) {
h := http.Header{}
h.Set("origin", "ws://localhost/")
h.Set("x-iterm2-library-version", "go 3.6")
h.Set("x-iterm2-disable-auth-ui", "true")
if cookie := os.Getenv("ITERM2_COOKIE"); cookie != "" {
// Disable using env var cookie due to
if cookie := os.Getenv("ITERM2_COOKIE"); cookie != "" && false {

This comment has been minimized.

Copy link
@lonnywong

lonnywong Nov 16, 2023

Author Contributor

@marwan-at-work Is the false for test only?

This comment has been minimized.

Copy link
@lonnywong

lonnywong Nov 16, 2023

Author Contributor

Should we use the cookie argument instead of Getenv ?

This comment has been minimized.

Copy link
@marwan-at-work

marwan-at-work Nov 16, 2023

Author Owner

That was by mistake while i was testing, removed it in a follow up commit.

This comment has been minimized.

Copy link
@lonnywong

lonnywong Nov 16, 2023

Author Contributor

There's another issue. We call newClient on line 38 with empty cookie argument, but the ITERM2_COOKIE environment may not be empty.

h.Set("x-iterm2-cookie", cookie)
} else {
resp, err := mack.Tell("iTerm2", fmt.Sprintf("request cookie and key for app named %q", appName))
Expand All @@ -56,7 +75,7 @@ func New(appName string) (*Client, error) {
}
c, resp, err := d.Dial("ws://localhost", h)
if err != nil && resp != nil {
b, _ := ioutil.ReadAll(resp.Body)
b, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("error connecting to iTerm2: %v - body: %s", err, b)
}
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/goiterm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -39,9 +38,9 @@ func main() {
}
p := filepath.Join(homedir, "/Library/Application Support/iTerm2/Scripts/", appName+".py")
f := fmt.Sprintf(pyFile, bin)
err = ioutil.WriteFile(p, []byte(f), 0666)
err = os.WriteFile(p, []byte(f), 0666)
if err != nil {
return fmt.Errorf("ioutil.WriteFile: %w", err)
return fmt.Errorf("os.WriteFile: %w", err)
}
return nil
},
Expand Down

0 comments on commit e268530

Please sign in to comment.