-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionManager.swift
153 lines (123 loc) · 5.07 KB
/
ConnectionManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// This source file is part of the https://github.com/ColdGrub1384/Pisth open source project
//
// Copyright (c) 2017 - 2018 Adrian Labbé
// Licensed under Apache License v2.0
//
// See https://raw.githubusercontent.com/ColdGrub1384/Pisth/master/LICENSE for license information
import NMSSH
import Pisth_Shared
private let sharedInstance = ConnectionManager()
private let importInstance = ConnectionManager()
/// A class that manages SSH connections.
class ConnectionManager {
/// Shared instance of `ConnectionManager`. Different instance is returned for importing file with the api.
static var shared: ConnectionManager {
if AppDelegate.shared.action != nil {
return importInstance
} else {
return sharedInstance
}
}
/// Initialize a `ConnectionManager` for managing given connection.
///
/// - Parameters:
/// - connection: Connection used to connect.
init(connection: RemoteConnection? = nil) {
self.connection = connection
}
/// Background task to keep the session active.
var backgroundTask: UIBackgroundTaskIdentifier?
// NMSSH cannot download files and write to an SSH Shell at same time, so two sessions are used.
/// Session used for SSH Shell.
var session: NMSSHSession?
/// Session used for reading and writing files.
var filesSession: NMSSHSession?
/// Text file to be uploaded after being edited.
var saveFile: SaveFile?
/// Representation of the connection to use.
var connection: RemoteConnection?
/// Representation of the result connecting.
var result = ConnectionResult.notConnected
/// List files in directory.
/// - Parameters:
/// - directory: Directory where list files.
/// - Returns: Files listed, nil in case of error.
func files(inDirectory directory: String, showHiddenFiles: Bool = false) -> [NMSFTPFile]? {
guard let session = filesSession else { return nil }
guard var files = session.sftp.contentsOfDirectory(atPath: directory) else {
return nil
}
if !showHiddenFiles { // Remove hidden files if is necessary
for file in files {
if file.filename.hasPrefix(".") {
guard let i = files.index(of: file) else { break }
files.remove(at: i)
}
}
}
return files
}
/// Open SSH sessions for `connection`.
func connect() {
guard let connection = connection else {
result = .notConnected
return
}
session = NMSSHSession(host: connection.host, port: Int(connection.port), andUsername: connection.username)
session?.connect()
if session!.isConnected {
result = .connected
if let privKey = connection.privateKey {
session?.authenticateBy(inMemoryPublicKey: connection.publicKey, privateKey: privKey, andPassword: connection.password)
} else {
session?.authenticate(byPassword: connection.password)
}
} else {
result = .notConnected
return
}
if session!.isConnected && session!.isAuthorized {
if connection.useSFTP {
session?.sftp.connect()
}
result = .connectedAndAuthorized
} else {
return
}
if connection.useSFTP {
filesSession = NMSSHSession(host: connection.host, port: Int(connection.port), andUsername: connection.username)
filesSession?.connect()
if filesSession!.isConnected {
result = .connected
if let privKey = connection.privateKey {
filesSession?.authenticateBy(inMemoryPublicKey: connection.publicKey, privateKey: privKey, andPassword: connection.password)
} else {
filesSession?.authenticate(byPassword: connection.password)
}
} else {
result = .notConnected
return
}
if filesSession!.isConnected && filesSession!.isAuthorized {
filesSession?.sftp.connect()
result = .connectedAndAuthorized
} else {
return
}
}
session!.channel.requestPty = true
session!.channel.ptyTerminalType = .xterm
if result == .connectedAndAuthorized {
do {
// Start the shell to not close the connection when enter in background
if connection.useSFTP {
try session?.channel.startShell()
try filesSession?.channel.startShell()
backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
}
} catch {
result = .notConnected
}
}
}
}