@@ -9,7 +9,9 @@ import 'package:server_box/data/model/server/discovery_result.dart';
99class SshDiscoveryService {
1010 static const _sshPort = 22 ;
1111
12- static Future <SshDiscoveryReport > discover ([SshDiscoveryConfig config = const SshDiscoveryConfig ()]) async {
12+ static Future <SshDiscoveryReport > discover ([
13+ SshDiscoveryConfig config = const SshDiscoveryConfig (),
14+ ]) async {
1315 final t0 = DateTime .now ();
1416 final candidates = < InternetAddress > {};
1517
@@ -32,7 +34,11 @@ class SshDiscoveryService {
3234
3335 // Filter out unwanted addresses: loopback, link-local, 0.0.0.0, broadcast, multicast
3436 candidates.removeWhere (
35- (a) => a.isLoopback || a.isLinkLocal || a.address == '0.0.0.0' || _isBroadcastOrMulticast (a),
37+ (a) =>
38+ a.isLoopback ||
39+ a.isLinkLocal ||
40+ a.address == '0.0.0.0' ||
41+ _isBroadcastOrMulticast (a),
3642 );
3743
3844 // 4) Concurrent SSH port scanning
@@ -45,7 +51,13 @@ class SshDiscoveryService {
4551 results.sort ((a, b) => a.addr.address.compareTo (b.addr.address));
4652
4753 final discoveryResults = results
48- .map ((r) => SshDiscoveryResult (ip: r.addr.address, port: _sshPort, banner: r.banner? .trim ()))
54+ .map (
55+ (r) => SshDiscoveryResult (
56+ ip: r.addr.address,
57+ port: _sshPort,
58+ banner: r.banner? .trim (),
59+ ),
60+ )
4961 .toList ();
5062
5163 return SshDiscoveryReport (
@@ -56,7 +68,11 @@ class SshDiscoveryService {
5668 );
5769 }
5870
59- static Future <String ?> _run (String exe, List <String > args, {Duration ? timeout}) async {
71+ static Future <String ?> _run (
72+ String exe,
73+ List <String > args, {
74+ Duration ? timeout,
75+ }) async {
6076 try {
6177 final p = await Process .start (exe, args, runInShell: false );
6278 final out = await p.stdout
@@ -75,7 +91,11 @@ class SshDiscoveryService {
7591 if (out.trim ().isNotEmpty) return out;
7692 return null ;
7793 } catch (e, s) {
78- Loggers .app.warning ('Failed to run command: $exe ${args .join (' ' )}' , e, s);
94+ Loggers .app.warning (
95+ 'Failed to run command: $exe ${args .join (' ' )}' ,
96+ e,
97+ s,
98+ );
7999 return null ;
80100 }
81101 }
@@ -92,7 +112,8 @@ class SshDiscoveryService {
92112 final tok = line.split (RegExp (r'\s+' ));
93113 if (tok.isNotEmpty) {
94114 final ip = tok[0 ];
95- if (InternetAddress .tryParse (ip)? .type == InternetAddressType .IPv4 ) {
115+ if (InternetAddress .tryParse (ip)? .type ==
116+ InternetAddressType .IPv4 ) {
96117 set .add (InternetAddress (ip));
97118 }
98119 }
@@ -126,7 +147,8 @@ class SshDiscoveryService {
126147 if (s != null ) {
127148 for (final line in const LineSplitter ().convert (s)) {
128149 final ip = line.split (RegExp (r'\s+' )).firstOrNull;
129- if (ip != null && InternetAddress .tryParse (ip)? .type == InternetAddressType .IPv6 ) {
150+ if (ip != null &&
151+ InternetAddress .tryParse (ip)? .type == InternetAddressType .IPv6 ) {
130152 set .add (InternetAddress (ip));
131153 }
132154 }
@@ -136,7 +158,8 @@ class SshDiscoveryService {
136158 if (s != null ) {
137159 for (final line in const LineSplitter ().convert (s)) {
138160 final ip = line.trim ().split (RegExp (r'\s+' )).firstOrNull;
139- if (ip != null && InternetAddress .tryParse (ip)? .type == InternetAddressType .IPv6 ) {
161+ if (ip != null &&
162+ InternetAddress .tryParse (ip)? .type == InternetAddressType .IPv6 ) {
140163 set .add (InternetAddress (ip));
141164 }
142165 }
@@ -148,10 +171,19 @@ class SshDiscoveryService {
148171 static Future <List <_Cidr >> _localIPv4Cidrs () async {
149172 final res = < _Cidr > [];
150173 if (_isLinux) {
151- final s = await _run ('ip' , ['-o' , '-4' , 'addr' , 'show' , 'scope' , 'global' ]);
174+ final s = await _run ('ip' , [
175+ '-o' ,
176+ '-4' ,
177+ 'addr' ,
178+ 'show' ,
179+ 'scope' ,
180+ 'global' ,
181+ ]);
152182 if (s != null ) {
153183 for (final line in const LineSplitter ().convert (s)) {
154- final m = RegExp (r'inet\s+(\d+\.\d+\.\d+\.\d+)\/(\d+)' ).firstMatch (line);
184+ final m = RegExp (
185+ r'inet\s+(\d+\.\d+\.\d+\.\d+)\/(\d+)' ,
186+ ).firstMatch (line);
155187 if (m != null ) {
156188 final ip = InternetAddress (m.group (1 )! );
157189 final prefix = int .parse (m.group (2 )! );
@@ -177,7 +209,9 @@ class SshDiscoveryService {
177209 r'inet\s+(\d+\.\d+\.\d+\.\d+)\s+netmask\s+0x([0-9a-fA-F]+)(?:\s+broadcast\s+(\d+\.\d+\.\d+\.\d+))?' ,
178210 ).firstMatch (line);
179211 if (ipm == null ) {
180- Loggers .app.warning ('[ssh_discovery] Warning: Unexpected ifconfig line format: $line ' );
212+ Loggers .app.warning (
213+ '[ssh_discovery] Warning: Unexpected ifconfig line format: $line ' ,
214+ );
181215 continue ;
182216 }
183217 final ip = InternetAddress (ipm.group (1 )! );
@@ -187,10 +221,14 @@ class SshDiscoveryService {
187221 final mask = InternetAddress (dotted);
188222 final prefix = _maskToPrefix (mask.address);
189223 final net = _networkAddress (ip, mask);
190- final brd = InternetAddress (ipm.group (3 ) ?? _broadcastAddress (ip, mask).address);
224+ final brd = InternetAddress (
225+ ipm.group (3 ) ?? _broadcastAddress (ip, mask).address,
226+ );
191227 res.add (_Cidr (ip, prefix, mask, net, brd));
192228 } catch (e) {
193- Loggers .app.warning ('[ssh_discovery] Error parsing ifconfig output: $e , line: $line ' );
229+ Loggers .app.warning (
230+ '[ssh_discovery] Error parsing ifconfig output: $e , line: $line ' ,
231+ );
194232 continue ;
195233 }
196234 }
@@ -219,7 +257,10 @@ class SshDiscoveryService {
219257 final set = < InternetAddress > {};
220258 if (_isMac) {
221259 try {
222- final proc = await Process .start ('/usr/bin/dns-sd' , ['-B' , '_ssh._tcp' ]);
260+ final proc = await Process .start ('/usr/bin/dns-sd' , [
261+ '-B' ,
262+ '_ssh._tcp' ,
263+ ]);
223264 final lines = < String > [];
224265 final subscription = proc.stdout
225266 .transform (utf8.decoder)
@@ -250,7 +291,11 @@ class SshDiscoveryService {
250291 }
251292 }
252293 } catch (e, s) {
253- Loggers .app.warning ('Failed to discover mDNS SSH candidates on macOS' , e, s);
294+ Loggers .app.warning (
295+ 'Failed to discover mDNS SSH candidates on macOS' ,
296+ e,
297+ s,
298+ );
254299 }
255300 } else if (_isLinux) {
256301 final s = await _run ('/usr/bin/avahi-browse' , ['-rat' , '_ssh._tcp' ]);
@@ -316,7 +361,11 @@ class _Scanner {
316361 Socket ? socket;
317362 StreamSubscription ? sub;
318363 try {
319- socket = await Socket .connect (ip, SshDiscoveryService ._sshPort, timeout: timeout);
364+ socket = await Socket .connect (
365+ ip,
366+ SshDiscoveryService ._sshPort,
367+ timeout: timeout,
368+ );
320369 socket.timeout (timeout);
321370 final c = Completer <String ?>();
322371 sub = socket.listen (
@@ -370,7 +419,8 @@ class _Semaphore {
370419 }
371420}
372421
373- Future <T > _guarded <T >(_Semaphore sem, Future <T > Function () fn) => sem.withPermit (fn);
422+ Future <T > _guarded <T >(_Semaphore sem, Future <T > Function () fn) =>
423+ sem.withPermit (fn);
374424
375425// IPv4 utilities
376426
@@ -379,7 +429,8 @@ int _ipv4ToInt(String ip) {
379429 return (p[0 ] << 24 ) | (p[1 ] << 16 ) | (p[2 ] << 8 ) | p[3 ];
380430}
381431
382- String _intToIPv4 (int v) => '${(v >> 24 ) & 0xff }.${(v >> 16 ) & 0xff }.${(v >> 8 ) & 0xff }.${v & 0xff }' ;
432+ String _intToIPv4 (int v) =>
433+ '${(v >> 24 ) & 0xff }.${(v >> 16 ) & 0xff }.${(v >> 8 ) & 0xff }.${v & 0xff }' ;
383434
384435InternetAddress _prefixToMask (int prefix) {
385436 final mask = prefix == 0 ? 0 : 0xffffffff << (32 - prefix);
0 commit comments