1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- //
- // UIKitSshTerminalView.swift
- // iOS
- //
- // Created by Miguel de Icaza on 4/22/20.
- // Copyright © 2020 Miguel de Icaza. All rights reserved.
- //
- import Foundation
- import UIKit
- import SwiftTerm
- import SwiftSH
- public class SshTerminalView: TerminalView, TerminalViewDelegate {
- var shell: SSHShell?
- var authenticationChallenge: AuthenticationChallenge?
-
- public override init (frame: CGRect)
- {
- super.init (frame: frame)
- delegate = self
- do {
-
- authenticationChallenge = .byPassword(username: "miguel", password: try String (contentsOfFile: "/Users/miguel/password"))
- shell = try? SSHShell(sshLibrary: Libssh2.self,
- host: "192.168.86.78",
- port: 22,
- environment: [Environment(name: "LANG", variable: "en_US.UTF-8")],
- terminal: "xterm-256color")
- connect()
- } catch {
-
- }
- }
-
- func connect()
- {
-
- if let s = shell {
- s.withCallback { [unowned self] (data: Data?, error: Data?) in
- if let d = data {
- DispatchQueue.main.async {
- let slice = Array(d) [0...]
- self.feed(byteArray: slice)
- }
- }
- }
- .connect()
- .authenticate(self.authenticationChallenge)
- .open { [unowned self] (error) in
- if let error = error {
- self.feed(text: "[ERROR] \(error)\n")
- } else {
- let t = self.getTerminal()
- s.setTerminalSize(width: UInt (t.cols), height: UInt (t.rows))
- }
- }
- }
- }
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- // TerminalViewDelegate conformance
- public func scrolled(source: TerminalView, position: Double) {
- //
- }
-
- public func setTerminalTitle(source: TerminalView, title: String) {
- //
- }
-
- public func sizeChanged(source: TerminalView, newCols: Int, newRows: Int) {
- if let s = shell {
- s.setTerminalSize(width: UInt (newCols), height: UInt (newRows))
- }
- }
-
- public func send(source: TerminalView, data: ArraySlice<UInt8>) {
-
- shell?.write(Data (data)) { err in
- print ("Error sending")
- }
- }
-
- }
|