简介

Yarp 是微软团队开发的一个反向代理组件, 除了常规的 http 和 https 转换通讯,它最大的特点是可定制化,很容易根据特定场景开发出需要的定制代理通道。

详细介绍:https://devblogs.microsoft.com/dotnet/announcing-yarp-1-0-release

源码仓库:https://github.com/microsoft/reverse-proxy

文档地址 :https://microsoft.github.io/reverse-proxy/

基础使用

1、创建 ASP.NET Core  空项目

使用 Visual Studio :

 
使用 .NET CLI 命令行创建:
dotnet new web -o MyProxy

2、 修改代码 Program.cs 文件

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.MapGet("/Ping", () => "Hello World!");
app.MapReverseProxy();
app.Run();

3、修改配置文件 appsettings.json

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"routeAll": {
"ClusterId": "clusterBaidu",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"clusterBaidu": {
"Destinations": {
"baidu": {
"Address": "https://www.baidu.com/"
}
}
}
}
}
}

这里的配置是将所有的请求都转发到百度。

在 Program.cs 里,还注册了一个 Get 路由 Ping 。

4、启动项目

能够看到在浏览器访问程序监听的端口号后,显示的是百度的页面。打开 F12 ,看到请求头也是本地的,并不是百度的域名。

测试手动注册的路由 Ping :

能够显示正常。

5、问题整理

(1) Yarp 是不是只能做这种简单的转发?

不是,往下有配置文件说明。

(2) JSON 配置文件里有什么要注意的地方吗?

有。在这个演示的配置文件中 ReverseProxy:Clusters:cluster1:Destinations:destination1:Address 对应的值是:https://www.baidu.com/ ,如果去掉 www ,在项目启动后会跳转到百度首页,不是代理转发。去掉末尾的 / 符合没有任何影响。

(3) Yarp 会影响到程序中注册的路由吗?

不会影响到程序内部注册的路由。在 Program.cs 中无论 app.MapReverseProxy(); 在上还是在下,在访问 Ping 的时候,都是返回 Hello World!

var app = builder.Build();
app.MapReverseProxy();
app.MapGet("/Ping", () => "Hello World!");
app.Run();

进阶探索

1、多地址代理

修改配置文件 appsettings.json ,实现默认路由跳转百度,当访问 /movie 是访问 b站。

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"routeBaidu": {
"ClusterId": "clusterBaidu",
"Match": {
"Path": "{**catch-all}"
}
},
"routeBiliBili": {
"ClusterId": "clusterBiliBili",
"Match": {
"Path": "/movie/{**catch-all}"
}
}
},
"Clusters": {
"clusterBaidu": {
"Destinations": {
"baidu": {
"Address": "https://www.baidu.com/"
}
}
},
"clusterBiliBili": {
"Destinations": {
"bilibili": {
"Address": "https://www.bilibili.com/"
}
}
}
}
}
}

测试结果:

在后面输入路由 /movie 后能够跳转到b站。但是b站网页没有完整显示,图片都没有,这是网站上的策略问题,对于数据接口没有这些问题。

详细的配置文件说明,可以查看 https://microsoft.github.io/reverse-proxy/articles/config-files.html

2、规则匹配

网页上太多资源,为了方便测试,启用两个 api 接口。地址分别是:http://localhost:5241/ 和 https://localhost:7184/

两个 api 接口中分别注册 /test 路由。

// http://localhost:5241/
app.MapGet("/test", () => "Welcome to Api111!"); // https://localhost:7184/
app.MapGet("/test", () => "Welcome to Api222!");

启动两个 api 程序。

C:\Users\Test>curl http://localhost:5241/test
Welcome to Api111! C:\Users\Test>curl https://localhost:7184/test
Welcome to Api222!

修改 MyProxy 项目的配置文件 appsettings.json

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"routeOne": {
"ClusterId": "clusterOne",
"Match": {
"Path": "/test/{**catch-all}",
"QueryParameters": [
{
"Name": "number",
"Values": [ "1" ]
}
]
}
},
"routeTwo": {
"ClusterId": "clusterTwo",
"Match": {
"Path": "/test/{**catch-all}",
"QueryParameters": [
{
"Name": "number",
"Values": [ "2" ]
}
]
}
},
"routeBaidu": {
"ClusterId": "clusterBaidu",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"clusterOne": {
"Destinations": {
"apiOne": {
"Address": "http://localhost:5241/"
}
}
},
"clusterTwo": {
"Destinations": {
"apiTwo": {
"Address": "https://localhost:7184/"
}
}
},
"clusterBaidu": {
"Destinations": {
"baidu": {
"Address": "https://www.baidu.com/"
}
}
}
}
}
}

Path :监听路由地址。

QueryParameters:匹配参数。

QueryParameters:Name:参数名。

QueryParameters:Values:参数值。

MyProxy 的监听端口是 http://localhost:5024/ 访问结果如下

C:\Users\Test>curl http://localhost:5024/ping
Hello World! C:\Users\Test>curl http://localhost:5024/test
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /test was not found on this server.</p>
</body></html> C:\Users\Test>curl http://localhost:5024/test?number=1
Welcome to Api111! C:\Users\Test>curl http://localhost:5024/test?number=2
Welcome to Api222!

能够根据参数以及参数值导向对应的地址。

3、问题整理

(1)为什么访问 /movie 不能正常显示网页。

因为 b站某些接口开启了防盗链,还有跨域检测。

(2)在根据参数匹配中,如果匹配的路由一样,监听的参数一样,参数值也一样会怎么样?

访问该路由地址会报错。

(3)路由匹配的优先级?

程序内注册的路由优先级最高,其次才是 Yarp 在配置文件里加载的。

小试牛刀

最近的工作是做企业内数据安全方面的。推动公司数据安全体系,通过技术手段提升公司信息安全。

有一个很老的OA系统,十几年了, .NET Framework 2.0 写的。漏洞一大堆,包括不限于xss、sql注入等,权限只到表单级别。浏览器上按下 F12 能查看到表单链接,直接复制出去,别人也能访问。

在这个系统上要做安全,我想的是在中间加代理,正好适合使用 Yarp 来完成,也方便写业务处理代码。嗯,很真实, .NET Core 写的,方便写业务代码。

用户登录成功后,会记录下用户的 Host 和 Cookie,每次访问的时候系统的时候,在 Yarp 这里都校验一下是否与用户登录时的匹配。

 

解决了两个问题:

1、从网络层捕获到所有的请求,方便后面做排查。参数、传值,出了事故可以找到责任人。

2、隔离真实的站点地址,杜绝弱安全等级网站暴露后被坏人攻击的风险。

踩坑集锦

1、non-ASCII

项目要代理某网页,在使用下载功能的时候,接口返回 502 。

info: Yarp.ReverseProxy.Forwarder.HttpForwarder[48]
ResponseHeaders: The destination returned a response that cannot be proxied back to the client.
System.InvalidOperationException: Invalid non-ASCII or control character in header: 0x00E4
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowInvalidHeaderCharacter(Char ch)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ValidateHeaderValueCharacters(StringValues headerValues)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.SetValueFast(String key, StringValues value)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Item(String key, StringValues value)
at Yarp.ReverseProxy.Forwarder.HttpTransformer.CopyResponseHeaders(HttpHeaders source, IHeaderDictionary destination)
at Yarp.ReverseProxy.Forwarder.HttpTransformer.TransformResponseAsync(HttpContext httpContext, HttpResponseMessage proxyResponse)
at Yarp.ReverseProxy.Transforms.Builder.StructuredTransformer.TransformResponseAsync(HttpContext httpContext, HttpResponseMessage proxyResponse)
at Yarp.ReverseProxy.Forwarder.HttpForwarder.SendAsync(HttpContext context, String destinationPrefix, HttpMessageInvoker httpClient, ForwarderRequestConfig requestConfig, HttpTransformer transformer)

去 GitHub 翻 Issues

下载接口能正常访问,文件流也能完整地拿到。重写了所有的响应头没有用。这种不开源的商业站点,也猜不到字符编码。

最后妥协了,用了一个 .NET 服务在服务器上下载后再转发。

代理非常规服务接口时,一定要多测试。

Yarp 让系统内调度更灵活 http、https、websocket 反向代理的更多相关文章

  1. Yarp 让系统内调度更灵活

    简介 Yarp 是微软团队开发的一个反向代理组件, 除了常规的 http 和 https 转换通讯,它最大的特点是可定制化,很容易根据特定场景开发出需要的定制代理通道. 详细介绍:https://de ...

  2. nginx通过https方式反向代理多实例tomcat

    案例说明:前面一层nginx+Keepalived部署的LB,后端两台web服务器部署了多实例的tomcat,通过https方式部署nginx反向代理tomcat请求.配置一如下: 1)LB层的ngi ...

  3. nginx证书制作以及配置https并设置访问http自动跳转https(反向代理转发jboss)

    nginx证书制作以及配置https并设置访问http自动跳转https 默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖 ...

  4. NGINX之——配置HTTPS加密反向代理訪问–自签CA

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46695495 出于公司内部訪问考虑,採用的CA是本机Openssl自签名生成的,因 ...

  5. NC 使用Nginx实现https的反向代理

    summary: [通过Nginx实现NCC的https访问,并解决UClient应用的问题] 1 概述 通过Nginx 安装配置反向代理,实现NC.NCC的https访问. 本文以NCC2005为例 ...

  6. Nginx中配置http和https做反向代理

    参考:http://www.zslin.com/web/article/detail/73 1.安装 sudo apt-get install nginx 2.配置: http_demo.conf # ...

  7. docker使用nginx实现ssl(https)反向代理其他容器应用

    安装nginx容器 搜索nginx镜像 docker search nginx 拉取最新版nginx docker pull nginx:latest 运行容器 docker run --name=n ...

  8. Nginx设置Https反向代理,指向Docker Gitlab11.3.9 Https服务

    目录 目录 1.GitLab11.3.9的安装 2.域名在阿里云托管,申请免费的1年证书 3.Gitlab 的 https 配置 4.Nginx 配置 https,反向代理指向 Gitlab 配置 目 ...

  9. Demo1_iOS9网络适配_改用更安全的HTTPS

    iOS9把所有的http请求都改为https了:iOS9系统发送的网络请求将统一 使用TLS 1.2 SSL.采用TLS 1.2 协议,目的是 强制增强数据访问安全,而且 系统 Foundation ...

  10. graphicview和widgets没本质区别。它只是更轻量级,更灵活,性能更高的widgets

    graphicview和widgets没本质区别.它只是更轻量级,更灵活,性能更高的widgets.核心就是把widgets变成了更轻量级的graphicitem,把QWidget的各种事件转换成了g ...

随机推荐

  1. 重走py 之路 ——字典和集合(二)

    前言 python 中有6大标准类型: 数字(Number) 字符串(String) 列表(List) 元组(Tumple) 集合(Set) 字典(Dictionary) 前面已经介绍了上面4种,还有 ...

  2. Tencent 闲聊对话机器人接口调用,画像:设计员小白

    from datetime import datetime import time import requests from hashlib import md5 from urllib import ...

  3. Python环境和PyCharm搭建教程

    1.python下载和安装 1.访问Python 官网:https://www.python.org/ 2.以Windows为例,我们选择一个稳定的版本进行安装,这里需要注意选择和自己操作系统类型一致 ...

  4. mysql8在Win10下安装教程

    一.准备工作 下载mysql8安装包,下载URL地址:https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/ 二.管理员权限执行 ...

  5. 阿里巴巴在 Envoy Gateway 的演进历程浅析

    ​简介:最近阅读 <Envoy Gateway 来了>这篇文章,深感 Envoy 强大的可扩展性和基于 Envoy Gateway 带来的易用性,在 K8s 架构下,Envoy 重新定义了 ...

  6. 阿里云开源业内首个应用多活项目 AppActive,与社区共建云原生容灾标准

    ​简介:继高可用架构团队的 Sentinel.Chaosblade 开源后,第三个重磅高可用产品:应用多活 AppActive 正式开源,形成高可用的三架马车,帮助企业构建稳定可靠的企业级生产系统,提 ...

  7. WPF 双向绑定到非公开 set 方法属性在 NET 45 和 NET Core 行为的不同

    本文记录 WPF 在 .NET Framework 4.5 和 .NET Core 3.0 或更高版本对使用 Binding 下的 TwoWay 双向绑定模式绑定到非公开的 set 属性上的行为变更 ...

  8. 2019-9-27-微软的-P2P-下载方式

    title author date CreateTime categories 微软的 P2P 下载方式 lindexi 2019-09-27 09:44:44 +0800 2019-09-27 09 ...

  9. SQL Server实战二:创建、修改、复制、删除数据库表并加以数据处理

      本文介绍基于Microsoft SQL Server软件,实现数据库表的创建.修改.复制.删除与表数据处理的方法. 目录 1 交互式创建数据库表T 2 交互式创建数据库表S 3 T-SQL创建数据 ...

  10. 在线程中使用Spring的Bean的方法、不推荐把“线程”注入到Spring

    一.不推荐把"线程"注入到spring 将线程注入到Spring容器中并不是一个常见的做法,而且通常也不推荐这样做,原因如下: 生命周期管理困难: Spring管理的Bean生命周 ...