Compare commits

..

9 Commits

Author SHA1 Message Date
Christoph Hagen
ec309e77db Random messages 2022-09-27 21:38:16 +02:00
Christoph Hagen
c19bc3c299 Remove pull-down 2022-09-27 20:51:20 +02:00
Christoph Hagen
5cc7782d4e Log token info 2022-09-05 19:28:44 +02:00
Christoph Hagen
2f0827a430 Configure file log 2022-09-05 19:27:31 +02:00
Christoph Hagen
34edc88611 Extract APNS code to own type 2022-09-05 18:16:10 +02:00
Christoph Hagen
f2f802a24f Store tokens on disk 2022-09-05 18:10:42 +02:00
Christoph Hagen
151c6a432f Remove tokens from config 2022-09-05 18:00:11 +02:00
Christoph Hagen
baf3a1c06d Show config error as status 2022-09-04 17:09:51 +02:00
Christoph Hagen
ec0b94ee39 Rename application for clarity 2022-09-04 15:55:30 +02:00
8 changed files with 157 additions and 90 deletions

View File

@ -29,7 +29,7 @@ let package = Package(
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
]
),
.executableTarget(name: "Run", dependencies: [.target(name: "App")]),
.executableTarget(name: "FlurSchnaps-Server", dependencies: [.target(name: "App")]),
.testTarget(name: "AppTests", dependencies: [
.target(name: "App"),
.product(name: "XCTVapor", package: "vapor"),

View File

@ -0,0 +1,89 @@
import Foundation
import APNSwift
import NIO
import Crypto
struct APNSInterface {
private struct Payload: Codable {}
private var apnsConfiguration: APNSClientConfiguration!
private let title: String
private let messages: [String]
private let topic: String
private let apnsEventGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
private let apnsRequestEncoder = JSONEncoder()
private let apnsResponseDecoder = JSONDecoder()
init?(_ config: ServerConfiguration) {
guard let key = load(keyAtPath: config.privateKeyFilePath) else {
return nil
}
let authentication = APNSClientConfiguration.AuthenticationMethod.jwt(
privateKey: key,
keyIdentifier: config.keyIdentifier,
teamIdentifier: config.teamIdentifier)
apnsConfiguration = APNSClientConfiguration(
authenticationMethod: authentication,
environment: .sandbox)
title = config.messageTitle
messages = config.messages
topic = config.topic
}
func sendPush(to tokens: Set<String>) async {
let client = APNSClient(
configuration: apnsConfiguration,
eventLoopGroupProvider: .shared(apnsEventGroup),
responseDecoder: apnsResponseDecoder,
requestEncoder: apnsRequestEncoder)
log(info: "Client created")
let alert = APNSAlertNotificationContent(
title: .raw(title),
body: .raw(messages.randomElement()!))
let apnsNotification = APNSAlertNotification(
alert: alert,
expiration: .none,
priority: .immediately,
topic: topic,
payload: Payload(),
sound: .default)
do {
for token in tokenStorage.tokens {
log(info: "Sending push to \(token.prefix(6))...")
try await client.sendAlertNotification(
apnsNotification,
deviceToken: token,
deadline: .now() + .seconds(10))
log(info: "Sent push to \(token.prefix(6))...")
}
} catch let error as APNSError {
print(error)
} catch {
log(error: error.localizedDescription)
}
do {
log("Closing client")
try client.syncShutdown()
} catch {
log(error: error.localizedDescription)
}
}
}
private func load(keyAtPath path: String) -> P256.Signing.PrivateKey? {
do {
guard let key = try P256.Signing.PrivateKey.loadFrom(filePath: path) else {
print("Failed to read key \(path)")
return nil
}
return key
} catch {
print("Failed to load key \(error)")
return nil
}
}

View File

@ -16,11 +16,9 @@ struct ServerConfiguration: Codable {
let messageTitle: String
let messageBody: String
let messages: [String]
let buttonPin: Int
let bounceTime: Double
let tokens: [String]
}

View File

@ -9,4 +9,6 @@ enum ServerStatus: Int, Codable {
case pushFailed = 2
case starting = 3
case failedToStart = 4
}

View File

@ -0,0 +1,47 @@
import Foundation
import BinaryCodable
struct TokenStorage {
private(set) var tokens: Set<String>
private let fileUrl: URL
private let encoder = BinaryEncoder()
init(in folder: URL) {
tokens = []
fileUrl = folder.appendingPathComponent("tokens")
loadTokensFromDisk()
}
private mutating func loadTokensFromDisk() {
guard FileManager.default.fileExists(atPath: fileUrl.path) else {
log(info: "No tokens loaded")
return
}
do {
let data = try Data(contentsOf: fileUrl)
tokens = try BinaryDecoder().decode(from: data)
log(info: "\(tokens.count) tokens loaded")
} catch {
log(error: "Failed to read token file: \(error)")
}
}
mutating func add(_ tokenUpload: TokenUpload) {
if let oldToken = tokenUpload.previousToken {
tokens.remove(oldToken.hex)
}
tokens.insert(tokenUpload.currentToken.hex)
do {
let data = try encoder.encode(tokens)
try data.write(to: fileUrl)
} catch {
log(error: "Failed to write token file: \(error)")
}
}
}

View File

@ -1,16 +1,8 @@
import Vapor
import SwiftyGPIO
import APNSwift
import NIO
import Crypto
private struct Payload: Codable {}
private var apnsConfiguration: APNSClientConfiguration!
private var apnsNotification: APNSAlertNotification<Payload>!
private let apnsEventGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
private let apnsRequestEncoder = JSONEncoder()
private let apnsResponseDecoder = JSONDecoder()
var apns: APNSInterface?
var tokenStorage: TokenStorage!
// configures your application
public func configure(_ app: Application) throws {
@ -23,13 +15,18 @@ public func configure(_ app: Application) throws {
let data = try Data(contentsOf: configUrl)
config = try JSONDecoder().decode(from: data)
} catch {
print("Failed to load config: \(error)")
print("Failed to load config from \(configUrl.path): \(error)")
return
}
Log.set(logFile: URL(fileURLWithPath: config.logPath))
tokenStorage = .init(in: resourcesFolderUrl)
app.http.server.configuration.port = config.port
guard configureAPNS(config) else {
apns = .init(config)
guard apns != nil else {
serverStatus = .failedToStart
return
}
@ -39,6 +36,7 @@ public func configure(_ app: Application) throws {
try routes(app)
serverStatus = .running
log(info: "Server is running")
}
private extension JSONDecoder {
@ -48,47 +46,6 @@ private extension JSONDecoder {
}
}
private func load(keyAtPath path: String) -> P256.Signing.PrivateKey? {
do {
guard let key = try P256.Signing.PrivateKey.loadFrom(filePath: path) else {
print("Failed to read key \(path)")
return nil
}
return key
} catch {
print("Failed to load key \(error)")
return nil
}
}
private func configureAPNS(_ config: ServerConfiguration) -> Bool {
guard let key = load(keyAtPath: config.privateKeyFilePath) else {
return false
}
let authentication = APNSClientConfiguration.AuthenticationMethod.jwt(
privateKey: key,
keyIdentifier: config.keyIdentifier,
teamIdentifier: config.teamIdentifier)
apnsConfiguration = APNSClientConfiguration(
authenticationMethod: authentication,
environment: .sandbox)
let alert = APNSAlertNotificationContent(
title: .raw(config.messageTitle),
body: .raw(config.messageBody))
apnsNotification = APNSAlertNotification(
alert: alert,
expiration: .none,
priority: .immediately,
topic: config.topic,
payload: Payload(),
sound: .default)
return true
}
private func configureGPIO(_ config: ServerConfiguration) {
let gpio = RaspberryGPIO(
name: "GPIO\(config.buttonPin)",
@ -96,7 +53,7 @@ private func configureGPIO(_ config: ServerConfiguration) {
baseAddr: 0x7E000000)
gpio.direction = .IN
gpio.pull = .down
gpio.pull = .neither
gpio.bounceTime = config.bounceTime
gpio.onChange { _ in
log(info: "Push detected")
@ -106,32 +63,11 @@ private func configureGPIO(_ config: ServerConfiguration) {
}
private func sendPush() {
guard !tokenStorage.tokens.isEmpty else {
log(info: "No tokens registered to send push")
return
}
Task(priority: .userInitiated) {
let client = APNSClient(
configuration: apnsConfiguration,
eventLoopGroupProvider: .shared(apnsEventGroup),
responseDecoder: apnsResponseDecoder,
requestEncoder: apnsRequestEncoder)
log(info: "Client created")
do {
for token in knownTokens {
log(info: "Sending push to \(token.prefix(6))...")
try await client.sendAlertNotification(
apnsNotification,
deviceToken: token,
deadline: .now() + .seconds(10))
log(info: "Sent push to \(token.prefix(6))...")
}
} catch let error as APNSError {
print(error)
} catch {
log(error: error.localizedDescription)
}
do {
log("Closing client")
try client.syncShutdown()
} catch {
log(error: error.localizedDescription)
}
await apns?.sendPush(to: tokenStorage.tokens)
}
}

View File

@ -6,8 +6,6 @@ private let requestDecoder = BinaryDecoder()
var serverStatus: ServerStatus = .starting
var knownTokens = Set<String>()
func routes(_ app: Application) throws {
app.post("token") { req async throws -> HTTPResponseStatus in
@ -15,10 +13,7 @@ func routes(_ app: Application) throws {
return .badRequest
}
let tokenUpload: TokenUpload = try requestDecoder.decode(from: data)
if let oldToken = tokenUpload.previousToken {
knownTokens.remove(oldToken.hex)
}
knownTokens.insert(tokenUpload.currentToken.hex)
tokenStorage.add(tokenUpload)
return .ok
}