| | |
| | | |
| | | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; |
| | | import com.genersoft.iot.vmp.service.IMediaServerService; |
| | | import org.apache.catalina.connector.ClientAbortException; |
| | | import org.apache.http.HttpHost; |
| | | import org.apache.http.HttpRequest; |
| | | import org.apache.http.HttpResponse; |
| | | import org.junit.jupiter.api.Order; |
| | | import org.mitre.dsmiley.httpproxy.ProxyServlet; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | import org.springframework.boot.web.servlet.ServletRegistrationBean; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.net.ConnectException; |
| | | |
| | |
| | | */ |
| | | @SuppressWarnings(value = {"rawtypes", "unchecked"}) |
| | | @Configuration |
| | | @Order(1) |
| | | public class ProxyServletConfig { |
| | | |
| | | private final static Logger logger = LoggerFactory.getLogger(ProxyServletConfig.class); |
| | |
| | | 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(); |
| | |
| | | } catch (IOException ioException) { |
| | | if (ioException instanceof ConnectException) { |
| | | logger.error("zlm 连接失败"); |
| | | } else if (ioException instanceof ClientAbortException) { |
| | | logger.error("zlm: 用户已中断连接,代理终止"); |
| | | } else { |
| | | } else { |
| | | logger.error("zlm 代理失败: ", e); |
| | | } |
| | | } catch (RuntimeException exception){ |
| | |
| | | MediaServerItem getMediaInfoByUri(String uri){ |
| | | String[] split = uri.split("/"); |
| | | String mediaServerId = split[2]; |
| | | if ("default".equals(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() + "/", "").replace("default/", ""); |
| | | if (!ObjectUtils.isEmpty(mediaInfo.getId())) { |
| | | url = url.replace(mediaInfo.getId() + "/", ""); |
| | | } |
| | | return url.replace("default/", ""); |
| | | } |
| | | } |
| | | |
| | |
| | | MediaServerItem mediaInfo = getMediaInfoByUri(servletRequest.getRequestURI()); |
| | | String remoteHost = String.format("http://%s:%s", mediaInfo.getIp(), mediaInfo.getHttpPort()); |
| | | if (mediaInfo != null) { |
| | | if (!StringUtils.isEmpty(queryStr)) { |
| | | if (!ObjectUtils.isEmpty(queryStr)) { |
| | | queryStr += "&remoteHost=" + remoteHost; |
| | | }else { |
| | | queryStr = "remoteHost=" + remoteHost; |
| | |
| | | } catch (IOException ioException) { |
| | | if (ioException instanceof ConnectException) { |
| | | logger.error("录像服务 连接失败"); |
| | | } else if (ioException instanceof ClientAbortException) { |
| | | logger.error("录像服务:用户已中断连接,代理终止"); |
| | | } else { |
| | | }else { |
| | | logger.error("录像服务 代理失败: ", e); |
| | | } |
| | | } catch (RuntimeException exception){ |
| | |
| | | MediaServerItem getMediaInfoByUri(String uri){ |
| | | String[] split = uri.split("/"); |
| | | String mediaServerId = split[2]; |
| | | if ("default".equals(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() + "/", "").replace("default/", ""); |
| | | if (!ObjectUtils.isEmpty(mediaInfo.getId())) { |
| | | url = url.replace(mediaInfo.getId() + "/", ""); |
| | | } |
| | | return url.replace("default/", ""); |
| | | } |
| | | } |
| | | |