Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grpc web proxy #2668

Merged
merged 11 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions arthas-grpc-web-proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## netty grpc web proxy

from: https://github.com/grpc/grpc-web/tree/1.4.2/src/connector

原项目已废弃删除,本项目改用 netty 来做转发。

## 缺点

原项目需要 `.proto` 文件编译的 `.class`才能运行,比如`GreeterGrpc`,本项目同样有这个问题。


## 测试

工程导入IDE之后,进入test目录

在 com.taobao.arthas.grpcweb.proxy.server.GrpcWebProxyServerTest 启动测试

也可以用原项目的相关工程来测试

* https://github.com/grpc/grpc-web/

## 开发验证

可以用其它的 grpc web proxy来抓包辅助验证。

### 用 envoy

下载envoy 后,可以用本项目里的`envoy.yaml`

* `envoy --config-path ./envoy.yaml`

### 使用 grpcwebproxy

* https://github.com/improbable-eng/grpc-web/blob/master/go/grpcwebproxy/README.md

下载后,启动:

* `grpcwebproxy --backend_addr 127.0.0.1:9090 --run_tls_server=false --allow_all_origins`


147 changes: 147 additions & 0 deletions arthas-grpc-web-proxy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>arthas-all</artifactId>
<groupId>com.taobao.arthas</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>arthas-grpc-web-proxy</artifactId>
<name>arthas-grpc-web-proxy</name>
<url>https://github.com/alibaba/arthas</url>
<properties>
<java.version>1.8</java.version>
<grpc.version>1.46.0</grpc.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-bom</artifactId>
<version>${grpc.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.arthas</groupId>
<artifactId>arthas-repackage-logger</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-services</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-common</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<os.detected.classifier>osx-x86_64</os.detected.classifier>
</properties>
</profile>
</profiles>
<build>
<finalName>${project.artifactId}</finalName>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protoSourceRoot>${basedir}/src/main/proto</protoSourceRoot>
<protocArtifact>com.google.protobuf:protoc:3.11.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.28.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>test-compile</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.taobao.arthas.grpcweb.proxy;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;

/**
* TODO 支持让用户配置更精细的 cors header
* @author hengyunabc 2023-09-07
*
*/
public class CorsUtils {

public static void updateCorsHeader(HttpHeaders headers) {
// headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS,
// StringUtils.joinWith(",", "user-agent", "cache-control", "content-type", "content-transfer-encoding",
// "grpc-timeout", "keep-alive"));
headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "*");

headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
headers.set(HttpHeaderNames.ACCESS_CONTROL_REQUEST_HEADERS, "content-type,x-grpc-web,x-user-agent");
headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS,GET,POST,HEAD");

// headers.set(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS,
// StringUtils.joinWith(",", "grpc-status", "grpc-message"));
headers.set(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.taobao.arthas.grpcweb.proxy;

import io.grpc.Channel;
import io.grpc.ClientInterceptors;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import com.alibaba.arthas.deps.org.slf4j.Logger;
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;

/**
* TODO: Manage the connection pool to talk to the grpc-service
*/
public class GrpcServiceConnectionManager {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass().getName());
private final ManagedChannel channel;

public GrpcServiceConnectionManager(int grpcPortNum) {
// TODO: Manage a connection pool.
channel = ManagedChannelBuilder.forAddress("localhost", grpcPortNum).usePlaintext().build();
logger.info("**** connection channel initiated");
}

Channel getChannelWithClientInterceptor(GrpcWebClientInterceptor interceptor) {
return ClientInterceptors.intercept(channel, interceptor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.taobao.arthas.grpcweb.proxy;

import io.grpc.*;
import io.grpc.ClientCall.Listener;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;

import java.util.concurrent.CountDownLatch;

class GrpcWebClientInterceptor implements ClientInterceptor {

private final CountDownLatch latch;
private final SendGrpcWebResponse sendResponse;

GrpcWebClientInterceptor(CountDownLatch latch, SendGrpcWebResponse send) {
this.latch = latch;
sendResponse = send;
}

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel channel) {
return new SimpleForwardingClientCall<ReqT, RespT>(channel.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
super.start(new MetadataResponseListener<RespT>(responseListener), headers);
}
};
}

class MetadataResponseListener<T> extends SimpleForwardingClientCallListener<T> {
private boolean headersSent = false;

MetadataResponseListener(Listener<T> responseListener) {
super(responseListener);
}

@Override
public void onHeaders(Metadata h) {
sendResponse.writeHeaders(h);
headersSent = true;
}

@Override
public void onClose(Status s, Metadata t) {
// TODO 这个函数会在 onCompleted 之前回调,这里有点奇怪
if (!headersSent) {
// seems, sometimes onHeaders() is not called before this method is called!
// so far, they are the error cases. let onError() method in ClientListener
// handle this call. Could ignore this.
// TODO is this correct? what if onError() never gets called?
} else {
sendResponse.writeTrailer(s, t);
latch.countDown();
}
super.onClose(s, t);
}
}
}
Loading