2019-05-23 18:15:42 +00:00
|
|
|
//
|
|
|
|
// NETCPLink.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 5/23/19.
|
2023-03-17 15:58:36 +00:00
|
|
|
// Copyright (c) 2023 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
|
2021-10-25 14:27:27 +00:00
|
|
|
import TunnelKitCore
|
|
|
|
import TunnelKitAppExtension
|
2022-11-06 16:46:10 +00:00
|
|
|
import TunnelKitOpenVPNCore
|
2021-11-07 20:54:05 +00:00
|
|
|
import CTunnelKitOpenVPNProtocol
|
2019-05-23 18:15:42 +00:00
|
|
|
|
|
|
|
class NETCPLink: LinkInterface {
|
|
|
|
private let impl: NWTCPConnection
|
|
|
|
|
|
|
|
private let maxPacketSize: Int
|
2022-11-06 16:46:10 +00:00
|
|
|
|
|
|
|
private let xorMethod: OpenVPN.XORMethod?
|
|
|
|
|
|
|
|
private let xorMask: Data?
|
|
|
|
|
|
|
|
init(impl: NWTCPConnection, maxPacketSize: Int? = nil, xorMethod: OpenVPN.XORMethod?) {
|
2019-05-23 18:15:42 +00:00
|
|
|
self.impl = impl
|
|
|
|
self.maxPacketSize = maxPacketSize ?? (512 * 1024)
|
2022-11-06 16:46:10 +00:00
|
|
|
self.xorMethod = xorMethod
|
|
|
|
xorMask = xorMethod?.mask
|
2019-05-23 18:15:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: LinkInterface
|
|
|
|
|
|
|
|
let isReliable: Bool = true
|
|
|
|
|
|
|
|
var remoteAddress: String? {
|
2022-10-13 16:44:44 +00:00
|
|
|
(impl.remoteAddress as? NWHostEndpoint)?.hostname
|
2019-05-23 18:15:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 16:44:44 +00:00
|
|
|
var remoteProtocol: String? {
|
|
|
|
guard let remote = impl.remoteAddress as? NWHostEndpoint else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return "TCP:\(remote.port)"
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:15:42 +00:00
|
|
|
var packetBufferSize: Int {
|
|
|
|
return maxPacketSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
|
|
|
|
loopReadPackets(queue, Data(), handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func loopReadPackets(_ queue: DispatchQueue, _ buffer: Data, _ handler: @escaping ([Data]?, Error?) -> Void) {
|
|
|
|
|
|
|
|
// WARNING: runs in Network.framework queue
|
|
|
|
impl.readMinimumLength(2, maximumLength: packetBufferSize) { [weak self] (data, error) in
|
2019-09-03 20:11:53 +00:00
|
|
|
guard let self = self else {
|
2019-05-23 18:15:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
queue.sync {
|
|
|
|
guard (error == nil), let data = data else {
|
|
|
|
handler(nil, error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var newBuffer = buffer
|
|
|
|
newBuffer.append(contentsOf: data)
|
|
|
|
var until = 0
|
2022-11-06 16:46:10 +00:00
|
|
|
let packets = PacketStream.packets(
|
|
|
|
fromInboundStream: newBuffer,
|
|
|
|
until: &until,
|
|
|
|
xorMethod: self.xorMethod?.native ?? .none,
|
|
|
|
xorMask: self.xorMask
|
|
|
|
)
|
2019-05-23 18:15:42 +00:00
|
|
|
newBuffer = newBuffer.subdata(in: until..<newBuffer.count)
|
2019-09-03 20:11:53 +00:00
|
|
|
self.loopReadPackets(queue, newBuffer, handler)
|
2019-05-23 18:15:42 +00:00
|
|
|
|
|
|
|
handler(packets, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
|
2022-11-06 16:46:10 +00:00
|
|
|
let stream = PacketStream.outboundStream(
|
|
|
|
fromPacket: packet,
|
|
|
|
xorMethod: xorMethod?.native ?? .none,
|
|
|
|
xorMask: xorMask
|
|
|
|
)
|
2019-05-23 18:15:42 +00:00
|
|
|
impl.write(stream) { (error) in
|
|
|
|
completionHandler?(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
|
2022-11-06 16:46:10 +00:00
|
|
|
let stream = PacketStream.outboundStream(
|
|
|
|
fromPackets: packets,
|
|
|
|
xorMethod: xorMethod?.native ?? .none,
|
|
|
|
xorMask: xorMask
|
|
|
|
)
|
2019-05-23 18:15:42 +00:00
|
|
|
impl.write(stream) { (error) in
|
|
|
|
completionHandler?(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NETCPSocket: LinkProducer {
|
2022-11-06 16:46:10 +00:00
|
|
|
public func link(userObject: Any?) -> LinkInterface {
|
|
|
|
let xorMethod = userObject as? OpenVPN.XORMethod
|
|
|
|
return NETCPLink(impl: impl, maxPacketSize: nil, xorMethod: xorMethod)
|
2019-05-23 18:15:42 +00:00
|
|
|
}
|
|
|
|
}
|