当您在防盗链、黑白名单、定制化请求头和响应头控制、定制化改写和重定向等场景下,需要自定义转发规则时,您可参考本文AScript示例代码自定义流量转发规则。
防盗链需求
自定义鉴权算法
-
需求:
-
请求URL格式:
/path/digest/?.tskey=&t=。 -
针对
.ts类请求,自定义防盗链需求如下:-
规则1:参数
t或参数key不存在,响应403,增加响应头X-AUTH-MSG标识鉴权失败原因。 -
规则2:参数
t表示绝对过期时间,若参数t小于当前时间,则响应403,增加响应头X-AUTH-MSG标识鉴权失败原因。 -
规则3:
md5与digest匹配。若md5与digest不匹配,响应403。md5取值格式为:
私钥 + path + 文件名.后缀。
-
-
-
对应的AScript规则:
if eq(substr($uri, -3, -1), '.ts') { if or(not($arg_t), not($arg_key)) { add_rsp_header('X-AUTH-MSG', 'auth failed - missing necessary arg') exit(403) } t = tonumber($arg_t) if not(t) { add_rsp_header('X-AUTH-MSG', 'auth failed - invalid time') exit(403) } if gt(now(), t) { add_rsp_header('X-AUTH-MSG', 'auth failed - expired url') exit(403) } pcs = capture_re($request_uri,'^/([^/]+)/([^/]+)/([^?]+)%?(.*)') sec1 = get(pcs, 1) sec2 = get(pcs, 2) sec3 = get(pcs, 3) if or(not(sec1), not(sec2), not(sec3)) { add_rsp_header('X-AUTH-MSG', 'auth failed - malformed url') exit(403) } key = 'b98d643a-9170-4937-8524-6c33514bbc23' signstr = concat(key, sec1, sec3) digest = md5(signstr) if ne(digest, sec2) { add_rsp_header('X-AUTH-DEBUG', concat('signstr: ', signstr)) add_rsp_header('X-AUTH-MSG', 'auth failed - invalid digest') exit(403) } }
User-Agent黑名单
-
需求:当请求
User-Agent以ijkplayer或Ysten开头时,响应403。 -
对应的AScript规则:
if and($http_user_agent, match_re($http_user_agent, '^(ijkplayer|Ysten).*$')) { add_rsp_header('X-BLOCKLIST-DEBUG', 'deny') exit(403) }
Referer白名单
-
需求:当
referer不是http[s]://***alibaba.com***时,响应403。 -
对应的AScript规则:
if and($http_referer, match_re($http_referer, '^(http|https)://(.)+\.alibaba\.com.*$')) { return true } add_rsp_header('X-WHITELIST-DEBUG', 'missing') exit(403)
黑白名单
IP黑名单
-
需求:当请求客户端IP为
127.0.0.1或10.0.0.1时,响应403。 -
对应的AScript规则:
if match_re($remote_addr, '127.0.0.1|10.0.0.1') { add_rsp_header('X-IPBLOCK-DEBUG', 'hit') exit(403) }
定制化请求头和响应头控制
文件自动重命名
-
需求:当有参数
filename时,自动重命名为filename,无参数时,使用默认命名。 -
对应的AScript规则:
if $arg_filename { hn = 'Content-Disposition' hv = concat('attachment;filename=', $arg_filename) add_rsp_header(hn, hv) } -
示例:
add_rsp_header('Content-Disposition', concat('attachment;filename=', tochar(34), filename, tochar(34)))说明-
通过在HTTP应答中添加响应头
Content-Disposition:attachment来实现消息体的强制下载,并且有参数filename时,自动重命名为filename,无参数时,使用默认命名。 -
filename增加双引号,双引号的ASCII编码为34,可经tochar转回字符串。
-
-
输出:
Content-Disposition: attachment;filename="monitor.apk"
覆盖源站响应头
-
需求:覆盖源站
Content-Type响应头。 -
对应的AScript规则:
add_rsp_header('Content-Type', 'audio/mpeg')
定制化改写和重定向
精确URI改写
-
需求:将用户请求
/hello内部改写成/index.html,回源和缓存的URI都将变成/index.html,参数保持原样。 -
对应的AScript规则:
if match_re($uri, '^/hello$') { rewrite('/index.html', 'break') }
文件后缀改写
-
需求:将用户请求
/1.txt内部改写成/1.<url参数type>。例如:/1.txt?type=mp4将会被改成/1.mp4?type=mp4回源并缓存。 -
对应的AScript规则:
if and(match_re($uri, '^/1.txt$'), $arg_type) { rewrite(concat('/1.', $arg_type), 'break') }
文件后缀小写化
-
需求:将URI改成小写。
-
对应的AScript规则:
pcs = capture_re($uri, '^(.+\.)([^.]+)') section = get(pcs, 1) postfix = get(pcs, 2) if and(section, postfix) { rewrite(concat(section, lower(postfix)), 'break') }
添加URI前缀
-
需求:将用户请求
^/nn_live/(.*)内部改写为/3rd/nn_live/$1。 -
对应的AScript规则:
pcs = capture_re($uri, '^/nn_live/(.*)') sec = get(pcs, 1) if sec { dst = concat('/3rd/nn_live/', sec) rewrite(dst, 'break') }
302重定向
-
需求:将根目录
/,302重定向到/app/movie/pages/index/index.html页面。 -
对应的AScript规则:
if eq($uri, '/') { rewrite('/app/movie/pages/index/index.html', 'redirect') }
302重定向HTTPS
-
需求:
将以下URI(对根目录匹配,
^/$)-
http://rtmp.cdnpe.com -
https://rtmp.cdnpe.com
跳转到
https://aliyun.com,跳转后的URI可按需填写。 -
-
对应的AScript规则:
if eq($uri, '/') { rewrite('https://aliyun.com', 'redirect') }
URL跳转(不带URI参数)
-
需求:将
www.test.com的请求跳转到www.example.com,不携带原先的URI参数。 -
对应的AScript规则:
if and(eq(req_header('Host'), 'www.test.com')) { rewrite('www.example.com', 'redirect') }效果:重定向后丢失URI参数,但结尾存在一个?号。
去除结尾的?
-
对应的AScript规则:
if and(eq(req_header('Host'), 'www.test.com')) { rewrite('www.example.com', 'enhance_redirect') }
URL跳转(携带URI参数)
-
需求:将
www.test.com的请求跳转到www.example.com,携带原先的URI参数。 -
对应的AScript规则:
if and(eq(req_header('Host'), 'www.test.com')) { rewrite(concat('www.example.com', $uri), 'enhance_redirect') }
正则表达式匹配泛域名(添加URI后缀)
-
需求:若业务存在多个域名,可使用正则表达式匹配来缩减配置工作量。匹配泛域名并添加特定URI后缀参数。
-
对应的AScript规则:
host_re = match_re(req_header('Host'), '.*.test.com') url_match = null(find($uri, 'index.php')) if and(host_re, url_match) { rewrite(concat($host, '/index.php?s=', $uri), 'enhance_redirect') }
HTTP重定向到HTTPS(去除查询参数)
-
需求:将
http://www.test.com/tmp/?a=b重定向为https://www.test.com,去除请求携带的查询参数。 -
对应的AScript规则:
host_re = match_re(req_header('Host'), 'www.test.com') url_match = not(null(find($request_uri, '?'))) # 防止一直重定向 if req_scheme('http') { if and(host_re, url_match) { rewrite('https://www.test.com', 'enhance_redirect') # 状态码302 } }
问题排查
添加x-request-id
-
需求:利用AScript生成
x-request-id,并在请求和响应中添加,便于请求追踪及问题排查。 -
对应的AScript规则:
requestid=md5(concat(rand(1, 10000), now())) add_req_header('x-request-id', requestid) add_rsp_header('x-request-id', requestid)