| 1 | |
| 2 | |
| 3 | |
| 4 | package remotehost |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "net/http" |
| 9 | ) |
| 10 | |
| 11 | const APIProtocol = 1 |
| 12 | |
| 13 | type AuthConfig struct { |
| 14 | Kind string `json:"kind"` |
| 15 | IdentityFile string `json:"identity_file,omitempty"` |
| 16 | } |
| 17 | |
| 18 | type HostKey struct { |
| 19 | Algorithm string `json:"algorithm"` |
| 20 | Fingerprint string `json:"fingerprint"` |
| 21 | } |
| 22 | |
| 23 | type HostProfile struct { |
| 24 | ID string `json:"id"` |
| 25 | Name string `json:"name"` |
| 26 | Host string `json:"host"` |
| 27 | Port int `json:"port"` |
| 28 | User string `json:"user"` |
| 29 | Auth AuthConfig `json:"auth"` |
| 30 | HostKey *HostKey `json:"host_key,omitempty"` |
| 31 | Executable string `json:"executable,omitempty"` |
| 32 | ConfigPath string `json:"config_path,omitempty"` |
| 33 | } |
| 34 | |
| 35 | type PublicManifest struct { |
| 36 | APIProtocol int `json:"api_protocol"` |
| 37 | InstanceID string `json:"instance_id"` |
| 38 | Version string `json:"version"` |
| 39 | Platform string `json:"platform"` |
| 40 | Runtime string `json:"runtime"` |
| 41 | Capabilities map[string]bool `json:"capabilities"` |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | type ServerManifest struct { |
| 47 | APIProtocol int `json:"api_protocol"` |
| 48 | InstanceID string `json:"instance_id"` |
| 49 | Version string `json:"version"` |
| 50 | Platform string `json:"platform"` |
| 51 | Runtime string `json:"runtime"` |
| 52 | Capabilities map[string]bool `json:"capabilities"` |
| 53 | Origin string `json:"origin"` |
| 54 | Ticket string `json:"ticket"` |
| 55 | } |
| 56 | |
| 57 | func (m ServerManifest) Public() PublicManifest { |
| 58 | return PublicManifest{m.APIProtocol, m.InstanceID, m.Version, m.Platform, m.Runtime, m.Capabilities} |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | type ConnectSecrets struct { |
| 64 | Deploy bool `json:"-"` |
| 65 | Passphrase []byte `json:"-"` |
| 66 | } |
| 67 | |
| 68 | type ConnectionRef struct { |
| 69 | ID string `json:"connection_id"` |
| 70 | HostID string `json:"host_id"` |
| 71 | InstanceID string `json:"instance_id"` |
| 72 | } |
| 73 | |
| 74 | type ConnectionState struct { |
| 75 | State string `json:"state"` |
| 76 | ConnectionID string `json:"connection_id,omitempty"` |
| 77 | HostID string `json:"host_id,omitempty"` |
| 78 | InstanceID string `json:"instance_id,omitempty"` |
| 79 | Phase string `json:"phase,omitempty"` |
| 80 | Message string `json:"message,omitempty"` |
| 81 | ErrorCode string `json:"error_code,omitempty"` |
| 82 | HostKey *HostKey `json:"host_key,omitempty"` |
| 83 | Manifest *PublicManifest `json:"manifest,omitempty"` |
| 84 | } |
| 85 | |
| 86 | type Error struct { |
| 87 | Code string `json:"code"` |
| 88 | Message string `json:"message"` |
| 89 | HostKey *HostKey `json:"host_key,omitempty"` |
| 90 | } |
| 91 | |
| 92 | func (e *Error) Error() string { return e.Message } |
| 93 | |
| 94 | func failure(code, message string) error { return &Error{Code: code, Message: message} } |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | type Session struct { |
| 100 | Ref ConnectionRef |
| 101 | Manifest PublicManifest |
| 102 | Client *http.Client |
| 103 | Transport http.RoundTripper |
| 104 | Context context.Context |
| 105 | Origin string |
| 106 | close func() error |
| 107 | } |
| 108 | |
| 109 | func (s *Session) Close() error { |
| 110 | if s == nil || s.close == nil { |
| 111 | return nil |
| 112 | } |
| 113 | return s.close() |
| 114 | } |