反向代理支持https

This commit is contained in:
ztzh_xieyun 2024-08-01 18:39:56 +08:00
parent 08d0713d5d
commit fff45e56eb
4 changed files with 56 additions and 6 deletions

View File

@ -20,4 +20,8 @@ public class SACConstants {
* 业务码 key * 业务码 key
*/ */
public static final String GATEWAY_SERVICE_CODE = "serviceCode"; public static final String GATEWAY_SERVICE_CODE = "serviceCode";
public static final String HTTPS = "https";
public static final String HTTP = "http";
} }

View File

@ -14,6 +14,7 @@ import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import com.sf.vertx.httpproxy.impl.ReverseProxy; import com.sf.vertx.httpproxy.impl.ReverseProxy;
import com.sf.vertx.util.SacSocketAddress;
import io.vertx.codegen.annotations.Fluent; import io.vertx.codegen.annotations.Fluent;
import io.vertx.codegen.annotations.GenIgnore; import io.vertx.codegen.annotations.GenIgnore;
@ -91,7 +92,16 @@ public interface HttpProxy extends Handler<HttpServerRequest> {
default HttpProxy originSelector(Function<HttpServerRequest, Future<SocketAddress>> selector) { default HttpProxy originSelector(Function<HttpServerRequest, Future<SocketAddress>> selector) {
return originRequestProvider((req, client) -> selector return originRequestProvider((req, client) -> selector
.apply(req) .apply(req)
.flatMap(server -> client.request(new RequestOptions().setServer(server)))); .flatMap(server -> {
RequestOptions requestOptions = new RequestOptions().setServer(server);
if(server instanceof SacSocketAddress) {
SacSocketAddress socketAddress = (SacSocketAddress)server;
if(socketAddress.isSSL()) {
requestOptions.setSsl(socketAddress.isSSL());
}
}
return client.request(requestOptions);
}));
} }
/** /**

View File

@ -0,0 +1,26 @@
package com.sf.vertx.util;
import java.net.InetSocketAddress;
import io.vertx.core.net.impl.SocketAddressImpl;
public class SacSocketAddress extends SocketAddressImpl {
private boolean isSSL; // "http","https"
public SacSocketAddress(InetSocketAddress address) {
super(address);
}
public SacSocketAddress(Integer port, String ip) {
super(port, ip);
}
public boolean isSSL() {
return isSSL;
}
public void setSSL(boolean isSSL) {
this.isSSL = isSSL;
}
}

View File

@ -2,15 +2,19 @@ package com.sf.vertx.utils;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static com.sf.vertx.constans.SACConstants.HTTPS;
import org.apache.commons.lang3.StringUtils;
import com.sf.vertx.api.pojo.Node; import com.sf.vertx.api.pojo.Node;
import com.sf.vertx.api.pojo.RouteContent; import com.sf.vertx.api.pojo.RouteContent;
import com.sf.vertx.arithmetic.roundRobin.SacLoadBalancing; import com.sf.vertx.arithmetic.roundRobin.SacLoadBalancing;
import com.sf.vertx.arithmetic.roundRobin.WeightedRoundRobin; import com.sf.vertx.arithmetic.roundRobin.WeightedRoundRobin;
import com.sf.vertx.handle.AppConfigHandler; import com.sf.vertx.handle.AppConfigHandler;
import com.sf.vertx.util.SacSocketAddress;
import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.net.SocketAddress; import io.vertx.core.net.SocketAddress;
import io.vertx.core.net.impl.SocketAddressImpl;
import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.HttpException; import io.vertx.ext.web.handler.HttpException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -42,12 +46,15 @@ public class ProxyTool {
// 判断 "routeType": "WEIGHT_ROUTE", // 路由类型 WEIGHT_ROUTE HEADER_ROUTE // 判断 "routeType": "WEIGHT_ROUTE", // 路由类型 WEIGHT_ROUTE HEADER_ROUTE
String key = appCode + ":" + apiCode; String key = appCode + ":" + apiCode;
Integer routerType = AppConfigHandler.routerType(key); Integer routerType = AppConfigHandler.routerType(key);
SocketAddress socketAddress = null; //SocketAddress socketAddress = null;
SacSocketAddress socketAddress = null;
switch (routerType) { switch (routerType) {
case 1: case 1:
SacLoadBalancing sacLoadBalancing = AppConfigHandler.getLoadBalancing(key); SacLoadBalancing sacLoadBalancing = AppConfigHandler.getLoadBalancing(key);
Node node = sacLoadBalancing.selectNode(); Node node = sacLoadBalancing.selectNode();
socketAddress = SocketAddress.inetSocketAddress(node.getPort(), node.getIp()); //socketAddress = SocketAddress.inetSocketAddress(node.getPort(), node.getIp());
socketAddress = new SacSocketAddress(node.getPort(), node.getIp());
socketAddress.setSSL(StringUtils.equals(node.getProtocol(), HTTPS));
log.info("sacLoadBalancing address:{},port:{}", socketAddress.host(), socketAddress.port()); log.info("sacLoadBalancing address:{},port:{}", socketAddress.host(), socketAddress.port());
return socketAddress; return socketAddress;
case 2: case 2:
@ -56,10 +63,13 @@ public class ProxyTool {
for (RouteContent routeContent : routeContentList) { for (RouteContent routeContent : routeContentList) {
String headerValue = request.getHeader(routeContent.getHeaderKey()); String headerValue = request.getHeader(routeContent.getHeaderKey());
List<String> headerValues = routeContent.getHeaderValues(); List<String> headerValues = routeContent.getHeaderValues();
// String matchType = routeContent.getMatchType(); //String matchType = routeContent.getMatchType();
if (headerValues.contains(headerValue)) { if (headerValues.contains(headerValue)) {
socketAddress = SocketAddress.inetSocketAddress(routeContent.getServerAddress().getPort(), // socketAddress = SocketAddress.inetSocketAddress(routeContent.getServerAddress().getPort(),
routeContent.getServerAddress().getHost()); // routeContent.getServerAddress().getHost());
socketAddress = new SacSocketAddress(routeContent.getServerAddress().getPort(),
routeContent.getServerAddress().getHost());
socketAddress.setSSL(StringUtils.equals(routeContent.getServerAddress().getProtocol(), HTTPS));
log.info("sacLoadBalancing address:{},port:{}", socketAddress.host(), socketAddress.port()); log.info("sacLoadBalancing address:{},port:{}", socketAddress.host(), socketAddress.port());
return socketAddress; return socketAddress;
} }