IPv6SupportMidleware.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Text;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7. using System.Text.RegularExpressions;
  8. public class IPv6SupportMidleware
  9. {
  10. #if UNITY_IOS && !UNITY_EDITOR
  11. [DllImport("__Internal")]
  12. private static extern string getIPv6(string mHost);
  13. #endif
  14. private static string GetIPv6 (string host) {
  15. #if UNITY_IOS && !UNITY_EDITOR
  16. return getIPv6 (host);
  17. #else
  18. return host + "&&ipv4";
  19. #endif
  20. }
  21. // Get IP type and synthesize IPv6, if needed, for iOS
  22. private static void GetIPType (string serverIp, out String newServerIp, out AddressFamily IPType) {
  23. IPType = AddressFamily.InterNetwork;
  24. newServerIp = serverIp;
  25. try {
  26. string IPv6 = GetIPv6 (serverIp);
  27. if (!string.IsNullOrEmpty (IPv6)) {
  28. string[] tmp = Regex.Split (IPv6, "&&");
  29. if (tmp != null && tmp.Length >= 2) {
  30. string type = tmp[1];
  31. if (type == "ipv6") {
  32. newServerIp = tmp[0];
  33. IPType = AddressFamily.InterNetworkV6;
  34. }
  35. }
  36. }
  37. } catch (Exception e) {
  38. Debug.LogErrorFormat ("GetIPv6 error: {0}", e.Message);
  39. }
  40. }
  41. // Get IP address by AddressFamily and domain
  42. private static string GetIPAddress (string hostName, AddressFamily addressFamily) {
  43. if (addressFamily == AddressFamily.InterNetworkV6 && !System.Net.Sockets.Socket.OSSupportsIPv6)
  44. return null;
  45. if (string.IsNullOrEmpty (hostName))
  46. return null;
  47. System.Net.IPHostEntry host;
  48. string connectIP = "";
  49. try {
  50. host = System.Net.Dns.GetHostEntry (hostName);
  51. foreach (System.Net.IPAddress ip in host.AddressList) {
  52. if (ip.AddressFamily == addressFamily) {
  53. connectIP = ip.ToString ();
  54. }
  55. }
  56. } catch (Exception e) {
  57. Debug.LogErrorFormat ("GetIPAddress error: {0}", e.Message);
  58. }
  59. return connectIP;
  60. }
  61. // Check IP or not
  62. private static bool IsIPAddress (string data) {
  63. Match match = Regex.Match (data, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
  64. if (match == null)
  65. {
  66. return false;
  67. }
  68. return match.Success;
  69. }
  70. public static bool GetValidServerIpInfo(string hostNameOrIp, out String newServerIp, out AddressFamily IPType)
  71. {
  72. if (IsIPAddress(hostNameOrIp)) {
  73. GetIPType(hostNameOrIp, out newServerIp, out IPType);
  74. }
  75. else
  76. {
  77. newServerIp = GetIPAddress(hostNameOrIp, AddressFamily.InterNetworkV6);
  78. if (string.IsNullOrEmpty(newServerIp))
  79. {
  80. newServerIp = GetIPAddress(hostNameOrIp, AddressFamily.InterNetwork);
  81. IPType = AddressFamily.InterNetwork;
  82. }
  83. else
  84. {
  85. IPType = AddressFamily.InterNetworkV6;
  86. }
  87. }
  88. if (string.IsNullOrEmpty(newServerIp))
  89. {
  90. return false;
  91. }
  92. return true;
  93. }
  94. }