-
Notifications
You must be signed in to change notification settings - Fork 22
/
dartssh.dart
135 lines (118 loc) · 3.76 KB
/
dartssh.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2019 dartssh developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:args/args.dart';
import 'package:dartssh/client.dart';
import 'package:dartssh/identity.dart';
import 'package:dartssh/pem.dart';
import 'package:dartssh/ssh.dart';
import 'package:dartssh/transport.dart';
Identity identity;
SSHClient client;
Channel forwardChannel;
void main(List<String> arguments) async {
stdin.lineMode = false;
stdin.echoMode = false;
ProcessSignal.sigint.watch().listen((_) {
if (client != null) send(Uint8List.fromList(<int>[3]));
});
ProcessSignal.sigwinch.watch().listen((_) {
if (client != null) {
client.setTerminalWindowSize(
stdout.terminalColumns, stdout.terminalLines);
}
});
exitCode = await ssh(
arguments, stdin, (_, String v) => stdout.write(v), () => exit(0),
termWidth: stdout.terminalColumns, termHeight: stdout.terminalLines);
}
void send(Uint8List x) {
if (forwardChannel != null) {
client.sendToChannel(forwardChannel, x);
} else {
client.sendChannelData(x);
}
}
Future<int> ssh(List<String> arguments, Stream<List<int>> input,
ResponseCallback response, VoidCallback done,
{int termWidth = 80, int termHeight = 25}) async {
final argParser = ArgParser()
..addOption('login', abbr: 'l')
..addOption('identity', abbr: 'i')
..addOption('password')
..addOption('tunnel')
..addOption('kex')
..addOption('key')
..addOption('cipher')
..addOption('mac')
..addFlag('debug')
..addFlag('trace')
..addFlag('agentForwarding', abbr: 'A');
final ArgResults args = argParser.parse(arguments);
identity = null;
client = null;
forwardChannel = null;
if (args.rest.length != 1) {
print('usage: ssh -l login url [args]');
print(argParser.usage);
return 1;
}
final String host = args.rest.first,
login = args['login'],
identityFile = args['identity'],
tunnel = args['tunnel'];
if (login == null || login.isEmpty) {
print('no login specified');
return 1;
}
if (tunnel != null && tunnel.split(':').length != 2) {
print('tunnel target should be specified host:port');
return 2;
}
applyCipherSuiteOverrides(
args['kex'], args['key'], args['cipher'], args['mac']);
try {
client = SSHClient(
hostport: parseUri(host),
login: login,
print: print,
termWidth: termWidth,
termHeight: termHeight,
termvar: Platform.environment['TERM'] ?? 'xterm',
agentForwarding: args['agentForwarding'],
debugPrint: args['debug'] ? print : null,
tracePrint: args['trace'] ? print : null,
getPassword: ((args['password'] != null)
? () => utf8.encode(args['password'])
: null),
response: response,
loadIdentity: () {
if (identity == null && identityFile != null) {
identity = parsePem(File(identityFile).readAsStringSync());
}
return identity;
},
disconnected: done,
startShell: tunnel == null,
success: tunnel == null
? null
: () {
List<String> tunnelTarget = tunnel.split(':');
forwardChannel = client.openTcpChannel(
'127.0.0.1',
1234,
tunnelTarget[0],
int.parse(tunnelTarget[1]),
(_, Uint8List m) => response(client, utf8.decode(m)));
});
await for (String x in input.transform(utf8.decoder)) {
send(utf8.encode(x));
}
} catch (error, stacktrace) {
print('ssh: exception: $error: $stacktrace');
return -1;
}
return 0;
}