|
| 1 | +package marasi |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "path" |
| 7 | + "runtime" |
| 8 | +) |
| 9 | + |
| 10 | +// getChromePath determines the Chrome executable path based on the operating system. |
| 11 | +// It checks common installation locations for Chrome and Chromium on macOS, Windows, and Linux. |
| 12 | +// |
| 13 | +// Returns: |
| 14 | +// - string: Path to Chrome executable, or empty string if not found |
| 15 | +func getChromePath() string { |
| 16 | + var paths []string |
| 17 | + switch runtime.GOOS { |
| 18 | + case "darwin": |
| 19 | + paths = []string{ |
| 20 | + `/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`, |
| 21 | + `/Applications/Chromium.app/Contents/MacOS/Chromium`, |
| 22 | + `/usr/local/bin/chrome`, // Alternative common symlink |
| 23 | + `/usr/local/bin/chromium`, // Alternative common symlink for Chromium |
| 24 | + } |
| 25 | + case "windows": |
| 26 | + paths = []string{ |
| 27 | + `C:\Program Files\Google\Chrome\Application\chrome.exe`, |
| 28 | + `C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`, |
| 29 | + `C:\Program Files\Chromium\Application\chrome.exe`, |
| 30 | + } |
| 31 | + case "linux": |
| 32 | + paths = []string{ |
| 33 | + `/usr/bin/google-chrome`, |
| 34 | + `/usr/bin/chromium-browser`, |
| 35 | + `/usr/bin/chromium`, |
| 36 | + `/snap/bin/chromium`, |
| 37 | + } |
| 38 | + default: |
| 39 | + return "" |
| 40 | + } |
| 41 | + |
| 42 | + // Find the first valid path |
| 43 | + for _, path := range paths { |
| 44 | + if _, err := exec.LookPath(path); err == nil { |
| 45 | + return path |
| 46 | + } |
| 47 | + } |
| 48 | + return "" |
| 49 | +} |
| 50 | + |
| 51 | +// StartChrome launches Chrome with proxy configuration and security settings. |
| 52 | +// It configures Chrome to use the proxy server, creates an isolated user profile, |
| 53 | +// and disables various Chrome features that might interfere with testing. |
| 54 | +// |
| 55 | +// Returns: |
| 56 | +// - error: Chrome launch error if executable not found or process fails to start |
| 57 | +func (proxy *Proxy) StartChrome() error { |
| 58 | + // Determine Chrome path based on OS |
| 59 | + chromePath := getChromePath() |
| 60 | + if chromePath == "" { |
| 61 | + return fmt.Errorf("unsupported operating system") |
| 62 | + } |
| 63 | + |
| 64 | + // Set flags for Chrome |
| 65 | + flags := []string{ |
| 66 | + fmt.Sprintf("--user-data-dir=%s", path.Join(proxy.ConfigDir, "chrome-profile")), |
| 67 | + fmt.Sprintf("--proxy-server=http://%s:%s", proxy.Addr, proxy.Port), |
| 68 | + fmt.Sprintf("--ignore-certificate-errors-spki-list=%s", proxy.SPKIHash), |
| 69 | + "--disable-background-networking", |
| 70 | + "--disable-client-side-phishing-detection", |
| 71 | + "--disable-default-apps", |
| 72 | + "--disable-features=NetworkPrediction,OmniboxUIExperimentMaxAutocompleteMatches", |
| 73 | + "--disable-sync", |
| 74 | + "--metrics-recording-only", |
| 75 | + "--disable-domain-reliability", |
| 76 | + "--no-first-run", |
| 77 | + "--disable-component-update", |
| 78 | + "--disable-suggestions-service", |
| 79 | + "--disable-search-geolocation-disclosure", |
| 80 | + "--disable-search-engine-choice", |
| 81 | + "--disable-omnibox-autocomplete-offers", // Disables autocomplete suggestions |
| 82 | + "--proxy-bypass-list=<-loopback>", |
| 83 | + "about:blank", |
| 84 | + } |
| 85 | + |
| 86 | + // Start Chrome process |
| 87 | + cmd := exec.Command(chromePath, flags...) |
| 88 | + if err := cmd.Start(); err != nil { |
| 89 | + return fmt.Errorf("starting chrome : %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
0 commit comments