### Minimal NEPacketTunnelProvider Setup Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Implement a basic NEPacketTunnelProvider to integrate SwiftyXrayKit for running Xray-core. Ensure network settings are applied before starting the Xray bridge. ```swift import NetworkExtension import SwiftyXrayKit class PacketTunnelProvider: NEPacketTunnelProvider { var bridge: XrayBridge? override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) { setTunnelNetworkSettings(makeNetworkSettings()) { [weak self] error in guard let self, error == nil else { completionHandler(error); return } do { try self.startXray() completionHandler(nil) } catch { completionHandler(error) } } } private func startXray() throws { let config = try String(contentsOf: configFileURL, encoding: .utf8) let bridge = XrayBridge(packetFlow: packetFlow) try bridge.start( config: .json(config), dataDir: geoDataDir, finalConfigPath: finalConfigURL ) self.bridge = bridge } override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) { bridge?.stop() bridge = nil completionHandler() } } ``` -------------------------------- ### Start Xray with Share Link Input Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Initiate the Xray bridge using a VMess, VLESS, or other share link. The kit automatically converts the link into a JSON configuration. ```swift try bridge.start(config: .url("vless://..."), dataDir: geoDir, finalConfigPath: finalPath) ``` -------------------------------- ### Swift Package Manager Installation Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Add SwiftyXrayKit to your project's dependencies using Swift Package Manager. ```swift dependencies: [ .package(url: "https://github.com/dima-u/SwiftyXrayKit.git", from: "1.1.0") ] ``` -------------------------------- ### Run with Raw Configuration File Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Bypass all SwiftyXrayKit patching and directly run Xray using a pre-built JSON configuration file. This is useful for complete control over the Xray setup. ```swift try bridge.startWithRawConfig(rawConfigPath: myConfigURL, dataDir: geoDir) ``` -------------------------------- ### Get and Clear Transfer Statistics Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Retrieve and reset the sent and received byte statistics for the Xray bridge. Call this method periodically to monitor data transfer. ```swift // Call periodically to get stats since last call let stats = bridge.getAndClearStats() print("↑ \(stats.sent) ↓ \(stats.received)") ``` -------------------------------- ### Intercept and Transform Config Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Utilize the `configTransform` closure to intercept and modify the generated Xray configuration dictionary before it's used to start Xray. This allows for dynamic adjustments like setting log levels or DNS servers. ```swift try bridge.start(config: .json(config), dataDir: geoDir, finalConfigPath: finalPath, configTransform: { config in var c = config c["log"] = ["loglevel": "warning"] c["dns"] = ["servers": ["1.1.1.1", "8.8.8.8"]] return c }) ``` -------------------------------- ### Apply Tuning Presets Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Use predefined tuning presets like `.mobile` or `.desktop` for optimized performance. The `.default` preset adapts based on the operating system. ```swift try bridge.start(config: .json(config), dataDir: geoDir, finalConfigPath: finalPath, preset: .mobile) ``` -------------------------------- ### Download GeoIP and GeoSite Files Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Download the necessary `geoip.dat` and `geosite.dat` files using `GeoFilesLoader`. The download process provides progress tracking. ```swift let loader = GeoFilesLoader() try await loader.download(to: geoDataDir) { progress in print("Downloading: \(Int(progress * 100))%") } ``` -------------------------------- ### Customize Tuning Presets Source: https://github.com/dima-u/swiftyxraykit/blob/main/README.md Modify individual parameters of a tuning preset, such as `memoryLimitMB` or `tcpMaxInFlight`, to fine-tune Xray's behavior. ```swift var preset = XrayTuningPreset.mobile preset.memoryLimitMB = 40 preset.tcpMaxInFlight = 256 try bridge.start(config: .json(config), dataDir: geoDir, finalConfigPath: finalPath, preset: preset) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.