# Automatic Setup（自动设置）

> 使用 Proxyman Automatic Setup 一键打开预先配置好的终端，即可抓取来自 NodeJS、Python、Ruby、Golang 和 cURL 的 HTTP/HTTPS 流量。

## 1. 问题 {#1-problems}

Proxyman **无法抓取**以下环境中的 HTTP/HTTPS 流量：

* **NodeJS**：Axios、got、superagent、fetch 和 node-fetch
* **Python**：http、https、aiohttp、requests
* **Ruby**：http、net/http、net/https、faraday、httparty 和 fastlane
* Golang：net/http、fasthttp、resty、gorequest、req、grequests
* ElectronJS
* cURL

这是一个已知问题：从终端应用运行的 NodeJS、Python、Ruby 和 cURL 不会使用系统 HTTP 代理，因此 Proxyman 中不会出现任何流量。

你需要阅读每个库的技术文档，并手动配置：

* HTTP 代理
* 对自签名证书的信任

\=> 耗时且容易出错 ❌

## 2. 解决方案：Automatic Setup {#2-solution-automatic-setup}

### 优势： {#benefit}

* ✅ **一键自动设置 HTTP 代理和证书**，适用于多种开发环境
* 从 NodeJS、Python、Ruby、Golang、终端或 Web 浏览器等抓取 HTTP(S) 流量
* 安全。它只在当前会话中生效，不会影响你的操作系统

### 使用方法： {#how-to-use}

1. 打开 Proxyman -> **Setup** 菜单 -> **Automatic Setup…**
2. 点击 **Open New Terminal…**。你可以在旁边的菜单中选择 Apple Terminal、iTerm2 或 Ghostty。
3. 如有需要，请接受 AppleScript 的权限提示
4. 新的终端窗口会打开 -> 启动你的后端服务器或运行脚本 => Proxyman 会自动抓取所有流量。
5. 完成 ✅

![Proxyman Automatic Setup 窗口，包含 Apple Terminal 和 Open New Terminal，以及 Web Browsers 和 ElectronJS App 区域](https://proxyman.com/docs/assets/proxyman-automatic-setup-window.jpg)

_启动预先配置好的终端应用_

![终端窗口显示 Proxyman Automatic Setup 脚本的提示信息，可以开始启动后端服务器](https://proxyman.com/docs/assets/CleanShot%202023-04-26%20at%2015.53.00%402x.jpg)

_已启动新的终端应用_

### 注意事项： {#notes}

* Automatic Setup 可以打开 Apple Terminal、iTerm2 或 Ghostty。如需使用其他终端应用（例如 Hyper），请在同一菜单中选择 **My own Terminal…**，或使用 [Manual Setup](https://proxyman.com/docs/zh-cn/automatic-setup/manual-setup)。
* 它是安全的，因为它只在当前会话中运行，不会更改你的系统配置。

#### ElectronJS {#electronjs}

1. 通过 Automatic Setup 打开一个新的终端
2. 使用以下命令行：

```
open ~/Applications/your_electron_app.app
```

你也可以在 Automatic Setup 窗口中点击 **Select ElectronJS App…**。请参阅 [ElectronJS](https://proxyman.com/docs/zh-cn/debug-devices/electronjs)。

### 支持的网络库： {#support-libraries}

Proxyman（配合 Automatic Setup）开箱即可支持以下网络库。

* NodeJS：[axios](https://www.npmjs.com/package/axios)、[fetch](https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch)（v18+）、[node-fetch](https://www.npmjs.com/package/node-fetch)、[got](https://www.npmjs.com/package/got)、[https](https://nodejs.org/api/https.html) 和 [superagent](https://www.npmjs.com/package/superagent)
* Ruby：[http](https://ruby-doc.org/stdlib-3.0.2/libdoc/net/http/rdoc/Net/HTTP.html)、[net/http](https://ruby-doc.org/stdlib-2.7.0/libdoc/net/http/rdoc/Net/HTTP.html)、[net/https](https://ruby-doc.org/stdlib-2.7.0/libdoc/net/http/rdoc/Net/HTTP.html)、[httparty](https://github.com/jnunemaker/httparty)、[faraday](https://github.com/lostisland/faraday) 和 fastlane
* Python：[requests](https://pypi.org/project/requests/)、[aiohttp](https://docs.aiohttp.org/en/stable/)、http.client、urllib3 和 httpx
* Golang：net/http、fasthttp、resty、gorequest、req、grequests
* ElectronJS 应用
* 不带 --proxy 参数的 cURL

> **提示:** 由于更改只影响你当前的终端会话，因此它是完全**安全**的。它不会修改你的 **bash\_profile** 或 **zshrc** 文件。

## 3. 进阶：工作原理 {#3-advanced-how-does-it-work}

点击 **Open New Terminal…** 时，Proxyman 会执行以下操作：

1. 使用 AppleScript 启动终端应用。
2. 在新的终端窗口中运行以下命令行：

```bash
set -a && source "$HOME/.proxyman/proxyman_env_automatic_setup.sh" && set +a
```

3. proxyman\_env\_automatic\_setup.sh 是一个 bash 脚本，用于设置 Proxyman 所需的环境变量。

例如：

* HTTP\_PROXY 和 HTTPS\_PROXY 环境变量
* PATH
* RUBYLIB
* PYTHONPATH
* NODE\_OPTIONS
* GLOBAL\_AGENT\_HTTP\_PROXY

**NodeJS**：

1. Proxyman 会在 $PATH 环境变量前面添加一个新的 Node 目录。
2. Proxyman 通过猴子补丁（monkey-patch）方式使用 [global-agent](https://www.npmjs.com/package/global-agent) 包修改 Node，从而为 axios 和 fetch 添加 HTTP 代理支持。

**Ruby**：

1. 将 $RUBYLIB 覆盖为指向 Proxyman 应用
2. 修补 http、net/http 和 net/https 等常用库 -> 设置 HTTP 代理并信任 Proxyman 自签名证书。

**Python**：

1. 将 $PYTHONPATH 覆盖为指向 Proxyman 应用
2. 修补 aiohttp、httplib、http.client 等常用库 -> 设置 HTTP 代理并信任 Proxyman 自签名证书。

**Go**：

1. 覆盖 HTTP\_PROXY 和 HTTPS\_PROXY 环境变量
2. 覆盖部分 Go 环境变量，以信任自签名证书

## 4. 故障排除 {#4-troubleshooting}

请参阅 [故障排除](https://proxyman.com/docs/zh-cn/automatic-setup/troubleshooting)页面。

## 了解更多 {#learn-more}

* [从 axios（NodeJS）抓取 HTTP/HTTPS 请求](https://proxyman.com/posts/2023-05-14-Capture-HTTP-HTTPS-Request-From-Axios-NodeJS)
* [从 Ruby（net::http 或 http gem）脚本抓取 HTTP/HTTPS 请求](https://proxyman.com/posts/2023-05-09-Capture-HTTP-HTTPS-Request-Response-From-Ruby-Net-HTTP-library)
* [如何从 Python requests 或 httpx 库抓取 HTTPS 流量](https://proxyman.com/posts/2025-01-29-How-To-Capture-HTTPS-Traffic-From-Python-Requests-httpx)
