NaiveProxy 是一个简单易用的网络代理工具,主要用于调试和分析网络请求。它可以帮助你快速搭建一个代理服务器,捕获和查看 HTTP 请求。以下是使用 NaiveProxy 的基本方法

安装 NaiveProxy

你需要安装 NaiveProxy,根据你的操作系统选择相应的安装方法:

Mac

brew install naive-proxy

Linux

如果你使用的是 Debian 或 Ubuntu:

sudo apt-get install naive-proxy

如果你使用的是 npm,可以通过全球安装:

npm install -g naive-proxy

Windows

下载 NaiveProxy 的 Windows 版本并安装。

配置 NaiveProxy

安装完成后,你需要配置 NaiveProxy,默认配置文件位于 ~/.config/naive-proxy/config.json。

修改配置文件

打开配置文件,修改如下内容:

{
  "http_port": 8888,
  "https_port": 8443,
  "socks_port": 108,
  "transparent_proxy": true,
  "enable_http2": true,
  "enable_tunneled_local": true,
  "enable_promiscuous_mode": true
}

保存后,重启 NaiveProxy:

naive-proxy restart

使用 NaiveProxy 作为系统代理

设置 NaiveProxy 作为系统代理,确保所有流量都通过它。

Mac

设置系统代理:

  1. 打开终端,输入以下命令:
    export HTTP_PROXY=127...1:8888
    export HTTPS_PROXY=127...1:8443
  2. 或者通过系统偏好设置中的网络设置,手动设置代理服务器。

Linux

设置系统代理:

  1. 修改 /etc/profile 文件:
    echo "export HTTP_PROXY=127...1:8888" >> /etc/profile
    echo "export HTTPS_PROXY=127...1:8443" >> /etc/profile
  2. 重新加载配置:
    source /etc/profile

Windows

  1. 右键点击“开始”按钮,选择“系统” > “高级网络设置” > “更多网络选项”。
  2. 在“设置”下,点击“高级网络” > “代理设置”。
  3. 在“HTTP代理”和“HTTPS代理”中,输入 ..1:8888 和 ..1:8443。

使用 NaiveProxy 作为 API 代理

你可以将 NaiveProxy 用于 API 调试或代理开发,捕获所有请求并查看详细信息。

启动 NaiveProxy

启动后,所有本地请求都会通过 NaiveProxy:

naive-proxy start

查看请求详情

打开浏览器,访问 http://127...1:8888/ 或 http://127...1:8443/,你会看到所有通过 NaiveProxy 的请求详情。

使用 NaiveProxy 的日志功能

NaiveProxy 提供详细的日志功能,帮助你调试和分析请求。

启用日志

在配置文件中添加以下内容:

{
  "log_level": "debug",
  "log_to_console": true,
  ...
}

查看日志

运行以下命令查看日志:

naive-proxy logs

过滤和修改请求

NaiveProxy 提供了过滤请求的功能,允许你根据规则修改或重定向请求。

配置过滤规则

在配置文件中添加过滤规则:

{
  "filters": {
    "match": [
      {
        "path": "/api",
        "action": "forward",
        "to": "http://target.com:808"
      }
    ],
    "replace": [
      {
        "path": "/static",
        "to": "/staticfiles"
      }
    ]
  }
}

应用过滤规则

保存配置文件后,请求会根据规则自动处理。

集成到自动化测试

NaiveProxy 支持自动化测试,例如设置重试次数或验证响应状态码。

配置重试

在配置文件中添加重试设置:

{
  "retry": {
    "count": 3,
    "delay": 100
  }
}

验证响应

设置响应验证规则:

{
  "responses": {
    "200": true,
    "500": false
  }
}

示例代码

如果你想使用 NaiveProxy 在代码中进行代理,可以使用以下示例:

Node.js

const naiveProxy = require('naive-proxy');
const proxy = naiveProxy.createProxy({
  server: {
    listen: {
      host: '...',
      port: 8888
    }
  }
});
const handler = {
  http: {
    onRequest: function(request) {
      console.log('收到请求:', request);
      // 代理到目标地址
      return naiveProxy.proxy(request, {
        host: 'target.com',
        port: 808
      });
    }
  }
};
proxy.createServer(handler);

Python

import http.server
import socket
class NaiveProxyServer(http.server.HTTPServer):
    def handle(self, request):
        print("收到请求:", request)
        # 代理到目标地址
        target = "http://target.com:808"
        response = http.server.HTTPResponse(self)
        response.status = 200
        response.headers['Content-Type'] = 'text/plain'
        response.write("请求已被代理到目标地址: %s" % target)
        response.end()
        return True
def main():
    host = '...'
    port = 8888
    http.server.HTTPServer((host, port), NaiveProxyServer).handle()
if __name__ == "__main__":
    main()

NaiveProxy 是一个强大的工具,能够帮助你快速搭建代理服务器,捕获和分析网络请求,你可以根据需要配置代理规则、设置日志、集成到自动化测试等,通过合理使用,NaiveProxy 能够显著提升你的开发效率。

NaiveProxy 是一个简单易用的网络代理工具,主要用于调试和分析网络请求。它可以帮助你快速搭建一个代理服务器,捕获和查看 HTTP 请求。以下是使用 NaiveProxy 的基本方法

扫码添加原子VPN加速器微信

扫码添加原子VPN加速器微信

400-815-7263
扫码添加原子VPN加速器微信

扫码添加原子VPN加速器微信

网站地图