前言
流量转发一般是由ng还有网关来处理,但是在架构组工作里面,有些应用是不能接到业务网关,技术网关还在建设中,暂时实现服务进行流量转发
实现思路
可以重写servlet,然后通过httpclient来请求,然后复制对应response值、header、cookie来实现流量转发
上代码
- 第一步配置servlet
@Bean
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyProxyServlet(), "/proxy/*");
servletRegistrationBean.setName("forward1");
servletRegistrationBean.addInitParameter("targetUri", "xx.com");
servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "true");
return servletRegistrationBean;
}
就是/proxy的请求都会转发到xx.com这个域名
- MyProxyServlet 继承HttpServlet
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.http.*;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.AbortableHttpRequest;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.message.HeaderGroup;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpCookie;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.List;
@Slf4j
public class MyProxyServlet extends HttpServlet {
public static final String P_LOG = "log";
public static final String P_FORWARDEDFOR = "forwardip";
protected static final String P_TARGET_URI = "targetUri";
protected static final String ATTR_TARGET_URI =
org.mitre.dsmiley.httpproxy.ProxyServlet.class.getSimpleName() + ".targetUri";
protected static final String ATTR_TARGET_HOST =
org.mitre.dsmiley.httpproxy.ProxyServlet.class.getSimpleName() + ".targetHost";
protected boolean doLog = false;
protected boolean doForwardIP = true;
protected boolean doSendUrlFragment = true;
protected String targetUri;
protected URI targetUriObj;
protected HttpHost targetHost;
private HttpClient proxyClient;
@Override
public String getServletInfo() {
return "";
}
protected String getTargetUri(HttpServletRequest servletRequest) {
return (String) servletRequest.getAttribute(ATTR_TARGET_URI);
}
private HttpHost getTargetHost(HttpServletRequest servletRequest) {
return (HttpHost) servletRequest.getAttribute(ATTR_TARGET_HOST);
}
protected String getConfigParam(String key) {
return getServletConfig().getInitParameter(key);
}
@Override
public void init() throws ServletException {
String doLogStr = getConfigParam(P_LOG);
if (doLogStr != null) {
this.doLog = Boolean.parseBoolean(doLogStr);
}
String doForwardIPString = getConfigParam(P_FORWARDEDFOR);
if (doForwardIPString != null) {
this.doForwardIP = Boolean.parseBoolean(doForwardIPString);
}
initTarget();
HttpParams hcParams = new BasicHttpParams();
hcParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
readConfigParam(hcParams, ClientPNames.HANDLE_REDIRECTS, Boolean.class);
proxyClient = createHttpClient(hcParams);
}
protected void initTarget() throws ServletException {
targetUri = getConfigParam(P_TARGET_URI);
if (targetUri == null)
throw new ServletException(P_TARGET_URI + " is required.");
try {
targetUriObj = new URI(targetUri);
} catch (Exception e) {
throw new ServletException("Trying to process targetUri init parameter: " + e, e);
}
targetHost = URIUtils.extractHost(targetUriObj);
}
private static CookieStore cookieStore = new BasicCookieStore();
@SneakyThrows
@SuppressWarnings({"unchecked", "deprecation"})
protected HttpClient createHttpClient(HttpParams hcParams) {
HttpClient httpClient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.setHostnameVerifier(new AllowAllHostnameVerifier())
.setSslcontext(new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build()).build();
return httpClient;
}
protected HttpClient getProxyClient() {
return proxyClient;
}
protected void readConfigParam(HttpParams hcParams, String hcParamName, Class type) {
String val_str = getConfigParam(hcParamName);
if (val_str == null)
return;
Object val_obj;
if (type == String.class) {
val_obj = val_str;
} else {
try {
val_obj = type.getMethod("valueOf", String.class).invoke(type, val_str);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
hcParams.setParameter(hcParamName, val_obj);
}
@Override
public void destroy() {
if (proxyClient instanceof Closeable) {
try {
((Closeable) proxyClient).close();
} catch (IOException e) {
log("While destroying servlet, shutting down HttpClient: " + e, e);
}
} else {
if (proxyClient != null)
proxyClient.getConnectionManager().shutdown();
}
cookieStore = new BasicCookieStore();
super.destroy();
}
@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
throws ServletException, IOException {
if (servletRequest.getAttribute(ATTR_TARGET_URI) == null) {
servletRequest.setAttribute(ATTR_TARGET_URI, targetUri);
}
if (servletRequest.getAttribute(ATTR_TARGET_HOST) == null) {
servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost);
}
String method = servletRequest.getMethod();
String proxyRequestUri = rewriteUrlFromRequest(servletRequest);
HttpRequest proxyRequest;
if (servletRequest.getHeader(HttpHeaders.CONTENT_LENGTH) != null ||
servletRequest.getHeader(HttpHeaders.TRANSFER_ENCODING) != null) {
HttpEntityEnclosingRequest eProxyRequest = new BasicHttpEntityEnclosingRequest(method, proxyRequestUri);
eProxyRequest.setEntity(new InputStreamEntity(servletRequest.getInputStream(), servletRequest.getContentLength()));
proxyRequest = eProxyRequest;
} else
proxyRequest = new BasicHttpRequest(method, proxyRequestUri);
copyRequestHeaders(servletRequest, proxyRequest);
setXForwardedForHeader(servletRequest, proxyRequest);
HttpResponse proxyResponse = null;
try {
if (doLog) {
log("proxy " + method + " uri: " + servletRequest.getRequestURI() + " -- " + proxyRequest.getRequestLine().getUri());
}
proxyResponse = proxyClient.execute(getTargetHost(servletRequest), proxyRequest);
int statusCode = proxyResponse.getStatusLine().getStatusCode();
for (org.apache.http.cookie.Cookie cookie : cookieStore.getCookies()) {
String proxyCookieName = cookie.getName();
Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
servletCookie.setComment(cookie.getComment());
servletCookie.setPath("/");
servletCookie.setSecure(false);
servletCookie.setVersion(cookie.getVersion());
servletResponse.addCookie(servletCookie);
}
copyResponseHeaders(proxyResponse, servletRequest, servletResponse);
if (doResponseRedirectOrNotModifiedLogic(servletRequest, servletResponse, proxyResponse, statusCode)) {
return;
}
servletResponse.setStatus(statusCode, proxyResponse.getStatusLine().getReasonPhrase());
copyResponseEntity(proxyResponse, servletResponse);
} catch (Exception e) {
if (proxyRequest instanceof AbortableHttpRequest) {
AbortableHttpRequest abortableHttpRequest = (AbortableHttpRequest) proxyRequest;
abortableHttpRequest.abort();
}
if (e instanceof RuntimeException)
throw (RuntimeException) e;
if (e instanceof ServletException)
throw (ServletException) e;
if (e instanceof IOException)
throw (IOException) e;
throw new RuntimeException(e);
} finally {
if (proxyResponse != null)
consumeQuietly(proxyResponse.getEntity());
cookieStore.clear();
}
}
protected boolean doResponseRedirectOrNotModifiedLogic(
HttpServletRequest servletRequest, HttpServletResponse servletResponse,
HttpResponse proxyResponse, int statusCode)
throws ServletException, IOException {
if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES
&& statusCode < HttpServletResponse.SC_NOT_MODIFIED ) {
Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION);
if (locationHeader == null) {
throw new ServletException("Received status code: " + statusCode
+ " but no " + HttpHeaders.LOCATION + " header was found in the response");
}
String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue());
servletResponse.sendRedirect(locStr);
return true;
}
if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0);
servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return true;
}
return false;
}
protected void closeQuietly(Closeable closeable) {
try {
closeable.close();
} catch (IOException e) {
log(e.getMessage(), e);
}
}
protected void consumeQuietly(HttpEntity entity) {
try {
EntityUtils.consume(entity);
} catch (IOException e) {
log(e.getMessage(), e);
}
}
protected static final HeaderGroup hopByHopHeaders;
static {
hopByHopHeaders = new HeaderGroup();
String[] headers = new String[]{
"Connection", "Keep-Alive", "Proxy-Authenticate", "Proxy-Authorization",
"TE", "Trailers", "Transfer-Encoding", "Upgrade"};
for (String header : headers) {
hopByHopHeaders.addHeader(new BasicHeader(header, null));
}
}
protected void copyRequestHeaders(HttpServletRequest servletRequest, HttpRequest proxyRequest) {
Enumeration enumerationOfHeaderNames = servletRequest.getHeaderNames();
while (enumerationOfHeaderNames.hasMoreElements()) {
String headerName = (String) enumerationOfHeaderNames.nextElement();
if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH))
continue;
if (hopByHopHeaders.containsHeader(headerName))
continue;
Enumeration headers = servletRequest.getHeaders(headerName);
while (headers.hasMoreElements()) {
String headerValue = (String) headers.nextElement();
if (headerName.equalsIgnoreCase(HttpHeaders.HOST)) {
HttpHost host = getTargetHost(servletRequest);
headerValue = host.getHostName();
if (host.getPort() != -1)
headerValue += ":" + host.getPort();
} else if (headerName.equalsIgnoreCase(org.apache.http.cookie.SM.COOKIE)) {
headerValue = getRealCookie(headerValue);
}
proxyRequest.addHeader(headerName, headerValue);
}
}
}
private void setXForwardedForHeader(HttpServletRequest servletRequest,
HttpRequest proxyRequest) {
String headerName = "X-Forwarded-For";
if (doForwardIP) {
String newHeader = servletRequest.getRemoteAddr();
String existingHeader = servletRequest.getHeader(headerName);
if (existingHeader != null) {
newHeader = existingHeader + ", " + newHeader;
}
proxyRequest.setHeader(headerName, newHeader);
}
}
protected void copyResponseHeaders(HttpResponse proxyResponse, HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
for (Header header : proxyResponse.getAllHeaders()) {
if (hopByHopHeaders.containsHeader(header.getName()))
continue;
if (header.getName().equalsIgnoreCase(org.apache.http.cookie.SM.SET_COOKIE) ||
header.getName().equalsIgnoreCase(org.apache.http.cookie.SM.SET_COOKIE2)) {
copyProxyCookie(servletRequest, servletResponse, header);
} else {
servletResponse.addHeader(header.getName(), header.getValue());
}
}
}
protected void copyProxyCookie(HttpServletRequest servletRequest,
HttpServletResponse servletResponse, Header header) {
List<HttpCookie> cookies = HttpCookie.parse(header.getValue());
String path = servletRequest.getContextPath();
path += servletRequest.getServletPath();
for (HttpCookie cookie : cookies) {
String proxyCookieName = cookie.getName();
Cookie servletCookie = new Cookie(proxyCookieName, cookie.getValue());
servletCookie.setComment(cookie.getComment());
servletCookie.setMaxAge((int) cookie.getMaxAge());
servletCookie.setPath(path);
servletCookie.setSecure(cookie.getSecure());
servletCookie.setVersion(cookie.getVersion());
servletResponse.addCookie(servletCookie);
}
}
protected String getRealCookie(String cookieValue) {
StringBuilder escapedCookie = new StringBuilder();
String cookies[] = cookieValue.split("; ");
for (String cookie : cookies) {
String cookieSplit[] = cookie.split("=");
if (cookieSplit.length == 2) {
String cookieName = cookieSplit[0];
if (cookieName.startsWith(getCookieNamePrefix())) {
cookieName = cookieName.substring(getCookieNamePrefix().length());
if (escapedCookie.length() > 0) {
escapedCookie.append("; ");
}
escapedCookie.append(cookieName).append("=").append(cookieSplit[1]);
}
}
cookieValue = escapedCookie.toString();
}
return cookieValue;
}
protected String getCookieNamePrefix() {
return getServletConfig().getServletName();
}
protected void copyResponseEntity(HttpResponse proxyResponse, HttpServletResponse servletResponse) throws IOException {
HttpEntity entity = proxyResponse.getEntity();
if (entity != null) {
OutputStream servletOutputStream = servletResponse.getOutputStream();
entity.writeTo(servletOutputStream);
}
}
protected String rewriteUrlFromRequest(HttpServletRequest servletRequest) {
StringBuilder uri = new StringBuilder(500);
uri.append(getTargetUri(servletRequest));
if (servletRequest.getPathInfo() != null) {
uri.append(encodeUriQuery(servletRequest.getPathInfo()));
}
String queryString = servletRequest.getQueryString();
String fragment = null;
if (queryString != null) {
int fragIdx = queryString.indexOf('#');
if (fragIdx >= 0) {
fragment = queryString.substring(fragIdx + 1);
queryString = queryString.substring(0, fragIdx);
}
}
queryString = rewriteQueryStringFromRequest(servletRequest, queryString);
if (queryString != null && queryString.length() > 0) {
uri.append('?');
uri.append(encodeUriQuery(queryString));
}
if (doSendUrlFragment && fragment != null) {
uri.append('#');
uri.append(encodeUriQuery(fragment));
}
return uri.toString();
}
protected String rewriteQueryStringFromRequest(HttpServletRequest servletRequest, String queryString) {
return queryString;
}
protected String rewriteUrlFromResponse(HttpServletRequest servletRequest, String theUrl) {
final String targetUri = getTargetUri(servletRequest);
if (theUrl.startsWith(targetUri)) {
String curUrl = servletRequest.getRequestURL().toString();
String pathInfo = servletRequest.getPathInfo();
if (pathInfo != null) {
assert curUrl.endsWith(pathInfo);
curUrl = curUrl.substring(0, curUrl.length() - pathInfo.length());
}
theUrl = curUrl + theUrl.substring(targetUri.length());
}
return theUrl;
}
public String getTargetUri() {
return targetUri;
}
protected static CharSequence encodeUriQuery(CharSequence in) {
StringBuilder outBuf = null;
Formatter formatter = null;
for (int i = 0; i < in.length(); i++) {
char c = in.charAt(i);
boolean escape = true;
if (c < 128) {
if (asciiQueryChars.get((int) c)) {
escape = false;
}
} else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {
escape = false;
}
if (!escape) {
if (outBuf != null)
outBuf.append(c);
} else {
if (outBuf == null) {
outBuf = new StringBuilder(in.length() + 5 * 3);
outBuf.append(in, 0, i);
formatter = new Formatter(outBuf);
}
formatter.format("%%%02X", (int) c);
}
}
return outBuf != null ? outBuf : in;
}
protected static final BitSet asciiQueryChars;
static {
char[] c_unreserved = "_-!.~'()*".toCharArray();
char[] c_punct = ",;:$&+=".toCharArray();
char[] c_reserved = "?/[]@".toCharArray();
asciiQueryChars = new BitSet(128);
for (char c = 'a'; c <= 'z'; c++) asciiQueryChars.set((int) c);
for (char c = 'A'; c <= 'Z'; c++) asciiQueryChars.set((int) c);
for (char c = '0'; c <= '9'; c++) asciiQueryChars.set((int) c);
for (char c : c_unreserved) asciiQueryChars.set((int) c);
for (char c : c_punct) asciiQueryChars.set((int) c);
for (char c : c_reserved) asciiQueryChars.set((int) c);
asciiQueryChars.set((int) '%');
}
}
- 讲解
重写service方法 封装proxyRequest跟proxyResponse,实际上就是proxy来进行请求,然后将数据反传到response cookie、header的复制 cookie采用CookieStore,配置到HttpClient里头
复制响应status,信息 最后关闭proxy链接,清楚cookie
|