自定义推送加密

This commit is contained in:
Fin 2023-03-07 18:07:05 +08:00
parent 8eb52bcd6e
commit 52a3e35907
No known key found for this signature in database
GPG Key ID: CFB59B99D87A7B93
9 changed files with 108 additions and 44 deletions

View File

@ -76,6 +76,8 @@
0642B55C27EB149900453D91 /* MutableTextCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0642B55B27EB149900453D91 /* MutableTextCellViewModel.swift */; };
064CAB9E256BE9090018155C /* PreviewCardCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 064CAB9D256BE9090018155C /* PreviewCardCellViewModel.swift */; };
064CABA6256BE9510018155C /* PreviewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 064CABA5256BE9510018155C /* PreviewModel.swift */; };
0653677629B719BC0038BDB8 /* CryptoSettingManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06F08EA329B098DD006AB9CA /* CryptoSettingManager.swift */; };
0653677829B727A60038BDB8 /* CryptoSettingRelay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0653677729B727A60038BDB8 /* CryptoSettingRelay.swift */; };
065A4D4220EE1A31002EB2DB /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 063C499720E36BF9001BCA35 /* Localizable.strings */; };
065AE76B2987777F00323230 /* ArchiveSettingRelay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 065AE76A2987777F00323230 /* ArchiveSettingRelay.swift */; };
065BE4402563D649002A8CA4 /* SoundsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 065BE43F2563D649002A8CA4 /* SoundsViewModel.swift */; };
@ -258,6 +260,7 @@
0642B55B27EB149900453D91 /* MutableTextCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutableTextCellViewModel.swift; sourceTree = "<group>"; };
064CAB9D256BE9090018155C /* PreviewCardCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewCardCellViewModel.swift; sourceTree = "<group>"; };
064CABA5256BE9510018155C /* PreviewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewModel.swift; sourceTree = "<group>"; };
0653677729B727A60038BDB8 /* CryptoSettingRelay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CryptoSettingRelay.swift; sourceTree = "<group>"; };
065AE76A2987777F00323230 /* ArchiveSettingRelay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArchiveSettingRelay.swift; sourceTree = "<group>"; };
065BE43F2563D649002A8CA4 /* SoundsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundsViewModel.swift; sourceTree = "<group>"; };
065BE4452563D7E5002A8CA4 /* ViewModelType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModelType.swift; sourceTree = "<group>"; };
@ -518,6 +521,7 @@
06840DBA272298FB001B3193 /* BKColor.swift */,
065AE76A2987777F00323230 /* ArchiveSettingRelay.swift */,
06F08EA329B098DD006AB9CA /* CryptoSettingManager.swift */,
0653677729B727A60038BDB8 /* CryptoSettingRelay.swift */,
06F08EA629B1DDFE006AB9CA /* Error+Extension.swift */,
06F08EAB29B1DECD006AB9CA /* NSLocalizedString+Extension.swift */,
);
@ -1015,6 +1019,7 @@
06F08EA729B1DDFE006AB9CA /* Error+Extension.swift in Sources */,
06B1158F247BB1FB006D91FB /* Message.swift in Sources */,
06172FDA27F6DAEF002333A4 /* ServerListTableViewCell.swift in Sources */,
0653677829B727A60038BDB8 /* CryptoSettingRelay.swift in Sources */,
061894C529962EB900E001C2 /* GradientButton.swift in Sources */,
06C595362481160F006B98F3 /* BKLabel.swift in Sources */,
0637FA7820E0926D00E80174 /* BarkTargetType.swift in Sources */,
@ -1069,6 +1074,7 @@
files = (
06CF784C21C7A51200A052D7 /* NotificationService.swift in Sources */,
06F08EAD29B1DED6006AB9CA /* NSLocalizedString+Extension.swift in Sources */,
0653677629B719BC0038BDB8 /* CryptoSettingManager.swift in Sources */,
06F08EA529B1DDA7006AB9CA /* Algorithm.swift in Sources */,
06BBB89125650CCF0076F63E /* ArchiveSettingManager.swift in Sources */,
06B11591247BC132006D91FB /* Message.swift in Sources */,

View File

@ -7,7 +7,6 @@
//
import Foundation
import RxCocoa
class CryptoSettingManager: NSObject {
static let shared = CryptoSettingManager()
@ -39,16 +38,3 @@ class CryptoSettingManager: NSObject {
}
}
class CryptoSettingRelay: NSObject {
static let shared = CryptoSettingRelay()
let fields: BehaviorRelay<CryptoSettingFields?>
override private init() {
self.fields = BehaviorRelay<CryptoSettingFields?>(value: CryptoSettingManager.shared.fields)
super.init()
self.fields.subscribe { val in
CryptoSettingManager.shared.fields = val
}.disposed(by: rx.disposeBag)
}
}

View File

@ -0,0 +1,24 @@
//
// CryptoSettingRelay.swift
// Bark
//
// Created by huangfeng on 2023/3/7.
// Copyright © 2023 Fin. All rights reserved.
//
import Foundation
import RxCocoa
class CryptoSettingRelay: NSObject {
static let shared = CryptoSettingRelay()
let fields: BehaviorRelay<CryptoSettingFields?>
override private init() {
self.fields = BehaviorRelay<CryptoSettingFields?>(value: CryptoSettingManager.shared.fields)
super.init()
self.fields.subscribe { val in
CryptoSettingManager.shared.fields = val
}.disposed(by: rx.disposeBag)
}
}

View File

@ -9,3 +9,25 @@
import Foundation
extension String: Error {}
public enum ApiError: Swift.Error {
case Error(info: String)
case AccountBanned(info: String)
}
extension Swift.Error {
func rawString() -> String {
if let err = self as? String {
return err
}
guard let err = self as? ApiError else {
return self.localizedDescription
}
switch err {
case .Error(let info):
return info
case .AccountBanned(let info):
return info
}
}
}

View File

@ -14,28 +14,6 @@ import RxSwift
import SwiftyJSON
import UIKit
public enum ApiError: Swift.Error {
case Error(info: String)
case AccountBanned(info: String)
}
extension Swift.Error {
func rawString() -> String {
if let err = self as? String {
return err
}
guard let err = self as? ApiError else {
return self.localizedDescription
}
switch err {
case .Error(let info):
return info
case .AccountBanned(let info):
return info
}
}
}
extension Observable where Element: Moya.Response {
/// HTTP
func filterHttpError() -> Observable<Result<Element, ApiError>> {

View File

@ -31,6 +31,7 @@ class CryptoSettingViewModel: ViewModel, ViewModelType {
struct Dependencies {
let settingFieldRelay: BehaviorRelay<CryptoSettingFields?>
let deviceKey: Driver<String>
let serverAddress: Driver<String>
}
private let dependencies: Dependencies
@ -39,7 +40,8 @@ class CryptoSettingViewModel: ViewModel, ViewModelType {
Dependencies(
settingFieldRelay: CryptoSettingRelay.shared.fields,
// Key just
deviceKey: Driver.just(ServerManager.shared.currentServer.key)
deviceKey: Driver.just(ServerManager.shared.currentServer.key),
serverAddress: Driver.just(ServerManager.shared.currentServer.address)
)
) {
self.dependencies = dependencies
@ -94,8 +96,8 @@ class CryptoSettingViewModel: ViewModel, ViewModelType {
return false
}
}
let copy = Driver.combineLatest(copyScript, dependencies.deviceKey)
.map { fields, deviceKey in
let copy = Driver.combineLatest(copyScript, dependencies.deviceKey, dependencies.serverAddress)
.map { fields, deviceKey,serverAddress in
return
"""
#!/usr/bin/env bash
@ -120,7 +122,7 @@ class CryptoSettingViewModel: ViewModel, ViewModelType {
# The console will print "\((try? AESCryptoModel(cryptoFields: fields).encrypt(text: "{\"body\": \"test\"}")) ?? "")"
echo $ciphertext
# curl --data-urlencode "ciphertext=$ciphertext" https://api.day.app/$deviceKey
curl --data-urlencode "ciphertext=$ciphertext" \(serverAddress)/$deviceKey
"""
}

View File

@ -10,8 +10,10 @@ import Intents
import Kingfisher
import MobileCoreServices
import RealmSwift
import SwiftyJSON
import UIKit
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
lazy var realm: Realm? = {
@ -232,14 +234,57 @@ class NotificationService: UNNotificationServiceExtension {
}
}
func decrypt(ciphertext: String) throws -> [String: Any] {
guard let fields = CryptoSettingManager.shared.fields else {
throw "No encryption key set"
}
let aes = try AESCryptoModel(cryptoFields: fields)
let json = try aes.decrypt(ciphertext: ciphertext)
guard let data = json.data(using: .utf8), let map = JSON(data).dictionaryObject else {
throw "JSON parsing failed"
}
return map
}
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> ()) {
guard let bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
contentHandler(request.content)
return
}
let userInfo = bestAttemptContent.userInfo
var userInfo = bestAttemptContent.userInfo
// 使 bestAttemptContent
if let ciphertext = userInfo["ciphertext"] as? String {
do {
var map = try decrypt(ciphertext: ciphertext)
var alert = [String: Any]()
if let title = map["title"] as? String {
bestAttemptContent.title = title
alert["title"] = title
}
if let body = map["body"] as? String {
bestAttemptContent.body = body
alert["body"] = body
}
if let sound = map["sound"] as? String {
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: sound))
}
if let badge = map["badge"] as? Int {
bestAttemptContent.badge = badge as NSNumber
}
map["aps"] = ["alert": alert]
userInfo = map
bestAttemptContent.userInfo = userInfo
}
catch {
bestAttemptContent.body = "Decryption Failed"
return
}
}
//
if #available(iOSApplicationExtension 15.0, *) {
if let level = userInfo["level"] as? String {

View File

@ -44,6 +44,7 @@ target 'NotificationServiceExtension' do
pod 'IceCream'
pod 'Kingfisher'
pod 'CryptoSwift'
pod 'SwiftyJSON'
end
post_install do |installer|

View File

@ -147,6 +147,6 @@ SPEC CHECKSUMS:
SVProgressHUD: 1428aafac632c1f86f62aa4243ec12008d7a51d6
SwiftyJSON: 2f33a42c6fbc52764d96f13368585094bfd8aa5e
PODFILE CHECKSUM: caaf0ea0ab301807957ab7217eb404aff861f92c
PODFILE CHECKSUM: 1760a939649c77ffaef8188767d7496b7afb14c6
COCOAPODS: 1.11.3