forked from thebaselab/codeapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use websocket to communicate with extension thebaselab#1104
- Loading branch information
1 parent
dcf4ea3
commit b1764a2
Showing
11 changed files
with
585 additions
and
404 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,96 @@ | ||
// | ||
// AppExtensionService.swift | ||
// Code App | ||
// | ||
// Created by Ken Chung on 02/07/2024. | ||
// | ||
|
||
import Dynamic | ||
import Foundation | ||
import ios_system | ||
|
||
class AppExtensionService: NSObject { | ||
static let PORT = 50002 | ||
static let shared = AppExtensionService() | ||
private var task: URLSessionWebSocketTask? = nil | ||
|
||
func startServer() { | ||
let BLE: AnyClass = (NSClassFromString("TlNFeHRlbnNpb24=".base64Decoded()!)!) | ||
let ext = Dynamic(BLE).extensionWithIdentifier("thebaselab.VS-Code.extension", error: nil) | ||
let frameworkDir = Bundle.main.privateFrameworksPath! | ||
let frameworkDirBookmark = try! URL(fileURLWithPath: frameworkDir).bookmarkData() | ||
let item = NSExtensionItem() | ||
item.userInfo = [ | ||
"frameworksDirectoryBookmark": frameworkDirBookmark, | ||
"port": AppExtensionService.PORT, | ||
] | ||
ext.setRequestInterruptionBlock( | ||
{ uuid in | ||
print("Extension server crashed, attempting to restart") | ||
self.startServer() | ||
} as RequestInterruptionBlock) | ||
|
||
ext.beginExtensionRequestWithInputItems( | ||
[item], | ||
completion: { uuid in | ||
let pid = ext.pid(forRequestIdentifier: uuid) | ||
if let uuid = uuid { | ||
print("Started extension request: \(uuid). Extension PID is \(pid)") | ||
} | ||
print("Extension server listening on 127.0.0.1:\(AppExtensionService.PORT)") | ||
} as RequestBeginBlock) | ||
} | ||
func terminate() { | ||
self.task?.cancel() | ||
} | ||
|
||
func call( | ||
args: [String], | ||
t_stdin: UnsafeMutablePointer<FILE>, | ||
t_stdout: UnsafeMutablePointer<FILE> | ||
) async throws { | ||
defer { | ||
self.task = nil | ||
signal(SIGINT, SIG_DFL) | ||
} | ||
|
||
let signalCallback: sig_t = { signal in | ||
AppExtensionService.shared.terminate() | ||
} | ||
signal(SIGINT, signalCallback) | ||
|
||
let task = URLSession.shared.webSocketTask( | ||
with: URL(string: "ws://127.0.0.1:\(String(AppExtensionService.PORT))/websocket")!) | ||
self.task = task | ||
task.resume() | ||
|
||
let handle = FileHandle(fileDescriptor: fileno(t_stdin), closeOnDealloc: false) | ||
handle.readabilityHandler = { fileHandle in | ||
if let str = String(data: fileHandle.availableData, encoding: .utf8) { | ||
Task { | ||
try await task.send(.string(str)) | ||
print("Sending -> \(str)") | ||
} | ||
} | ||
} | ||
|
||
let frame = ExecutionRequestFrame( | ||
args: args, | ||
workingDirectoryBookmark: try? URL( | ||
fileURLWithPath: FileManager.default.currentDirectoryPath | ||
).bookmarkData() | ||
) | ||
try await task.send(.string(frame.stringRepresentation)) | ||
print("Sending -> \(frame.stringRepresentation)") | ||
|
||
while let message = try? await task.receive() { | ||
switch message { | ||
case .string(let text): | ||
print("Receiving <- \(text)") | ||
fputs(text, t_stdout) | ||
default: | ||
continue | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.