main
go 273 lines 8.49 KB
Raw
1 package types
2
3 import (
4 "encoding/base64"
5 "encoding/json"
6 "fmt"
7 "strings"
8 "time"
9 )
10
11 type APIEnvelope[T any] struct {
12 Data T `json:"data,omitempty"`
13 Error *APIError `json:"error,omitempty"`
14 OK bool `json:"ok"`
15 }
16
17 type APIError struct {
18 Code string `json:"code"`
19 Message string `json:"message"`
20 }
21
22 type APIRequestError struct {
23 StatusCode int `json:"-"`
24 Code string `json:"code,omitempty"`
25 Message string `json:"message,omitempty"`
26 }
27
28 func (e *APIRequestError) Error() string {
29 if e == nil {
30 return ""
31 }
32 code := strings.TrimSpace(e.Code)
33 message := strings.TrimSpace(e.Message)
34 if code != "" {
35 return code + ": " + message
36 }
37 if message != "" {
38 return message
39 }
40 if e.StatusCode > 0 {
41 return fmt.Sprintf("api request failed with status %d", e.StatusCode)
42 }
43 return "api request failed"
44 }
45
46 func (e *APIRequestError) Is(target error) bool {
47 other, ok := target.(*APIRequestError)
48 if !ok {
49 return false
50 }
51 if other.Code != "" && e.Code != other.Code {
52 return false
53 }
54 if other.StatusCode != 0 && e.StatusCode != other.StatusCode {
55 return false
56 }
57 return true
58 }
59
60 type RegisterRequest struct {
61 ChallengeID string `json:"challenge_id"`
62 SIWEMessage string `json:"siwe_message"`
63 SIWESignature string `json:"siwe_signature"`
64 ReportedIP string `json:"reported_ip,omitempty"`
65 }
66
67 type RegisterChallengeRequest struct {
68 Identity Identity `json:"identity"`
69 Metadata LeaseMetadata `json:"metadata"`
70 TTL int `json:"ttl,omitempty"`
71 UDPEnabled bool `json:"udp_enabled,omitempty"`
72 TCPEnabled bool `json:"tcp_enabled,omitempty"`
73 HopToken string `json:"hop_token,omitempty"`
74 RouteHostname string `json:"route_hostname,omitempty"`
75 HostnameHash string `json:"hostname_hash,omitempty"`
76 ECHConfigList []byte `json:"ech_config_list,omitempty"`
77 }
78
79 type RegisterChallengeResponse struct {
80 ChallengeID string `json:"challenge_id"`
81 ExpiresAt time.Time `json:"expires_at"`
82 SIWEMessage string `json:"siwe_message"`
83 }
84
85 type RegisterResponse struct {
86 Identity Identity `json:"identity"`
87 ExpiresAt time.Time `json:"expires_at"`
88 AccessToken string `json:"access_token"`
89 SNIPort int `json:"sni_port,omitempty"`
90 UDPAddr string `json:"udp_addr,omitempty"`
91 UDPEnabled bool `json:"udp_enabled,omitempty"`
92 TCPAddr string `json:"tcp_addr,omitempty"`
93 TCPEnabled bool `json:"tcp_enabled,omitempty"`
94 }
95
96 type DiscoveryResponse struct {
97 ProtocolVersion string `json:"protocol_version"`
98 GeneratedAt time.Time `json:"generated_at"`
99 Relays []RelayDescriptor `json:"relays"`
100 }
101
102 type DiscoveryAnnounceRequest struct {
103 ProtocolVersion string `json:"protocol_version"`
104 Descriptor RelayDescriptor `json:"descriptor"`
105 }
106
107 type DiscoveryAnnounceResponse struct {
108 ProtocolVersion string `json:"protocol_version"`
109 Accepted bool `json:"accepted"`
110 }
111
112 type RenewRequest struct {
113 AccessToken string `json:"access_token"`
114 TTL int `json:"ttl,omitempty"`
115 ReportedIP string `json:"reported_ip,omitempty"`
116 Metadata LeaseMetadata `json:"metadata,omitempty"`
117 }
118
119 type RenewResponse struct {
120 ExpiresAt time.Time `json:"expires_at"`
121 AccessToken string `json:"access_token"`
122 }
123
124 type UnregisterRequest struct {
125 AccessToken string `json:"access_token"`
126 }
127
128 type HopRoute struct {
129 OwnerPublicKey string `json:"owner_public_key,omitempty"`
130 RelayURL string `json:"relay_url"`
131 PublicHostname string `json:"public_hostname,omitempty"`
132 RouteHostname string `json:"route_hostname,omitempty"`
133 HostnameHash string `json:"hostname_hash,omitempty"`
134 ECHConfigList []byte `json:"ech_config_list,omitempty"`
135 MatchToken string `json:"match_token,omitempty"`
136 Metadata LeaseMetadata `json:"metadata,omitempty"`
137 ForwardRelay RelayDescriptor `json:"forward_relay"`
138 ForwardToken string `json:"forward_token"`
139 FirstSeenAt time.Time `json:"first_seen_at,omitempty"`
140 ExpiresAt time.Time `json:"expires_at,omitempty"`
141 Signature string `json:"signature,omitempty"`
142 }
143
144 type HopRouteResponse struct {
145 AccessToken string `json:"access_token,omitempty"`
146 SNIPort int `json:"sni_port,omitempty"`
147 }
148
149 func HopRouteBytes(method string, route HopRoute) ([]byte, error) {
150 forwardRelay, err := CanonicalBytes(route.ForwardRelay)
151 if err != nil {
152 return nil, err
153 }
154 payload := struct {
155 Purpose string `json:"purpose"`
156 Method string `json:"method"`
157 OwnerPublicKey string `json:"owner_public_key"`
158 RelayURL string `json:"relay_url"`
159 PublicHostname string `json:"public_hostname"`
160 RouteHostname string `json:"route_hostname"`
161 HostnameHash string `json:"hostname_hash"`
162 ECHConfigList string `json:"ech_config_list"`
163 MatchToken string `json:"match_token"`
164 ForwardRelay json.RawMessage `json:"forward_relay"`
165 ForwardToken string `json:"forward_token"`
166 FirstSeenAtUnixNano int64 `json:"first_seen_at_unix_nano"`
167 ExpiresAtUnixNano int64 `json:"expires_at_unix_nano"`
168 }{
169 Purpose: "portal hop route v1",
170 Method: strings.ToUpper(strings.TrimSpace(method)),
171 OwnerPublicKey: strings.TrimSpace(route.OwnerPublicKey),
172 RelayURL: strings.TrimSpace(route.RelayURL),
173 PublicHostname: strings.TrimSpace(route.PublicHostname),
174 RouteHostname: strings.TrimSpace(route.RouteHostname),
175 HostnameHash: strings.TrimSpace(route.HostnameHash),
176 ECHConfigList: base64.StdEncoding.EncodeToString(route.ECHConfigList),
177 MatchToken: strings.TrimSpace(route.MatchToken),
178 ForwardRelay: json.RawMessage(forwardRelay),
179 ForwardToken: strings.TrimSpace(route.ForwardToken),
180 FirstSeenAtUnixNano: route.FirstSeenAt.UTC().UnixNano(),
181 ExpiresAtUnixNano: route.ExpiresAt.UTC().UnixNano(),
182 }
183 return json.Marshal(payload)
184 }
185
186 type DomainResponse struct {
187 ProtocolVersion string `json:"protocol_version"`
188 ReleaseVersion string `json:"release_version"`
189 ENS ENSStatus `json:"ens"`
190 X402 X402FacilitatorInfo `json:"x402"`
191 }
192
193 type ENSStatus struct {
194 Enabled bool `json:"enabled"`
195 Verified bool `json:"verified"`
196 Provider string `json:"provider,omitempty"`
197 Address string `json:"address,omitempty"`
198 DNSSECState string `json:"dnssec_state,omitempty"`
199 DSRecord string `json:"ds_record,omitempty"`
200 Message string `json:"message,omitempty"`
201 LastError string `json:"last_error,omitempty"`
202 }
203
204 type PublicStateResponse struct {
205 Leases []Lease `json:"leases,omitempty"`
206 }
207
208 type AdminAuthLoginRequest struct {
209 Token string `json:"token"`
210 }
211
212 type AdminAuthLoginResponse struct {
213 AccessToken string `json:"access_token,omitempty"`
214 }
215
216 type AdminAuthStatusResponse struct {
217 Authenticated bool `json:"authenticated"`
218 }
219
220 type WalletAuthChallengeRequest struct {
221 Address string `json:"address"`
222 }
223
224 type WalletAuthChallengeResponse struct {
225 ChallengeID string `json:"challenge_id"`
226 ExpiresAt time.Time `json:"expires_at"`
227 SIWEMessage string `json:"siwe_message"`
228 }
229
230 type WalletAuthLoginRequest struct {
231 ChallengeID string `json:"challenge_id"`
232 SIWEMessage string `json:"siwe_message"`
233 SIWESignature string `json:"siwe_signature"`
234 }
235
236 type WalletAuthLoginResponse struct {
237 AccessToken string `json:"access_token,omitempty"`
238 WalletAddress string `json:"wallet_address,omitempty"`
239 }
240
241 type WalletAuthStatusResponse struct {
242 Authenticated bool `json:"authenticated"`
243 WalletAddress string `json:"wallet_address,omitempty"`
244 }
245
246 type PolicyStateResponse struct {
247 Policy PolicySettings `json:"policy"`
248 Leases []PolicyLease `json:"leases,omitempty"`
249 }
250
251 type PolicySettings struct {
252 ApprovalMode string `json:"approval_mode"`
253 UDP PolicyPortSettings `json:"udp"`
254 TCPPort PolicyPortSettings `json:"tcp_port"`
255 }
256
257 type LeasePolicyUpdate struct {
258 IdentityKey string `json:"identity_key"`
259 BPS *int64 `json:"bps,omitempty"`
260 IsApproved *bool `json:"is_approved,omitempty"`
261 IsBanned *bool `json:"is_banned,omitempty"`
262 IsDenied *bool `json:"is_denied,omitempty"`
263 }
264
265 type PolicyPortSettings struct {
266 Enabled bool `json:"enabled"`
267 MaxLeases int `json:"max_leases"`
268 }
269
270 type IPPolicyUpdate struct {
271 IP string `json:"ip"`
272 IsBanned bool `json:"is_banned"`
273 }