Skip to content

Commit

Permalink
fix: local installation
Browse files Browse the repository at this point in the history
feat: allow users to use their own files
  • Loading branch information
khcrysalis committed Jan 24, 2025
1 parent 991e375 commit 0c726e9
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 277 deletions.
12 changes: 12 additions & 0 deletions Shared/Data/CoreData/Download/Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ class SourceGET {
}
}

func parseCert(data: Data) -> Result<ServerPack, Error> {
do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let source = try decoder.decode(ServerPack.self, from: data)
return .success(source)
} catch {
Debug.shared.log(message: "Failed to parse JSON: \(error)", type: .error)
return .failure(error)
}
}

func parsec(data: Data) -> Result<[CreditsPerson], Error> {
do {
let decoder = JSONDecoder()
Expand Down
3 changes: 3 additions & 0 deletions Shared/Data/UserDefaults/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ enum Preferences {
// Default repo is from the repository
static var defaultRepos: Bool

@Storage(key: "Feather.gotSSLCerts", defaultValue: false)
static var gotSSLCerts: Bool

@Storage(key: "Feather.BDefaultRepos", defaultValue: false)
// Default beta repo is from the repository
static var bDefaultRepos: Bool
Expand Down
37 changes: 37 additions & 0 deletions Shared/Server/DownloadCertificate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@

import Foundation

func getCertificates() {
let sourceGET = SourceGET()
let dispatchGroup = DispatchGroup()
let uri = URL(string: "https://backloop.dev/pack.json")!

func writeToFile(content: String, filename: String) throws {
let path = getDocumentsDirectory().appendingPathComponent(filename)
try content.write(to: path, atomically: true, encoding: .utf8)
}

dispatchGroup.enter()

defer {
dispatchGroup.leave()
}

sourceGET.downloadURL(from: uri) { result in
switch result {
case .success(let (data, _)):
switch sourceGET.parseCert(data: data) {
case .success(let serverPack):
do {
try writeToFile(content: serverPack.key, filename: "server.pem")
try writeToFile(content: serverPack.cert, filename: "server.crt")
try writeToFile(content: serverPack.info.domains.commonName, filename: "commonName.txt")
} catch {
Debug.shared.log(message: "Error writing files: \(error.localizedDescription)")
}
case .failure(let error):
Debug.shared.log(message: "Error parsing certificate: \(error.localizedDescription)")
}
case .failure(let error):
Debug.shared.log(message: "Error fetching data from \(uri): \(error.localizedDescription)")
}
}
}

func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
Expand Down
49 changes: 49 additions & 0 deletions Shared/Server/Model/ServerPack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// ServerPack.swift
// feather
//
// Created by samara on 23.01.2025.
//

// we should prompt the user to download these when finishing the onboarding, and make the exact same way of downloading the files in the server options
struct ServerPack: Decodable {
var cert: String
var ca: String
var key: String
var info: ServerPackInfo

private enum CodingKeys: String, CodingKey {
case cert, ca, key1, key2, info
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
cert = try container.decode(String.self, forKey: .cert)
ca = try container.decode(String.self, forKey: .ca)
let key1 = try container.decode(String.self, forKey: .key1)
let key2 = try container.decode(String.self, forKey: .key2)
key = key1 + key2
info = try container.decode(ServerPackInfo.self, forKey: .info)
}
}

struct ServerPackInfo: Decodable {
var issuer: Issuer
var domains: Domains
}

struct Issuer: Decodable {
var commonName: String

private enum CodingKeys: String, CodingKey {
case commonName = "commonName"
}
}

struct Domains: Decodable {
var commonName: String

private enum CodingKeys: String, CodingKey {
case commonName = "commonName"
}
}
21 changes: 20 additions & 1 deletion Shared/Server/Server+TLS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ func getLocalIPAddress() -> String? {


extension Installer {
static let sni = Preferences.userSelectedServer ? (getLocalIPAddress() ?? "0.0.0.0") : "0.0.0.0"
static let commonName = getDocumentsDirectory().appendingPathComponent("commonName.txt")

static let sni: String = {
if Preferences.userSelectedServer {
return getLocalIPAddress() ?? "0.0.0.0"
} else {
return readCommonName() ?? "0.0.0.0"
}
}()

static let documentsKeyURL = getDocumentsDirectory().appendingPathComponent("server.pem")
static let documentsCrtURL = getDocumentsDirectory().appendingPathComponent("server.crt")
Expand All @@ -64,3 +72,14 @@ extension Installer {
privateKey: .privateKey(try NIOSSLPrivateKey(file: keyURL.path, format: .pem)))
}
}

extension Installer {
static func readCommonName() -> String? {
do {
return try String(contentsOf: commonName, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
} catch {
Debug.shared.log(message: "Error reading commonName file: \(error.localizedDescription)")
return nil
}
}
}
100 changes: 0 additions & 100 deletions Shared/Server/localhost.direct.crt

This file was deleted.

28 changes: 0 additions & 28 deletions Shared/Server/localhost.direct.key

This file was deleted.

Loading

0 comments on commit 0c726e9

Please sign in to comment.