Skip to content

Commit 47f553e

Browse files
author
Stossy11
committed
Update new TunnelProv
1 parent a65c415 commit 47f553e

File tree

2 files changed

+18
-55
lines changed

2 files changed

+18
-55
lines changed

TunnelProv/PacketTunnelProvider.swift

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,68 +33,31 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
3333
settings.ipv4Settings = ipv4
3434

3535
setTunnelNetworkSettings(settings) { error in
36-
if error == nil {
37-
self.readPackets()
38-
}
39-
completionHandler(error)
36+
guard error == nil else { return completionHandler(error) }
37+
self.setPackets()
38+
completionHandler(nil)
4039
}
4140
}
4241

43-
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
44-
completionHandler()
45-
}
46-
47-
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
48-
completionHandler?(messageData)
49-
}
50-
51-
override func sleep(completionHandler: @escaping () -> Void) {
52-
completionHandler()
53-
}
54-
55-
override func wake() {}
56-
57-
private func readPackets() {
58-
packetFlow.readPackets { packets, protocols in
59-
var output = [Data](repeating: Data(), count: packets.count)
60-
61-
for (i, packet) in packets.enumerated() {
62-
guard protocols[i].int32Value == AF_INET, packet.count >= 20 else {
63-
output[i] = packet
64-
continue
42+
func setPackets() {
43+
packetFlow.readPackets { [self] packets, protocols in
44+
let fakeip = self.fakeIpValue
45+
let deviceip = self.deviceIpValue
46+
var modified = packets
47+
for i in modified.indices where protocols[i].int32Value == AF_INET && modified[i].count >= 20 {
48+
modified[i].withUnsafeMutableBytes { bytes in
49+
guard let ptr = bytes.baseAddress?.assumingMemoryBound(to: UInt32.self) else { return }
50+
let src = UInt32(bigEndian: ptr[3])
51+
let dst = UInt32(bigEndian: ptr[4])
52+
if src == deviceip { ptr[3] = fakeip.bigEndian }
53+
if dst == fakeip { ptr[4] = deviceip.bigEndian }
6554
}
66-
67-
output[i] = self.processPacket(packet)
6855
}
69-
70-
self.packetFlow.writePackets(output, withProtocols: protocols)
71-
self.readPackets()
72-
}
73-
}
74-
75-
private func processPacket(_ packet: Data) -> Data {
76-
var bytes = [UInt8](packet)
77-
78-
let srcIP = UInt32(bigEndian: bytes.withUnsafeBytes { $0.load(fromByteOffset: 12, as: UInt32.self) })
79-
let dstIP = UInt32(bigEndian: bytes.withUnsafeBytes { $0.load(fromByteOffset: 16, as: UInt32.self) })
80-
81-
if srcIP == deviceIpValue {
82-
let replacement = fakeIpValue.bigEndian
83-
withUnsafeBytes(of: replacement) { bytes.replaceSubrange(12..<16, with: $0) }
84-
}
85-
if dstIP == fakeIpValue {
86-
let replacement = deviceIpValue.bigEndian
87-
withUnsafeBytes(of: replacement) { bytes.replaceSubrange(16..<20, with: $0) }
56+
self.packetFlow.writePackets(modified, withProtocols: protocols)
57+
setPackets()
8858
}
89-
90-
bytes.swapAt(12, 16)
91-
bytes.swapAt(13, 17)
92-
bytes.swapAt(14, 18)
93-
bytes.swapAt(15, 19)
94-
95-
return Data(bytes)
9659
}
97-
60+
9861
private func ipToUInt32(_ ipString: String) -> UInt32 {
9962
let components = ipString.split(separator: ".")
10063
guard components.count == 4,

0 commit comments

Comments
 (0)