2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// IssueReporter.swift
|
2021-01-01 10:13:41 +00:00
|
|
|
// Passepartout-macOS
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
2021-01-01 10:13:41 +00:00
|
|
|
// Created by Davide De Rosa on 9/5/19.
|
2020-12-27 16:30:25 +00:00
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
2018-11-03 21:33:30 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2018-10-26 22:17:28 +00:00
|
|
|
import TunnelKit
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
class IssueReporter: NSObject {
|
|
|
|
static let shared = IssueReporter()
|
|
|
|
|
|
|
|
override private init() {
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
func present(withIssue issue: Issue) {
|
2019-09-05 20:50:56 +00:00
|
|
|
if issue.debugLog {
|
2021-01-01 10:13:41 +00:00
|
|
|
let alert = Macros.warning(L10n.Core.IssueReporter.title, L10n.Core.IssueReporter.message)
|
|
|
|
alert.present(in: nil, withOK: L10n.Core.IssueReporter.Buttons.accept, cancel: L10n.Core.Global.cancel, handler: {
|
2018-10-23 08:25:41 +00:00
|
|
|
VPN.shared.requestDebugLog(fallback: AppConstants.Log.debugSnapshot) {
|
2019-11-07 11:53:12 +00:00
|
|
|
self.composeEmail(withDebugLog: $0, issue: issue)
|
2018-10-23 08:25:41 +00:00
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}, cancelHandler: nil)
|
2018-10-23 08:25:41 +00:00
|
|
|
} else {
|
2019-11-07 11:53:12 +00:00
|
|
|
composeEmail(withDebugLog: nil, issue: issue)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:53:12 +00:00
|
|
|
private func composeEmail(withDebugLog debugLog: String?, issue: Issue) {
|
2021-01-01 10:13:41 +00:00
|
|
|
guard let sharing = NSSharingService(named: .composeEmail) else {
|
|
|
|
// TODO: show error alert
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sharing.recipients = [AppConstants.IssueReporter.Email.recipient]
|
|
|
|
sharing.subject = AppConstants.IssueReporter.Email.subject
|
|
|
|
|
|
|
|
var items: [Any] = []
|
2019-11-07 11:53:12 +00:00
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
// delete temporary files on exit
|
|
|
|
// NO, they're needed until NSSharingService is dismissed (who knows when?)
|
|
|
|
// defer {
|
|
|
|
// for item in items {
|
|
|
|
// guard let url = item as? URL else {
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
// try? FileManager.default.removeItem(at: url)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2019-11-07 11:53:12 +00:00
|
|
|
let bodyContent = AppConstants.IssueReporter.Email.template
|
|
|
|
var bodyMetadata = "--\n\n"
|
|
|
|
bodyMetadata += DebugLog(raw: "").decoratedString()
|
2019-12-11 23:55:34 +00:00
|
|
|
if let metadata = issue.infrastructureMetadata {
|
2019-12-11 23:46:19 +00:00
|
|
|
bodyMetadata += "Provider: \(metadata.description)\n"
|
|
|
|
if let lastUpdated = InfrastructureFactory.shared.modificationDate(forName: metadata.name) {
|
2019-11-07 11:53:12 +00:00
|
|
|
bodyMetadata += "Last updated: \(lastUpdated)\n"
|
|
|
|
}
|
|
|
|
bodyMetadata += "\n"
|
|
|
|
}
|
|
|
|
bodyMetadata += "--"
|
2021-01-01 10:13:41 +00:00
|
|
|
let body = AppConstants.IssueReporter.Email.body(bodyContent, bodyMetadata)
|
|
|
|
items.append(body)
|
2019-11-07 11:53:12 +00:00
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
if let raw = debugLog {
|
|
|
|
let attachment = DebugLog(raw: raw).decoratedData()
|
2021-01-01 10:13:41 +00:00
|
|
|
if let item = attachment.temporaryURL(withFileName: AppConstants.IssueReporter.Filenames.debugLog) {
|
|
|
|
items.append(item)
|
|
|
|
}
|
2018-10-23 08:25:41 +00:00
|
|
|
}
|
2019-11-07 11:53:12 +00:00
|
|
|
if let url = issue.configurationURL {
|
2018-10-26 22:17:28 +00:00
|
|
|
do {
|
2019-05-23 21:13:45 +00:00
|
|
|
let parsedFile = try OpenVPN.ConfigurationParser.parsed(fromURL: url, returnsStripped: true)
|
2021-01-01 10:13:41 +00:00
|
|
|
if let attachment = parsedFile.strippedLines?.joined(separator: "\n").data(using: .utf8),
|
|
|
|
let item = attachment.temporaryURL(withFileName: AppConstants.IssueReporter.Filenames.configuration) {
|
|
|
|
|
|
|
|
items.append(item)
|
2018-10-26 22:17:28 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
guard sharing.canPerform(withItems: items) else {
|
|
|
|
// TODO: show error alert
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sharing.perform(withItems: items)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
private extension Data {
|
|
|
|
func temporaryURL(withFileName fileName: String) -> URL? {
|
|
|
|
let tempURL = URL(fileURLWithPath: NSTemporaryDirectory())
|
|
|
|
let dest = tempURL.appendingPathComponent(fileName)
|
|
|
|
do {
|
|
|
|
try write(to: dest)
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return dest
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|