From 151699137259a9a92463b36795a5f935c62349c6 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期二, 06 十二月 2022 17:12:12 +0800
Subject: [PATCH] 回滚via获取地址的方式

---
 src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
index 26c8e4e..56fdb5b 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
@@ -1,9 +1,11 @@
 package com.genersoft.iot.vmp.gb28181.utils;
 
+import com.genersoft.iot.vmp.gb28181.bean.RemoteAddressInfo;
 import com.genersoft.iot.vmp.utils.GitUtil;
 import gov.nist.javax.sip.address.AddressImpl;
 import gov.nist.javax.sip.address.SipUri;
 import gov.nist.javax.sip.header.Subject;
+import gov.nist.javax.sip.message.SIPRequest;
 import org.springframework.util.ObjectUtils;
 
 import javax.sip.PeerUnavailableException;
@@ -11,6 +13,7 @@
 import javax.sip.header.FromHeader;
 import javax.sip.header.Header;
 import javax.sip.header.UserAgentHeader;
+import javax.sip.header.ViaHeader;
 import javax.sip.message.Request;
 import java.text.ParseException;
 import java.util.ArrayList;
@@ -119,4 +122,32 @@
         return builder.toString();
     }
 
+    /**
+     * 浠庤姹備腑鑾峰彇璁惧ip鍦板潃鍜岀鍙e彿
+     * @param request 璇锋眰
+     * @param sipUseSourceIpAsRemoteAddress  false 浠巚ia涓幏鍙栧湴鍧�锛� true 鐩存帴鑾峰彇杩滅▼鍦板潃
+     * @return 鍦板潃淇℃伅
+     */
+    public static RemoteAddressInfo getRemoteAddressFromRequest(SIPRequest request, boolean sipUseSourceIpAsRemoteAddress) {
+
+        String remoteAddress;
+        int remotePort;
+        if (sipUseSourceIpAsRemoteAddress) {
+            remoteAddress = request.getRemoteAddress().getHostAddress();
+            remotePort = request.getRemotePort();
+        }else {
+            // 鍒ゆ柇RPort鏄惁鏀瑰彉锛屾敼鍙樺垯璇存槑璺敱nat淇℃伅鍙樺寲锛屼慨鏀硅澶囦俊鎭�
+            // 鑾峰彇鍒伴�氫俊鍦板潃绛変俊鎭�
+            ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
+            remoteAddress = viaHeader.getReceived();
+            remotePort = viaHeader.getRPort();
+            // 瑙f瀽鏈湴鍦板潃鏇夸唬
+            if (ObjectUtils.isEmpty(remoteAddress) || remotePort == -1) {
+                remoteAddress = viaHeader.getHost();
+                remotePort = viaHeader.getPort();
+            }
+        }
+
+        return new RemoteAddressInfo(remoteAddress, remotePort);
+    }
 }

--
Gitblit v1.8.0