handlers.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2021 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "mime"
  20. "net"
  21. "net/http"
  22. "os"
  23. "os/exec"
  24. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  25. "github.com/squat/kilo/pkg/mesh"
  26. )
  27. type graphHandler struct {
  28. mesh *mesh.Mesh
  29. granularity mesh.Granularity
  30. hostname *string
  31. subnet *net.IPNet
  32. serviceCIDRs []*net.IPNet
  33. }
  34. func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. ns, err := h.mesh.Nodes().List()
  36. if err != nil {
  37. http.Error(w, fmt.Sprintf("failed to list nodes: %v", err), http.StatusInternalServerError)
  38. return
  39. }
  40. ps, err := h.mesh.Peers().List()
  41. if err != nil {
  42. http.Error(w, fmt.Sprintf("failed to list peers: %v", err), http.StatusInternalServerError)
  43. return
  44. }
  45. nodes := make(map[string]*mesh.Node)
  46. for _, n := range ns {
  47. if n.Ready() {
  48. nodes[n.Name] = n
  49. }
  50. }
  51. if len(nodes) == 0 {
  52. http.Error(w, "did not find any valid Kilo nodes in the cluster", http.StatusInternalServerError)
  53. return
  54. }
  55. peers := make(map[string]*mesh.Peer)
  56. for _, p := range ps {
  57. if p.Ready() {
  58. peers[p.Name] = p
  59. }
  60. }
  61. topo, err := mesh.NewTopology(nodes, peers, h.granularity, *h.hostname, 0, wgtypes.Key{}, h.subnet, h.serviceCIDRs, nodes[*h.hostname].PersistentKeepalive, nil)
  62. if err != nil {
  63. http.Error(w, fmt.Sprintf("failed to create topology: %v", err), http.StatusInternalServerError)
  64. return
  65. }
  66. dot, err := topo.Dot()
  67. if err != nil {
  68. http.Error(w, fmt.Sprintf("failed to generate graph: %v", err), http.StatusInternalServerError)
  69. return
  70. }
  71. buf := bytes.NewBufferString(dot)
  72. format := r.URL.Query().Get("format")
  73. switch format {
  74. case "":
  75. format = "svg"
  76. case "dot", "gv":
  77. // If the raw dot data is requested, return it as string.
  78. // This allows client-side rendering rather than server-side.
  79. _, err = w.Write(buf.Bytes())
  80. if err != nil {
  81. http.Error(w, fmt.Sprintf("failed to generate graph: %v", err), http.StatusInternalServerError)
  82. return
  83. }
  84. return
  85. case "svg", "png", "bmp", "fig", "gif", "json", "ps":
  86. // Accepted format
  87. default:
  88. http.Error(w, "unsupported format", http.StatusInternalServerError)
  89. return
  90. }
  91. layout := r.URL.Query().Get("layout")
  92. switch layout {
  93. case "":
  94. layout = "circo"
  95. case "circo", "dot", "neato", "twopi", "fdp":
  96. // Accepted layout
  97. default:
  98. http.Error(w, "unsupported layout", http.StatusInternalServerError)
  99. return
  100. }
  101. command := exec.Command("dot", "-K"+layout, "-T"+format)
  102. command.Stderr = os.Stderr
  103. stdin, err := command.StdinPipe()
  104. if err != nil {
  105. http.Error(w, err.Error(), http.StatusInternalServerError)
  106. return
  107. }
  108. if _, err = io.Copy(stdin, buf); err != nil {
  109. http.Error(w, err.Error(), http.StatusInternalServerError)
  110. return
  111. }
  112. if err = stdin.Close(); err != nil {
  113. http.Error(w, err.Error(), http.StatusInternalServerError)
  114. return
  115. }
  116. output, err := command.Output()
  117. if err != nil {
  118. http.Error(w, "unable to render graph", http.StatusInternalServerError)
  119. return
  120. }
  121. mimeType := mime.TypeByExtension("." + format)
  122. if mimeType == "" {
  123. mimeType = "application/octet-stream"
  124. }
  125. w.Header().Add("content-type", mimeType)
  126. _, err = w.Write(output)
  127. if err != nil {
  128. http.Error(w, err.Error(), http.StatusInternalServerError)
  129. return
  130. }
  131. }
  132. func healthHandler(w http.ResponseWriter, _ *http.Request) {
  133. w.WriteHeader(http.StatusOK)
  134. }