checkpoint

This commit is contained in:
Jett Chen 2023-12-10 03:44:31 +08:00
parent ede0d13bca
commit db1750a045
39 changed files with 514 additions and 359 deletions

View file

@ -113,7 +113,7 @@ final class BurrowIpc {
return data
}
func request<U: Decodable>(_ request: Request, type: U.Type) async throws -> U {
func request<U: Decodable>(_ request: any Request, type: U.Type) async throws -> U {
do {
var data: Data = try JSONEncoder().encode(request)
data.append(contentsOf: [10])

View file

@ -7,16 +7,40 @@ enum BurrowError: Error {
case resultIsNone
}
protocol Request: Codable {
protocol Request: Codable where T: Codable{
associatedtype T
var id: UInt { get set }
var command: String { get set }
var command: T { get set }
}
struct BurrowRequest: Request {
struct BurrowSingleCommand: Request {
var id: UInt
var command: String
}
struct BurrowRequest<T>: Request where T: Codable{
var id: UInt
var command: T
}
struct BurrowStartRequest: Codable {
struct TunOptions: Codable{
let name: String?
let no_pi: Bool
let tun_excl: Bool
let seek_utun: Int?
let address: String?
}
struct StartOptions: Codable{
let tun: TunOptions
}
let Start: StartOptions
}
func start_req_fd(id: UInt, fd: Int) -> BurrowRequest<BurrowStartRequest> {
return BurrowRequest(id: id, command: BurrowStartRequest(Start: BurrowStartRequest.StartOptions(tun: BurrowStartRequest.TunOptions(name: nil, no_pi: false, tun_excl: false, seek_utun: fd, address: nil))))
}
struct Response<T>: Decodable where T: Decodable {
var id: UInt
var result: T

View file

@ -17,7 +17,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
logger.info("Started server")
Task {
do {
let command = BurrowRequest(id: 0, command: "ServerConfig")
let command = BurrowSingleCommand(id: 0, command: "ServerConfig")
guard let data = try await client?.request(command, type: Response<BurrowResult<ServerConfigData>>.self)
else {
throw BurrowError.cantParseResult
@ -32,6 +32,16 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
try await self.setTunnelNetworkSettings(tunNs)
self.logger.info("Set remote tunnel address to \(tunNs.tunnelRemoteAddress)")
// let tunFd = self.packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int;
// self.logger.info("Found File Descriptor: \(tunFd)")
let start_command = start_req_fd(id: 1, fd: 0)
guard let data = try await client?.request(start_command, type: Response<BurrowResult<String>>.self)
else {
throw BurrowError.cantParseResult
}
let encoded_startres = try JSONEncoder().encode(data.result)
self.logger.log("Received start server response: \(String(decoding: encoded_startres, as: UTF8.self))")
completionHandler(nil)
} catch {
self.logger.error("An error occurred: \(error)")