Skip to content

Commit

Permalink
Fixed NSKeyedUnarchiver deprecations.
Browse files Browse the repository at this point in the history
  • Loading branch information
tladesignz committed Apr 11, 2023
1 parent 18939fe commit e1372f1
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 89 deletions.
12 changes: 8 additions & 4 deletions Shared/Messages/CloseCircuitsMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import Foundation
import Tor

class CloseCircuitsMessage: NSObject, Message {
class CloseCircuitsMessage: Message {

static var supportsSecureCoding = true
override class var supportsSecureCoding: Bool {
true
}

let circuits: [TorCircuit]

Expand All @@ -25,10 +27,12 @@ class CloseCircuitsMessage: NSObject, Message {
circuits = coder.decodeObject(of: [NSArray.self, TorCircuit.self],
forKey: "circuits") as? [TorCircuit] ?? []

super.init()
super.init(coder: coder)
}

func encode(with coder: NSCoder) {
override func encode(with coder: NSCoder) {
super.encode(with: coder)

coder.encode(circuits, forKey: "circuits")
}
}
16 changes: 3 additions & 13 deletions Shared/Messages/ConfigChangedMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@

import Foundation

class ConfigChangedMessage: NSObject, Message {
class ConfigChangedMessage: Message {

static var supportsSecureCoding = true


override init() {
super.init()
}

required init?(coder: NSCoder) {
super.init()
}

func encode(with coder: NSCoder) {
override class var supportsSecureCoding: Bool {
true
}
}
15 changes: 3 additions & 12 deletions Shared/Messages/DebugMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@

import Foundation

class DebugMessage: NSObject, Message {
class DebugMessage: Message {

static var supportsSecureCoding = true

override init() {
super.init()
}

required init?(coder: NSCoder) {
super.init()
}

func encode(with coder: NSCoder) {
override class var supportsSecureCoding: Bool {
true
}
}
15 changes: 3 additions & 12 deletions Shared/Messages/GetCircuitsMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@

import Foundation

class GetCircuitsMessage: NSObject, Message {
class GetCircuitsMessage: Message {

static var supportsSecureCoding = true

override init() {
super.init()
}

required init?(coder: NSCoder) {
super.init()
}

func encode(with coder: NSCoder) {
override class var supportsSecureCoding: Bool {
true
}
}
18 changes: 17 additions & 1 deletion Shared/Messages/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,21 @@

import Foundation

protocol Message: NSObject, NSSecureCoding {
class Message: NSObject, NSSecureCoding {

class var supportsSecureCoding: Bool {
true
}

override init() {
super.init()
}

required init?(coder: NSCoder) {
super.init()
}


func encode(with coder: NSCoder) {
}
}
12 changes: 8 additions & 4 deletions Shared/Messages/ProgressMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

import Foundation

class ProgressMessage: NSObject, Message {
class ProgressMessage: Message {

static var supportsSecureCoding = true
override class var supportsSecureCoding: Bool {
true
}

let progress: Float

Expand All @@ -23,10 +25,12 @@ class ProgressMessage: NSObject, Message {
required init?(coder: NSCoder) {
progress = coder.decodeFloat(forKey: "progress")

super.init()
super.init(coder: coder)
}

func encode(with coder: NSCoder) {
override func encode(with coder: NSCoder) {
super.encode(with: coder)

coder.encode(progress, forKey: "progress")
}
}
3 changes: 2 additions & 1 deletion Shared/Tunnel/BasePTProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ class BasePTProvider: NEPacketTunnelProvider {
}

override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
let request = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(messageData)
let unarchiver = try? NSKeyedUnarchiver(forReadingFrom: messageData)
let request = unarchiver?.decodeObject(of: Message.self, forKey: NSKeyedArchiveRootObjectKey)

log("#handleAppMessage messageData=\(messageData), request=\(String(describing: request))")

Expand Down
90 changes: 49 additions & 41 deletions Shared/VpnManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -455,42 +455,7 @@ class VpnManager: BridgesConfDelegate {
}

private func commTunnel() {
if (session?.status ?? .invalid) != .invalid {
do {
try session?.sendProviderMessage(Data()) { response in
if let response = response {
if let response = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(response) as? [Message] {
for message in response {
if let pm = message as? ProgressMessage {
print("[\(String(describing: type(of: self)))] ProgressMessage=\(pm.progress)")

DispatchQueue.main.async {
NotificationCenter.default.post(name: .vpnProgress, object: pm.progress)
}
}
else if message is ConfigChangedMessage {
print("[\(String(describing: type(of: self)))] ConfigChangedMessage")

DispatchQueue.main.async {
NotificationCenter.default.post(name: .vpnStatusChanged, object: nil)
}
}
}
}
}
}
}
catch {
NSLog("[\(String(describing: type(of: self)))] "
+ "Could not establish communications channel with extension. "
+ "Error: \(error)")
}

if poll {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: self.commTunnel)
}
}
else {
if (session?.status ?? .invalid) == .invalid {
NSLog("[\(String(describing: type(of: self)))] "
+ "Could not establish communications channel with extension. "
+ "VPN configuration does not exist or is not enabled. "
Expand All @@ -499,6 +464,45 @@ class VpnManager: BridgesConfDelegate {
error = Errors.couldNotConnect

postChange()

return
}

do {
try session?.sendProviderMessage(Data()) { response in
guard let response = response,
let unarchiver = try? NSKeyedUnarchiver(forReadingFrom: response),
let response = unarchiver.decodeArrayOfObjects(ofClass: Message.self, forKey: NSKeyedArchiveRootObjectKey)
else {
return
}

for message in response {
if let pm = message as? ProgressMessage {
print("[\(String(describing: type(of: self)))] ProgressMessage=\(pm.progress)")

DispatchQueue.main.async {
NotificationCenter.default.post(name: .vpnProgress, object: pm.progress)
}
}
else if message is ConfigChangedMessage {
print("[\(String(describing: type(of: self)))] ConfigChangedMessage")

DispatchQueue.main.async {
NotificationCenter.default.post(name: .vpnStatusChanged, object: nil)
}
}
}
}
}
catch {
NSLog("[\(String(describing: type(of: self)))] "
+ "Could not establish communications channel with extension. "
+ "Error: \(error)")
}

if poll {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: self.commTunnel)
}
}

Expand All @@ -518,15 +522,19 @@ class VpnManager: BridgesConfDelegate {
return callback(nil, nil)
}

if T.self is Data.Type {
return callback(response as? T, nil)
}

do {
if let error = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(response) as? Error {
let unarchiver = try NSKeyedUnarchiver(forReadingFrom: response)
unarchiver.requiresSecureCoding = false

if let error = unarchiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as? Error {
callback(nil, error)
}
else if T.self is Data.Type {
callback(response as? T, nil)
}
else {
let payload = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(response) as? T
let payload = unarchiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as? T
callback(payload, nil)
}
}
Expand Down
3 changes: 2 additions & 1 deletion Shared/exit-node-countries.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<string>de</string>
<string>dk</string>
<string>es</string>
<string>eu</string>
<string>fi</string>
<string>fr</string>
<string>gb</string>
Expand All @@ -28,6 +29,7 @@
<string>il</string>
<string>is</string>
<string>it</string>
<string>jp</string>
<string>kr</string>
<string>lt</string>
<string>lu</string>
Expand All @@ -40,7 +42,6 @@
<string>pe</string>
<string>pl</string>
<string>ro</string>
<string>rs</string>
<string>ru</string>
<string>se</string>
<string>sg</string>
Expand Down

0 comments on commit e1372f1

Please sign in to comment.