diff --git a/Kit/plugins/Server.swift b/Kit/plugins/Server.swift new file mode 100644 index 00000000..49123b6a --- /dev/null +++ b/Kit/plugins/Server.swift @@ -0,0 +1,75 @@ +// +// Server.swift +// Kit +// +// Created by Serhiy Mytrovtsiy on 04/07/2021. +// Using Swift 5.0. +// Running on macOS 10.15. +// +// Copyright © 2021 Serhiy Mytrovtsiy. All rights reserved. +// + +import Foundation + +struct event: Codable { + var ID: String + + var version: String + var build: String + var modules: [String] + + var device: String + var os: String + var language: String +} + +public class Server { + public static let shared = Server(url: URL(string: "https://api.serhiy.io/v1/stats")!) + + public var ID: String { + get { + return Store.shared.string(key: "id", defaultValue: UUID().uuidString) + } + } + private let url: URL + + public init(url: URL) { + self.url = url + + if !Store.shared.exist(key: "id") { + Store.shared.set(key: "id", value: self.ID) + } + } + + public func sendEvent(modules: [String]) { + let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String + let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String + let systemVersion = ProcessInfo().operatingSystemVersion + + let e = event( + ID: self.ID, + version: version ?? "unknown", + build: build ?? "unknown", + modules: modules, device: SystemKit.shared.modelName() ?? "unknown", + os: systemVersion.getFullVersion(), + language: Locale.current.languageCode ?? "unknown" + ) + + var request = URLRequest(url: self.url) + request.httpMethod = "POST" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + + do { + request.httpBody = try JSONEncoder().encode(e) + } catch let err { + error("failed to encode json: \(err)") + } + + let task = URLSession.shared.dataTask(with: request) { (_, _, err) in + if err != nil { + error("send report \(String(describing: err))") + } + } + task.resume() + } +} diff --git a/Stats.xcodeproj/project.pbxproj b/Stats.xcodeproj/project.pbxproj index 638b13ab..f9fef71c 100644 --- a/Stats.xcodeproj/project.pbxproj +++ b/Stats.xcodeproj/project.pbxproj @@ -74,6 +74,7 @@ 9A81C75E2449A41400825D92 /* RAM.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9A81C7562449A41400825D92 /* RAM.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 9A81C7692449A43600825D92 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A81C7672449A43600825D92 /* main.swift */; }; 9A81C76A2449A43600825D92 /* readers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A81C7682449A43600825D92 /* readers.swift */; }; + 9A8AE0A326921A2A00B13054 /* Server.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A8AE0A226921A2A00B13054 /* Server.swift */; }; 9A8DE58E253DEFA9006A748F /* Fans.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A8DE587253DEFA9006A748F /* Fans.framework */; }; 9A8DE58F253DEFA9006A748F /* Fans.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9A8DE587253DEFA9006A748F /* Fans.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 9A8DE5E4253DF4E2006A748F /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A8DE5E3253DF4E2006A748F /* main.swift */; }; @@ -376,6 +377,7 @@ 9A81C7592449A41400825D92 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 9A81C7672449A43600825D92 /* main.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 9A81C7682449A43600825D92 /* readers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = readers.swift; sourceTree = ""; }; + 9A8AE0A226921A2A00B13054 /* Server.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Server.swift; sourceTree = ""; }; 9A8DE587253DEFA9006A748F /* Fans.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Fans.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 9A8DE58A253DEFA9006A748F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 9A8DE5E3253DF4E2006A748F /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; @@ -769,6 +771,7 @@ 9A2848032666AB2F00EC1F6D /* SystemKit.swift */, 9A2848072666AB3000EC1F6D /* Updater.swift */, 9A6EEBBD2685259500897371 /* Logger.swift */, + 9A8AE0A226921A2A00B13054 /* Server.swift */, ); path = plugins; sourceTree = ""; @@ -1421,6 +1424,7 @@ 9A28480E2666AB3000EC1F6D /* Updater.swift in Sources */, 9A2847622666AA2700EC1F6D /* Label.swift in Sources */, 9A28477C2666AA5000EC1F6D /* reader.swift in Sources */, + 9A8AE0A326921A2A00B13054 /* Server.swift in Sources */, 9A2847652666AA2700EC1F6D /* Memory.swift in Sources */, 9A2847642666AA2700EC1F6D /* Battery.swift in Sources */, 9A28480B2666AB3000EC1F6D /* Charts.swift in Sources */, diff --git a/Stats/AppDelegate.swift b/Stats/AppDelegate.swift index 668a3754..82baf4cd 100755 --- a/Stats/AppDelegate.swift +++ b/Stats/AppDelegate.swift @@ -52,6 +52,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele self.defaultValues() self.updateCron() info("Stats started in \((startingPoint.timeIntervalSinceNow * -1).rounded(toPlaces: 4)) seconds") + Server.shared.sendEvent(modules: modules.filter({ $0.enabled != false && $0.available != false }).map({ $0.config.name })) } func applicationWillTerminate(_ aNotification: Notification) { diff --git a/Stats/Supporting Files/Info.plist b/Stats/Supporting Files/Info.plist index f0b3df33..347410ea 100755 --- a/Stats/Supporting Files/Info.plist +++ b/Stats/Supporting Files/Info.plist @@ -17,7 +17,7 @@ CFBundleShortVersionString $(MARKETING_VERSION) CFBundleVersion - 286 + 287 Description Simple macOS system monitor in your menu bar LSApplicationCategoryType @@ -26,6 +26,11 @@ $(MACOSX_DEPLOYMENT_TARGET) LSUIElement + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + NSHumanReadableCopyright Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved. NSPrincipalClass diff --git a/Stats/Views/AppSettings.swift b/Stats/Views/AppSettings.swift index a9a25636..34316795 100644 --- a/Stats/Views/AppSettings.swift +++ b/Stats/Views/AppSettings.swift @@ -96,6 +96,7 @@ class ApplicationSettings: NSScrollView { statsName.font = NSFont.systemFont(ofSize: 20, weight: .regular) statsName.stringValue = "Stats" statsName.isSelectable = true + statsName.toolTip = Server.shared.ID let versionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String