handlers.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. }
  70. buf := bytes.NewBufferString(dot)
  71. format := r.URL.Query().Get("format")
  72. switch format {
  73. case "":
  74. format = "svg"
  75. case "dot", "gv":
  76. // If the raw dot data is requested, return it as string.
  77. // This allows client-side rendering rather than server-side.
  78. w.Write(buf.Bytes())
  79. return
  80. case "svg", "png", "bmp", "fig", "gif", "json", "ps":
  81. // Accepted format
  82. default:
  83. http.Error(w, "unsupported format", http.StatusInternalServerError)
  84. return
  85. }
  86. layout := r.URL.Query().Get("layout")
  87. switch layout {
  88. case "":
  89. layout = "circo"
  90. case "circo", "dot", "neato", "twopi", "fdp":
  91. // Accepted layout
  92. default:
  93. http.Error(w, "unsupported layout", http.StatusInternalServerError)
  94. return
  95. }
  96. command := exec.Command("dot", "-K"+layout, "-T"+format)
  97. command.Stderr = os.Stderr
  98. stdin, err := command.StdinPipe()
  99. if err != nil {
  100. http.Error(w, err.Error(), http.StatusInternalServerError)
  101. return
  102. }
  103. if _, err = io.Copy(stdin, buf); err != nil {
  104. http.Error(w, err.Error(), http.StatusInternalServerError)
  105. return
  106. }
  107. if err = stdin.Close(); err != nil {
  108. http.Error(w, err.Error(), http.StatusInternalServerError)
  109. return
  110. }
  111. output, err := command.Output()
  112. if err != nil {
  113. http.Error(w, "unable to render graph", http.StatusInternalServerError)
  114. return
  115. }
  116. mimeType := mime.TypeByExtension("." + format)
  117. if mimeType == "" {
  118. mimeType = "application/octet-stream"
  119. }
  120. w.Header().Add("content-type", mimeType)
  121. w.Write(output)
  122. }
  123. func healthHandler(w http.ResponseWriter, _ *http.Request) {
  124. w.WriteHeader(http.StatusOK)
  125. }