-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
92 changed files
with
6,146 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// MuxProxy.swift | ||
// BBackupp | ||
// | ||
// Created by 秋星桥 on 2024/1/4. | ||
// | ||
|
||
import Foundation | ||
import NIO | ||
|
||
class MuxProxy { | ||
let socketPath: URL | ||
|
||
let group: MultiThreadedEventLoopGroup | ||
let bootstrap: ServerBootstrap | ||
let channel: Channel | ||
|
||
init() throws { | ||
socketPath = tempDir.appendingPathComponent("muxd.socket") | ||
group = .init(numberOfThreads: System.coreCount) | ||
bootstrap = ServerBootstrap(group: group) | ||
.serverChannelOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 1) | ||
.childChannelOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 1) | ||
.childChannelInitializer { $0.pipeline.addHandlers([ | ||
BackPressureHandler(), | ||
MuxRequestPreProcess(), | ||
MuxRequestHandler(), | ||
]) } | ||
|
||
print("[*] \(socketPath.path)") | ||
setenv("USBMUXD_SOCKET_ADDRESS", "UNIX:\(socketPath.path)", 1) | ||
|
||
channel = try bootstrap.bind( | ||
unixDomainSocketPath: socketPath.path, | ||
cleanupExistingSocketFile: true | ||
).wait() | ||
print("[*] unix socket is now ready to accept connections") | ||
} | ||
|
||
deinit { try? channel.close().wait() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// MuxRequest.swift | ||
// BBackupp | ||
// | ||
// Created by 秋星桥 on 2024/1/4. | ||
// | ||
|
||
import Foundation | ||
import NIO | ||
|
||
// | ||
// MuxRequest.message | ||
// | ||
// result = 1 | ||
// connect = 2 | ||
// listen = 3 | ||
// deviceAdd = 4 | ||
// deviceRemove = 5 | ||
// devicePaired = 6 | ||
// plist = 8 | ||
// | ||
|
||
struct MuxRequest { | ||
var length: UInt32 // length of message, including header | ||
var version: UInt32 | ||
var message: UInt32 | ||
var tag: UInt32 // responses to this query will echo back this tag | ||
var payload: Data | ||
|
||
init(length: UInt32, version: UInt32, message: UInt32, tag: UInt32, payload: Data, originalData _: Data) { | ||
self.length = length | ||
self.version = version | ||
self.message = message | ||
self.tag = tag | ||
self.payload = payload | ||
} | ||
|
||
enum DecodeError: Error { | ||
case notEnoughData | ||
} | ||
|
||
init(data: Data) throws { | ||
guard data.count > MemoryLayout<UInt32>.size else { | ||
throw DecodeError.notEnoughData | ||
} | ||
length = UInt32(data[0 ... 3].withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
guard data.count == length else { | ||
throw DecodeError.notEnoughData | ||
} | ||
version = UInt32(data[4 ... 7].withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
message = UInt32(data[8 ... 11].withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
tag = UInt32(data[12 ... 15].withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
payload = data[16 ..< length] | ||
|
||
// NSLog("MuxRequest >>>") | ||
// print("\(String(data: payload, encoding: .utf8) ?? "?")") | ||
// NSLog("MuxRequest <<<") | ||
} | ||
|
||
func serialize() -> Data { | ||
var data = Data() | ||
data.append(contentsOf: withUnsafeBytes(of: length) { Data($0) }) | ||
data.append(contentsOf: withUnsafeBytes(of: version) { Data($0) }) | ||
data.append(contentsOf: withUnsafeBytes(of: message) { Data($0) }) | ||
data.append(contentsOf: withUnsafeBytes(of: tag) { Data($0) }) | ||
data.append(payload) | ||
assert(data.count == length) | ||
return data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// MuxRequestHandler.swift | ||
// BBackupp | ||
// | ||
// Created by 秋星桥 on 2024/1/4. | ||
// | ||
|
||
import Foundation | ||
import NIO | ||
|
||
private let upstreamSocket = try! SocketAddress(unixDomainSocketPath: "/var/run/usbmuxd") | ||
|
||
class MuxRequestHandler: ChannelInboundHandler { | ||
typealias InboundIn = ByteBuffer | ||
|
||
var upstreamChannel: Channel? | ||
var pendingBuffer: [NIOAny] = [] | ||
|
||
func channelActive(context: ChannelHandlerContext) { | ||
let bootstrap = ClientBootstrap(group: context.channel.eventLoop) | ||
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) | ||
.channelInitializer { channel in | ||
channel.pipeline.addHandlers([ | ||
BackPressureHandler(), | ||
UpstreamResponsePreProcess(), | ||
UpstreamResponseHandler(downstreamContext: context), | ||
]) | ||
} | ||
|
||
let future = bootstrap.connect(to: upstreamSocket) | ||
future.whenSuccess { channel in | ||
self.upstreamChannel = channel | ||
self.flushPendingRequests() | ||
} | ||
future.whenFailure { _ in | ||
context.close(promise: nil) | ||
} | ||
} | ||
|
||
func channelRead(context _: ChannelHandlerContext, data: NIOAny) { | ||
pendingBuffer.append(data) | ||
flushPendingRequests() | ||
} | ||
|
||
func flushPendingRequests() { | ||
guard let upstreamChannel else { return } | ||
pendingBuffer.forEach { upstreamChannel.write($0, promise: nil) } | ||
pendingBuffer.removeAll() | ||
upstreamChannel.flush() | ||
} | ||
|
||
func channelInactive(context _: ChannelHandlerContext) { | ||
upstreamChannel?.close(promise: nil) | ||
} | ||
|
||
func errorCaught(context: ChannelHandlerContext, error _: Error) { | ||
context.close(promise: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// MuxRequestPreProcess.swift | ||
// BBackupp | ||
// | ||
// Created by 秋星桥 on 2024/1/4. | ||
// | ||
|
||
import Foundation | ||
import NIO | ||
|
||
class MuxRequestPreProcess: ChannelInboundHandler { | ||
typealias InboundIn = ByteBuffer | ||
typealias InboundOut = ByteBuffer | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
// _ = try? MuxRequest(data: Data(unwrapInboundIn(data).readableBytesView)) | ||
context.fireChannelRead(data) | ||
} | ||
|
||
func channelActive(context: ChannelHandlerContext) { | ||
context.fireChannelActive() | ||
} | ||
|
||
func channelInactive(context: ChannelHandlerContext) { | ||
context.fireChannelInactive() | ||
} | ||
} | ||
|
||
extension MuxRequestPreProcess { | ||
func shouldProcessToUpstream(data _: Data) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// MuxResponse.swift | ||
// BBackupp | ||
// | ||
// Created by 秋星桥 on 2024/1/4. | ||
// | ||
|
||
import Foundation | ||
import NIO | ||
|
||
// | ||
// MuxResponse.result | ||
// | ||
// ok = 0 | ||
// badCommand = 1 | ||
// badDev = 2 | ||
// connRefused = 3 | ||
// badVersion = 6 | ||
// | ||
|
||
struct MuxResponse { | ||
var length: UInt32 | ||
var version: UInt32 | ||
var message: UInt32 | ||
var tag: UInt32 | ||
var payload: Data | ||
|
||
init(length: UInt32, version: UInt32, message: UInt32, tag: UInt32, /* result: UInt32, */ payload: Data) { | ||
self.length = length | ||
self.version = version | ||
self.message = message | ||
self.tag = tag | ||
self.payload = payload | ||
} | ||
|
||
enum DecodeError: Error { | ||
case notEnoughData | ||
} | ||
|
||
init(data: Data) throws { | ||
guard data.count > MemoryLayout<UInt32>.size else { throw DecodeError.notEnoughData } | ||
length = UInt32(data[0 ... 3].withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
guard data.count == length else { throw DecodeError.notEnoughData } | ||
version = UInt32(data[4 ... 7].withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
message = UInt32(data[8 ... 11].withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
tag = UInt32(data[12 ... 15].withUnsafeBytes { $0.load(as: UInt32.self) }) | ||
payload = data[16 ..< length] | ||
|
||
// NSLog("MuxResponse >>>") | ||
// print("\(String(data: payload, encoding: .utf8) ?? "?")") | ||
// NSLog("MuxResponse <<<") | ||
} | ||
|
||
func serialize() -> Data { | ||
var data = Data() | ||
data.append(contentsOf: withUnsafeBytes(of: length) { Data($0) }) | ||
data.append(contentsOf: withUnsafeBytes(of: version) { Data($0) }) | ||
data.append(contentsOf: withUnsafeBytes(of: message) { Data($0) }) | ||
data.append(contentsOf: withUnsafeBytes(of: tag) { Data($0) }) | ||
data.append(payload) | ||
assert(data.count == length) | ||
return data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// UpstreamResponseHandler.swift | ||
// BBackupp | ||
// | ||
// Created by 秋星桥 on 2024/1/4. | ||
// | ||
|
||
import Foundation | ||
import NIO | ||
|
||
class UpstreamResponseHandler: ChannelInboundHandler { | ||
typealias InboundIn = ByteBuffer | ||
let downstreamContext: ChannelHandlerContext | ||
|
||
init(downstreamContext: ChannelHandlerContext) { | ||
self.downstreamContext = downstreamContext | ||
} | ||
|
||
func channelRead(context _: ChannelHandlerContext, data: NIOAny) { | ||
downstreamContext.writeAndFlush(data, promise: nil) | ||
} | ||
|
||
func errorCaught(context: ChannelHandlerContext, error _: Error) { | ||
context.close(promise: nil) | ||
} | ||
} |
Oops, something went wrong.