3333import io .milvus .client .MilvusServiceClient ;
3434import io .milvus .grpc .*;
3535import io .milvus .v2 .client .ConnectConfig ;
36+ import io .grpc .HttpConnectProxiedSocketAddress ;
37+ import io .grpc .ProxiedSocketAddress ;
38+ import io .grpc .ProxyDetector ;
3639import org .apache .commons .lang3 .StringUtils ;
3740import org .jetbrains .annotations .NotNull ;
3841import org .slf4j .Logger ;
4649import java .time .LocalDateTime ;
4750import java .util .Base64 ;
4851import java .util .concurrent .TimeUnit ;
52+ import java .net .InetSocketAddress ;
53+ import java .net .SocketAddress ;
4954
5055public class ClientUtils {
5156 Logger logger = LoggerFactory .getLogger (ClientUtils .class );
@@ -73,6 +78,11 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
7378 .keepAliveWithoutCalls (connectConfig .isKeepAliveWithoutCalls ())
7479 .idleTimeout (connectConfig .getIdleTimeoutMs (), TimeUnit .MILLISECONDS )
7580 .intercept (MetadataUtils .newAttachHeadersInterceptor (metadata ));
81+
82+ if (StringUtils .isNotEmpty (connectConfig .getProxyAddress ())) {
83+ configureProxy (builder , connectConfig .getProxyAddress ());
84+ }
85+
7686 if (connectConfig .isSecure ()) {
7787 builder .useTransportSecurity ();
7888 }
@@ -95,14 +105,19 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
95105 .keepAliveWithoutCalls (connectConfig .isKeepAliveWithoutCalls ())
96106 .idleTimeout (connectConfig .getIdleTimeoutMs (), TimeUnit .MILLISECONDS )
97107 .intercept (MetadataUtils .newAttachHeadersInterceptor (metadata ));
108+
109+ if (StringUtils .isNotEmpty (connectConfig .getProxyAddress ())) {
110+ configureProxy (builder , connectConfig .getProxyAddress ());
111+ }
112+
98113 if (connectConfig .isSecure ()){
99114 builder .useTransportSecurity ();
100115 }
101116 channel = builder .build ();
102117 } else if (StringUtils .isNotEmpty (connectConfig .getClientPemPath ())
103118 && StringUtils .isNotEmpty (connectConfig .getClientKeyPath ())
104119 && StringUtils .isNotEmpty (connectConfig .getCaPemPath ())) {
105- // tow -way tls
120+ // two -way tls
106121 SslContext sslContext = GrpcSslContexts .forClient ()
107122 .trustManager (new File (connectConfig .getCaPemPath ()))
108123 .keyManager (new File (connectConfig .getClientPemPath ()), new File (connectConfig .getClientKeyPath ()))
@@ -116,6 +131,11 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
116131 .keepAliveWithoutCalls (connectConfig .isKeepAliveWithoutCalls ())
117132 .idleTimeout (connectConfig .getIdleTimeoutMs (), TimeUnit .MILLISECONDS )
118133 .intercept (MetadataUtils .newAttachHeadersInterceptor (metadata ));
134+
135+ if (StringUtils .isNotEmpty (connectConfig .getProxyAddress ())) {
136+ configureProxy (builder , connectConfig .getProxyAddress ());
137+ }
138+
119139 if (connectConfig .getSecure ()) {
120140 builder .useTransportSecurity ();
121141 }
@@ -133,6 +153,9 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
133153 .keepAliveWithoutCalls (connectConfig .isKeepAliveWithoutCalls ())
134154 .idleTimeout (connectConfig .getIdleTimeoutMs (), TimeUnit .MILLISECONDS )
135155 .intercept (MetadataUtils .newAttachHeadersInterceptor (metadata ));
156+ if (StringUtils .isNotEmpty (connectConfig .getProxyAddress ())) {
157+ configureProxy (builder , connectConfig .getProxyAddress ());
158+ }
136159 if (connectConfig .isSecure ()){
137160 builder .useTransportSecurity ();
138161 }
@@ -145,6 +168,30 @@ public ManagedChannel getChannel(ConnectConfig connectConfig){
145168 return channel ;
146169 }
147170
171+ /**
172+ * Configures the proxy settings for a NettyChannelBuilder if proxy address is specified
173+ *
174+ * @param builder NettyChannelBuilder to configure
175+ * @param connectConfig Connection configuration containing proxy settings
176+ */
177+ public static void configureProxy (ManagedChannelBuilder builder , String proxyAddress ) {
178+ String [] hostPort = proxyAddress .split (":" );
179+ if (hostPort .length == 2 ) {
180+ String proxyHost = hostPort [0 ];
181+ int proxyPort = Integer .parseInt (hostPort [1 ]);
182+
183+ builder .proxyDetector (new ProxyDetector () {
184+ @ Override
185+ public ProxiedSocketAddress proxyFor (SocketAddress targetServerAddress ) {
186+ return HttpConnectProxiedSocketAddress .newBuilder ()
187+ .setProxyAddress (new InetSocketAddress (proxyHost , proxyPort ))
188+ .setTargetAddress ((InetSocketAddress ) targetServerAddress )
189+ .build ();
190+ }
191+ });
192+ }
193+ }
194+
148195 private static JdkSslContext convertJavaSslContextToNetty (ConnectConfig connectConfig ) {
149196 ApplicationProtocolConfig applicationProtocolConfig = new ApplicationProtocolConfig (ApplicationProtocolConfig .Protocol .NONE ,
150197 ApplicationProtocolConfig .SelectorFailureBehavior .FATAL_ALERT , ApplicationProtocolConfig .SelectedListenerFailureBehavior .FATAL_ALERT );
0 commit comments