Browse Source

tunning SS and Notif background

alqindiirsyam 5 months ago
parent
commit
6f171b9a25

+ 11 - 16
NexilisLite/NexilisLite/Source/APIS.swift

@@ -852,7 +852,6 @@ public class APIS: NSObject {
     
     public static var uuidCall: UUID?
     public static var fpinCall: String?
-    public static var listIdentifierNotif: [String] = []
     public static func showNotificationNexilis(_ userInfo: [AnyHashable : Any]) {
 //        let center = UNUserNotificationCenter.current()
 //        let content = UNMutableNotificationContent()
@@ -873,31 +872,27 @@ public class APIS: NSObject {
                     if let messagePayload = payload["message"] as? [String: Any] {
                         if let data = messagePayload["data"] as? [String: Any] {
                             let code = data["nx_code"] as? String ?? ""
-//                            if API.nGetCLXConnState() == 0 {
-//                                do {
-//                                    let id = Utils.getConnectionID()
-//                                    try API.initConnection(bSwitchIP: false, sAPIK: Nexilis.sAPIKey, aAppMain: nil, cbiI: Callback(), sTCPAddr: Nexilis.ADDRESS, nTCPPort: Nexilis.PORT, sUserID: id, sStartWH: "09:00")
-//                                } catch {
-//
-//                                }
-//                                while (!API.bnuSDKServiceReady() || API.nGetCLXConnState() == 0) {
-//                                    Thread.sleep(forTimeInterval: 1)
-//                                }
-//                            }
                             while (!API.bnuSDKServiceReady() || API.nGetCLXConnState() == 0) {
                             }
                             if code == "CL01" {
                                 if let message = data["bodies"] as? [String: String] {
+                                    var messageExist = false
+                                    Database.shared.database?.inTransaction({ (fmdb, rollback) in
+                                        if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select message_id from MESSAGE where message_id = '\(message[CoreMessage_TMessageKey.MESSAGE_ID] ?? "")'"), cursor.next() {
+                                            messageExist = true
+                                            cursor.close()
+                                        }
+                                    })
+                                    if messageExist {
+                                        return
+                                    }
                                     let messageToSave = TMessage()
                                     messageToSave.mBodies = message
                                     Nexilis.saveMessage(message: messageToSave, withStatus: false, fromAPNS: true)
                                     DispatchQueue.global().async {
                                         _ = Nexilis.write(message: CoreMessage_TMessageBank.getAckMessage(messageId: message[CoreMessage_TMessageKey.MESSAGE_ID] ?? ""))
                                     }
-                                    if !listIdentifierNotif.contains(message[CoreMessage_TMessageKey.MESSAGE_ID] ?? ""){
-                                        listIdentifierNotif.append(message[CoreMessage_TMessageKey.MESSAGE_ID] ?? "")
-                                        APIS.addNotificationNexilis(messageToSave)
-                                    }
+                                    APIS.addNotificationNexilis(messageToSave)
                                 }
                             } else if code == "CL03" {
                                 let callFromName = data["call-from-name"] as? String ?? ""

+ 8 - 7
NexilisLite/NexilisLite/Source/IncomingThread.swift

@@ -1272,6 +1272,13 @@ class IncomingThread {
             ack(message: message)
             return
         }
+        var messageExist = false
+        Database.shared.database?.inTransaction({ (fmdb, rollback) in
+            if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select message_id from MESSAGE where message_id = '\(message.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID))'"), cursor.next() {
+                messageExist = true
+                cursor.close()
+            }
+        })
         let media = message.getMedia()
         //print("MEDIA \(media)");
         let thumb_id = message.getBody(key: CoreMessage_TMessageKey.THUMB_ID)
@@ -1281,12 +1288,7 @@ class IncomingThread {
             let url = documentDir.appendingPathComponent(thumb_id)
             //print("write thumb \(url.path)")
             try data.write(to: url, options: .atomic)
-//                let image = UIImage(data: data)
-//                if save {
-//                    UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
-//                }
         } catch {
-            //print(error)
         }
         if (!thumb_id.isEmpty) {
             Download().startHTTP(forKey: thumb_id) { (file, progress) in
@@ -1301,8 +1303,7 @@ class IncomingThread {
         }
         DispatchQueue.main.async {
             if APIS.checkAppStateisBackground() {
-                if !APIS.listIdentifierNotif.contains(message.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID)){
-                    APIS.listIdentifierNotif.append(message.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID))
+                if !messageExist {
                     APIS.addNotificationNexilis(message)
                 }
             }

+ 175 - 4
StreamShield/StreamShield/Source/SecurityShield.swift

@@ -22,11 +22,112 @@ public class SecurityShield: NSObject {
     public static func check(appName: String, apiKey: String) {
         Preference.setAppId(value: appName)
         Preference.setAccount(value: apiKey)
-        pull()
+        DispatchQueue.global().async {
+            do {
+                if !API.bnuSDKServiceReady() || API.nGetCLXConnState() == 0 {
+                    let address = getAddressNew(apiKey:Preference.getAccount())
+                    if address.isEmpty {
+                        return
+                    }
+                    var id = Preference.getConnectionID()
+                    let addressConn = address.components(separatedBy: ":")[0]
+                    let port = Int(address.components(separatedBy: ":")[1]) ?? 0
+                    if id.isEmpty {
+                        let sDID = UIDevice.current.identifierForVendor?.uuidString ?? "UNK-DEVICE"
+                        id = String(sDID[sDID.index(sDID.endIndex, offsetBy: -5)...])
+                        Preference.setConnectionID(value: id)
+                    }
+                    try API.initConnection(sAPIK: apiKey, cbiI: CallBackSS(), sTCPAddr: addressConn, nTCPPort: port, sUserID: id, sStartWH: "09:00")
+                    while (!API.bnuSDKServiceReady() || API.nGetCLXConnState() == 0) {
+                        Thread.sleep(forTimeInterval: 1)
+                    }
+                }
+                pull()
+            } catch {
+                
+            }
+        }
+    }
+    
+    private static func getAddressNew(apiKey: String) -> String {
+        var result = ""
+        let url = URL(string: "\(Preference.getDomainOpr())dipp/NuN1v3rs3/Qm3r4i0/get_ip_domain?account=\(apiKey)")!
+        let urlConfig = URLSessionConfiguration.default
+        let sessionDelegate = SelfSignedURLSessionDelegate()
+        urlConfig.requestCachePolicy = .returnCacheDataElseLoad
+        urlConfig.timeoutIntervalForRequest = 10.0
+        urlConfig.timeoutIntervalForResource = 10.0
+        let semaphore = DispatchSemaphore(value: 0)
+        let task = URLSession(configuration: urlConfig, delegate: sessionDelegate, delegateQueue: nil).dataTask(with: url) {(data, response, error) in
+            guard let data = data,
+                let url = response?.url,
+                let httpResponse = response as? HTTPURLResponse,
+                let fields = httpResponse.allHeaderFields as? [String: String] else {
+                semaphore.signal()
+                return
+            }
+            let dataEncode = String(data: data, encoding: .utf8)!
+            if !dataEncode.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
+                let dataDecodeBase64 = String(data: Data(base64Encoded: dataEncode)!, encoding: .utf8)!
+                let dataRealDecode = UtilsSS.decrypt(str: dataDecodeBase64)
+                do {
+                    if let jsonData = dataRealDecode.data(using: .utf8), let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] {
+                        var newDomain = jsonObject["domain"] as! String
+                        let jsonAddress = jsonObject["address"] as! [[String: Any]]
+                        let newIp = jsonAddress[0]["ip"] as! String
+                        let newPort = jsonAddress[0]["portI"] as! String
+                        if newDomain.substring(from: newDomain.count-1, to: nil) != "/" {
+                            newDomain += "/"
+                        }
+                        if (newIp+":"+newPort) != Preference.getIpOpr() || newDomain != Preference.getDomainOpr() {
+                            //check new domain
+                            if checkNewDomain(newDomain) {
+                                Preference.setDomainOpr(value: newDomain)
+                                Preference.setIpPortOpr(value: (newIp+":"+newPort))
+                            }
+                        }
+                    }
+                } catch {
+                    
+                }
+            }
+            semaphore.signal()
+        }
+        task.resume()
+        _ = semaphore.wait(timeout: .distantFuture)
+        result = Preference.getIpOpr()
+        return result
+    }
+    
+    private static func checkNewDomain(_ newDomain: String) -> Bool {
+        var result = false
+        let url = URL(string: "\(newDomain)dipp/NuN1v3rs3/Qm3r4i0/get_ip_domain?account=\(Preference.getAccount())")!
+        let urlConfig = URLSessionConfiguration.default
+        let sessionDelegate = SelfSignedURLSessionDelegate()
+        urlConfig.requestCachePolicy = .returnCacheDataElseLoad
+        urlConfig.timeoutIntervalForRequest = 10.0
+        urlConfig.timeoutIntervalForResource = 10.0
+        let semaphore = DispatchSemaphore(value: 0)
+        let task = URLSession(configuration: urlConfig, delegate: sessionDelegate, delegateQueue: nil).dataTask(with: url) {(data, response, error) in
+            if let httpResponse = response as? HTTPURLResponse {
+                if httpResponse.statusCode == 200 {
+                    guard let url = response?.url,
+                        let fields = httpResponse.allHeaderFields as? [String: String] else {
+                        semaphore.signal()
+                        return
+                    }
+                    result = true
+                }
+            }
+            semaphore.signal()
+        }
+        task.resume()
+        _ = semaphore.wait(timeout: .distantFuture)
+        return result
     }
     
     private static func pull() {
-        let me: String! = SecureUserDefaultsSS.shared.value(forKey: "me")!
+        let me: String! = SecureUserDefaultsSS.shared.value(forKey: "me") ?? Preference.getConnectionID()
         let tmessage = TMessageSS()
         tmessage.mCode = "SS01"
         tmessage.mStatus = CoreMessage_TMessageUtil.getTID()
@@ -922,6 +1023,16 @@ private class Service {
 }
 
 private class Preference {
+    static func setConnectionID(value: String) {
+        SecureUserDefaultsSS.shared.set(value, forKey: PreferencesKey.SS_CONNECTION_ID)
+    }
+
+    static func getConnectionID() -> String {
+        if let value: String = SecureUserDefaultsSS.shared.value(forKey: PreferencesKey.SS_CONNECTION_ID) {
+            return value
+        }
+        return ""
+    }
     static func getAppId() -> String {
         if let value: String = SecureUserDefaultsSS.shared.value(forKey: PreferencesKey.SS_USER_APP_ID) {
             return value
@@ -944,6 +1055,10 @@ private class Preference {
         SecureUserDefaultsSS.shared.set(value, forKey: PreferencesKey.SS_USER_ACCOUNT)
     }
     
+    static func setDomainOpr(value: String){
+        SecureUserDefaultsSS.shared.set(value, forKey: PreferencesKey.SS_DOMAIN_OPR)
+    }
+    
     static func getDomainOpr() -> String {
         if let value: String = SecureUserDefaultsSS.shared.value(forKey: PreferencesKey.SS_DOMAIN_OPR) {
             return value
@@ -951,6 +1066,10 @@ private class Preference {
         return "https://nexilis.io/"
     }
     
+    static func setIpPortOpr(value: String){
+        SecureUserDefaultsSS.shared.set(value, forKey: PreferencesKey.SS_IP_PORT_OPR)
+    }
+    
     static func getIpOpr() -> String {
         if let value: String = SecureUserDefaultsSS.shared.value(forKey: PreferencesKey.SS_IP_PORT_OPR) {
             return value
@@ -1734,6 +1853,8 @@ private class PreferencesKey {
     static let ERR131 = "131:SIM Swap detected"
     static let ERR132 = "132:Behavioral Anomaly detected"
     
+    static let SS_CONNECTION_ID = "ss_connection_id"
+    
     static let SS_USER_APP_ID = "ss_user_app_id"
     static let SS_USER_ACCOUNT = "ss_user_account"
     static let SS_DOMAIN_OPR = "domain_opr"
@@ -1854,7 +1975,7 @@ private class SelfSignedURLSessionDelegate: NSObject, URLSessionTaskDelegate, UR
     }
 }
 
-private class Utils {
+private class UtilsSS {
     private static let I_BB = 48   // 0
     private static let I_BBT_1 = 57 // 9
     private static let I_BAT_1 = 65 // A
@@ -1879,7 +2000,7 @@ private class Utils {
         icIGNORE.insert(32)// <space>
     }
     
-    private static func decrypt(str: String) -> String {
+    public static func decrypt(str: String) -> String {
         var arr: [Character]
         var iRandom = 0
         var sDecrypt: String
@@ -2575,3 +2696,53 @@ private class SecureUserDefaultsSS {
         defaults.synchronize()
     }
 }
+
+class CallBackSS : CallBack {
+    var sID: String = ""
+    
+    func connectionStateChanged(sUserID: String!, sDeviceID: String!, bConState: Bool!, nConType: Int!, nConSubType: Int!, nCLMConStat: UInt8!) {
+        
+    }
+    
+    func gpsStateChanged(nState: Int!) {
+        
+    }
+    
+    func sleepStateChanged(bState: Bool!) {
+        
+    }
+    
+    func callStateChanged(nState: Int!, sMessage: String!) -> Int {
+        return 1
+    }
+    
+    func bcastStateChanged(nState: Int!, sMessage: String!) -> Int {
+        return 1
+    }
+    
+    func sshareStateChanged(nState: Int!, sMessage: String!) -> Int {
+        return 1
+    }
+    
+    func incomingData(sPacketID: String!, oData: AnyObject!) throws {
+        
+    }
+    
+    func lateResponse(sPacketID: String!, sResponse: String!) throws {
+        
+    }
+    
+    func asycnACKReceived(sPacketID: String!) throws {
+        
+    }
+    
+    func locationUpdated(lTime: Int64!, sLocationInfo: String!) {
+        
+    }
+    
+    func resetDB() {
+        
+    }
+    
+    
+}