WIP: UI for adding config

This commit is contained in:
Jett Chen 2024-05-12 01:07:05 +08:00
parent dd3f5d0d92
commit f6241e90d5
4 changed files with 36 additions and 7 deletions

View file

@ -13,7 +13,7 @@
</array> </array>
<key>com.apple.security.application-groups</key> <key>com.apple.security.application-groups</key>
<array> <array>
<string>$(APP_GROUP_IDENTIFIER)</string> <string>group.com.hackclub.burrow</string>
</array> </array>
</dict> </dict>
</plist> </plist>

View file

@ -1,10 +1,13 @@
import AuthenticationServices import AuthenticationServices
import SwiftUI import SwiftUI
import BurrowShared
#if !os(macOS) #if !os(macOS)
struct BurrowView: View { struct BurrowView: View {
@Environment(\.webAuthenticationSession) @Environment(\.webAuthenticationSession)
private var webAuthenticationSession private var webAuthenticationSession
@State private var rpcClient: Client? = nil
@State private var showAlert = false
var body: some View { var body: some View {
NavigationStack { NavigationStack {
@ -17,6 +20,7 @@ struct BurrowView: View {
Menu { Menu {
Button("Hack Club", action: addHackClubNetwork) Button("Hack Club", action: addHackClubNetwork)
Button("WireGuard", action: addWireGuardNetwork) Button("WireGuard", action: addWireGuardNetwork)
Button("Custom", action: sncAddCustomnetwork)
} label: { } label: {
Image(systemName: "plus.circle.fill") Image(systemName: "plus.circle.fill")
.font(.title) .font(.title)
@ -34,7 +38,7 @@ struct BurrowView: View {
.handleOAuth2Callback() .handleOAuth2Callback()
} }
} }
private func addHackClubNetwork() { private func addHackClubNetwork() {
Task { Task {
try await authenticateWithSlack() try await authenticateWithSlack()
@ -42,7 +46,31 @@ struct BurrowView: View {
} }
private func addWireGuardNetwork() { private func addWireGuardNetwork() {
}
private func getClient() throws -> Client {
if self.rpcClient == nil {
let client = try Client()
self.rpcClient = client
}
return self.rpcClient!
}
private func sncAddCustomnetwork() {
Task {
try await addCustomnetwork()
}
}
private func addCustomnetwork() async {
do {
let networkToml = ""
let client = try getClient()
try await client.single_request("AddConfigToml", params: networkToml, type: BurrowResult<AnyResponseData>.self)
alert("Successs!", isPresented: $showAlert){
Button("OK", role: .cancel) {}
}
} catch {
}
} }
private func authenticateWithSlack() async throws { private func authenticateWithSlack() async throws {

View file

@ -41,7 +41,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
override func stopTunnel(with reason: NEProviderStopReason) async { override func stopTunnel(with reason: NEProviderStopReason) async {
do { do {
let client = try Client() let client = try Client()
_ = try await client.single_request("Stop", type: BurrowResult<AnyResponseData>.self) _ = try await client.single_request("Stop", params:nil, type: BurrowResult<AnyResponseData>.self)
self.logger.log("Stopped client.") self.logger.log("Stopped client.")
} catch { } catch {
self.logger.error("Failed to stop tunnel: \(error)") self.logger.error("Failed to stop tunnel: \(error)")
@ -51,7 +51,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
guard let client = self.client else { guard let client = self.client else {
throw BurrowError.noClient throw BurrowError.noClient
} }
let srvConfig = try await client.single_request("ServerConfig", type: BurrowResult<ServerConfig>.self) let srvConfig = try await client.single_request("ServerConfig", params: nil, type: BurrowResult<ServerConfig>.self)
guard let serverconfig = srvConfig.Ok else { guard let serverconfig = srvConfig.Ok else {
throw BurrowError.resultIsError throw BurrowError.resultIsError
} }

View file

@ -81,10 +81,11 @@ public final class Client {
) )
return try await send(req) return try await send(req)
} }
public func single_request<U: Decodable>(_ request: String, type: U.Type = U.self) async throws -> U { public func single_request<U: Decodable>(_ request: String, params: String? = nil, type: U.Type = U.self) async throws -> U {
let req = BurrowSimpleRequest( let req = BurrowSimpleRequest(
id: generator.next(upperBound: UInt.max), id: generator.next(upperBound: UInt.max),
command: request command: request,
params:params
) )
return try await send(req) return try await send(req)
} }