handlers.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  33. func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  34. ns, err := h.mesh.Nodes().List()
  35. if err != nil {
  36. http.Error(w, fmt.Sprintf("failed to list nodes: %v", err), http.StatusInternalServerError)
  37. return
  38. }
  39. ps, err := h.mesh.Peers().List()
  40. if err != nil {
  41. http.Error(w, fmt.Sprintf("failed to list peers: %v", err), http.StatusInternalServerError)
  42. return
  43. }
  44. nodes := make(map[string]*mesh.Node)
  45. for _, n := range ns {
  46. if n.Ready() {
  47. nodes[n.Name] = n
  48. }
  49. }
  50. if len(nodes) == 0 {
  51. http.Error(w, "did not find any valid Kilo nodes in the cluster", http.StatusInternalServerError)
  52. return
  53. }
  54. peers := make(map[string]*mesh.Peer)
  55. for _, p := range ps {
  56. if p.Ready() {
  57. peers[p.Name] = p
  58. }
  59. }
  60. topo, err := mesh.NewTopology(nodes, peers, h.granularity, *h.hostname, 0, wgtypes.Key{}, h.subnet, nodes[*h.hostname].PersistentKeepalive, nil)
  61. if err != nil {
  62. http.Error(w, fmt.Sprintf("failed to create topology: %v", err), http.StatusInternalServerError)
  63. return
  64. }
  65. dot, err := topo.Dot()
  66. if err != nil {
  67. http.Error(w, fmt.Sprintf("failed to generate graph: %v", err), http.StatusInternalServerError)
  68. }
  69. buf := bytes.NewBufferString(dot)
  70. format := r.URL.Query().Get("format")
  71. switch format {
  72. case "":
  73. format = "svg"
  74. case "dot", "gv":
  75. // If the raw dot data is requested, return it as string.
  76. // This allows client-side rendering rather than server-side.
  77. w.Write(buf.Bytes())
  78. return
  79. case "svg", "png", "bmp", "fig", "gif", "json", "ps":
  80. // Accepted format
  81. default:
  82. http.Error(w, "unsupported format", http.StatusInternalServerError)
  83. return
  84. }
  85. layout := r.URL.Query().Get("layout")
  86. switch layout {
  87. case "":
  88. layout = "circo"
  89. case "circo", "dot", "neato", "twopi", "fdp":
  90. // Accepted layout
  91. default:
  92. http.Error(w, "unsupported layout", http.StatusInternalServerError)
  93. return
  94. }
  95. command := exec.Command("dot", "-K"+layout, "-T"+format)
  96. command.Stderr = os.Stderr
  97. stdin, err := command.StdinPipe()
  98. if err != nil {
  99. http.Error(w, err.Error(), http.StatusInternalServerError)
  100. return
  101. }
  102. if _, err = io.Copy(stdin, buf); err != nil {
  103. http.Error(w, err.Error(), http.StatusInternalServerError)
  104. return
  105. }
  106. if err = stdin.Close(); err != nil {
  107. http.Error(w, err.Error(), http.StatusInternalServerError)
  108. return
  109. }
  110. output, err := command.Output()
  111. if err != nil {
  112. http.Error(w, "unable to render graph", http.StatusInternalServerError)
  113. return
  114. }
  115. mimeType := mime.TypeByExtension("." + format)
  116. if mimeType == "" {
  117. mimeType = "application/octet-stream"
  118. }
  119. w.Header().Add("content-type", mimeType)
  120. w.Write(output)
  121. }
  122. func healthHandler(w http.ResponseWriter, _ *http.Request) {
  123. w.WriteHeader(http.StatusOK)
  124. }