| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "time" |
| 8 | "unicode" |
| 9 | ) |
| 10 | |
| 11 | var cliStartupEscape = regexp.MustCompile(`\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)`) |
| 12 | var cliOwnedChannel = regexp.MustCompile(`Channels:server:cage(?:[❯›>]?1\.|Iamusingthisforlocaldevelopment)`) |
| 13 | |
| 14 | func cliStartupText(data []byte) string { |
| 15 | return strings.Map(func(r rune) rune { |
| 16 | if unicode.IsSpace(r) { |
| 17 | return -1 |
| 18 | } |
| 19 | return r |
| 20 | }, cliStartupEscape.ReplaceAllString(string(data), "")) |
| 21 | } |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | func cliStartupDialog(text, workspace string) string { |
| 27 | if strings.Contains(text, "WARNING:Loadingdevelopmentchannels") && |
| 28 | cliOwnedChannel.MatchString(text) && |
| 29 | strings.Contains(text, "Iamusingthisforlocaldevelopment") { |
| 30 | return "channel" |
| 31 | } |
| 32 | if workspace != "" && strings.Contains(strings.ToLower(text), "quicksafetycheck:isthisaprojectyoucreatedoroneyoutrust?") && |
| 33 | strings.Contains(text, cliStartupText([]byte(workspace))) && |
| 34 | strings.Contains(text, "Yes,Itrustthisfolder") { |
| 35 | return "project" |
| 36 | } |
| 37 | return "" |
| 38 | } |
| 39 | |
| 40 | func (s *Server) prepareCLIConnection(terminal *terminalSession, id string) { |
| 41 | terminal.mu.Lock() |
| 42 | terminal.cliStartupDone = make(chan struct{}) |
| 43 | terminal.mu.Unlock() |
| 44 | s.activities.Add(1) |
| 45 | go func() { |
| 46 | defer s.activities.Done() |
| 47 | defer terminal.finishCLIStartup() |
| 48 | ctx, cancel := context.WithTimeout(s.lifetimeContext(), 45*time.Second) |
| 49 | defer cancel() |
| 50 | s.mobileCLI.mu.Lock() |
| 51 | record, err := s.mobileCLI.record(s.profile, id) |
| 52 | s.mobileCLI.mu.Unlock() |
| 53 | if err != nil { |
| 54 | return |
| 55 | } |
| 56 | ticker := time.NewTicker(200 * time.Millisecond) |
| 57 | defer ticker.Stop() |
| 58 | seen := map[string]time.Time{} |
| 59 | completed := map[string]bool{} |
| 60 | nextCheck := time.Now() |
| 61 | for { |
| 62 | select { |
| 63 | case <-ctx.Done(): |
| 64 | return |
| 65 | case <-terminal.done: |
| 66 | return |
| 67 | case <-ticker.C: |
| 68 | } |
| 69 | if time.Now().After(nextCheck) { |
| 70 | var current terminalConversation |
| 71 | if err := s.runCLIChannel(ctx, "view", map[string]string{"cli_id": record.CLIID, "config": record.GuestConfig, "connection": record.Connection}, ¤t); err != nil { |
| 72 | nextCheck = time.Now().Add(time.Second) |
| 73 | continue |
| 74 | } |
| 75 | if current.ChannelReady || current.CanSend { |
| 76 | var response struct { |
| 77 | OK bool `json:"ok"` |
| 78 | } |
| 79 | if err := s.runCLIChannel(ctx, "rpc", map[string]string{"cli_id": record.CLIID, "connection": record.Connection, "action": "resume"}, &response); err != nil || !response.OK { |
| 80 | continue |
| 81 | } |
| 82 | return |
| 83 | } |
| 84 | if current.Active || current.ConnectionNotice != "" { |
| 85 | return |
| 86 | } |
| 87 | nextCheck = time.Now().Add(time.Second) |
| 88 | } |
| 89 | terminal.mu.Lock() |
| 90 | text := cliStartupText(terminal.history) |
| 91 | terminal.mu.Unlock() |
| 92 | dialog := cliStartupDialog(text, record.GuestWorkspace) |
| 93 | if dialog == "" || completed[dialog] { |
| 94 | continue |
| 95 | } |
| 96 | if seen[dialog].IsZero() { |
| 97 | seen[dialog] = time.Now() |
| 98 | continue |
| 99 | } |
| 100 | |
| 101 | if time.Since(seen[dialog]) < time.Second { |
| 102 | continue |
| 103 | } |
| 104 | input := "\r" |
| 105 | if dialog == "project" && strings.Contains(text, "❯No,exit") { |
| 106 | input = "\x1b[B\r" |
| 107 | } |
| 108 | if err := terminal.enqueueInput(ctx, input); err != nil { |
| 109 | return |
| 110 | } |
| 111 | completed[dialog] = true |
| 112 | } |
| 113 | }() |
| 114 | } |