transport.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package acn
  2. /*
  3. ArozOS Cluster Node protocol (ACN) - outbound transport
  4. Transport.Do sends one signed request to a peer, picking the route:
  5. 1. the peer is tunnelled to THIS node -> over the tunnel
  6. 2. the peer advertises a URL -> direct HTTPS
  7. 3. the peer is tunnelled to another node -> HTTPS to that node's
  8. relay endpoint, which
  9. forwards over its tunnel
  10. Callers never deal with routing; higher layers just name the node.
  11. */
  12. import (
  13. "bytes"
  14. "context"
  15. "crypto/tls"
  16. "encoding/json"
  17. "errors"
  18. "io"
  19. "net/http"
  20. "strings"
  21. "time"
  22. )
  23. // DefaultRequestTimeout bounds one node-to-node request. Cloudflare returns
  24. // 524 after 100 seconds, so anything long-running must be split into chunks.
  25. const DefaultRequestTimeout = 60 * time.Second
  26. // Transport sends signed requests to cluster peers.
  27. type Transport struct {
  28. Signer *Signer
  29. Resolver PeerResolver
  30. Hub *TunnelHub //Tunnels terminating at this node, may be nil
  31. Client *http.Client
  32. }
  33. func tlsConfig(insecure bool) *tls.Config {
  34. if !insecure {
  35. return nil
  36. }
  37. return &tls.Config{InsecureSkipVerify: true} //Admin opted in for self-signed LAN certificates
  38. }
  39. // NewTransport creates a transport. insecureTLS accepts self-signed peer
  40. // certificates, which the admin can enable for LAN-only clusters.
  41. func NewTransport(signer *Signer, resolver PeerResolver, hub *TunnelHub, insecureTLS bool) *Transport {
  42. tr := http.DefaultTransport.(*http.Transport).Clone()
  43. tr.TLSClientConfig = tlsConfig(insecureTLS)
  44. tr.MaxIdleConnsPerHost = 8
  45. return &Transport{
  46. Signer: signer,
  47. Resolver: resolver,
  48. Hub: hub,
  49. Client: &http.Client{
  50. Transport: tr,
  51. Timeout: DefaultRequestTimeout,
  52. },
  53. }
  54. }
  55. // SetInsecureTLS toggles acceptance of self-signed peer certificates.
  56. func (t *Transport) SetInsecureTLS(insecure bool) {
  57. if tr, ok := t.Client.Transport.(*http.Transport); ok {
  58. tr.TLSClientConfig = tlsConfig(insecure)
  59. tr.CloseIdleConnections()
  60. }
  61. }
  62. // JoinURL glues a node base URL and an ACN path.
  63. func JoinURL(baseURL string, path string) string {
  64. return strings.TrimRight(baseURL, "/") + path
  65. }
  66. // signedHeaders returns the headers a relayed or tunnelled request must
  67. // carry: the signature headers, the content type and every X-Aroz-* header
  68. // (higher layers use those for per-chunk metadata).
  69. func signedHeaders(req *http.Request) http.Header {
  70. h := http.Header{}
  71. for k, vals := range req.Header {
  72. ck := http.CanonicalHeaderKey(k)
  73. if ck == "Content-Type" || strings.HasPrefix(ck, "X-Aroz-") {
  74. for _, v := range vals {
  75. h.Add(ck, v)
  76. }
  77. }
  78. }
  79. return h
  80. }
  81. func readResponse(resp *http.Response) (*Response, error) {
  82. defer resp.Body.Close()
  83. body, err := io.ReadAll(io.LimitReader(resp.Body, MaxSignedBody))
  84. if err != nil {
  85. return nil, err
  86. }
  87. return &Response{Status: resp.StatusCode, Header: resp.Header, Body: body}, nil
  88. }
  89. // DoURL sends a request straight to a base URL. When sign is true the ACN
  90. // headers are added; otherwise the request is anonymous (used before a node
  91. // has joined, e.g. for hello and join).
  92. func (t *Transport) DoURL(ctx context.Context, baseURL string, method string, path string, body []byte, sign bool) (*Response, error) {
  93. if body == nil {
  94. body = []byte{}
  95. }
  96. req, err := http.NewRequestWithContext(ctx, method, JoinURL(baseURL, path), bytes.NewReader(body))
  97. if err != nil {
  98. return nil, err
  99. }
  100. if len(body) > 0 {
  101. req.Header.Set("Content-Type", "application/json")
  102. }
  103. if sign {
  104. if t.Signer == nil {
  105. return nil, errors.New("transport has no signer")
  106. }
  107. t.Signer.Sign(req, path, body)
  108. }
  109. resp, err := t.Client.Do(req)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return readResponse(resp)
  114. }
  115. // Do sends a signed request to the named cluster node. path is the ACN path
  116. // including any query string.
  117. func (t *Transport) Do(ctx context.Context, nodeID string, method string, path string, body []byte) (*Response, error) {
  118. return t.DoWithHeaders(ctx, nodeID, method, path, body, nil)
  119. }
  120. func applyExtra(req *http.Request, extra http.Header) {
  121. for k, vals := range extra {
  122. for _, v := range vals {
  123. req.Header.Add(k, v)
  124. }
  125. }
  126. }
  127. // DoWithHeaders is Do with additional request headers (only X-Aroz-* and
  128. // Content-Type survive tunnel and relay hops).
  129. func (t *Transport) DoWithHeaders(ctx context.Context, nodeID string, method string, path string, body []byte, extra http.Header) (*Response, error) {
  130. if body == nil {
  131. body = []byte{}
  132. }
  133. if t.Signer == nil {
  134. return nil, errors.New("transport has no signer")
  135. }
  136. contentType := "application/json"
  137. if extra != nil && extra.Get("Content-Type") != "" {
  138. contentType = extra.Get("Content-Type")
  139. }
  140. //Route 1: tunnelled to this node
  141. if t.Hub != nil && t.Hub.Connected(nodeID) {
  142. req, _ := http.NewRequest(method, path, nil)
  143. if len(body) > 0 {
  144. req.Header.Set("Content-Type", contentType)
  145. }
  146. applyExtra(req, extra)
  147. t.Signer.Sign(req, path, body)
  148. return t.Hub.Do(ctx, nodeID, method, path, signedHeaders(req), body)
  149. }
  150. peer, ok := t.Resolver.ResolvePeer(nodeID)
  151. if !ok || peer == nil {
  152. return nil, ErrUnknownNode
  153. }
  154. //Route 2: direct
  155. if peer.AdvertiseURL != "" {
  156. req, err := http.NewRequestWithContext(ctx, method, JoinURL(peer.AdvertiseURL, path), bytes.NewReader(body))
  157. if err != nil {
  158. return nil, err
  159. }
  160. if len(body) > 0 {
  161. req.Header.Set("Content-Type", contentType)
  162. }
  163. applyExtra(req, extra)
  164. t.Signer.Sign(req, path, body)
  165. resp, err := t.Client.Do(req)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return readResponse(resp)
  170. }
  171. //Route 3: relay through the node terminating the peer's tunnel
  172. if peer.TunnelVia != "" && peer.TunnelVia != t.Signer.NodeID {
  173. via, ok := t.Resolver.ResolvePeer(peer.TunnelVia)
  174. if !ok || via == nil || via.AdvertiseURL == "" {
  175. return nil, ErrPeerNoEndpoint
  176. }
  177. req, err := http.NewRequestWithContext(ctx, method, JoinURL(via.AdvertiseURL, RelayPrefix+"/"+nodeID+path), bytes.NewReader(body))
  178. if err != nil {
  179. return nil, err
  180. }
  181. if len(body) > 0 {
  182. req.Header.Set("Content-Type", contentType)
  183. }
  184. applyExtra(req, extra)
  185. t.Signer.Sign(req, path, body) //signed over the ORIGINAL path
  186. resp, err := t.Client.Do(req)
  187. if err != nil {
  188. return nil, err
  189. }
  190. return readResponse(resp)
  191. }
  192. if peer.TunnelVia == t.Signer.NodeID {
  193. return nil, ErrTunnelNotConnected
  194. }
  195. return nil, ErrPeerNoEndpoint
  196. }
  197. // DoJSON2 sends in as JSON and returns the raw response, leaving status
  198. // interpretation to the caller (for endpoints that use 409 as a signal).
  199. func (t *Transport) DoJSON2(ctx context.Context, nodeID string, method string, path string, in interface{}) (*Response, error) {
  200. var body []byte
  201. if in != nil {
  202. js, err := json.Marshal(in)
  203. if err != nil {
  204. return nil, err
  205. }
  206. body = js
  207. }
  208. return t.Do(ctx, nodeID, method, path, body)
  209. }
  210. // DoJSON posts in as JSON to a node and decodes the JSON reply into out
  211. // (which may be nil). Non-2xx replies become errors.
  212. func (t *Transport) DoJSON(ctx context.Context, nodeID string, method string, path string, in interface{}, out interface{}) error {
  213. var body []byte
  214. if in != nil {
  215. js, err := json.Marshal(in)
  216. if err != nil {
  217. return err
  218. }
  219. body = js
  220. }
  221. resp, err := t.Do(ctx, nodeID, method, path, body)
  222. if err != nil {
  223. return err
  224. }
  225. if err := resp.Error(); err != nil {
  226. return err
  227. }
  228. if out != nil && len(resp.Body) > 0 {
  229. return json.Unmarshal(resp.Body, out)
  230. }
  231. return nil
  232. }