devproxy

其他类别 2025-08-21

devproxy

什么是devproxy ?

devproxy旨在成为用于Web应用程序开发的易于配置的前向HTTP代理。

它具有以下功能:

  • URL重写

    如果要通过http://e*x*am*ple.com请求,请访问浏览器,以访问上游http服务器在127.0.0.1:3000侦听,则配置应如下:

     hosts:
      http://e*x*am*ple.com:
        - ^(/.*)$: http://127.0.0.1:3000$1
    

    可以完成此操作,因为该名称分辨率是在devproxy中完成的,该名称分辨率已配置为将http://e*x*am*ple.com的任何请求映射到http://127.0.0.1.1:3000。

  • 透明的TLS终止 /包装(启用SSL / TLS环境的模拟)

    您还可以通过添加以下配置来将请求引导到https://*example.**com/到上游是可能的:

     hosts:
      http://e*x*am*ple.com:
        - ^(/.*)$: http://127.0.0.1:3000$1
      https://e*xam*ple.c*om:
        - ^(/.*)$: http://127.0.0.1:3000$1
    

    即使您没有准备有效的证书, example.com devproxy也会自动生成它。但是,有必要设置用于发行虚假服务器证书的私人PKI,并让您的浏览器信任PKI的Root CA证书。按照自己的风险进行。

    发行虚假服务器证书的CA配置如下:

     tls:
      ca:
        cert: testca.rsa.crt.pem
        key: testca.rsa.key.pem
    hosts:
      ...
    
  • 请求标题修改

    您可以添加 /删除任意请求HTTP标头,以重写该请求:

     hosts:
      http://e*x*am*ple.com:
        - ^(/.*)$: http://127.0.0.1:3000$1
          headers:
            X-Forwarded-Proto: https
            Removed-Header: null
    
  • 测试启用FastCGI的上游

    您可以将请求转发到启用了FastCGI的上游:

     hosts:
      http://e*x*am*ple.com:
        - ^(((?:/.*)*/[^/]+\.php)(/.*|$)): fastcgi://localhost$1
          headers:
            X-Cgi-Script-Filename: /var/www/document/root$2
            X-Cgi-Script-Name: $2
            X-Cgi-Path-Info: $3
    
  • 在文件系统上服务文件

    您还可以通过指定文件来服务本地file:方案作为上游:

     hosts:
      http://e*x*am*ple.com:
        - ^(/.*)?/$: file:///some-document-root$1/index.html
        - ^(/.*)$: file:///some-document-root$1
    

    警告:此功能执行如此幼稚的路径翻译,可轻松利用文档根以外的路径遍历。使用服务器时,切勿将服务器公开。

     file_tx:
      root: /var/empty
      mime_type_file: /usr/share/mime/globs
      mime_type_file_format: xdg-globs
    

    Toplevel file_tx节配置文件传输。

    • root (字符串,可选)

      指定基本目录,用于解决匹配项的相对形式URI产生的相对形式时解决绝对路径。

    • mime_type_file (字符串,可选)

      指定用于从文件扩展程序中推导MIME类型的MIME类型到扩展映射文件的路径。

      未指定时, devproxy将使用GO的标准mime.TypeForExtension()函数。

    • mime_type_file_format (字符串,可选)

      指定MIME类型文件的格式。接受的值是apachexdg-globs

  • 代理链接

    您可以将传出请求引向另一台代理服务器。这在受限制的网络环境中很有用。

     proxy:
      http: http://anooth**er-*proxy-server:8080
      https: http://anoth*e*r*-proxy-server:8080
    

    当您要防止对特定主机的请求进行代理时,可以使用excluded指令。

     excluded: 
      - 127.0.0.1
      - localhost
      - intranet.example.com
    

    或成反比,在白名单的情况下:

     included:
      - intranet.example.com
      - foobar.example.com
    

    也可以指定TLS代理。

     proxy:
      http: https://anoo*th*e*r-proxy-server:8443
      https: https://an*other*-p*roxy-server:8443
      tls:
        ca_certs: cabundle.crt.pem
        certs:
          - cert: client_crt.pem # this can be either the filename of a PEM-formatted certificate or a PEM string itself.
            key: client_key.pem # this can be either the filename of a PEM-formatted private key or a PEM string itself.
    

安装

 go get github.com/moriyoshi/ devproxy

使用devproxy

devproxy -l listen_addr configuration_file # ex: $GOPATH/bin/ devproxy -l 127.0.0.1:8080 config.yml">
 $GOPATH/bin/ devproxy -l listen_addr configuration_file
# ex: $GOPATH/bin/ devproxy -l 127.0.0.1:8080 config.yml

并将浏览器的代理设置调整为确切给出的-l选项。

设置私人PKI

 openssl genrsa 2048 > testca.rsa.key.pem
openssl req -new -key testca.rsa.key.pem -out testca.rsa.csr.pem
openssl x509 -req -in testca.rsa.csr.pem -signkey testca.rsa.key.pem -days 3650 -sha256 -extfile x509.ini -extensions CA -out testca.rsa.crt.pem

x509.ini:

 [CA]
basicConstraints=critical,CA:TRUE,pathlen:1
keyUsage=digitalSignature,keyCertSign,cRLSign

配置文件示例

 tls:
  client:
    verify: true
  ca:
    cert: testca.rsa.crt.pem
    key: testca.rsa.key.pem

hosts:
  http://api.e*x**ample.com:
    - ^(/v1/.*)$: http://localhost:8000$1
    - ^(/v2/.*)$: http://localhost:8001$1
  http://e*x*am*ple.com:
    - ^(/asset.*)$: http://localhost:8002$1
    - ^(/.*)$: http://localhost:8003$1
下载源码

通过命令行克隆项目:

git clone https://github.com/moriyoshi/devproxy.git