123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- #import "RLMAnalytics.hpp"
- #import <Foundation/Foundation.h>
- #if TARGET_IPHONE_SIMULATOR || TARGET_OS_MAC || (TARGET_OS_WATCH && TARGET_OS_SIMULATOR) || (TARGET_OS_TV && TARGET_OS_SIMULATOR)
- #import "RLMRealm.h"
- #import "RLMUtil.hpp"
- #import <array>
- #import <sys/socket.h>
- #import <sys/sysctl.h>
- #import <net/if.h>
- #import <net/if_dl.h>
- #import <CommonCrypto/CommonDigest.h>
- #ifndef REALM_COCOA_VERSION
- #import "RLMVersion.h"
- #endif
- #if REALM_ENABLE_SYNC
- #import <realm/sync/version.hpp>
- #endif
- @interface NSObject (SwiftVersion)
- + (NSString *)swiftVersion;
- @end
- static auto RLMSysCtl(int *mib, u_int mibSize, size_t *bufferSize) {
- std::unique_ptr<void, decltype(&free)> buffer(nullptr, &free);
- int ret = sysctl(mib, mibSize, nullptr, bufferSize, nullptr, 0);
- if (ret != 0) {
- return buffer;
- }
- buffer.reset(malloc(*bufferSize));
- if (!buffer) {
- return buffer;
- }
- ret = sysctl(mib, mibSize, buffer.get(), bufferSize, nullptr, 0);
- if (ret != 0) {
- buffer.reset();
- }
- return buffer;
- }
- static NSString *RLMOSVersion() {
- std::array<int, 2> mib = {{CTL_KERN, KERN_OSRELEASE}};
- size_t bufferSize;
- auto buffer = RLMSysCtl(&mib[0], mib.size(), &bufferSize);
- if (!buffer) {
- return nil;
- }
- return [[NSString alloc] initWithBytesNoCopy:buffer.release()
- length:bufferSize - 1
- encoding:NSUTF8StringEncoding
- freeWhenDone:YES];
- }
- static NSString *RLMHashData(const void *bytes, size_t length) {
- unsigned char buffer[CC_SHA256_DIGEST_LENGTH];
- CC_SHA256(bytes, static_cast<CC_LONG>(length), buffer);
- char formatted[CC_SHA256_DIGEST_LENGTH * 2 + 1];
- for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; ++i) {
- sprintf(formatted + i * 2, "%02x", buffer[i]);
- }
- return [[NSString alloc] initWithBytes:formatted
- length:CC_SHA256_DIGEST_LENGTH * 2
- encoding:NSUTF8StringEncoding];
- }
- static NSString *RLMMACAddress() {
- int en0 = static_cast<int>(if_nametoindex("en0"));
- if (!en0) {
- return nil;
- }
- std::array<int, 6> mib = {{CTL_NET, PF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, en0}};
- size_t bufferSize;
- auto buffer = RLMSysCtl(&mib[0], mib.size(), &bufferSize);
- if (!buffer) {
- return nil;
- }
-
- auto sockaddr = reinterpret_cast<sockaddr_dl *>(static_cast<if_msghdr *>(buffer.get()) + 1);
- auto mac = reinterpret_cast<const unsigned char *>(sockaddr->sdl_data + sockaddr->sdl_nlen);
- return RLMHashData(mac, 6);
- }
- static NSDictionary *RLMAnalyticsPayload() {
- NSBundle *appBundle = NSBundle.mainBundle;
- NSString *hashedBundleID = appBundle.bundleIdentifier;
-
-
- if (!hashedBundleID) {
- for (NSBundle *bundle in NSBundle.allBundles) {
- if ((hashedBundleID = bundle.bundleIdentifier)) {
- appBundle = bundle;
- break;
- }
- }
- }
-
-
- if (hashedBundleID) {
- NSData *data = [hashedBundleID dataUsingEncoding:NSUTF8StringEncoding];
- hashedBundleID = RLMHashData(data.bytes, data.length);
- }
- NSString *osVersionString = [[NSProcessInfo processInfo] operatingSystemVersionString];
- Class swiftObjectUtilClass = NSClassFromString(@"RealmSwiftObjectUtil");
- BOOL isSwift = swiftObjectUtilClass != nil;
- NSString *swiftVersion = isSwift ? [swiftObjectUtilClass swiftVersion] : @"N/A";
- static NSString *kUnknownString = @"unknown";
- NSString *hashedMACAddress = RLMMACAddress() ?: kUnknownString;
- return @{
- @"event": @"Run",
- @"properties": @{
-
- @"token": @"ce0fac19508f6c8f20066d345d360fd0",
-
- @"distinct_id": hashedMACAddress,
- @"Anonymized MAC Address": hashedMACAddress,
- @"Anonymized Bundle ID": hashedBundleID ?: kUnknownString,
-
- @"Binding": @"cocoa",
- @"Language": isSwift ? @"swift" : @"objc",
- @"Realm Version": REALM_COCOA_VERSION,
- #if REALM_ENABLE_SYNC
- @"Sync Version": @(REALM_SYNC_VER_STRING),
- #endif
- #if TARGET_OS_WATCH
- @"Target OS Type": @"watchos",
- #elif TARGET_OS_TV
- @"Target OS Type": @"tvos",
- #elif TARGET_OS_IPHONE
- @"Target OS Type": @"ios",
- #else
- @"Target OS Type": @"osx",
- #endif
- @"Swift Version": swiftVersion,
-
- @"Target OS Version": osVersionString,
-
- @"Target OS Minimum Version": appBundle.infoDictionary[@"MinimumOSVersion"] ?: kUnknownString,
-
- @"Host OS Type": @"osx",
- @"Host OS Version": RLMOSVersion() ?: kUnknownString,
- }
- };
- }
- void RLMSendAnalytics() {
- if (getenv("REALM_DISABLE_ANALYTICS") || !RLMIsDebuggerAttached() || RLMIsRunningInPlayground()) {
- return;
- }
- NSData *payload = [NSJSONSerialization dataWithJSONObject:RLMAnalyticsPayload() options:0 error:nil];
- NSString *url = [NSString stringWithFormat:@"https://api.mixpanel.com/track/?data=%@&ip=1", [payload base64EncodedStringWithOptions:0]];
-
-
- [[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:url]] resume];
- }
- #else
- void RLMSendAnalytics() {}
- #endif
|