| gio | e72b54f | 2024-04-22 10:44:41 +0400 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | type Server struct { |
| 11 | s *http.Server |
| 12 | m *http.ServeMux |
| 13 | store RecordStore |
| 14 | zone string |
| 15 | ds string |
| 16 | nameserver []string |
| 17 | } |
| 18 | |
| 19 | func NewServer(port int, zone string, ds string, store RecordStore, nameserver []string) *Server { |
| 20 | m := http.NewServeMux() |
| 21 | s := &Server{ |
| 22 | s: &http.Server{ |
| 23 | Addr: fmt.Sprintf(":%d", port), |
| 24 | Handler: m, |
| 25 | }, |
| 26 | m: m, |
| 27 | store: store, |
| 28 | zone: zone, |
| 29 | ds: ds, |
| 30 | nameserver: nameserver, |
| 31 | } |
| 32 | m.HandleFunc("/records-to-publish", s.recordsToPublish) |
| 33 | m.HandleFunc("/create-txt-record", s.createTxtRecord) |
| 34 | m.HandleFunc("/delete-txt-record", s.deleteTxtRecord) |
| 35 | return s |
| 36 | } |
| 37 | |
| 38 | func (s *Server) Start() error { |
| 39 | return s.s.ListenAndServe() |
| 40 | } |
| 41 | |
| 42 | type record struct { |
| 43 | Domain string `json:"domain,omitempty"` |
| 44 | Entry string `json:"entry,omitempty"` |
| 45 | Text string `json:"text,omitempty"` |
| 46 | } |
| 47 | |
| 48 | func (s *Server) recordsToPublish(w http.ResponseWriter, r *http.Request) { |
| 49 | subdomain := strings.Split(s.zone, ".")[0] |
| 50 | if _, err := fmt.Fprintln(w, s.ds); err != nil { |
| 51 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 52 | return |
| 53 | } |
| 54 | for i, ip := range s.nameserver { |
| 55 | if _, err := fmt.Fprintf(w, "ns%d.%s. 10800 IN A %s\n", i+1, s.zone, ip); err != nil { |
| 56 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 57 | return |
| 58 | } |
| 59 | if _, err := fmt.Fprintf(w, "%s 10800 IN NS ns%d.%s.\n", subdomain, i+1, s.zone); err != nil { |
| 60 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 61 | return |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func (s *Server) createTxtRecord(w http.ResponseWriter, r *http.Request) { |
| 67 | var req record |
| 68 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 69 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 70 | return |
| 71 | } |
| 72 | fmt.Printf("CREATE: %+v\n", req) |
| 73 | if err := s.store.Add(req.Entry, req.Text); err != nil { |
| 74 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 75 | return |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func (s *Server) deleteTxtRecord(w http.ResponseWriter, r *http.Request) { |
| 80 | var req record |
| 81 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 82 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 83 | return |
| 84 | } |
| 85 | fmt.Printf("DELETE: %+v\n", req) |
| 86 | if err := s.store.Delete(req.Entry, req.Text); err != nil { |
| 87 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 88 | return |
| 89 | } |
| 90 | } |