From d097303a39af07a64713acdff85c86c7ddbe98f8 Mon Sep 17 00:00:00 2001 From: OttoMao Date: Fri, 3 Mar 2017 13:53:37 +0800 Subject: [PATCH] update 4.x doc --- .DS_Store | Bin 8196 -> 8196 bytes 4.x/assets/css/custom.css | 4 +- 4.x/assets/css/hljs-github.min.css | 96 --------------------------- 4.x/index.html | 101 +++++++++++++++++++++++------ 4 files changed, 85 insertions(+), 116 deletions(-) delete mode 100644 4.x/assets/css/hljs-github.min.css diff --git a/.DS_Store b/.DS_Store index 5f3d46b83a526e02eb4296826a916c226f630ebb..8d7176673b80664bc8d589522d778990e052ca1a 100644 GIT binary patch delta 1125 zcmb`FOGs2v7{|Z=jE{S7KF+-KDvg?>IM!&R4ZVCcYDCbJveaHSTvB40=L~B3kLxA}A<=piM!!un3|>o2V8oGHOw0yhV#Pf!)P{`~A-A`~Ao%oBGvIY)@N2>rf&gHL`n)8d4%L zb(gC2h!*o%u{jdxQYKhdc4khlEVuRs)qTBc>;Q8Kg0a_Zxjy@|tKUxiZ8_ zJe$e0l0ytaP9njaS#NT56_ z=2mNIJ+)DY`soCnqfxp>w5+`_$dF7=}Co}N~lRp)eoSAtd2CH5mIaw-~Z_q`HC5ff*bPQTs z$7veR!JrL|oKo_kh=R-S{`KPiavlFI`zDRkLoWIiy`gtp@@JX?Aq^SGK>>>3LJ3My z2`{R#06wmF85+cI@Dy_h2u==t2bjT=M`9VGu`g4979TeV@a5T);(K j!ew07?>P!_Bk|fX*OW+?zof)x+|kS$YWRO0m6?75Rz?PC delta 1136 zcmb`FOGs2v7{|Z=_&WENdPbd@JL>ovL#r{T$uu9-nE8OB!qlW9nQ?A#X&guIXfI-{ zq&8O8g$fFysGtlg#7z)F8(l;NxzI%|dTm5Z%SO=Y0PUzVrY6&#-^k zKN?7w3P$hsol7}0lscZmvKgj~B0iLEKXjVCH%MgZGrn=VVr`bYc*dZn?%NWLX}uA>L+#ha-zc!IU)QO1_eQp92h}dl z2XwVn4F_V{pt>OtQ*{n$Y_Tmn-(6bftE#J?Nli=V)(lzRp@q~)OxvTW{l-X^Qrs2^ z^r-xyBrSB<9g5O95Y+kxwAcaeW^9J1rKcJnt@il*M#)u*7~2^V0~-2Ov4$3Od5KaY z@c?Vn#a+zGGT9?>U25B_Ma&+hTwcj|NNNuUf@*l7NvV)48Q&;vA2dCg#cH{hS>cg7 zyL)?j#8q{(oU>N(Gd|6B&kx@e&|_PoYDCPR_hnqk$d0@HUcpg(5;1ZLn0{)ZU8GW! z4$uglqSJJduF)Nuph=pdNA#GU&{LYDm-L$6&|7*>ALtW(20?<1MR1@1g>a)7OX0&R ztVShju>p-}MhiA$J36rgJJF3WdXN)E9}eOehH)Gxa1NJo6Spvi+n6vmXBJ}8h-TW# zJA%=$rpLVASy&Phlaf=IBrkF`W;OjeX5U|8o?C2Yo0(TwG-U}z@_LrK(e~Ui;GyAJFM)w8xXEaMM=oP&aw7=4KBq3dpb|M!p36%j{JCwJWjtB=k{DN<|LW+!81nzSqhAb_T7Cejbqo>! diff --git a/4.x/assets/css/custom.css b/4.x/assets/css/custom.css index 5658f6d..b8f01c6 100644 --- a/4.x/assets/css/custom.css +++ b/4.x/assets/css/custom.css @@ -30,7 +30,9 @@ body{ .toc-container{ width: 200px; position: fixed; - top: 42px; + top: 20px; + height: 95vh; + overflow-y: auto; } .toc-container ul{ diff --git a/4.x/assets/css/hljs-github.min.css b/4.x/assets/css/hljs-github.min.css deleted file mode 100644 index cb2cc27..0000000 --- a/4.x/assets/css/hljs-github.min.css +++ /dev/null @@ -1,96 +0,0 @@ -/* - -Atom One Light by Daniel Gamage -Original One Light Syntax theme from https://github.com/atom/one-light-syntax - -base: #fafafa -mono-1: #383a42 -mono-2: #686b77 -mono-3: #a0a1a7 -hue-1: #0184bb -hue-2: #4078f2 -hue-3: #a626a4 -hue-4: #50a14f -hue-5: #e45649 -hue-5-2: #c91243 -hue-6: #986801 -hue-6-2: #c18401 - -*/ - -.hljs { - display: block; - overflow-x: auto; - padding: 0.5em; - color: #383a42; - background: #fafafa; -} - -.hljs-comment, -.hljs-quote { - color: #a0a1a7; - font-style: italic; -} - -.hljs-doctag, -.hljs-keyword, -.hljs-formula { - color: #a626a4; -} - -.hljs-section, -.hljs-name, -.hljs-selector-tag, -.hljs-deletion, -.hljs-subst { - color: #e45649; -} - -.hljs-literal { - color: #0184bb; -} - -.hljs-string, -.hljs-regexp, -.hljs-addition, -.hljs-attribute, -.hljs-meta-string { - color: #50a14f; -} - -.hljs-built_in, -.hljs-class .hljs-title { - color: #c18401; -} - -.hljs-attr, -.hljs-variable, -.hljs-template-variable, -.hljs-type, -.hljs-selector-class, -.hljs-selector-attr, -.hljs-selector-pseudo, -.hljs-number { - color: #986801; -} - -.hljs-symbol, -.hljs-bullet, -.hljs-link, -.hljs-meta, -.hljs-selector-id, -.hljs-title { - color: #4078f2; -} - -.hljs-emphasis { - font-style: italic; -} - -.hljs-strong { - font-weight: bold; -} - -.hljs-link { - text-decoration: underline; -} \ No newline at end of file diff --git a/4.x/index.html b/4.x/index.html index 3d22551..3ee1e84 100644 --- a/4.x/index.html +++ b/4.x/index.html @@ -29,11 +29,19 @@ - + + + + + + + + + @@ -170,7 +178,11 @@ anyproxy --intercept #启动AnyProxy,并解析所

处理流程

    -
  • 当http请求经过代理服务器时,代理服务器的处理流程是:

    +
  • 处理流程图如下
  • +
+

+
    +
  • 当http请求经过代理服务器时,代理服务器的具体流程是:

    • 收集请求所有请求参数,包括method, header, body等
    • AnyProxy调用规则模块beforeSendRequest方法,由模块做处理,返回新的请求参数,或返回响应内容
    • @@ -186,10 +198,7 @@ anyproxy --intercept #启动AnyProxy,并解析所
    • 被明文解析后的https请求,处理流程同http一致。未明文解析请求不会再进入规则模块做处理。
  • -
  • 完整的请求处理流程如下,供参考

    -
-

如何引用

如下几种方案都可以用来引用规则模块:

    @@ -203,17 +212,27 @@ anyproxy --intercept #启动AnyProxy,并解析所
    anyproxy --rule ./myRulePkg/ #本地包
     npm i -g myRulePkg && anyproxy --rule myRulePkg #全局包
-

接口详解

-

规则模块应该符合cmd规范,一个典型的规则模块代码结构如下

+

规则接口文档

+

规则模块应该符合cmd规范,一个典型的规则模块代码结构如下。模块中所有方法都是可选的,只需实现业务感兴趣的部分即可。

module.exports = {
-  summary() { return 'my customized rule for AnyProxy'; },
-  *beforeSendRequest(requestDetail) { /* ... */ },
-  *beforeSendResponse(requestDetail, responseDetail) { /* ... */ },
-  *beforeDealHttpsRequest(requestDetail) { /* ... */ }
-};

summary()

+ // 模块介绍 + summary() { return 'my customized rule for AnyProxy'; }, + // 发送请求前拦截处理 + *beforeSendRequest(requestDetail) { /* ... */ }, + // 发送响应前处理 + *beforeSendResponse(requestDetail, responseDetail) { /* ... */ }, + // 是否处理https请求 + *beforeDealHttpsRequest(requestDetail) { /* ... */ }, + // 请求出错的事件 + *onError(requestDetail, error) { /* ... */ }, + // https连接服务器出错 + *onConnectError(requestDetail, error) { /* ... */ } +};

summary

+

summary()

  • 返回规则模块介绍,用于AnyProxy提示用户
+

beforeSendRequest

beforeSendRequest(requestDetail)

  • AnyProxy向服务端发送请求前,会调用beforeSendRequest,并带上参数requestDetail
  • @@ -225,7 +244,7 @@ npm i -g myRulePkg && anyproxy --rule myRulePkg { protocol: 'http', url: 'http://anyproxy.io/', @@ -266,14 +285,17 @@ newOption.path = '/redirect/to/another/path'; requestData: 'my new request data' //这里也可以同时加上requestOptions };
      -
    • 直接返回客户端,不再发起请求
    • +
    • 直接返回客户端,不再发起请求,其中statusCode header 是必选字段
    return {
    -  statusCode: 200,
    -  header: { 'content-type': 'text/html' },
    -  body: 'this could be a <string> or <buffer>'
    +  response: {
    +    statusCode: 200,
    +    header: { 'content-type': 'text/html' },
    +    body: 'this could be a <string> or <buffer>'
    +  }
     };
+

beforeSendResponse

beforeSendResponse(requestDetail, responseDetail)

  • AnyProxy向客户端发送请求前,会调用beforeSendResponse,并带上参数requestDetail responseDetail
  • @@ -283,7 +305,19 @@ newOption.path = '/redirect/to/another/path';
  • _res {object} 原始的服务端返回对象
-
  • 举例,请求www.qq.com时,responseDetail参数内容大致如下
  • +
  • 举例,请求 anyproxy.io 时,responseDetail参数内容大致如下

    +
    { 
    +  response: { 
    +    statusCode: 200,
    +    header: { 
    +      'Content-Type': 'image/gif',
    +      Connection: 'close',
    +      'Cache-Control': '...'
    +    },
    +    body: '...'
    +  },
    +  _res: { /* ... */ }
    +}
  • 以下几种返回都是合法的

    • 不做任何处理,返回null
    • @@ -304,6 +338,7 @@ newResponse.body += '--from anyproxy--'; response: newResponse };
    +

    beforeDealHttpsRequest

    beforeDealHttpsRequest(requestDetail)

    • AnyProxy收到https请求时,会调用beforeDealHttpsRequest,并带上参数requestDetail
    • @@ -316,10 +351,38 @@ newResponse.body += '--from anyproxy--';
  • 返回值
      -
    • true或者false,是否需要AnyProxy解析https
    • +
    • true或者false,表示是否需要AnyProxy替换证书并解析https
  • +

    onError

    +

    onError(requestDetail, error)

    +
      +
    • 在请求处理过程中发生错误时,AnyProxy会调用onError方法,并提供对应的错误信息
    • +
    • 多数场景下,错误会在请求目标服务器的时候发生,比如DNS解析失败、请求超时等
    • +
    • requestDetailbeforeSendRequest中的参数
    • +
    • 以下几种返回都是合法的

      +
        +
      • 不做任何处理。此时AnyProxy会返回一个默认的错误页。
      • +
      +
      return null;
        +
      • 返回自定义错误页
      • +
      +
      return {
      +  response: {
      +    statusCode: 200,
      +    header: { 'content-type': 'text/html' },
      +    body: 'this could be a <string> or <buffer>'
      +  }
      +};
    • +
    +

    onConnectError

    +

    onConnectError(requestDetail, error)

    +
      +
    • AnyProxy在与目标HTTPS服务器建立连接的过程中,如果发生错误,AnyProxy会调用这个方法
    • +
    • requestDetailbeforeDealHttpsRequest中的参数
    • +
    • 此处无法控制向客户端的返回信息,无需返回值。
    • +

    FAQ

    • Q: 为什么https请求不能进入处理函数?