77#import " TIoTCoreXP2PBridge.h"
88#import " TIoTCoreLogger.h"
99#include < string.h>
10+ #include < CommonCrypto/CommonDigest.h>
11+ #include < CommonCrypto/CommonHMAC.h>
1012
1113NSNotificationName const TIoTCoreXP2PBridgeNotificationDisconnect = @" xp2disconnect" ; // p2p通道断开
1214NSNotificationName const TIoTCoreXP2PBridgeNotificationReady = @" xp2preconnect" ; // app本地已ready,表示探测完成,可以发起请求了
1618
1719FILE *p2pOutLogFile;
1820// NSFileHandle *fileHandle;
21+ static BOOL p2p_log_enabled = NO ;
22+ static BOOL ops_report_enabled = NO ;
23+ @implementation TIoTP2PAPPConfig
24+ @end
1925
2026@interface TIoTCoreXP2PBridge ()<TIoTAVCaptionFLVDelegate>
2127@property (nonatomic , strong ) NSString *dev_name;
@@ -38,8 +44,8 @@ - (void)doTick:(data_report_t)data_buf;
3844 }
3945
4046 if (type == XP2PTypeLog) {
41- if (logEnable ) {
42- fwrite (msg, 1 , strlen (msg)>300 ?300 :strlen (msg), p2pOutLogFile);
47+ if (p2p_log_enabled ) {
48+ // fwrite(msg, 1, strlen(msg)>300?300:strlen(msg), p2pOutLogFile);
4349 [[TIoTCoreXP2PBridge sharedInstance ].logger addLog: [NSString stringWithCString: msg encoding: NSASCIIStringEncoding]];
4450 }
4551 return nullptr ;
@@ -219,6 +225,8 @@ - (instancetype)init {
219225 p2pOutLogFile = fopen (logFile.UTF8String , " wb" );
220226
221227 _uniReqStartTime = [NSMutableDictionary dictionary ];
228+
229+ [self getAppConfig ];
222230 }
223231 return self;
224232}
@@ -288,11 +296,92 @@ - (XP2PErrCode)setXp2pInfo:(NSString *)dev_name sec_id:(NSString *)sec_id sec_ke
288296 }
289297 setQcloudApiCred ([sec_id UTF8String ], [sec_key UTF8String ]); // 正式版app发布时候不需要传入secretid和secretkey,避免泄露secretid和secretkey,此处仅为演示
290298 }
299+
300+ TIoTP2PAPPConfig *config = [TIoTP2PAPPConfig new ];
301+ config.appkey = @" appkey" ; // 为explorer平台注册的应用信息(https://console.cloud.tencent.com/iotexplorer/v2/instance/app/detai) explorer控制台- 应用开发 - 选对应的应用下的 appkey/appsecret
302+ config.appsecret = @" appsecret" ; // 为explorer平台注册的应用信息(https://console.cloud.tencent.com/iotexplorer/v2/instance/app/detai) explorer控制台- 应用开发 - 选对应的应用下的 appkey/appsecret
303+ config.userid = [self getAppUUID ];
304+ [self setXp2pInfo: dev_name xp2pinfo: xp2pinfo appconfig: config];
305+ return XP2P_ERR_INIT_PRM;
306+ }
291307
308+ - (XP2PErrCode)setXp2pInfo : (NSString *)dev_name xp2pinfo : (NSString *)xp2pinfo appconfig : (TIoTP2PAPPConfig *)appconfig {
309+ if (!appconfig || appconfig.appkey .length < 1 || appconfig.appsecret .length < 1 || appconfig.userid .length < 1 ) {
310+ NSLog (@" 请输入正确的appconfig" );
311+ return XP2P_ERR_INIT_PRM;
312+ }
313+ if (xp2pinfo == nil || xp2pinfo.length < 1 ) {
314+ NSLog (@" 请输入正确的xp2pInfo" );
315+ return XP2P_ERR_INIT_PRM;
316+ }
317+ [self appGetUserConfig: appconfig];
292318 int ret = setDeviceXp2pInfo (dev_name.UTF8String , xp2pinfo.UTF8String );
293319 return (XP2PErrCode)ret;
294320}
295321
322+ NSString *createSortedQueryString (NSMutableDictionary *params) {
323+ NSArray *sortedKeys = [[params allKeys ] sortedArrayUsingSelector: @selector (compare: )];
324+ NSMutableArray *keyValuePairs = [NSMutableArray array ];
325+ for (NSString *key in sortedKeys) {
326+ NSString *value = [params objectForKey: key];
327+ NSString *keyValuePair = [NSString stringWithFormat: @" %@ =%@ " , key, value];
328+ [keyValuePairs addObject: keyValuePair];
329+ }
330+ NSString *queryString = [keyValuePairs componentsJoinedByString: @" &" ];
331+ return queryString;
332+ }
333+ - (NSString *)signMessage : (NSString *)message withSecret : (NSString *)secret {
334+ @try {
335+ // Base64 解码
336+ const char *cKey = [secret cStringUsingEncoding: NSASCIIStringEncoding];
337+ const char *cData = [message cStringUsingEncoding: NSASCIIStringEncoding];
338+
339+ // sha1
340+ unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
341+ CCHmac (kCCHmacAlgSHA1 , cKey, strlen (cKey), cData, strlen (cData), cHMAC);
342+
343+ NSData *HMAC = [[NSData alloc ] initWithBytes: cHMAC length: sizeof (cHMAC)];
344+
345+ NSString *hash = [HMAC base64EncodedStringWithOptions: 0 ];// 将加密结果进行一次BASE64编码。
346+ return hash;
347+ } @catch (NSException *exception) {
348+ NSLog (@" 签名错误:%@ " , exception);
349+ }
350+ return nil ;
351+ }
352+
353+ - (void )appGetUserConfig : (TIoTP2PAPPConfig *)appconfig {
354+ NSMutableDictionary *accessParam = [NSMutableDictionary dictionary ];
355+ [accessParam setValue: @" AppDescribeLogLevel" forKey: @" Action" ];
356+ [accessParam setValue: @([[TIoTCoreXP2PBridge getNowTimeTimestampSec ] integerValue ]) forKey: @" Timestamp" ];
357+ [accessParam setValue: @(arc4random ()) forKey: @" Nonce" ];
358+ [accessParam setValue: appconfig.appkey forKey: @" AppKey" ];
359+ [accessParam setValue: appconfig.userid forKey: @" UserId" ];
360+ [accessParam setValue: [[NSUUID UUID ] UUIDString ] forKey: @" RequestId" ];
361+
362+ NSString *content = createSortedQueryString (accessParam);
363+ NSString *signature = [self signMessage: content withSecret: appconfig.appsecret];
364+ [accessParam setValue: signature forKey: @" Signature" ];
365+
366+
367+ NSURL *url = [NSURL URLWithString: @" http://localhost:80/appapiv1" ];
368+ NSMutableURLRequest *reqlog = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 5 ];
369+ [reqlog setValue: @" application/json" forHTTPHeaderField: @" Content-Type" ];
370+ reqlog.HTTPMethod = @" POST" ;
371+ reqlog.HTTPBody = [NSJSONSerialization dataWithJSONObject: accessParam options: NSJSONWritingFragmentsAllowed error: nil ];;
372+ NSURLSessionDataTask *tasklog = [[NSURLSession sharedSession ] dataTaskWithRequest: reqlog completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
373+
374+ NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
375+ if (httpResponse.statusCode == 200 ) {
376+ NSError *jsonerror = nil ;
377+ NSDictionary *dic = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingAllowFragments error: &jsonerror];
378+ // NSLog(@"log serverapi:content===>%@, param==>%@, data===>%@",content,accessParam,dic);
379+ [self setAppConfig: [[dic objectForKey: @" Response" ] objectForKey: @" Data" ]];
380+ }
381+ }];
382+ [tasklog resume ];
383+ }
384+
296385- (NSString *)getUrlForHttpFlv : (NSString *)dev_name {
297386 const char *httpflv = delegateHttpFlv (dev_name.UTF8String );
298387 NSLog (@" httpflv---%s " ,httpflv);
@@ -647,7 +736,9 @@ - (void)doTick:(data_report_t)data_buf {
647736}
648737
649738- (void )reportUserList : (data_report_t )report {
650-
739+ if (!ops_report_enabled) {
740+ return ;
741+ }
651742 NSString *reqid = [NSString stringWithCString: (const char *)report.uniqueId encoding: NSASCIIStringEncoding];
652743 NSString *status = [NSString stringWithCString: (const char *)report.status encoding: NSASCIIStringEncoding];
653744 NSString *dataaction = [NSString stringWithCString: (const char *)report.data_action encoding: NSASCIIStringEncoding];
@@ -696,6 +787,18 @@ - (void)reportUserList:(data_report_t)report {
696787 }
697788}
698789
790+ - (void )getAppConfig {
791+ p2p_log_enabled = [self readKeychainValue: @" p2p_log_enabled" ].boolValue ;
792+ ops_report_enabled = [self readKeychainValue: @" ops_report_enabled" ].boolValue ;
793+ }
794+ - (void )setAppConfig : (NSDictionary *)appconfig {
795+ NSString * tmp_p2p_log_enabled = [appconfig objectForKey: @" P2PLogEnabled" ];
796+ NSString * tmp_ops_report_enabled = [appconfig objectForKey: @" OpsLogEnabled" ];
797+
798+ [self saveKeychainValue: tmp_p2p_log_enabled key: @" p2p_log_enabled" ];
799+ [self saveKeychainValue: tmp_ops_report_enabled key: @" ops_report_enabled" ];
800+ }
801+
699802+ (NSString *)getSDKVersion {
700803 return [NSString stringWithUTF8String: VIDEOSDKVERSION];
701804}
@@ -713,5 +816,9 @@ +(NSString *)getNowTimeTimestamp {
713816 NSString *timeSp = [NSString stringWithFormat: @" %ld " , (long )([datenow timeIntervalSince1970 ]*1000 )];
714817 return timeSp;
715818}
716-
819+ +(NSString *)getNowTimeTimestampSec {
820+ NSDate *datenow = [NSDate date ];
821+ NSString *timeSp = [NSString stringWithFormat: @" %ld " , (long )[datenow timeIntervalSince1970 ]];
822+ return timeSp;
823+ }
717824@end
0 commit comments