Skip to content

Commit

Permalink
Sync Update - 60a45f0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakr233 committed Apr 16, 2024
1 parent b89bd4c commit 0c0589b
Show file tree
Hide file tree
Showing 92 changed files with 6,146 additions and 18 deletions.
6 changes: 3 additions & 3 deletions BBackupp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,13 @@
buildConfigurationList = 506A0C242B46A119009C77A5 /* Build configuration list for PBXNativeTarget "BBackupp" */;
buildPhases = (
5054D8072BA8405000A7182C /* Run Swift Format @ Release */,
506A0C112B46A117009C77A5 /* Sources */,
506A0C122B46A117009C77A5 /* Frameworks */,
506A0C132B46A117009C77A5 /* Resources */,
50C52C492BA307E800DDA49D /* Embed MobileBackup */,
506A0C112B46A117009C77A5 /* Sources */,
501A477A2BA456BE00C6A431 /* Embed Restic */,
50C60CC12BA5622700DA4B3D /* Embed Pget */,
506A0C122B46A117009C77A5 /* Frameworks */,
501A47812BA45AAE00C6A431 /* Scan License @ Release */,
506A0C132B46A117009C77A5 /* Resources */,
50CA7B3A2BA842920012F487 /* Bump & Overwrite Version */,
);
buildRules = (
Expand Down
1 change: 1 addition & 0 deletions BBackupp/Backend/Backup/BackupPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class BackupPlan: ObservableObject, Codable, CopyableCodable, Identifiable {
struct BinaryExecutor: Codable, Identifiable {
var id: UUID = .init()
var name: String = ""
var path: String = ""
var enabled: Bool = false
}

Expand Down
4 changes: 3 additions & 1 deletion BBackupp/Backend/Backup/BackupTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ class BackupTask: ObservableObject, Identifiable {
overall.completedUnitCount = 100
self.recp = recp
status = .completed
if error == nil { error = .unexpectedExitCode }
if recp.exitCode != 0, error == nil {
error = .unexpectedExitCode
}
}

private var buffer = ""
Expand Down
41 changes: 41 additions & 0 deletions BBackupp/Backend/MuxProxy/MuxProxy.swift
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() }
}
70 changes: 70 additions & 0 deletions BBackupp/Backend/MuxProxy/MuxRequest.swift
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
}
}
59 changes: 59 additions & 0 deletions BBackupp/Backend/MuxProxy/MuxRequestHandler.swift
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)
}
}
31 changes: 31 additions & 0 deletions BBackupp/Backend/MuxProxy/MuxRequestPreProcess.swift
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) {}
}
64 changes: 64 additions & 0 deletions BBackupp/Backend/MuxProxy/MuxResponse.swift
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
}
}
26 changes: 26 additions & 0 deletions BBackupp/Backend/MuxProxy/UpstreamResponseHandler.swift
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)
}
}
Loading

0 comments on commit 0c0589b

Please sign in to comment.