|  |  |  | 
|---|
|  |  |  | import org.springframework.boot.web.servlet.ServletRegistrationBean; | 
|---|
|  |  |  | import org.springframework.context.annotation.Bean; | 
|---|
|  |  |  | import org.springframework.context.annotation.Configuration; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.util.StringUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.ServletException; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.net.ConnectException; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author lin | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @SuppressWarnings(value = {"rawtypes", "unchecked"}) | 
|---|
|  |  |  | @Configuration | 
|---|
|  |  |  | public class ProxyServletConfig { | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public ServletRegistrationBean zlmServletRegistrationBean(){ | 
|---|
|  |  |  | ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ZLMProxySerlet(),"/zlm/*"); | 
|---|
|  |  |  | ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ZlmProxyServlet(),"/zlm/*"); | 
|---|
|  |  |  | servletRegistrationBean.setName("zlm_Proxy"); | 
|---|
|  |  |  | servletRegistrationBean.addInitParameter("targetUri", "http://127.0.0.1:6080"); | 
|---|
|  |  |  | servletRegistrationBean.addUrlMappings(); | 
|---|
|  |  |  | 
|---|
|  |  |  | return servletRegistrationBean; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | class ZLMProxySerlet extends ProxyServlet{ | 
|---|
|  |  |  | class ZlmProxyServlet extends ProxyServlet{ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | protected String rewriteQueryStringFromRequest(HttpServletRequest servletRequest, String queryString) { | 
|---|
|  |  |  | String queryStr = super.rewriteQueryStringFromRequest(servletRequest, queryString); | 
|---|
|  |  |  | MediaServerItem mediaInfo = getMediaInfoByUri(servletRequest.getRequestURI()); | 
|---|
|  |  |  | if (mediaInfo != null) { | 
|---|
|  |  |  | if (!StringUtils.isEmpty(queryStr)) { | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(queryStr)) { | 
|---|
|  |  |  | queryStr += "&secret=" + mediaInfo.getSecret(); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | queryStr = "secret=" + mediaInfo.getSecret(); | 
|---|
|  |  |  | 
|---|
|  |  |  | MediaServerItem getMediaInfoByUri(String uri){ | 
|---|
|  |  |  | String[] split = uri.split("/"); | 
|---|
|  |  |  | String mediaServerId = split[2]; | 
|---|
|  |  |  | return mediaServerService.getOne(mediaServerId); | 
|---|
|  |  |  | if ("default".equalsIgnoreCase(mediaServerId)) { | 
|---|
|  |  |  | return mediaServerService.getDefaultMediaServer(); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | return mediaServerService.getOne(mediaServerId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | MediaServerItem mediaInfo = getMediaInfoByUri(requestURI); | 
|---|
|  |  |  | String url = super.rewriteUrlFromRequest(servletRequest); | 
|---|
|  |  |  | if (mediaInfo == null) { | 
|---|
|  |  |  | logger.error("[ZLM服务访问代理],错误:处理url信息时未找到流媒体信息=>{}", requestURI); | 
|---|
|  |  |  | return  url; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return url.replace(mediaInfo.getId() + "/", ""); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(mediaInfo.getId())) { | 
|---|
|  |  |  | url = url.replace(mediaInfo.getId() + "/", ""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return url.replace("default/", ""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public ServletRegistrationBean recordServletRegistrationBean(){ | 
|---|
|  |  |  | ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new RecordProxySerlet(),"/record_proxy/*"); | 
|---|
|  |  |  | ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new RecordProxyServlet(),"/record_proxy/*"); | 
|---|
|  |  |  | servletRegistrationBean.setName("record_proxy"); | 
|---|
|  |  |  | servletRegistrationBean.addInitParameter("targetUri", "http://127.0.0.1:18081"); | 
|---|
|  |  |  | servletRegistrationBean.addUrlMappings(); | 
|---|
|  |  |  | 
|---|
|  |  |  | return servletRegistrationBean; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | class RecordProxySerlet extends ProxyServlet{ | 
|---|
|  |  |  | class RecordProxyServlet extends ProxyServlet{ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | protected String rewriteQueryStringFromRequest(HttpServletRequest servletRequest, String queryString) { | 
|---|
|  |  |  | String queryStr = super.rewriteQueryStringFromRequest(servletRequest, queryString); | 
|---|
|  |  |  | MediaServerItem mediaInfo = getMediaInfoByUri(servletRequest.getRequestURI()); | 
|---|
|  |  |  | String remoteHost = String.format("http://%s:%s", mediaInfo.getIp(), mediaInfo.getHttpPort()); | 
|---|
|  |  |  | if (mediaInfo != null) { | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(queryStr)) { | 
|---|
|  |  |  | queryStr += "&remoteHost=" + remoteHost; | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | queryStr = "remoteHost=" + remoteHost; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return queryStr; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 异常处理 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | protected void handleRequestException(HttpRequest proxyRequest, HttpResponse proxyResonse, Exception e){ | 
|---|
|  |  |  | protected void handleRequestException(HttpRequest proxyRequest, HttpResponse proxyResponse, Exception e){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | super.handleRequestException(proxyRequest, proxyResonse, e); | 
|---|
|  |  |  | super.handleRequestException(proxyRequest, proxyResponse, e); | 
|---|
|  |  |  | } catch (ServletException servletException) { | 
|---|
|  |  |  | logger.error("录像服务 代理失败: ", e); | 
|---|
|  |  |  | } catch (IOException ioException) { | 
|---|
|  |  |  | 
|---|
|  |  |  | MediaServerItem getMediaInfoByUri(String uri){ | 
|---|
|  |  |  | String[] split = uri.split("/"); | 
|---|
|  |  |  | String mediaServerId = split[2]; | 
|---|
|  |  |  | return mediaServerService.getOne(mediaServerId); | 
|---|
|  |  |  | if ("default".equalsIgnoreCase(mediaServerId)) { | 
|---|
|  |  |  | return mediaServerService.getDefaultMediaServer(); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | return mediaServerService.getOne(mediaServerId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | MediaServerItem mediaInfo = getMediaInfoByUri(requestURI); | 
|---|
|  |  |  | String url = super.rewriteUrlFromRequest(servletRequest); | 
|---|
|  |  |  | if (mediaInfo == null) { | 
|---|
|  |  |  | logger.error("[录像服务访问代理],错误:处理url信息时未找到流媒体信息=>{}", requestURI); | 
|---|
|  |  |  | return  url; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return url.replace(mediaInfo.getId() + "/", ""); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(mediaInfo.getId())) { | 
|---|
|  |  |  | url = url.replace(mediaInfo.getId() + "/", ""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return url.replace("default/", ""); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|