Skip to content

Commit

Permalink
remote(server): implement autostart, autoblock, and specify port
Browse files Browse the repository at this point in the history
  • Loading branch information
osy committed Feb 13, 2024
1 parent 8ead53f commit 5664ee5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Platform/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct ContentView: View {
@State private var newPopupPresented = false
@State private var openSheetPresented = false
@Environment(\.openURL) var openURL

@AppStorage("ServerAutostart") private var isServerAutostart: Bool = false

var body: some View {
VMNavigationListView()
.overlay(data.showSettingsModal ? AnyView(EmptyView()) : AnyView(BusyOverlay()))
Expand Down Expand Up @@ -70,6 +71,11 @@ struct ContentView: View {
.onAppear {
Task {
await data.listRefresh()
#if os(macOS)
if isServerAutostart {
await data.remoteServer.start()
}
#endif
}
Task {
await releaseHelper.fetchReleaseNotes()
Expand Down
3 changes: 3 additions & 0 deletions Platform/macOS/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ struct ServerSettingsView: View {

@AppStorage("ServerAutostart") var isServerAutostart: Bool = false
@AppStorage("ServerExternal") var isServerExternal: Bool = false
@AppStorage("ServerAutoblock") var isServerAutoblock: Bool = false
@AppStorage("ServerPort") var serverPort: Int = 0
@AppStorage("ServerPasswordRequired") var isServerPasswordRequired: Bool = false
@AppStorage("ServerPassword") var serverPassword: String = ""
Expand All @@ -203,6 +204,8 @@ struct ServerSettingsView: View {
Toggle("Automatically start UTM server", isOn: $isServerAutostart)
}
Section(header: Text("Network")) {
Toggle("Reject unknown connections by default", isOn: $isServerAutoblock)
.help("If checked, you will not be prompted about any unknown connection and they will be rejected.")
Toggle("Allow access from external clients", isOn: $isServerExternal)
.help("By default, the server is only available on LAN but setting this will use UPnP/NAT-PMP to port forward to WAN.")
.onChange(of: isServerExternal) { newValue in
Expand Down
7 changes: 6 additions & 1 deletion Remote/UTMRemoteServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ actor UTMRemoteServer {

@Setting("ServerAutostart") private var isServerAutostart: Bool = false
@Setting("ServerExternal") private var isServerExternal: Bool = false
@Setting("ServerAutoblock") private var isServerAutoblock: Bool = false
@Setting("ServerPort") private var serverPort: Int = 0
@Setting("ServerPasswordRequired") private var isServerPasswordRequired: Bool = false
@Setting("ServerPassword") private var serverPassword: String = ""
Expand Down Expand Up @@ -127,7 +128,8 @@ actor UTMRemoteServer {
registerNotifications()
listener = Task {
await withErrorNotification {
for try await connection in Connection.advertise(forServiceType: service, txtRecord: metadata, identity: keyManager.identity) {
let port = serverPort > 0 ? NWEndpoint.Port(integerLiteral: UInt16(serverPort)) : .any
for try await connection in Connection.advertise(on: port, forServiceType: service, txtRecord: metadata, identity: keyManager.identity) {
if let connection = try? await Connection(connection: connection) {
await newRemoteConnection(connection)
}
Expand Down Expand Up @@ -164,6 +166,9 @@ actor UTMRemoteServer {
if await state.isApproved(fingerprint) {
await notifyNewConnection(remoteAddress: remoteAddress, fingerprint: fingerprint)
await establishConnection(connection)
} else if isServerAutoblock {
await state.block(fingerprint)
connection.close()
} else {
pendingConnections[fingerprint] = connection
await notifyNewConnection(remoteAddress: remoteAddress, fingerprint: fingerprint, isUnknown: true)
Expand Down

0 comments on commit 5664ee5

Please sign in to comment.