-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathssh.dart
111 lines (90 loc) · 2.45 KB
/
ssh.dart
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
import 'dart:async';
import 'dart:convert';
import 'package:dartssh2/dartssh2.dart';
import 'package:example/src/virtual_keyboard.dart';
import 'package:flutter/cupertino.dart';
import 'package:xterm/xterm.dart';
const host = 'localhost';
const port = 22;
const username = '<your username>';
const password = '<your password>';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoApp(
title: 'xterm.dart demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({super.key});
@override
// ignore: library_private_types_in_public_api
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late final terminal = Terminal(inputHandler: keyboard);
final keyboard = VirtualKeyboard(defaultInputHandler);
var title = host;
@override
void initState() {
super.initState();
initTerminal();
}
Future<void> initTerminal() async {
terminal.write('Connecting...\r\n');
final client = SSHClient(
await SSHSocket.connect(host, port),
username: username,
onPasswordRequest: () => password,
);
terminal.write('Connected\r\n');
final session = await client.shell(
pty: SSHPtyConfig(
width: terminal.viewWidth,
height: terminal.viewHeight,
),
);
terminal.buffer.clear();
terminal.buffer.setCursor(0, 0);
terminal.onTitleChange = (title) {
setState(() => this.title = title);
};
terminal.onResize = (width, height, pixelWidth, pixelHeight) {
session.resizeTerminal(width, height, pixelWidth, pixelHeight);
};
terminal.onOutput = (data) {
session.write(utf8.encode(data));
};
session.stdout
.cast<List<int>>()
.transform(Utf8Decoder())
.listen(terminal.write);
session.stderr
.cast<List<int>>()
.transform(Utf8Decoder())
.listen(terminal.write);
}
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text(title),
backgroundColor:
CupertinoTheme.of(context).barBackgroundColor.withOpacity(0.5),
),
child: Column(
children: [
Expanded(
child: TerminalView(terminal),
),
VirtualKeyboardView(keyboard),
],
),
);
}
}