反向代理支持https
This commit is contained in:
parent
08d0713d5d
commit
fff45e56eb
@ -20,4 +20,8 @@ public class SACConstants {
|
||||
* 业务码 key
|
||||
*/
|
||||
public static final String GATEWAY_SERVICE_CODE = "serviceCode";
|
||||
|
||||
public static final String HTTPS = "https";
|
||||
|
||||
public static final String HTTP = "http";
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.sf.vertx.httpproxy.impl.ReverseProxy;
|
||||
import com.sf.vertx.util.SacSocketAddress;
|
||||
|
||||
import io.vertx.codegen.annotations.Fluent;
|
||||
import io.vertx.codegen.annotations.GenIgnore;
|
||||
@ -91,7 +92,16 @@ public interface HttpProxy extends Handler<HttpServerRequest> {
|
||||
default HttpProxy originSelector(Function<HttpServerRequest, Future<SocketAddress>> selector) {
|
||||
return originRequestProvider((req, client) -> selector
|
||||
.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);
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -2,15 +2,19 @@ package com.sf.vertx.utils;
|
||||
|
||||
import java.util.List;
|
||||
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.RouteContent;
|
||||
import com.sf.vertx.arithmetic.roundRobin.SacLoadBalancing;
|
||||
import com.sf.vertx.arithmetic.roundRobin.WeightedRoundRobin;
|
||||
import com.sf.vertx.handle.AppConfigHandler;
|
||||
import com.sf.vertx.util.SacSocketAddress;
|
||||
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.net.SocketAddress;
|
||||
import io.vertx.core.net.impl.SocketAddressImpl;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.handler.HttpException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -42,12 +46,15 @@ public class ProxyTool {
|
||||
// 判断 "routeType": "WEIGHT_ROUTE", // 路由类型 WEIGHT_ROUTE ,HEADER_ROUTE
|
||||
String key = appCode + ":" + apiCode;
|
||||
Integer routerType = AppConfigHandler.routerType(key);
|
||||
SocketAddress socketAddress = null;
|
||||
//SocketAddress socketAddress = null;
|
||||
SacSocketAddress socketAddress = null;
|
||||
switch (routerType) {
|
||||
case 1:
|
||||
SacLoadBalancing sacLoadBalancing = AppConfigHandler.getLoadBalancing(key);
|
||||
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());
|
||||
return socketAddress;
|
||||
case 2:
|
||||
@ -56,10 +63,13 @@ public class ProxyTool {
|
||||
for (RouteContent routeContent : routeContentList) {
|
||||
String headerValue = request.getHeader(routeContent.getHeaderKey());
|
||||
List<String> headerValues = routeContent.getHeaderValues();
|
||||
// String matchType = routeContent.getMatchType();
|
||||
//String matchType = routeContent.getMatchType();
|
||||
if (headerValues.contains(headerValue)) {
|
||||
socketAddress = SocketAddress.inetSocketAddress(routeContent.getServerAddress().getPort(),
|
||||
routeContent.getServerAddress().getHost());
|
||||
// socketAddress = SocketAddress.inetSocketAddress(routeContent.getServerAddress().getPort(),
|
||||
// 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());
|
||||
return socketAddress;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user