diff --git a/client/client.go b/client/client.go index a2eccef..2df95c7 100644 --- a/client/client.go +++ b/client/client.go @@ -3,7 +3,7 @@ package client import ( "context" "fmt" - "io/ioutil" + "io" "math/rand" "net" "net/http" @@ -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 { h.Set("x-iterm2-cookie", cookie) } else { resp, err := mack.Tell("iTerm2", fmt.Sprintf("request cookie and key for app named %q", appName)) @@ -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 { diff --git a/cmd/goiterm/main.go b/cmd/goiterm/main.go index 91947a0..bcfe1c7 100644 --- a/cmd/goiterm/main.go +++ b/cmd/goiterm/main.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -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 },