2019-05-23 18:15:42 +00:00
|
|
|
//
|
|
|
|
// NEUDPLink.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 5/23/19.
|
2021-07-22 08:24:24 +00:00
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
2019-05-23 18:15:42 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// TunnelKit is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import NetworkExtension
|
|
|
|
|
|
|
|
class NEUDPLink: LinkInterface {
|
|
|
|
private let impl: NWUDPSession
|
|
|
|
|
|
|
|
private let maxDatagrams: Int
|
|
|
|
|
2019-09-03 20:11:53 +00:00
|
|
|
let xorMask: UInt8
|
|
|
|
|
|
|
|
init(impl: NWUDPSession, maxDatagrams: Int? = nil, xorMask: UInt8?) {
|
2019-05-23 18:15:42 +00:00
|
|
|
self.impl = impl
|
|
|
|
self.maxDatagrams = maxDatagrams ?? 200
|
2019-09-03 20:11:53 +00:00
|
|
|
self.xorMask = xorMask ?? 0
|
2019-05-23 18:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: LinkInterface
|
|
|
|
|
|
|
|
let isReliable: Bool = false
|
|
|
|
|
|
|
|
var remoteAddress: String? {
|
|
|
|
return (impl.resolvedEndpoint as? NWHostEndpoint)?.hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
var packetBufferSize: Int {
|
|
|
|
return maxDatagrams
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
|
|
|
|
|
|
|
|
// WARNING: runs in Network.framework queue
|
2019-09-03 20:11:53 +00:00
|
|
|
impl.setReadHandler({ [weak self] packets, error in
|
|
|
|
guard let self = self else {
|
2019-05-23 18:15:42 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-03 20:11:53 +00:00
|
|
|
var packetsToUse: [Data]?
|
|
|
|
if let packets = packets, self.xorMask != 0 {
|
|
|
|
packetsToUse = packets.map { packet in
|
|
|
|
Data(bytes: packet.map { $0 ^ self.xorMask }, count: packet.count)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
packetsToUse = packets
|
|
|
|
}
|
2019-05-23 18:15:42 +00:00
|
|
|
queue.sync {
|
2019-09-03 20:11:53 +00:00
|
|
|
handler(packetsToUse, error)
|
2019-05-23 18:15:42 +00:00
|
|
|
}
|
2019-09-03 20:11:53 +00:00
|
|
|
}, maxDatagrams: maxDatagrams)
|
2019-05-23 18:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
|
2019-09-03 20:11:53 +00:00
|
|
|
var dataToUse: Data
|
|
|
|
if xorMask != 0 {
|
|
|
|
dataToUse = Data(bytes: packet.map { $0 ^ xorMask }, count: packet.count)
|
|
|
|
} else {
|
|
|
|
dataToUse = packet
|
|
|
|
}
|
|
|
|
impl.writeDatagram(dataToUse) { error in
|
2019-05-23 18:15:42 +00:00
|
|
|
completionHandler?(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
|
2019-09-03 20:11:53 +00:00
|
|
|
var packetsToUse: [Data]
|
|
|
|
if xorMask != 0 {
|
|
|
|
packetsToUse = packets.map { packet in
|
|
|
|
Data(bytes: packet.map { $0 ^ xorMask }, count: packet.count)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
packetsToUse = packets
|
|
|
|
}
|
|
|
|
impl.writeMultipleDatagrams(packetsToUse) { error in
|
2019-05-23 18:15:42 +00:00
|
|
|
completionHandler?(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 19:49:20 +00:00
|
|
|
/// :nodoc:
|
2019-05-23 18:15:42 +00:00
|
|
|
extension NEUDPSocket: LinkProducer {
|
2019-09-03 20:11:53 +00:00
|
|
|
public func link(xorMask: UInt8?) -> LinkInterface {
|
|
|
|
return NEUDPLink(impl: impl, maxDatagrams: nil, xorMask: xorMask)
|
2019-05-23 18:15:42 +00:00
|
|
|
}
|
|
|
|
}
|