@@ -49,17 +49,17 @@ import ios_system
49
49
// return cmd.start(argc, argv: argv.args(count: argc))
50
50
// }
51
51
52
- enum MoshError : Error {
52
+ enum MoshError : Error , LocalizedError {
53
53
case NoBinaryAvailable
54
54
case NoBinaryExecFlag
55
55
case NoChecksumMatch
56
56
case UserCancelled
57
57
case NoMoshServerArgs
58
58
case NoRemoteServerIP
59
59
case AddressInfo( String )
60
- case StartMoshServerError ( String )
60
+ case MissingArguments ( String )
61
61
62
- public var description : String {
62
+ public var errorDescription : String ? {
63
63
switch self {
64
64
case . NoBinaryAvailable:
65
65
return " Could not find static binary for the remote platform and architecture. "
@@ -70,13 +70,13 @@ enum MoshError: Error {
70
70
case . UserCancelled:
71
71
return " User cancelled the operation "
72
72
case . NoMoshServerArgs:
73
- return " Error connecting to mosh- server. Could not parse arguments for client connection. "
73
+ return " Did not find mosh server startup message. (Have you installed mosh on your server?) "
74
74
case . NoRemoteServerIP:
75
- return " ExperimentalIPRemote error. IP not found ."
75
+ return " Bad Mosh SSH_CONNECTION String ."
76
76
case . AddressInfo( let error) :
77
77
return " Address resolution failed - \( error) "
78
- case . StartMoshServerError ( let error ) :
79
- return " Error starting remote mosh-server: \( error ) "
78
+ case . MissingArguments ( let message ) :
79
+ return " \( message ) "
80
80
}
81
81
}
82
82
}
@@ -165,7 +165,7 @@ enum MoshError: Error {
165
165
let moshServerParams : MoshServerParams
166
166
if let customKey = command. customKey {
167
167
guard let customUDPPort = moshClientParams. customUDPPort else {
168
- throw MoshError . StartMoshServerError ( " If MOSH_KEY is set, port is required. (-p) " )
168
+ throw MoshError . MissingArguments ( " If MOSH_KEY is set, port is required. (-p) " )
169
169
}
170
170
171
171
// Resolved as part of the host info or explicit on params.
@@ -200,6 +200,7 @@ enum MoshError: Error {
200
200
. flatMap { self . bootstrapMoshServer ( on: $0,
201
201
sequence: sequence,
202
202
experimentalRemoteIP: moshClientParams. experimentalRemoteIP,
203
+ family: command. addressFamily,
203
204
args: moshServerStartupArgs,
204
205
withPTY: pty) }
205
206
//.print()
@@ -298,10 +299,11 @@ enum MoshError: Error {
298
299
private func bootstrapMoshServer( on client: SSHClient ,
299
300
sequence: [ MoshBootstrap ] ,
300
301
experimentalRemoteIP: BKMoshExperimentalIP ,
302
+ family: AddressFamily ? ,
301
303
args: String ,
302
304
withPTY pty: SSH . SSHClient . PTY ? = nil ) -> AnyPublisher < MoshServerParams , Error > {
303
305
let log = logger. log ( " bootstrapMoshServer " )
304
- log. info ( " Trying bootstrap with sequence: \( sequence) , experimental: \( experimentalRemoteIP) , args: \( args) " )
306
+ log. info ( " Trying bootstrap with sequence: \( sequence) , experimental: \( experimentalRemoteIP) , family: \( family ) , args: \( args) " )
305
307
306
308
if sequence. isEmpty {
307
309
return Fail ( error: MoshError . NoBinaryAvailable) . eraseToAnyPublisher ( )
@@ -345,8 +347,7 @@ enum MoshError: Error {
345
347
return try MoshServerParams ( parsing: output, remoteIP: nil )
346
348
case BKMoshExperimentalIPLocal:
347
349
// local - resolve address on its own.
348
- // TODO Or to INET6 from CLI flag
349
- let remoteIP = try self . resolveAddress ( host: client. host, port: client. options. port, family: nil )
350
+ let remoteIP = try self . resolveAddress ( host: client. host, port: client. options. port, family: family)
350
351
return try MoshServerParams ( parsing: output, remoteIP: remoteIP)
351
352
default :
352
353
// default - get it from the established SSH Connection.
@@ -380,13 +381,25 @@ enum MoshError: Error {
380
381
// https://stackoverflow.com/questions/39857435/swift-getaddrinfo
381
382
// getnameinfo
382
383
// https://stackoverflow.com/questions/44478074/swift-getnameinfo-unreliable-results-for-ipv6
383
- private func resolveAddress( host: String , port: String ? , family: Int32 ? ) throws -> String {
384
+ private func resolveAddress( host: String , port: String ? , family: AddressFamily ? ) throws -> String {
384
385
guard let port = ( port ?? " 22 " ) . cString ( using: . utf8) else {
385
386
throw MoshError . AddressInfo ( " Invalid port " )
386
387
}
388
+
389
+ let ai_family = {
390
+ switch family {
391
+ case . IPv4:
392
+ AF_INET
393
+ case . IPv6:
394
+ AF_INET6
395
+ default :
396
+ AF_UNSPEC
397
+ }
398
+ } ( )
399
+
387
400
var hints = addrinfo (
388
401
ai_flags: 0 ,
389
- ai_family: family ?? AF_UNSPEC ,
402
+ ai_family: ai_family ,
390
403
ai_socktype: SOCK_STREAM,
391
404
ai_protocol: IPPROTO_TCP,
392
405
ai_addrlen: 0 ,
@@ -503,8 +516,8 @@ struct MoshLogger {
503
516
. format { [ ( $0 [ . component] as? String ) ? . appending ( " : " ) ?? " global: " ,
504
517
$0 [ . message] as? String ?? " "
505
518
] . joined ( separator: " " ) }
506
- . sink ( receiveValue: { print ( $0 [ . message] ) } )
507
- // .sinkToStream(output)
519
+ // .sink(receiveValue: { print($0[.message]) })
520
+ . sinkToStream ( output)
508
521
}
509
522
)
510
523
}
0 commit comments