10 Connection Protocol
Russtopia edited this page 2019-10-29 22:50:56 -07:00

Connection Protocol

Connection Sequence Diagram (Simplified)

Sequence Diagram Source Code (rendering above diagram via websequencediagrams.com)

title xs Connection Sequence
participant "User (shell)" as L
participant "User (shell)" as L
participant Client as C
participant "xsnet Lib" as HL
participant "Go pkg/net" as NL
participant "Server Listener" as SL
participant Server as S

S-->+S: Listen()
L-->+C: <<invoke client>>
C->HL: Dial(prot, ipport, KExAlg, CAlg, HAlg)
HL->NL: Dial(prot, ipport)
NL->S:
S-->+SL: Accept()
SL->NL:
NL->HL: c := net.Conn{}
HL-->HL: hc := new xsnet.Conn{c}
note over HL: hc.cipheropts\n= (CAlg, HAlg)
HL-->HL: hc.<KExAlg>DialSetup(c)

HL->C: conn = hc

#Client reads password from user
C-->C: ReadPassword()
C-->C: r := NewSession(op, uname,\nremHost, env, cmdStr, auth)
par KEx
#activate C
C->SL: sendSessionParams(conn, r)
activate SL
SL->C:
SL-->SL: auth(Client)
SL->C: conn.Read(authReply)
end
C-->+C: doShellMode()
par Encrypted
C<->SL: r.Status := doShellMode(conn) / doCopyMode(conn)\n... (session) ...
# ...
# ...

C-->SL: <<EOF/logout>>
end
SL-->C: <<Disconnect>>
deactivate SL

deactivate C
C-->L: shell r.Status
deactivate C
C-->L: shell r.Status
Session: [ Op byte | Who []byte | ConnHost []byte | TermType []byte | Cmd []byte | AuthCookie []byte ]


Op: [ 'A' | 'c' | 's' | 'D' | 'S' ]
'A': request Server generate auth token for client
'c': non-interactive command
's': interactive (shell) session
'D': file copy dest (client -> server)
'S': file copy src (server -> client)

All sessions return (hkexnet.CSOType = cmdStatus) in reply for client

SessionReply('A'): Enc([ AuthCookie []byte ]) 
SessionReply('c'): Enc([ cmd stdout/stderr ])
SessionReply('s'): Enc([ interactive shell session data ])
SessionReply('D'): Enc([ output of tarpipe receipt command stdout/stderr ])
SessionReply('S'): Enc([ tarpipe data for client ])
xsnet.Conn Packet Format

[ ctrlStatOp uint8 | hmacIn [HMAC_CHK_SZ]uint8 | payloadLen uint32 | {{payloadBytes []byte}} ]

* {{n}} = n hmac is calculated over n

Payload = [ padSide byte | padLen byte | (ciphertext ...) ]
padSide: 0 = prepended padding, 1 = appended padding