- Notifications
You must be signed in to change notification settings - Fork3
chore: add dylib downloader and validator#16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
3563eb4
48b35f0
f602fa1
8b60368
6398b00
fb36b59
9e1a956
e6208e8
ce4f0da
8453170
5fb8cdd
bfb98f0
f48b106
ae5b3e2
4eac98b
844df27
4d0b3da
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import NetworkExtension | ||
import os | ||
import VPNLib | ||
actor Manager { | ||
let ptp: PacketTunnelProvider | ||
var tunnelHandle: TunnelHandle? | ||
var speaker: Speaker<Vpn_ManagerMessage, Vpn_TunnelMessage>? | ||
// TODO: XPC Speaker | ||
private let dest = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) | ||
.first!.appending(path: "coder-vpn.dylib") | ||
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "manager") | ||
init(with: PacketTunnelProvider) { | ||
ptp = with | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import Foundation | ||
import os | ||
let startSymbol = "OpenTunnel" | ||
actor TunnelHandle { | ||
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "tunnel-handle") | ||
private let tunnelWritePipe: Pipe | ||
private let tunnelReadPipe: Pipe | ||
private let dylibHandle: UnsafeMutableRawPointer | ||
var writeHandle: FileHandle { tunnelReadPipe.fileHandleForWriting } | ||
var readHandle: FileHandle { tunnelWritePipe.fileHandleForReading } | ||
init(dylibPath: URL) throws(TunnelHandleError) { | ||
guard let dylibHandle = dlopen(dylibPath.path, RTLD_NOW | RTLD_LOCAL) else { | ||
throw .dylib(dlerror().flatMap { String(cString: $0) } ?? "UNKNOWN") | ||
} | ||
self.dylibHandle = dylibHandle | ||
guard let startSym = dlsym(dylibHandle, startSymbol) else { | ||
throw .symbol(startSymbol, dlerror().flatMap { String(cString: $0) } ?? "UNKNOWN") | ||
} | ||
let openTunnelFn = unsafeBitCast(startSym, to: OpenTunnel.self) | ||
tunnelReadPipe = Pipe() | ||
tunnelWritePipe = Pipe() | ||
let res = openTunnelFn(tunnelReadPipe.fileHandleForReading.fileDescriptor, | ||
tunnelWritePipe.fileHandleForWriting.fileDescriptor) | ||
guard res == 0 else { | ||
throw .openTunnel(OpenTunnelError(rawValue: res) ?? .unknown) | ||
} | ||
} | ||
// This could be an isolated deinit in Swift 6.1 | ||
func close() throws(TunnelHandleError) { | ||
var errs: [Error] = [] | ||
if dlclose(dylibHandle) == 0 { | ||
errs.append(TunnelHandleError.dylib(dlerror().flatMap { String(cString: $0) } ?? "UNKNOWN")) | ||
} | ||
do { | ||
try writeHandle.close() | ||
} catch { | ||
errs.append(error) | ||
} | ||
do { | ||
try readHandle.close() | ||
} catch { | ||
errs.append(error) | ||
} | ||
if !errs.isEmpty { | ||
throw .close(errs) | ||
} | ||
} | ||
} | ||
enum TunnelHandleError: Error { | ||
case dylib(String) | ||
case symbol(String, String) | ||
case openTunnel(OpenTunnelError) | ||
case pipe(any Error) | ||
case close([any Error]) | ||
var description: String { | ||
switch self { | ||
case let .pipe(err): return "pipe error: \(err)" | ||
case let .dylib(d): return d | ||
case let .symbol(symbol, message): return "\(symbol): \(message)" | ||
case let .openTunnel(error): return "OpenTunnel: \(error.message)" | ||
case let .close(errs): return "close tunnel: \(errs.map(\.localizedDescription).joined(separator: ", "))" | ||
} | ||
} | ||
} | ||
enum OpenTunnelError: Int32 { | ||
case errDupReadFD = -2 | ||
case errDupWriteFD = -3 | ||
case errOpenPipe = -4 | ||
case errNewTunnel = -5 | ||
case unknown = -99 | ||
var message: String { | ||
switch self { | ||
case .errDupReadFD: return "Failed to duplicate read file descriptor" | ||
case .errDupWriteFD: return "Failed to duplicate write file descriptor" | ||
case .errOpenPipe: return "Failed to open the pipe" | ||
case .errNewTunnel: return "Failed to create a new tunnel" | ||
case .unknown: return "Unknown error code" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef CoderPacketTunnelProvider_Bridging_Header_h | ||
#define CoderPacketTunnelProvider_Bridging_Header_h | ||
// GoInt32 OpenTunnel(GoInt32 cReadFD, GoInt32 cWriteFD); | ||
typedef int(*OpenTunnel)(int, int); | ||
#endif /* CoderPacketTunnelProvider_Bridging_Header_h */ |
Uh oh!
There was an error while loading.Please reload this page.