Repository navigation
Expand file tree
/
Copy pathhubapi.go
More file actions
151 lines (141 loc) · 7.02 KB
/
Copy pathhubapi.go
File metadata and controls
151 lines (141 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Package hubapi is the protocol a machine running AgentBox and a hub speak to
// each other: the JSON a hub's own API exchanges, the shape of its tokens, and
// how the tunnel between them is framed.
//
// It is the seam between the two halves of AgentBox. Everything here is
// implemented on both sides — by the client (internal/remote, the daemon) and
// by the hub (hub/), which is a separate program. It carries no server: nothing here reads a
// database, checks a password or decides who may see what. A hub does all of
// that, and this package says nothing about how.
//
// Unlike the rest of AgentBox's packages this one is not internal: it is the
// one thing the hub imports from the rest of AgentBox.
package hubapi
// Tokens a hub issues carry a prefix saying what they are, so a client can tell
// one from the other before sending it anywhere. A hub keeps only their hashes,
// and both are opaque to whoever holds one.
const (
// SessionTokenPrefix marks a device's token: what a device gets when it
// is approved (or, on hubs from before pairing, signs in with a
// password), sent as "Authorization: Bearer <token>". The hub lists each
// as a device, and revoking one ends it.
SessionTokenPrefix = "abx_s_"
// EnvironmentTokenPrefix marks a machine's own token, which it connects
// to its hub with. A hub shows it once, when the environment is added.
EnvironmentTokenPrefix = "abx_e_"
// PairingSecretPrefix marks the secret a device asking to be approved
// holds, and polls PathPairToken with. It is never shown to anyone.
PairingSecretPrefix = "abx_p_"
// SetupSecretPrefix marks the secret in a setup link (PathSetupPage),
// which makes whoever opens it first the hub's owner, or signs the owner
// in once on a hub that has one.
SetupSecretPrefix = "abx_l_"
)
// SessionCookie is where a browser on a hub's own pages keeps its session
// token. The CLI and the desktop app send the bearer header instead.
const SessionCookie = "abx_session"
// An environment announces itself in these headers when it dials the tunnel, so
// a hub can show which machine is connected and what it runs.
const (
HeaderVersion = "X-AgentBox-Version"
HeaderHostname = "X-AgentBox-Hostname"
)
// A hub's own endpoints. Everything else it serves belongs to an environment,
// and is reached through EnvironmentAPI.
const (
// PathInfo answers HubInfo, without a session: what a client checks
// before connecting.
PathInfo = "/v1/hub"
// PathLogin signs in with a password. Only accounts made before pairing
// have one; a client asks for a password only when HubInfo.Passwords.
PathLogin = "/v1/auth/login"
PathLogout = "/v1/auth/logout"
PathMe = "/v1/me"
// PathSetup takes a setup link's secret (HubSetupRequest) and answers a
// HubSession: the first to send it becomes the hub's owner.
PathSetup = "/v1/setup"
// PathPair is where a device or an environment asks to be approved:
// POST a HubPairRequest, without a session, and get a HubPairing.
PathPair = "/v1/pair"
// PathPairToken is polled with the pairing's secret
// (HubPairTokenRequest) until it answers a HubPairResult that isn't
// pending.
PathPairToken = "/v1/pair/token"
// PathPairings is where a signed-in device looks up a pairing by its
// code (GET PathPairings/<code>, a HubPairingInfo) and approves or denies
// it (POST PathPairings/<code>/approve or /deny).
PathPairings = "/v1/pairings"
// PathDevices lists the signed-in user's devices (GET, []HubDevice); a
// DELETE of PathDevices/<id> revokes one.
PathDevices = "/v1/devices"
PathEnvironments = "/v1/environments"
// PathConnect is the WebSocket an environment dials to open its tunnel,
// authenticating with its environment token.
PathConnect = "/v1/connect"
// PathEnvironmentTunnel is where an environment asks for its stable
// address for phones: POST, with its environment token, and no body. A hub
// that gives them out answers HubEnvironmentTunnel, the same hostname every
// time; one that doesn't answers 404 or 501.
PathEnvironmentTunnel = "/v1/environment/tunnel"
// PathEnvironmentAddress is where an environment on a Tailscale hub
// reports the address it serves on in the tailnet: POST a
// HubEnvironmentAddress, with its environment token. The hub lists it on
// the environment (HubEnvironment.Address); devices on the tailnet reach
// the environment there, never through the hub.
PathEnvironmentAddress = "/v1/environment/address"
// PathPeers is where an environment lists the other environments of its
// owner, with its environment token: GET, []HubEnvironment, itself left
// out. A lead on one machine directs agents on another through them
// (PeerAPI). A hub that predates peers answers 404.
PathPeers = "/v1/environment/peers"
// PathHealth answers without a session, for a load balancer.
PathHealth = "/healthz"
)
// A hub's own pages, for a browser: where a setup link and a pairing's
// approval go. A hub serves them whether or not it serves the web app.
const (
// PathSetupPage is a setup link without its secret, which follows as the
// URL's fragment (#abx_l_…), so it never reaches a server's logs.
PathSetupPage = "/hub/setup"
// PathApprovePage approves a pairing: PathApprovePage?code=<code>.
PathApprovePage = "/hub/approve"
)
// EnvironmentAPI is the prefix under which a hub passes requests through to an
// environment's daemon: a client speaks the daemon's own API below it, exactly
// as it would over the daemon's socket.
//
// The id is the environment's, and must already be escaped for a path.
func EnvironmentAPI(id string) string {
return PathEnvironments + "/" + id + "/api"
}
// PeerAPI is EnvironmentAPI for an environment reaching a peer: another
// environment of the same owner, with its own environment token rather than a
// device's session. The hub refuses a peer of another owner as if it didn't
// exist, and tells the peer who is asking in HeaderPeer and HeaderPeerName,
// which it strips from every other request it passes through, so an
// environment trusts them only on a request from its hub.
//
// The id is the peer's, and must already be escaped for a path.
func PeerAPI(id string) string {
return PathPeers + "/" + id + "/api"
}
// What a hub tells an environment about the peer whose request it passes
// through (PeerAPI): the peer's environment ID and name.
const (
HeaderPeer = "X-AgentBox-Peer"
HeaderPeerName = "X-AgentBox-Peer-Name"
)
// The transports a hub gives environments' devices a direct address on, rather
// than through its own proxy. TransportCloudflare is a Cloudflare Tunnel on a
// hostname in the hub's zone, reachable from the internet; TransportTailscale
// is the environment's own node in the hub's tailnet, reachable only by the
// tailnet's devices.
const (
TransportCloudflare = "cloudflare"
TransportTailscale = "tailscale"
)
// EnvironmentTunnelOrigin is where the tunnel a hub makes for an environment
// reaches the environment's daemon, on the loopback address of the machine
// cloudflared runs on: the hub configures its ingress with it, and the daemon
// listens on it.
const EnvironmentTunnelOrigin = "http://127.0.0.1:7781"