Skip to content

Commit

Permalink
添加iotexplore平台appkey/appsecret上报开关控制
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleychen committed Oct 17, 2024
1 parent 13bf4b2 commit 5c30f77
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ target 'LinkApp' do
pod 'CocoaAsyncSocket', '7.6.5'
pod 'TIoTLinkKit_TPNS', '1.0.1'
pod 'TIoTLinkKit_WechatOpenSDK', '1.0.1'
pod 'TIoTLinkKit_IJKPlayer', '2.0.14'
pod 'TIoTLinkKit_IJKPlayer', '2.0.15'
pod 'Bugly', '2.5.91'
end

Expand All @@ -42,7 +42,7 @@ target 'LinkSDKDemo' do
pod 'TIoTLinkKit/LinkRTC', :path => './'

pod 'TIoTLinkVideo', :path => './'
pod 'TIoTLinkKit_IJKPlayer', '2.0.14'
pod 'TIoTLinkKit_IJKPlayer', '2.0.15'
end

#older OS versions does not contain 'libarclite', at least iOS 11
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PODS:
- YYModel (= 1.0.4)
- TIoTLinkKit_FLV (2.2.3)
- TIoTLinkKit_GVoiceSE (1.0.9)
- TIoTLinkKit_IJKPlayer (2.0.14)
- TIoTLinkKit_IJKPlayer (2.0.15)
- TIoTLinkKit_SoundTouch (1.0.0)
- TIoTLinkKit_TPNS (1.0.1)
- TIoTLinkKit_WechatOpenSDK (1.0.1)
Expand Down Expand Up @@ -66,7 +66,7 @@ DEPENDENCIES:
- Tencent-MapSDK (= 4.3.9)
- TIoTLinkKit (from `./`)
- TIoTLinkKit/LinkRTC (from `./`)
- TIoTLinkKit_IJKPlayer (= 2.0.14)
- TIoTLinkKit_IJKPlayer (= 2.0.15)
- TIoTLinkKit_TPNS (= 1.0.1)
- TIoTLinkKit_WechatOpenSDK (= 1.0.1)
- TIoTLinkVideo (from `./`)
Expand Down Expand Up @@ -129,7 +129,7 @@ SPEC CHECKSUMS:
TIoTLinkKit: 0386fb583c61765a02ad5cca810c7661d5a0bc53
TIoTLinkKit_FLV: aec2b2ec9664990ec6c5b9f760fc1a9043d5a9a5
TIoTLinkKit_GVoiceSE: 27d0b2045a1acc600cd69c256e7667dacf7dc051
TIoTLinkKit_IJKPlayer: 1e265522952144af73724a31d6c80ebc15571f59
TIoTLinkKit_IJKPlayer: 55cc0faec1a9395118fe0309652e83b8136d0599
TIoTLinkKit_SoundTouch: cf79ae182b6c2349ff65bac7a338974bc538fa77
TIoTLinkKit_TPNS: 7cea4fc1d20ef6c4c11c4f0b66bbb10ecf4ed968
TIoTLinkKit_WechatOpenSDK: 7822d68cc5c46edf3f6020422202e7b65ea87614
Expand All @@ -141,6 +141,6 @@ SPEC CHECKSUMS:
TZImagePickerController: bf4c57b98d8707fce41ea6be872414a71c7a8c9d
YYModel: 2a7fdd96aaa4b86a824e26d0c517de8928c04b30

PODFILE CHECKSUM: 42de8ca533c86096306219fc1fd5570192d78ed0
PODFILE CHECKSUM: 795d3763c3242d732936fb2a6c17075ff99dd006

COCOAPODS: 1.14.3
9 changes: 8 additions & 1 deletion Source/SDK/LinkVideo/TIoTCoreXP2PBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ extern NSNotificationName const TIoTCoreXP2PBridgeNotificationDetectError;
extern NSNotificationName const TIoTCoreXP2PBridgeNotificationDeviceMsg;
extern NSNotificationName const TIoTCoreXP2PBridgeNotificationStreamEnd;

@interface TIoTP2PAPPConfig : NSObject
@property (nonatomic, strong)NSString *appkey; //为explorer平台注册的应用信息(https://console.cloud.tencent.com/iotexplorer/v2/instance/app/detai) explorer控制台- 应用开发 - 选对应的应用下的 appkey/appsecret
@property (nonatomic, strong)NSString *appsecret; //为explorer平台注册的应用信息(https://console.cloud.tencent.com/iotexplorer/v2/instance/app/detai)
@property (nonatomic, strong)NSString *userid; //用户纬度(每个手机区分开)使用用户自有的账号系统userid;若无请配置为[TIoTCoreXP2PBridge sharedInstance].getAppUUID; 查找日志是需提供此userid字段
@end

@protocol TIoTCoreXP2PBridgeDelegate <NSObject>

/*
Expand Down Expand Up @@ -84,7 +90,8 @@ extern NSNotificationName const TIoTCoreXP2PBridgeNotificationStreamEnd;
* 此接口只二者选一:传入xp2pinfo 就不需要填写 secretid和secretkey,xp2pinfo可从自建服务获取;
* 仅跑通流程的话,可设置 secretid和secretkey 两个参数,xp2pinfo传“”即可
*/
- (XP2PErrCode)setXp2pInfo:(NSString *)dev_name sec_id:(NSString *)sec_id sec_key:(NSString *)sec_key xp2pinfo:(NSString *)xp2pinfo;
- (XP2PErrCode)setXp2pInfo:(NSString *)dev_name sec_id:(NSString *)sec_id sec_key:(NSString *)sec_key xp2pinfo:(NSString *)xp2pinfo __attribute__((deprecated("Use -setXp2pInfo: xp2pinfo:")));
- (XP2PErrCode)setXp2pInfo:(NSString *)dev_name xp2pinfo:(NSString *)xp2pinfo appconfig:(TIoTP2PAPPConfig *)appconfig;

/*
* 使用播放器播放时,需先等待 SDK 初始化完成,ready事件(xp2preconnect 通知)之后,即可获取到 http-url
Expand Down
115 changes: 111 additions & 4 deletions Source/SDK/LinkVideo/TIoTCoreXP2PBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#import "TIoTCoreXP2PBridge.h"
#import "TIoTCoreLogger.h"
#include <string.h>
#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>

NSNotificationName const TIoTCoreXP2PBridgeNotificationDisconnect = @"xp2disconnect"; //p2p通道断开
NSNotificationName const TIoTCoreXP2PBridgeNotificationReady = @"xp2preconnect"; //app本地已ready,表示探测完成,可以发起请求了
Expand All @@ -16,6 +18,10 @@

FILE *p2pOutLogFile;
//NSFileHandle *fileHandle;
static BOOL p2p_log_enabled = NO;
static BOOL ops_report_enabled = NO;
@implementation TIoTP2PAPPConfig
@end

@interface TIoTCoreXP2PBridge ()<TIoTAVCaptionFLVDelegate>
@property (nonatomic, strong) NSString *dev_name;
Expand All @@ -38,8 +44,8 @@ - (void)doTick:(data_report_t)data_buf;
}

if (type == XP2PTypeLog) {
if (logEnable) {
fwrite(msg, 1, strlen(msg)>300?300:strlen(msg), p2pOutLogFile);
if (p2p_log_enabled) {
// fwrite(msg, 1, strlen(msg)>300?300:strlen(msg), p2pOutLogFile);
[[TIoTCoreXP2PBridge sharedInstance].logger addLog:[NSString stringWithCString:msg encoding:NSASCIIStringEncoding]];
}
return nullptr;
Expand Down Expand Up @@ -219,6 +225,8 @@ - (instancetype)init {
p2pOutLogFile = fopen(logFile.UTF8String, "wb");

_uniReqStartTime = [NSMutableDictionary dictionary];

[self getAppConfig];
}
return self;
}
Expand Down Expand Up @@ -288,11 +296,92 @@ - (XP2PErrCode)setXp2pInfo:(NSString *)dev_name sec_id:(NSString *)sec_id sec_ke
}
setQcloudApiCred([sec_id UTF8String], [sec_key UTF8String]); //正式版app发布时候不需要传入secretid和secretkey,避免泄露secretid和secretkey,此处仅为演示
}

TIoTP2PAPPConfig *config = [TIoTP2PAPPConfig new];
config.appkey = @"appkey"; //为explorer平台注册的应用信息(https://console.cloud.tencent.com/iotexplorer/v2/instance/app/detai) explorer控制台- 应用开发 - 选对应的应用下的 appkey/appsecret
config.appsecret = @"appsecret"; //为explorer平台注册的应用信息(https://console.cloud.tencent.com/iotexplorer/v2/instance/app/detai) explorer控制台- 应用开发 - 选对应的应用下的 appkey/appsecret
config.userid = [self getAppUUID];
[self setXp2pInfo:dev_name xp2pinfo:xp2pinfo appconfig:config];
return XP2P_ERR_INIT_PRM;
}

- (XP2PErrCode)setXp2pInfo:(NSString *)dev_name xp2pinfo:(NSString *)xp2pinfo appconfig:(TIoTP2PAPPConfig *)appconfig {
if (!appconfig || appconfig.appkey.length < 1 || appconfig.appsecret.length < 1 || appconfig.userid.length < 1) {
NSLog(@"请输入正确的appconfig");
return XP2P_ERR_INIT_PRM;
}
if (xp2pinfo == nil || xp2pinfo.length < 1) {
NSLog(@"请输入正确的xp2pInfo");
return XP2P_ERR_INIT_PRM;
}
[self appGetUserConfig:appconfig];
int ret = setDeviceXp2pInfo(dev_name.UTF8String, xp2pinfo.UTF8String);
return (XP2PErrCode)ret;
}

NSString *createSortedQueryString(NSMutableDictionary *params) {
NSArray *sortedKeys = [[params allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *keyValuePairs = [NSMutableArray array];
for (NSString *key in sortedKeys) {
NSString *value = [params objectForKey:key];
NSString *keyValuePair = [NSString stringWithFormat:@"%@=%@", key, value];
[keyValuePairs addObject:keyValuePair];
}
NSString *queryString = [keyValuePairs componentsJoinedByString:@"&"];
return queryString;
}
- (NSString *)signMessage:(NSString *)message withSecret:(NSString *)secret {
@try {
// Base64 解码
const char *cKey = [secret cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [message cStringUsingEncoding:NSASCIIStringEncoding];

//sha1
unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];

NSString *hash = [HMAC base64EncodedStringWithOptions:0];//将加密结果进行一次BASE64编码。
return hash;
} @catch (NSException *exception) {
NSLog(@"签名错误:%@", exception);
}
return nil;
}

- (void)appGetUserConfig:(TIoTP2PAPPConfig *)appconfig {
NSMutableDictionary *accessParam = [NSMutableDictionary dictionary];
[accessParam setValue:@"AppDescribeLogLevel" forKey:@"Action"];
[accessParam setValue:@([[TIoTCoreXP2PBridge getNowTimeTimestampSec] integerValue]) forKey:@"Timestamp"];
[accessParam setValue:@(arc4random()) forKey:@"Nonce"];
[accessParam setValue:appconfig.appkey forKey:@"AppKey"];
[accessParam setValue:appconfig.userid forKey:@"UserId"];
[accessParam setValue:[[NSUUID UUID] UUIDString] forKey:@"RequestId"];

NSString *content = createSortedQueryString(accessParam);
NSString *signature = [self signMessage:content withSecret:appconfig.appsecret];
[accessParam setValue:signature forKey:@"Signature"];


NSURL *url = [NSURL URLWithString:@"http://localhost:80/appapiv1"];
NSMutableURLRequest *reqlog = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
[reqlog setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
reqlog.HTTPMethod = @"POST";
reqlog.HTTPBody = [NSJSONSerialization dataWithJSONObject:accessParam options:NSJSONWritingFragmentsAllowed error:nil];;
NSURLSessionDataTask *tasklog = [[NSURLSession sharedSession] dataTaskWithRequest:reqlog completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200) {
NSError *jsonerror = nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonerror];
// NSLog(@"log serverapi:content===>%@, param==>%@, data===>%@",content,accessParam,dic);
[self setAppConfig:[[dic objectForKey:@"Response"] objectForKey:@"Data"]];
}
}];
[tasklog resume];
}

- (NSString *)getUrlForHttpFlv:(NSString *)dev_name {
const char *httpflv = delegateHttpFlv(dev_name.UTF8String);
NSLog(@"httpflv---%s",httpflv);
Expand Down Expand Up @@ -647,7 +736,9 @@ - (void)doTick:(data_report_t)data_buf {
}

- (void)reportUserList:(data_report_t)report {

if (!ops_report_enabled) {
return;
}
NSString *reqid = [NSString stringWithCString:(const char *)report.uniqueId encoding:NSASCIIStringEncoding];
NSString *status = [NSString stringWithCString:(const char *)report.status encoding:NSASCIIStringEncoding];
NSString *dataaction = [NSString stringWithCString:(const char *)report.data_action encoding:NSASCIIStringEncoding];
Expand Down Expand Up @@ -696,6 +787,18 @@ - (void)reportUserList:(data_report_t)report {
}
}

- (void)getAppConfig {
p2p_log_enabled = [self readKeychainValue:@"p2p_log_enabled"].boolValue;
ops_report_enabled = [self readKeychainValue:@"ops_report_enabled"].boolValue;
}
- (void)setAppConfig:(NSDictionary *)appconfig {
NSString * tmp_p2p_log_enabled = [appconfig objectForKey:@"P2PLogEnabled"];
NSString * tmp_ops_report_enabled = [appconfig objectForKey:@"OpsLogEnabled"];

[self saveKeychainValue:tmp_p2p_log_enabled key:@"p2p_log_enabled"];
[self saveKeychainValue:tmp_ops_report_enabled key:@"ops_report_enabled"];
}

+ (NSString *)getSDKVersion {
return [NSString stringWithUTF8String:VIDEOSDKVERSION];
}
Expand All @@ -713,5 +816,9 @@ +(NSString *)getNowTimeTimestamp {
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)];
return timeSp;
}

+(NSString *)getNowTimeTimestampSec {
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
return timeSp;
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
ReferencedContainer = "container:TIoTLinkKit.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "OS_ACTIVITY_MODE"
value = "disable"
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down

0 comments on commit 5c30f77

Please sign in to comment.