com.taptap.sdk.core 4.3.12 → 4.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Mobile/Editor/NativeDependencies.xml +2 -2
- package/Resources/TapMessage.prefab +352 -0
- package/Resources/TapMessage.prefab.meta +10 -0
- package/Resources/TapSDKUIRoot.prefab +1 -1
- package/Resources/tap_toast_background.png +0 -0
- package/Resources/tap_toast_background.png.meta +128 -0
- package/Resources/tap_toast_background1.png +0 -0
- package/Resources/tap_toast_background1.png.meta +128 -0
- package/Runtime/Internal/Log/TapLog.cs +1 -1
- package/Runtime/Internal/Platform/PlatformTypeUtils.cs +5 -5
- package/Runtime/Internal/Utils/TapMessage.cs +69 -0
- package/Runtime/Internal/Utils/TapMessage.cs.meta +13 -0
- package/Runtime/Public/TapTapSDK.cs +1 -1
- package/Standalone/Runtime/Internal/Bean/TapGatekeeper.cs +42 -0
- package/Standalone/Runtime/Internal/{Http/TimeUtil.cs.meta → Bean/TapGatekeeper.cs.meta} +1 -1
- package/Standalone/Runtime/Internal/Bean.meta +8 -0
- package/Standalone/Runtime/Internal/Constants.cs +2 -2
- package/Standalone/Runtime/Internal/EventSender.cs +30 -19
- package/Standalone/Runtime/Internal/Http/TapHttp.cs +377 -0
- package/Standalone/Runtime/Internal/Http/{HttpClient.cs.meta → TapHttp.cs.meta} +1 -1
- package/Standalone/Runtime/Internal/Http/TapHttpBuilder.cs +92 -0
- package/Standalone/Runtime/Internal/Http/{NetUtils.cs.meta → TapHttpBuilder.cs.meta} +1 -1
- package/Standalone/Runtime/Internal/Http/TapHttpErrorConstants.cs +114 -0
- package/Standalone/Runtime/Internal/Http/TapHttpErrorConstants.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpException.cs +71 -0
- package/Standalone/Runtime/Internal/Http/TapHttpException.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpParser.cs +132 -0
- package/Standalone/Runtime/Internal/Http/TapHttpParser.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpResponse.cs +37 -0
- package/Standalone/Runtime/Internal/Http/TapHttpResponse.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpResult.cs +90 -0
- package/Standalone/Runtime/Internal/Http/TapHttpResult.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpRetryStrategy.cs +237 -0
- package/Standalone/Runtime/Internal/Http/TapHttpRetryStrategy.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpSign.cs +137 -0
- package/Standalone/Runtime/Internal/Http/TapHttpSign.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Http/TapHttpUtils.cs +166 -0
- package/Standalone/Runtime/Internal/Http/TapHttpUtils.cs.meta +11 -0
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogHttpClient.cs +6 -17
- package/Standalone/Runtime/Internal/Openlog/TapOpenlogQueueBase.cs +1 -1
- package/Standalone/Runtime/Internal/service/ITapLoginService.cs +9 -0
- package/Standalone/Runtime/Internal/service/ITapLoginService.cs.meta +11 -0
- package/Standalone/Runtime/Internal/service.meta +8 -0
- package/Standalone/Runtime/Public/TapCoreStandalone.cs +67 -81
- package/link.xml +4 -0
- package/link.xml.meta +7 -0
- package/package.json +1 -1
- package/Standalone/Runtime/Internal/Http/HttpClient.cs +0 -211
- package/Standalone/Runtime/Internal/Http/NetUtils.cs +0 -48
- package/Standalone/Runtime/Internal/Http/TimeUtil.cs +0 -30
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Net;
|
|
3
|
+
using System.Threading;
|
|
4
|
+
|
|
5
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
6
|
+
{
|
|
7
|
+
/// <summary>
|
|
8
|
+
/// 重试策略接口。
|
|
9
|
+
/// </summary>
|
|
10
|
+
public interface ITapHttpRetryStrategy
|
|
11
|
+
{
|
|
12
|
+
/// <summary>
|
|
13
|
+
/// 获取下次重试的时间(毫秒)。
|
|
14
|
+
/// </summary>
|
|
15
|
+
/// <param name="errorType">错误类型。</param>
|
|
16
|
+
/// <param name="e">异常信息。</param>
|
|
17
|
+
/// <returns>下次重试的时间(毫秒),如果不重试返回 -1。</returns>
|
|
18
|
+
long NextRetryMillis(AbsTapHttpException e);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// <summary>
|
|
22
|
+
/// 后退策略接口。
|
|
23
|
+
/// </summary>
|
|
24
|
+
public interface ITapHttpBackoffStrategy
|
|
25
|
+
{
|
|
26
|
+
/// <summary>
|
|
27
|
+
/// 获取下一个后退的时间(毫秒)。
|
|
28
|
+
/// </summary>
|
|
29
|
+
/// <returns>下一个后退的时间(毫秒)。</returns>
|
|
30
|
+
long NextBackoffMillis();
|
|
31
|
+
|
|
32
|
+
/// <summary>
|
|
33
|
+
/// 判断是否可以重试无效时间。
|
|
34
|
+
/// </summary>
|
|
35
|
+
/// <returns>如果可以重试返回 true,否则返回 false。</returns>
|
|
36
|
+
bool CanInvalidTimeRetry();
|
|
37
|
+
|
|
38
|
+
/// <summary>
|
|
39
|
+
/// 重置策略状态。
|
|
40
|
+
/// </summary>
|
|
41
|
+
void Reset();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// <summary>
|
|
45
|
+
/// HTTP 重试策略实现。
|
|
46
|
+
/// </summary>
|
|
47
|
+
public class TapHttpRetryStrategy
|
|
48
|
+
{
|
|
49
|
+
/// <summary>
|
|
50
|
+
/// 创建默认重试策略。
|
|
51
|
+
/// </summary>
|
|
52
|
+
/// <param name="backoffStrategy">后退策略。</param>
|
|
53
|
+
/// <returns>默认重试策略。</returns>
|
|
54
|
+
public static ITapHttpRetryStrategy CreateDefault(ITapHttpBackoffStrategy backoffStrategy)
|
|
55
|
+
{
|
|
56
|
+
return new Default(backoffStrategy);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// <summary>
|
|
60
|
+
/// 创建不重试策略。
|
|
61
|
+
/// </summary>
|
|
62
|
+
/// <returns>不重试策略。</returns>
|
|
63
|
+
public static ITapHttpRetryStrategy CreateNone()
|
|
64
|
+
{
|
|
65
|
+
return new None();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private class None : ITapHttpRetryStrategy
|
|
69
|
+
{
|
|
70
|
+
public long NextRetryMillis(AbsTapHttpException e)
|
|
71
|
+
{
|
|
72
|
+
// 不重试返回 -1
|
|
73
|
+
return -1L;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private class Default : ITapHttpRetryStrategy
|
|
78
|
+
{
|
|
79
|
+
private readonly ITapHttpBackoffStrategy backoffStrategy;
|
|
80
|
+
|
|
81
|
+
public Default(ITapHttpBackoffStrategy backoffStrategy)
|
|
82
|
+
{
|
|
83
|
+
this.backoffStrategy = backoffStrategy;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public long NextRetryMillis(AbsTapHttpException e)
|
|
87
|
+
{
|
|
88
|
+
long nextRetryMillis = -1L;
|
|
89
|
+
if (e is TapHttpServerException se)
|
|
90
|
+
{
|
|
91
|
+
// 处理服务器错误状态码
|
|
92
|
+
if (se.StatusCode >= HttpStatusCode.InternalServerError && se.StatusCode <= (HttpStatusCode)599)
|
|
93
|
+
{
|
|
94
|
+
nextRetryMillis = backoffStrategy.NextBackoffMillis();
|
|
95
|
+
}
|
|
96
|
+
else if (TapHttpErrorConstants.ERROR_INVALID_TIME.Equals(se.ErrorData.Error))
|
|
97
|
+
{
|
|
98
|
+
// 修复时间并判断是否可以重试
|
|
99
|
+
TapHttpTime.FixTime(se.TapHttpResponse.Now);
|
|
100
|
+
if (backoffStrategy.CanInvalidTimeRetry())
|
|
101
|
+
{
|
|
102
|
+
nextRetryMillis = 0L; // 立马重试
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else if (TapHttpErrorConstants.ERROR_SERVER_ERROR.Equals(se.ErrorData.Error))
|
|
106
|
+
{
|
|
107
|
+
nextRetryMillis = backoffStrategy.NextBackoffMillis();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else if (e is TapHttpInvalidResponseException ie)
|
|
111
|
+
{
|
|
112
|
+
if (ie.StatusCode >= HttpStatusCode.InternalServerError && ie.StatusCode <= (HttpStatusCode)599)
|
|
113
|
+
{
|
|
114
|
+
nextRetryMillis = backoffStrategy.NextBackoffMillis();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return nextRetryMillis;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// <summary>
|
|
123
|
+
/// HTTP 后退策略实现。
|
|
124
|
+
/// </summary>
|
|
125
|
+
public class TapHttpBackoffStrategy
|
|
126
|
+
{
|
|
127
|
+
/// <summary>
|
|
128
|
+
/// 创建固定后退策略。
|
|
129
|
+
/// </summary>
|
|
130
|
+
/// <param name="maxCount">最大重试次数。</param>
|
|
131
|
+
/// <returns>固定后退策略。</returns>
|
|
132
|
+
public static ITapHttpBackoffStrategy CreateFixed(int maxCount)
|
|
133
|
+
{
|
|
134
|
+
return new Fixed(maxCount);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// <summary>
|
|
138
|
+
/// 创建指数后退策略。
|
|
139
|
+
/// </summary>
|
|
140
|
+
/// <returns>指数后退策略。</returns>
|
|
141
|
+
public static ITapHttpBackoffStrategy CreateExponential()
|
|
142
|
+
{
|
|
143
|
+
return new Exponential();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/// <summary>
|
|
147
|
+
/// 创建不后退策略。
|
|
148
|
+
/// </summary>
|
|
149
|
+
/// <returns>不后退策略。</returns>
|
|
150
|
+
public static ITapHttpBackoffStrategy CreateNone()
|
|
151
|
+
{
|
|
152
|
+
return new None();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private abstract class Base : ITapHttpBackoffStrategy
|
|
156
|
+
{
|
|
157
|
+
protected int CanTimeDeltaRetry = 1;
|
|
158
|
+
|
|
159
|
+
public abstract long NextBackoffMillis();
|
|
160
|
+
|
|
161
|
+
public abstract void Reset();
|
|
162
|
+
|
|
163
|
+
/// <summary>
|
|
164
|
+
/// 判断是否可以重试无效时间。
|
|
165
|
+
/// </summary>
|
|
166
|
+
/// <returns>如果可以重试返回 true,否则返回 false。</returns>
|
|
167
|
+
public bool CanInvalidTimeRetry()
|
|
168
|
+
{
|
|
169
|
+
return Interlocked.CompareExchange(ref CanTimeDeltaRetry, 0, 1) == 1;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private class Fixed : Base
|
|
174
|
+
{
|
|
175
|
+
private readonly int _maxCount;
|
|
176
|
+
private int CurrentCount = 0;
|
|
177
|
+
|
|
178
|
+
public Fixed(int maxCount)
|
|
179
|
+
{
|
|
180
|
+
_maxCount = maxCount;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public override long NextBackoffMillis()
|
|
184
|
+
{
|
|
185
|
+
if (++CurrentCount < _maxCount)
|
|
186
|
+
{
|
|
187
|
+
return 100L; // 固定的重试时间 100ms
|
|
188
|
+
}
|
|
189
|
+
return -1L; // 达到最大重试次数,返回 -1
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public override void Reset()
|
|
193
|
+
{
|
|
194
|
+
CurrentCount = 0;
|
|
195
|
+
Interlocked.Exchange(ref CanTimeDeltaRetry, 1);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private class Exponential : Base
|
|
200
|
+
{
|
|
201
|
+
private static readonly long INIT_INTERVAL_MILLIS = 2 * 1000L; // 初始时间 2 秒
|
|
202
|
+
private static readonly long MAX_INTERVAL_MILLIS = 600 * 1000L; // 最大时间 600 秒
|
|
203
|
+
private static readonly int MULTIPLIER = 2; // 指数倍数
|
|
204
|
+
|
|
205
|
+
private long CurrentIntervalMillis = INIT_INTERVAL_MILLIS;
|
|
206
|
+
|
|
207
|
+
public override long NextBackoffMillis()
|
|
208
|
+
{
|
|
209
|
+
if (CurrentIntervalMillis * MULTIPLIER > MAX_INTERVAL_MILLIS)
|
|
210
|
+
{
|
|
211
|
+
return MAX_INTERVAL_MILLIS; // 返回最大时间
|
|
212
|
+
}
|
|
213
|
+
CurrentIntervalMillis *= MULTIPLIER; // 增加当前时间
|
|
214
|
+
return CurrentIntervalMillis;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public override void Reset()
|
|
218
|
+
{
|
|
219
|
+
CurrentIntervalMillis = INIT_INTERVAL_MILLIS / MULTIPLIER; // 重置当前时间
|
|
220
|
+
Interlocked.Exchange(ref CanTimeDeltaRetry, 1);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private class None : Base
|
|
225
|
+
{
|
|
226
|
+
public override long NextBackoffMillis()
|
|
227
|
+
{
|
|
228
|
+
return -1L; // 不后退,返回 -1
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
public override void Reset()
|
|
232
|
+
{
|
|
233
|
+
Interlocked.Exchange(ref CanTimeDeltaRetry, 1);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Collections.Generic;
|
|
3
|
+
using System.Linq;
|
|
4
|
+
using System.Net.Http;
|
|
5
|
+
using System.Security.Cryptography;
|
|
6
|
+
using System.Text;
|
|
7
|
+
|
|
8
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
9
|
+
{
|
|
10
|
+
|
|
11
|
+
/// <summary>
|
|
12
|
+
/// 定义 HTTP 签名相关操作的接口。
|
|
13
|
+
/// </summary>
|
|
14
|
+
public interface ITapHttpSign
|
|
15
|
+
{
|
|
16
|
+
/// <summary>
|
|
17
|
+
/// 获取固定的 HTTP 请求头信息。
|
|
18
|
+
/// </summary>
|
|
19
|
+
/// <returns>返回包含固定请求头的字典。</returns>
|
|
20
|
+
Dictionary<string, string> GetFixedHeaders(string url, HttpMethod method, string moduleName, string moduleVersion, bool enableAuthorization);
|
|
21
|
+
|
|
22
|
+
/// <summary>
|
|
23
|
+
/// 获取固定的查询参数。
|
|
24
|
+
/// </summary>
|
|
25
|
+
/// <returns>返回包含固定查询参数的字典。</returns>
|
|
26
|
+
Dictionary<string, string> GetFixedQueryParams();
|
|
27
|
+
|
|
28
|
+
/// <summary>
|
|
29
|
+
/// 对 HTTP 请求数据进行签名处理。
|
|
30
|
+
/// </summary>
|
|
31
|
+
/// <param name="signData">包含请求数据的 <see cref="TapHttpSignData"/> 对象。</param>
|
|
32
|
+
void Sign(HttpRequestMessage signData);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public class TapHttpSign
|
|
36
|
+
{
|
|
37
|
+
public static ITapHttpSign CreateDefaultSign()
|
|
38
|
+
{
|
|
39
|
+
return new Default();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public static ITapHttpSign CreateNoneSign()
|
|
43
|
+
{
|
|
44
|
+
return new None();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private class Default : ITapHttpSign
|
|
48
|
+
{
|
|
49
|
+
public Dictionary<string, string> GetFixedHeaders(string url, HttpMethod method, string moduleName, string moduleVersion, bool enableAuthorization)
|
|
50
|
+
{
|
|
51
|
+
_ = method ?? throw new ArgumentNullException(nameof(method));
|
|
52
|
+
_ = moduleName ?? throw new ArgumentNullException(nameof(moduleName));
|
|
53
|
+
_ = moduleVersion ?? throw new ArgumentNullException(nameof(moduleVersion));
|
|
54
|
+
|
|
55
|
+
if (method == HttpMethod.Post || method == HttpMethod.Get)
|
|
56
|
+
{
|
|
57
|
+
Dictionary<string, string> headers = new Dictionary<string, string>
|
|
58
|
+
{
|
|
59
|
+
{ "X-Tap-PN", "TapSDK" },
|
|
60
|
+
{ "X-Tap-Device-Id", TapHttpUtils.GenerateDeviceId()},
|
|
61
|
+
{ "X-Tap-Platform", "PC"},
|
|
62
|
+
{ "X-Tap-SDK-Module", moduleName},
|
|
63
|
+
{ "X-Tap-SDK-Module-Version", moduleVersion},
|
|
64
|
+
{ "X-Tap-SDK-Artifact", "Unity"},
|
|
65
|
+
{ "X-Tap-Ts", TapHttpUtils.GenerateTime()},
|
|
66
|
+
{ "X-Tap-Nonce", TapHttpUtils.GenerateNonce()},
|
|
67
|
+
{ "X-Tap-Lang", TapHttpUtils.GenerateLanguage()},
|
|
68
|
+
{ "User-Agent", TapHttpUtils.GenerateUserAgent()},
|
|
69
|
+
};
|
|
70
|
+
if (enableAuthorization)
|
|
71
|
+
{
|
|
72
|
+
string authorization = TapHttpUtils.GenerateAuthorization(url, method.ToString());
|
|
73
|
+
if (authorization != null)
|
|
74
|
+
{
|
|
75
|
+
headers.Add("Authorization", authorization);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return headers;
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public Dictionary<string, string> GetFixedQueryParams()
|
|
84
|
+
{
|
|
85
|
+
return new Dictionary<string, string>
|
|
86
|
+
{
|
|
87
|
+
{ "client_id", TapCoreStandalone.coreOptions.clientId }
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public async void Sign(HttpRequestMessage requestMessage)
|
|
92
|
+
{
|
|
93
|
+
_ = requestMessage ?? throw new ArgumentNullException(nameof(requestMessage));
|
|
94
|
+
|
|
95
|
+
string clientToken = TapCoreStandalone.coreOptions.clientToken;
|
|
96
|
+
string methodPart = requestMessage.Method.Method;
|
|
97
|
+
string urlPathAndQueryPart = requestMessage.RequestUri.PathAndQuery;
|
|
98
|
+
var headerKeys = requestMessage.Headers
|
|
99
|
+
.Where(h => h.Key.StartsWith("x-tap-", StringComparison.OrdinalIgnoreCase))
|
|
100
|
+
.OrderBy(h => h.Key.ToLowerInvariant())
|
|
101
|
+
.Select(h => $"{h.Key.ToLowerInvariant()}:{string.Join(",", h.Value)}")
|
|
102
|
+
.ToList();
|
|
103
|
+
string headersPart = string.Join("\n", headerKeys);
|
|
104
|
+
string bodyPart = string.Empty;
|
|
105
|
+
if (requestMessage.Content != null)
|
|
106
|
+
{
|
|
107
|
+
bodyPart = await requestMessage.Content.ReadAsStringAsync();
|
|
108
|
+
}
|
|
109
|
+
string signParts = methodPart + "\n" + urlPathAndQueryPart + "\n" + headersPart + "\n" + bodyPart + "\n";
|
|
110
|
+
using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(clientToken)))
|
|
111
|
+
{
|
|
112
|
+
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(signParts));
|
|
113
|
+
string sign = Convert.ToBase64String(hash);
|
|
114
|
+
requestMessage.Headers.Add("X-Tap-Sign", sign);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private class None : ITapHttpSign
|
|
120
|
+
{
|
|
121
|
+
public Dictionary<string, string> GetFixedHeaders(string url, HttpMethod method, string moduleName, string moduleVersion, bool enableAuthorization)
|
|
122
|
+
{
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public Dictionary<string, string> GetFixedQueryParams()
|
|
127
|
+
{
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public void Sign(HttpRequestMessage signData)
|
|
132
|
+
{
|
|
133
|
+
// do nothing
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Linq;
|
|
3
|
+
using System.Net.Http;
|
|
4
|
+
using System.Text;
|
|
5
|
+
using TapSDK.Core.Internal.Log;
|
|
6
|
+
using TapSDK.Core.Standalone.Internal.Service;
|
|
7
|
+
using UnityEngine;
|
|
8
|
+
|
|
9
|
+
namespace TapSDK.Core.Standalone.Internal.Http
|
|
10
|
+
{
|
|
11
|
+
public static class TapHttpTime
|
|
12
|
+
{
|
|
13
|
+
private static int timeOffset = 0;
|
|
14
|
+
private static void SetTimeOffset(int offset)
|
|
15
|
+
{
|
|
16
|
+
timeOffset = offset;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 获取当前时间的秒级时间戳
|
|
20
|
+
public static int GetCurrentTime()
|
|
21
|
+
{
|
|
22
|
+
DateTime epochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
23
|
+
TimeSpan timeSpan = DateTime.UtcNow - epochStart;
|
|
24
|
+
return (int)timeSpan.TotalSeconds + timeOffset;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static void FixTime(int time)
|
|
28
|
+
{
|
|
29
|
+
if (time == 0)
|
|
30
|
+
{
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
SetTimeOffset(time - (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public static class TapHttpUtils
|
|
38
|
+
{
|
|
39
|
+
|
|
40
|
+
private static readonly TapLog tapLog = new TapLog("Http");
|
|
41
|
+
|
|
42
|
+
internal static string GenerateNonce()
|
|
43
|
+
{
|
|
44
|
+
string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
45
|
+
char[] nonce = new char[10];
|
|
46
|
+
for (int i = 0; i < 10; i++)
|
|
47
|
+
{
|
|
48
|
+
nonce[i] = chars[UnityEngine.Random.Range(0, chars.Length)];
|
|
49
|
+
}
|
|
50
|
+
return new string(nonce);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
internal static string GenerateUserAgent()
|
|
54
|
+
{
|
|
55
|
+
return $"TapSDK-Unity/{TapTapSDK.Version}";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
internal static string GenerateTime()
|
|
59
|
+
{
|
|
60
|
+
return TapHttpTime.GetCurrentTime().ToString();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
internal static string GenerateLanguage()
|
|
64
|
+
{
|
|
65
|
+
return Tracker.getServerLanguage();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
internal static string GenerateDeviceId()
|
|
69
|
+
{
|
|
70
|
+
return SystemInfo.deviceUniqueIdentifier;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
internal static string GenerateAuthorization(string url, string method)
|
|
74
|
+
{
|
|
75
|
+
Type interfaceType = typeof(ITapLoginService);
|
|
76
|
+
Type[] initTaskTypes = AppDomain.CurrentDomain.GetAssemblies()
|
|
77
|
+
.Where(asssembly =>
|
|
78
|
+
{
|
|
79
|
+
string fullName = asssembly.GetName().FullName;
|
|
80
|
+
return fullName.StartsWith("TapSDK.Login.Standalone.Runtime");
|
|
81
|
+
})
|
|
82
|
+
.SelectMany(assembly => assembly.GetTypes())
|
|
83
|
+
.Where(clazz =>
|
|
84
|
+
{
|
|
85
|
+
return interfaceType.IsAssignableFrom(clazz) && clazz.IsClass;
|
|
86
|
+
})
|
|
87
|
+
.ToArray();
|
|
88
|
+
if (initTaskTypes.Length != 1)
|
|
89
|
+
{
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
try
|
|
93
|
+
{
|
|
94
|
+
ITapLoginService tapLoginService = Activator.CreateInstance(initTaskTypes[0]) as ITapLoginService;
|
|
95
|
+
string authorization = tapLoginService.ObtainAuthorizationAsync(url, method);
|
|
96
|
+
return authorization;
|
|
97
|
+
}
|
|
98
|
+
catch (Exception e)
|
|
99
|
+
{
|
|
100
|
+
TapLog.Error("e = " + e);
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null)
|
|
106
|
+
{
|
|
107
|
+
if (TapLogger.LogDelegate == null)
|
|
108
|
+
{
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (client == null)
|
|
112
|
+
{
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (request == null)
|
|
116
|
+
{
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
StringBuilder sb = new StringBuilder();
|
|
120
|
+
sb.AppendLine("=== HTTP Request Start ===");
|
|
121
|
+
sb.AppendLine($"URL: {request.RequestUri}");
|
|
122
|
+
sb.AppendLine($"Method: {request.Method}");
|
|
123
|
+
sb.AppendLine($"Headers: ");
|
|
124
|
+
foreach (var header in client.DefaultRequestHeaders)
|
|
125
|
+
{
|
|
126
|
+
sb.AppendLine($"\t{header.Key}: {string.Join(",", header.Value.ToArray())}");
|
|
127
|
+
}
|
|
128
|
+
foreach (var header in request.Headers)
|
|
129
|
+
{
|
|
130
|
+
sb.AppendLine($"\t{header.Key}: {string.Join(",", header.Value.ToArray())}");
|
|
131
|
+
}
|
|
132
|
+
if (request.Content != null)
|
|
133
|
+
{
|
|
134
|
+
foreach (var header in request.Content.Headers)
|
|
135
|
+
{
|
|
136
|
+
sb.AppendLine($"\t{header.Key}: {string.Join(",", header.Value.ToArray())}");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (!string.IsNullOrEmpty(content))
|
|
140
|
+
{
|
|
141
|
+
sb.AppendLine($"Content: \n{content}");
|
|
142
|
+
}
|
|
143
|
+
sb.AppendLine("=== HTTP Request End ===");
|
|
144
|
+
tapLog.Log($"HTTP Request [{request.RequestUri.PathAndQuery}]", sb.ToString());
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public static void PrintResponse(HttpResponseMessage response, string content = null)
|
|
148
|
+
{
|
|
149
|
+
if (TapLogger.LogDelegate == null)
|
|
150
|
+
{
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
StringBuilder sb = new StringBuilder();
|
|
154
|
+
sb.AppendLine("=== HTTP Response Start ===");
|
|
155
|
+
sb.AppendLine($"URL: {response.RequestMessage.RequestUri}");
|
|
156
|
+
sb.AppendLine($"Status Code: {response.StatusCode}");
|
|
157
|
+
if (!string.IsNullOrEmpty(content))
|
|
158
|
+
{
|
|
159
|
+
sb.AppendLine($"Content: {content}");
|
|
160
|
+
}
|
|
161
|
+
sb.AppendLine("=== HTTP Response End ===");
|
|
162
|
+
tapLog.Log($"HTTP Response [{response.RequestMessage.RequestUri.PathAndQuery}]", sb.ToString());
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -15,10 +15,10 @@ namespace TapSDK.Core.Standalone.Internal
|
|
|
15
15
|
public class TapOpenlogHttpClient
|
|
16
16
|
{
|
|
17
17
|
|
|
18
|
-
private
|
|
19
|
-
private
|
|
18
|
+
private static readonly string HOST_CN = "openlog.tapapis.cn";
|
|
19
|
+
private static readonly string HOST_IO = "openlog.tapapis.com";
|
|
20
20
|
|
|
21
|
-
private string GetHost()
|
|
21
|
+
private static string GetHost()
|
|
22
22
|
{
|
|
23
23
|
if (TapCoreStandalone.coreOptions.region == TapTapRegionType.CN)
|
|
24
24
|
{
|
|
@@ -36,21 +36,11 @@ namespace TapSDK.Core.Standalone.Internal
|
|
|
36
36
|
|
|
37
37
|
private static TapLog log = new TapLog(module: "Openlog.HttpClient");
|
|
38
38
|
|
|
39
|
-
private static
|
|
39
|
+
private static HttpClient client;
|
|
40
40
|
|
|
41
41
|
public TapOpenlogHttpClient()
|
|
42
42
|
{
|
|
43
|
-
|
|
44
|
-
// ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
|
|
45
|
-
// {
|
|
46
|
-
// return true;
|
|
47
|
-
// };
|
|
48
|
-
// HttpClientHandler clientHandler = new HttpClientHandler
|
|
49
|
-
// {
|
|
50
|
-
// Proxy = new WebProxy(ip)
|
|
51
|
-
// };
|
|
52
|
-
// client = new System.Net.Http.HttpClient(clientHandler);
|
|
53
|
-
client = new System.Net.Http.HttpClient();
|
|
43
|
+
client = new HttpClient();
|
|
54
44
|
}
|
|
55
45
|
|
|
56
46
|
public async Task<bool> Post(string path, byte[] content)
|
|
@@ -67,7 +57,6 @@ namespace TapSDK.Core.Standalone.Internal
|
|
|
67
57
|
request.Headers.Add("x-log-bodyrawsize", content.Length.ToString());
|
|
68
58
|
|
|
69
59
|
byte[] compressContent = LZ4Compress(content);
|
|
70
|
-
// byte[] compressContent = content;
|
|
71
60
|
|
|
72
61
|
var contentMD5 = EncryptString(compressContent);
|
|
73
62
|
request.Headers.Add("x-content-md5", contentMD5);
|
|
@@ -154,7 +143,7 @@ namespace TapSDK.Core.Standalone.Internal
|
|
|
154
143
|
}
|
|
155
144
|
}
|
|
156
145
|
|
|
157
|
-
private string BuildUrl(string path)
|
|
146
|
+
private static string BuildUrl(string path)
|
|
158
147
|
{
|
|
159
148
|
string url = $"https://{GetHost()}/{path}?client_id={TapCoreStandalone.coreOptions.clientId}";
|
|
160
149
|
return url;
|