I'm a beginner in swift. Ways I tried:
- Tried adding a command line tool DNC observer to call a function when any screen sharing notification triggers, but later came to know that screen sharing doesn’t give any notifications.
import OSLog
import Foundation
os_log("TecMFA:: Starting screen sharing finder.")
let dnc = DistributedNotificationCenter.default()
dnc.addObserver(
forName: .init("com.apple.screensharing.server"), // tried many notification names like com.apple.screensharing.curtain etc.
object: nil,
queue: .main
) { notification in
os_log("TecMFA:: Started screen sharing deamon.")
}
dispatchMain()
- Created a server using vapor as following
//configure.swift
import Vapor
func routes(_ app: Application) throws {
// Define a route to handle POST requests to "/login"
app.post("login") { req -> HTTPStatus in
// Read the username and password from the request body
guard let loginData = try? req.content.decode(LoginData.self) else {
// Failed to parse request body or invalid data
return .badRequest
}
let username = loginData.username
let password = loginData.password
print(username)
print(password)
// Do something with the username and password
print("Received login request with username: \(username) and password: \(password)")
// Return a success response
return .ok
}
}
// Define a struct to represent the request body data
struct LoginData: Content {
let username: String
let password: String
}
// routes.swift
import Vapor
import Foundation
func getLocalIPAddress() -> String? {
let task = Process()
task.launchPath = "/usr/sbin/ipconfig"
task.arguments = ["getifaddr", "en0"] // Use "en0" for Wi-Fi, "en1" for Ethernet
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
return output
}
// Called before your application initializes.
public func configure(_ app: Application) throws {
// Register routes
try routes(app)
// Get the local IP address
guard let localIPAddress = getLocalIPAddress() else {
fatalError("Unable to get the local IP address.")
}
// Update the server configuration to bind to the local IP address and desired port
app.http.server.configuration.hostname = localIPAddress
app.http.server.configuration.port = 8080
}
It didn't work when same port numbers. I tried using different port numbers but the request comes through port 5900, so 8080 cannot access it, so it didn't work either. Any corrections and suggestions are welcome.