1 class SshSession : public Ssh {
2 public:
3     enum Methods : int {
4         METHOD_EXCHANGE = 0,
5         METHOD_HOSTKEY,
6         METHOD_CENCRYPTION,
7         METHOD_SENCRYPTION,
8         METHOD_CMAC,
9         METHOD_SMAC,
10         METHOD_CCOMPRESSION,
11         METHOD_SCOMPRESSION,
12         METHOD_CLANGUAGE,
13         METHOD_SLANGUAGE
14     };
15 
16     enum Phase : int {
17         PHASE_DNS,
18         PHASE_CONNECTION,
19         PHASE_HANDSHAKE,
20         PHASE_AUTHORIZATION,
21         PHASE_SUCCESS
22     };
23 
24 public:
Timeout(int ms)25     SshSession&         Timeout(int ms)                         { ssh->timeout = ms; return *this; }
26 
27     SshSession&         Compression(bool b = true)              { session->compression = b; return *this; }
NoCompression()28     SshSession&         NoCompression()                         { return Compression(false); }
29 
30     SshSession&         Keys(const String& prikey, const String& pubkey, const String& phrase, bool fromfile = true);
Method(int type,Value method)31     SshSession&         Method(int type, Value method)          { session->iomethods(type) << pick(method); return *this; }
Methods(ValueMap methods)32     SshSession&         Methods(ValueMap methods)               { session->iomethods = pick(methods); return *this; }
33 
PasswordAuth()34     SshSession&         PasswordAuth()                          { session->authmethod = PASSWORD;  return *this; }
PublicKeyAuth()35     SshSession&         PublicKeyAuth()                         { session->authmethod = PUBLICKEY; return *this; }
HostBasedAuth()36     SshSession&         HostBasedAuth()                         { session->authmethod = HOSTBASED; return *this; }
KeyboardAuth()37     SshSession&         KeyboardAuth()                          { session->authmethod = KEYBOARD;  return *this; }
AgentAuth()38     SshSession&         AgentAuth()                             { session->authmethod = SSHAGENT;  return *this; }
39 
GetHandle()40     LIBSSH2_SESSION*    GetHandle()                             { return ssh->session; }
41 
GetBanner()42     String              GetBanner() const                       { return ssh->session ? pick(String(libssh2_session_banner_get(ssh->session))) : Null; }
GetMD5Fingerprint()43     String              GetMD5Fingerprint() const               { return GetHostKeyHash(LIBSSH2_HOSTKEY_HASH_MD5, 16);    }
GetSHA1Fingerprint()44     String              GetSHA1Fingerprint() const              { return GetHostKeyHash(LIBSSH2_HOSTKEY_HASH_SHA1, 20);   }
GetSHA256Fingerprint()45     String              GetSHA256Fingerprint() const            { return GetHostKeyHash(LIBSSH2_HOSTKEY_HASH_SHA256, 32); }
GetAuthMethods()46     Vector<String>      GetAuthMethods()                        { return pick(Split(session->authmethods, ',')); }
GetSocket()47     TcpSocket&          GetSocket()                             { return session->socket;  }
48     ValueMap            GetMethods() const;
49 
50     SFtp                CreateSFtp();
51     SshChannel          CreateChannel();
52     SshExec             CreateExec();
53     Scp                 CreateScp();
54     SshTunnel           CreateTunnel();
55     SshShell            CreateShell();
56 
57     bool                Connect(const String& url);
58     bool                Connect(const String& host, int port, const String& user, const String& password);
59     void                Disconnect();
60 
61     Event<>             WhenConfig;
62     Event<>             WhenAuth;
63     Function<String()>  WhenPasswordChange;
64     Event<int>          WhenPhase;
65     Gate<String, int>   WhenVerify;
66     Gate<>              WhenProxy;
67     Event<SshX11Handle> WhenX11;
68     Function<String(String, String, String)>  WhenKeyboard;
69 
70     SshSession();
71     virtual ~SshSession();
72 
73     SshSession(SshSession&&) = default;
74 
75     // DEPRECATED stuff. Use GetxxxFingerprint() methods instead.
76     enum Hash           { HASH_MD5, HASH_SHA1, HASH_SHA256 };
GetFingerprint()77     [[deprecated]] String      GetFingerprint() const              { return session->fingerprint; }
HashType(Hash h)78     [[deprecated]] SshSession& HashType(Hash h)                    { session->hashtype = h; return *this; }
79 
80 private:
81     void                Exit() override;
82     String              GetHostKeyHash(int type, int length) const;
83     String              GetMethodNames(int type) const;
84     int                 TryAgent(const String& username);
85     void                FreeAgent(SshAgent* agent);
86 
87     struct SessionData {
88         TcpSocket       socket;
89         String          fingerprint;
90         int             hashtype;
91         String          authmethods;
92         int             authmethod;
93         String          prikey;
94         String          pubkey;
95         bool            keyfile;
96         String          phrase;
97         ValueMap        iomethods;
98         bool            connected;
99         bool            compression;
100     };
101     One<SessionData> session;
102 
103     enum AuthMethod     { PASSWORD, PUBLICKEY, HOSTBASED, KEYBOARD, SSHAGENT };
104     enum HostkeyType    { RSAKEY, DSSKEY };
105 };
106