summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/Msdk/HttpDns.cs
blob: 2d450c4b2942d81b0ac7a200fcc4aeabd6c9d763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using XUtliPoolLib;

namespace com.tencent.httpdns
{
    public class HttpDns
    {
#if UNITY_ANDROID && !UNITY_EDITOR
    	private static AndroidJavaObject m_dnsJo;
		private static AndroidJavaClass sGSDKPlatformClass;
        private static bool inited = false;

		// 初始化HttpDns
		public static void Init()
		{
			AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
			if (jc == null)
			{
				return;
			}
			
			AndroidJavaObject joactivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
			if (joactivity == null)
			{
				return;
			}

			AndroidJavaObject context = joactivity.Call<AndroidJavaObject>("getApplicationContext");

			AndroidJavaObject joDnsClass = new AndroidJavaObject("com.tencent.msdk.dns.MSDKDnsResolver");
			XDebug.singleton.AddLog("WGGetHostByName ========== " + joDnsClass);

			if (joDnsClass == null)
			{
				return;
			}

			m_dnsJo = joDnsClass.CallStatic<AndroidJavaObject>("getInstance");
			XDebug.singleton.AddLog("WGGetHostByName ========== " + m_dnsJo);

			if (m_dnsJo == null)
			{
				return;
			}

			m_dnsJo.Call("init", context);

            inited = true;
        }
#endif

#if UNITY_IOS && !UNITY_EDITOR
        [DllImport("__Internal")]
		private static extern string WGGetHostByName(string url);

		[DllImport("__Internal")]
		private static extern void WGGetHostByNameAsync(string url);
#endif

        // 解析域名
		public static string GetHostByName(string url)
		{
			string originalUrl = url;
			if (originalUrl == null || originalUrl.Equals(""))
			{
				return null;
			}
			XDebug.singleton.AddLog("originalUrl: " + originalUrl);

			bool isUrl = true;
			if (!originalUrl.Contains("://"))
			{
				isUrl = false;

				// 临时协议头,返回解析结果时注意去除
				originalUrl = "http://" + originalUrl;
			}

			Uri uri = new Uri(originalUrl);
			string domain = uri.Host;
			XDebug.singleton.AddLog("originalDomain: " + domain);

			

			
			string convertedUrl = originalUrl;

#if UNITY_EDITOR
            // Nothing to do
#endif

#if UNITY_IOS && !UNITY_EDITOR && HTTP_DNS
            string ips = string.Empty;
            string convertedDomain = domain;
			ips = WGGetHostByName(domain);
			XDebug.singleton.AddLog("convertedDomainArray: " + ips);

			if (ips != null && !ips.Equals(""))
			{
				// 注意,务必去除尾部的换行符
				ips = ips.TrimEnd((char[])"\n\r".ToCharArray());

				string[] ipArray = ips.Split(new char[] {';'});
				if (ipArray != null && ipArray.Length > 1)
				{
					Debug.Log("ipv4: " + ipArray[0] + " && " + "ipv6: " + ipArray[1]);

					if (!ipArray[1].Equals("0"))
					{
						// ipv6地址需加方框号[]进行处理
						convertedDomain = "[" + ipArray[1] + "]";
					}
					else
					{
						convertedDomain = ipArray[0];
					}
				}
			}
			XDebug.singleton.AddLog("convertedDomain: " + convertedDomain);

			convertedUrl = originalUrl.Replace(domain, convertedDomain);
#endif

#if UNITY_ANDROID && !UNITY_EDITOR && HTTP_DNS
            if (!inited) Init();
            if (!inited) return url;
            string ips = string.Empty;
            string convertedDomain = domain;
            ips = m_dnsJo.Call<string>("getAddrByName", domain);
			XDebug.singleton.AddLog("convertedDomainArray: " + ips);

			if (ips != null && !ips.Equals(""))
			{
				ips = ips.TrimEnd((char[])"\n\r".ToCharArray());

				string[] ipArray = ips.Split(new char[] {';'});
				convertedDomain = ipArray[0];
			}
			XDebug.singleton.AddLog("convertedDomain: " + convertedDomain);

			convertedUrl = originalUrl.Replace(domain, convertedDomain);
#endif

            if (!isUrl)
			{
				// 去除临时添加的协议头
				convertedUrl = convertedUrl.Replace("http://", "");
			}
			XDebug.singleton.AddLog("convertedUrl: " + convertedUrl);

			return convertedUrl;
        }
	}
}