RNAutoUpdater.m
12.1 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// RNAutoUpdater.m
// YH_Vendor
//
// Created by 盖剑秋 on 16/6/2.
// Copyright © 2016年 Facebook. All rights reserved.
//
#import "RNAutoUpdater.h"
#import "RCTBridgeModule.h"
#import <CommonCrypto/CommonDigest.h>
#import "SSZipArchive.h"
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif
#define kYH_JSBundleVersion(appVersion) [NSString stringWithFormat:@"YH_JSBundleVersion_%@", appVersion]
#define kRNBundleURL @"http://m.yohobuy.com/rn/v1"
#define kFileMD5Key @"yohorn2016"
typedef void (^YH_UpdateCallback)(NSError *error);
typedef enum {
YH_UpdateErrorHTTPStatusFailed = -1001,
YH_UpdateErrorResponseDataFailed,
YH_UpdateErrorFetchFileFailed,
YH_UpdateErrorVerifyFailed,
YH_UpdateErrorUnzipFailed,
YH_UpdateErrorCannotFindJSBundle,
} YH_UpdateError;
@interface RNAutoUpdater()<RCTBridgeModule>
@end
@implementation RNAutoUpdater
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(checkUpdate:(NSDictionary *)options){
INSTALL_MODE mode = [[options objectForKey:@"installMode"] integerValue];
[[self class] checkUpdateWithCallback:^(NSError *error) {
}];
}
- (NSDictionary *)constantsToExport {
return @{
@"jsBundleVersion":[[self class] currentVersion],
@"INSTALL_MODE_IMMEDIATE":@(INSTALL_MODE_IMMEDIATE),
@"INSTALL_MODE_ON_NEXT_START":@(INSTALL_MODE_ON_NEXT_START),
@"INSTALL_MODE_ON_NEXT_RESUME":@(INSTALL_MODE_ON_NEXT_RESUME),
};
}
+ (void)checkUpdate
{
[self checkUpdateWithCallback:^(NSError *error) {
if (!error) {
}
}];
}
+ (NSURL *)currentJSBundleLocation
{
NSURL *bundleURL;
NSString *version = [self currentVersion];
if ([version isEqualToString:@""]) {
bundleURL = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
} else {
NSString *scriptDirectory = [self fetchScriptDirectory];
NSString *fileName = [self fileName:[self currentVersion]];
NSString *filePath = [scriptDirectory stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
bundleURL = [NSURL URLWithString:filePath];
} else {
bundleURL = nil;
}
}
return bundleURL;
}
+ (NSMutableURLRequest *)createCheckUpdateRequest
{
//create url request
NSURL *hotfixURL = [NSURL URLWithString:kRNBundleURL];
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:hotfixURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
mutableRequest.HTTPMethod = @"POST";
NSString *appVersion = [self containerVersion];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"app_version"] = appVersion;
parameters[@"client_type"] = @"iOS";
NSMutableArray *pairs = [NSMutableArray array];
for (NSString *key in [parameters allKeys]) {
NSString *value = parameters[key];
[pairs addObject:[NSString stringWithFormat:@"%@=%@", key, value]];
}
NSString *bodyString = [pairs componentsJoinedByString:@"&"];
[mutableRequest setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
return mutableRequest;
}
+ (void)checkUpdateWithCallback:(YH_UpdateCallback)callback
{
DLog(@"YH_JSBundle: checkUpdate");
DLog(@"YH_JSBundle: request file %@", kRNBundleURL);
NSMutableURLRequest *mutableRequest = [self createCheckUpdateRequest];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:mutableRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
DLog(@"YH_JSBundle: request RNBundleURL failure, error:%@", error);
if (callback) {
callback(error);
}
return;
}
//解析json
NSError *jsonError;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!result) {
DLog(@"YH_JSBundle: unrecongized json string, error:%@", jsonError);
if (callback) {
callback(jsonError);
}
return;
}
DLog(@"YH_JSBundle: request RNBundleURL success, response data:%@", result);
//接口数据处理
NSInteger code = [result[@"code"] integerValue];
if (code != 200) {
DLog(@"YH_JSBundle: response status error, code:%ld", code);
if (callback) {
callback([NSError errorWithDomain:@"com.yohobuy" code:YH_UpdateErrorHTTPStatusFailed userInfo:nil]);
}
return;
}
NSDictionary *bundleData = result[@"data"];
NSString *zipURLString = bundleData[@"url"];
NSString *bundleVersion = bundleData[@"rnv"];
NSString *fileMD5 = bundleData[@"filecode"];
NSString *minContainerVersion = bundleData[@"minv"];
//获取脚本
[self fetchPatchFile:zipURLString version:bundleVersion md5:fileMD5 minContainerVersion:minContainerVersion callback:callback];
}];
[task resume];
}
+ (void)fetchPatchFile:(NSString *)fileURLString version:(NSString *)version md5:(NSString *)fileMD5 minContainerVersion:(NSString *)minContainerVersion callback:(YH_UpdateCallback)callback
{
if ([fileURLString isEqualToString:@""] || [version isEqualToString:@""] || [fileMD5 isEqualToString:@""]) {
DLog(@"YH_JSBundle: fetch js bundle file fail, url:%@, version:%@, md5:%@", fileURLString, version, fileMD5);
if (callback) {
callback([NSError errorWithDomain:@"com.yohobuy" code:YH_UpdateErrorResponseDataFailed userInfo:nil]);
}
return;
}
BOOL shouldDownload = [self shouldDownloadUpdateJSBundle:version forMinContainerVersion:minContainerVersion];
if (!shouldDownload) {
DLog(@"YH_JSBundle: js bundle file already exist");
return;
}
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fileURLString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
DLog(@"YH_JSBundle: fetch patch file fail, error:%@", error);
if (callback) {
callback([NSError errorWithDomain:@"com.yohobuy" code:YH_UpdateErrorFetchFileFailed userInfo:nil]);
}
return;
}
NSString *appVersion = [self containerVersion];
//脚本临时存放路径
NSString *downloadTmpPath = [NSString stringWithFormat:@"%@jsbundle_%@_%@.zip", NSTemporaryDirectory(), appVersion, version];
NSString *unzipTmpDirectory = [NSString stringWithFormat:@"%@jsbundle_%@_%@_unzip/", NSTemporaryDirectory(), appVersion, version];
[data writeToFile:downloadTmpPath atomically:YES];
//验证文件签名
BOOL pass = [self verifyFileSign:downloadTmpPath realSign:fileMD5];
if (!pass) {
DLog(@"YH_JSBundle: file is broken");
//清除临时文件
[[NSFileManager defaultManager] removeItemAtPath:downloadTmpPath error:nil];
if (callback) {
callback([NSError errorWithDomain:@"com.yohobuy" code:YH_UpdateErrorVerifyFailed userInfo:nil]);
}
return;
}
//解压zip
BOOL success = [SSZipArchive unzipFileAtPath:downloadTmpPath toDestination:unzipTmpDirectory];
if (!success) {
DLog(@"YH_JSBundle: unzip file error");
[[NSFileManager defaultManager] removeItemAtPath:unzipTmpDirectory error:nil];
if (callback) {
callback([NSError errorWithDomain:@"com.yohobuy" code:YH_UpdateErrorUnzipFailed userInfo:nil]);
}
return;
}
//判断zip中是否存在 main.jsbundle
NSString *bundleTmpPath = [unzipTmpDirectory stringByAppendingPathComponent:@"main.jsbundle"];
if (![[NSFileManager defaultManager] fileExistsAtPath:bundleTmpPath]) {
DLog(@"YH_JSBundle: can not find js bundle in zip");
[[NSFileManager defaultManager] removeItemAtPath:unzipTmpDirectory error:nil];
if (callback) {
callback([NSError errorWithDomain:@"com.yohobuy" code:YH_UpdateErrorCannotFindJSBundle userInfo:nil]);
}
return;
}
//存储js bundle
NSString *scriptDirectory = [self fetchScriptDirectory];
[[NSFileManager defaultManager] createDirectoryAtPath:scriptDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *fileName = [self fileName:version];
NSString *newFilePath = [scriptDirectory stringByAppendingPathComponent:fileName];
[[NSData dataWithContentsOfFile:bundleTmpPath] writeToFile:newFilePath atomically:YES];
DLog(@"YH_JSBundle: fetch patch file success, path:%@", newFilePath);
//存储最新patch版本号
[[NSUserDefaults standardUserDefaults] setObject:version forKey:kYH_JSBundleVersion(appVersion)];
[[NSUserDefaults standardUserDefaults] synchronize];
if (callback) {
callback(nil);
}
//清除临时文件和目录
[[NSFileManager defaultManager] removeItemAtPath:downloadTmpPath error:nil];
[[NSFileManager defaultManager] removeItemAtPath:unzipTmpDirectory error:nil];
}];
[task resume];
}
+ (BOOL)shouldDownloadUpdateJSBundle:(NSString *)bundleVersion forMinContainerVersion:(NSString *)minContainerVersion
{
NSString *containerVersion = [self containerVersion];
//应用版本号 < bundle要求的最低版本号,不下载更新
if ([containerVersion compare:minContainerVersion options:NSNumericSearch] == NSOrderedAscending) {
return NO;
}
//本地bundle版本号 == 最新bundle版本号,不下载更新
if ([[self currentVersion] isEqualToString:bundleVersion]) {
return NO;
}
return YES;
}
+ (BOOL)verifyFileSign:(NSString *)filePath realSign:(NSString *)realSign
{
NSString *fileSign = [self fileMD5:filePath];
NSString *joinString = [NSString stringWithFormat:@"%@%@", fileSign, kFileMD5Key];
NSString *actualSign = [self stringMD5:joinString];
if ([actualSign isEqualToString:realSign]) {
return YES;
} else {
return NO;
}
}
+ (NSString *)fileName:(NSString *)version
{
NSString *name = [NSString stringWithFormat:@"main_%@.jsbundle", version];
return name;
}
+ (NSString *)containerVersion
{
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}
+ (NSString *)currentVersion
{
NSString *appVersion = [self containerVersion];
NSString *version = [[NSUserDefaults standardUserDefaults] stringForKey:kYH_JSBundleVersion(appVersion)];
return version ?: @"";
}
+ (NSString *)fetchScriptDirectory
{
NSString *appVersion = [self containerVersion];
NSString *libraryDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
NSString *scriptDirectory = [libraryDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"YH_JSBundle/%@/", appVersion]];
return scriptDirectory;
}
#pragma mark utils
+ (NSString *)fileMD5:(NSString *)filePath
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
if(!handle)
{
return nil;
}
CC_MD5_CTX md5;
CC_MD5_Init(&md5);
BOOL done = NO;
while (!done)
{
NSData *fileData = [handle readDataOfLength:256];
CC_MD5_Update(&md5, [fileData bytes], (CC_LONG)[fileData length]);
if([fileData length] == 0)
done = YES;
}
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &md5);
NSString *result = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[0], digest[1],
digest[2], digest[3],
digest[4], digest[5],
digest[6], digest[7],
digest[8], digest[9],
digest[10], digest[11],
digest[12], digest[13],
digest[14], digest[15]];
return result;
}
+ (NSString *)stringMD5:(NSString *)rawString
{
const char *input = [rawString UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(input, (CC_LONG)strlen(input), result);
NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[digest appendFormat:@"%02x", result[i]];
}
return digest;
}
@end