From 78fac69cd569ede0ba5bf900f0df4f65c9229400 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Thu, 19 May 2022 17:00:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81sdp=20ip=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=B8=BA=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../genersoft/iot/vmp/conf/MediaConfig.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java index 85f46849..c24e0cad 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java @@ -6,6 +6,10 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.util.StringUtils; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.regex.Pattern; + @Configuration("mediaConfig") public class MediaConfig{ @@ -161,7 +165,18 @@ public class MediaConfig{ if (StringUtils.isEmpty(sdpIp)){ return ip; }else { - return sdpIp; + if (isValidIPAddress(sdpIp)) { + return sdpIp; + }else { + // 按照域名解析 + String hostAddress = null; + try { + hostAddress = InetAddress.getByName(sdpIp).getHostAddress(); + } catch (UnknownHostException e) { + throw new RuntimeException(e); + } + return hostAddress; + } } } @@ -211,4 +226,11 @@ public class MediaConfig{ return mediaServerItem; } + private boolean isValidIPAddress(String ipAddress) { + if ((ipAddress != null) && (!ipAddress.isEmpty())) { + return Pattern.matches("^([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$", ipAddress); + } + return false; + } + }