blob: 15be0eb3461dbeb3449dec33e344394ce5966a84 [file] [log] [blame]
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04001package welcome
2
3import (
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +04004 "embed"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04005 "encoding/json"
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +04006 "errors"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +04007 "fmt"
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +04008 "html/template"
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +04009 "io"
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +040010 "io/fs"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040011 "log"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040012 "net"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040013 "net/http"
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +040014 "net/netip"
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +040015 "strings"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040016
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040017 "github.com/gomarkdown/markdown"
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040018 "github.com/gorilla/mux"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040019
20 "github.com/giolekva/pcloud/core/installer"
gioe72b54f2024-04-22 10:44:41 +040021 "github.com/giolekva/pcloud/core/installer/dns"
22 phttp "github.com/giolekva/pcloud/core/installer/http"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040023 "github.com/giolekva/pcloud/core/installer/soft"
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +040024 "github.com/giolekva/pcloud/core/installer/tasks"
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040025)
26
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040027//go:embed env-manager-tmpl/*
28var tmpls embed.FS
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040029
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +040030var tmplsParsed templates
31
32func init() {
33 if t, err := parseTemplates(tmpls); err != nil {
34 panic(err)
35 } else {
36 tmplsParsed = t
37 }
38}
39
40type templates struct {
41 form *template.Template
42 status *template.Template
43}
44
45func parseTemplates(fs embed.FS) (templates, error) {
46 base, err := template.ParseFS(fs, "env-manager-tmpl/base.html")
47 if err != nil {
48 return templates{}, err
49 }
50 parse := func(path string) (*template.Template, error) {
51 if b, err := base.Clone(); err != nil {
52 return nil, err
53 } else {
54 return b.ParseFS(fs, path)
55 }
56 }
57 form, err := parse("env-manager-tmpl/form.html")
58 if err != nil {
59 return templates{}, err
60 }
61 status, err := parse("env-manager-tmpl/status.html")
62 if err != nil {
63 return templates{}, err
64 }
65 return templates{form, status}, nil
66}
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040067
68type Status string
69
70const (
71 StatusActive Status = "ACTIVE"
72 StatusAccepted Status = "ACCEPTED"
73)
74
75// TODO(giolekva): add CreatedAt and ValidUntil
76type invitation struct {
77 Token string `json:"token"`
78 Status Status `json:"status"`
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +040079}
80
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040081type EnvServer struct {
82 port int
gioe72b54f2024-04-22 10:44:41 +040083 ss soft.Client
84 repo soft.RepoIO
85 repoClient soft.ClientGetter
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040086 nsCreator installer.NamespaceCreator
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040087 dnsFetcher installer.ZoneStatusFetcher
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040088 nameGenerator installer.NameGenerator
gioe72b54f2024-04-22 10:44:41 +040089 httpClient phttp.Client
90 dnsClient dns.Client
giod9c398e2024-06-06 13:33:03 +040091 Tasks tasks.TaskManager
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040092 envInfo map[string]template.HTML
gioe72b54f2024-04-22 10:44:41 +040093 dns map[string]installer.EnvDNS
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +040094 dnsPublished map[string]struct{}
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +040095}
96
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040097func NewEnvServer(
98 port int,
gioe72b54f2024-04-22 10:44:41 +040099 ss soft.Client,
100 repo soft.RepoIO,
101 repoClient soft.ClientGetter,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400102 nsCreator installer.NamespaceCreator,
103 dnsFetcher installer.ZoneStatusFetcher,
104 nameGenerator installer.NameGenerator,
gioe72b54f2024-04-22 10:44:41 +0400105 httpClient phttp.Client,
106 dnsClient dns.Client,
giod9c398e2024-06-06 13:33:03 +0400107 tm tasks.TaskManager,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400108) *EnvServer {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400109 return &EnvServer{
110 port,
111 ss,
112 repo,
gioe72b54f2024-04-22 10:44:41 +0400113 repoClient,
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +0400114 nsCreator,
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400115 dnsFetcher,
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400116 nameGenerator,
gioe72b54f2024-04-22 10:44:41 +0400117 httpClient,
118 dnsClient,
giod9c398e2024-06-06 13:33:03 +0400119 tm,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400120 make(map[string]template.HTML),
gioe72b54f2024-04-22 10:44:41 +0400121 make(map[string]installer.EnvDNS),
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400122 make(map[string]struct{}),
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400123 }
124}
125
126func (s *EnvServer) Start() {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400127 r := mux.NewRouter()
128 r.PathPrefix("/static/").Handler(http.FileServer(http.FS(staticAssets)))
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400129 r.Path("/env/{key}").Methods("GET").HandlerFunc(s.monitorTask)
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400130 r.Path("/env/{key}").Methods("POST").HandlerFunc(s.publishDNSRecords)
Giorgi Lekveishvili123a3672023-12-04 13:01:29 +0400131 r.Path("/").Methods("GET").HandlerFunc(s.createEnvForm)
132 r.Path("/").Methods("POST").HandlerFunc(s.createEnv)
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400133 r.Path("/create-invitation").Methods("GET").HandlerFunc(s.createInvitation)
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400134 http.Handle("/", r)
135 log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", s.port), nil))
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400136}
137
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400138func (s *EnvServer) monitorTask(w http.ResponseWriter, r *http.Request) {
139 vars := mux.Vars(r)
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400140 key, ok := vars["key"]
141 if !ok {
142 http.Error(w, "Task key not provided", http.StatusBadRequest)
143 return
144 }
giod9c398e2024-06-06 13:33:03 +0400145 t, err := s.Tasks.Get(key)
146 if err != nil {
147 http.Error(w, err.Error(), http.StatusBadRequest)
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400148 return
149 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400150 dnsRecords := ""
151 if _, ok := s.dnsPublished[key]; !ok {
152 dnsRef, ok := s.dns[key]
153 if !ok {
154 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
155 return
156 }
gioe72b54f2024-04-22 10:44:41 +0400157 if records, err := s.dnsFetcher.Fetch(dnsRef.Address); err == nil {
158 dnsRecords = records
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400159 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400160 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400161 data := map[string]any{
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400162 "Root": t,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400163 "EnvInfo": s.envInfo[key],
164 "DNSRecords": dnsRecords,
165 }
166 if err := tmplsParsed.status.Execute(w, data); err != nil {
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400167 http.Error(w, err.Error(), http.StatusInternalServerError)
168 return
169 }
170}
171
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400172func (s *EnvServer) publishDNSRecords(w http.ResponseWriter, r *http.Request) {
173 vars := mux.Vars(r)
174 key, ok := vars["key"]
175 if !ok {
176 http.Error(w, "Task key not provided", http.StatusBadRequest)
177 return
178 }
179 dnsRef, ok := s.dns[key]
180 if !ok {
181 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
182 return
183 }
gioe72b54f2024-04-22 10:44:41 +0400184 records, err := s.dnsFetcher.Fetch(dnsRef.Address)
185 if err != nil {
186 http.Error(w, "Task dns configuration not found", http.StatusInternalServerError)
187 return
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400188 }
189 r.ParseForm()
190 if apiToken, err := getFormValue(r.PostForm, "api-token"); err != nil {
191 http.Error(w, err.Error(), http.StatusBadRequest)
192 return
193 } else {
194 p := NewGandiUpdater(apiToken)
gioe72b54f2024-04-22 10:44:41 +0400195 zone := strings.Join(strings.Split(dnsRef.Zone, ".")[1:], ".") // TODO(gio): this is not gonna work with no subdomain case
196 if err := p.Update(zone, strings.Split(records, "\n")); err != nil {
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400197 http.Error(w, err.Error(), http.StatusInternalServerError)
198 return
199 }
200 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400201 s.envInfo[key] = "Successfully published DNS records, waiting to propagate."
202 s.dnsPublished[key] = struct{}{}
Giorgi Lekveishvili1eec3e12023-12-18 21:12:29 +0400203 http.Redirect(w, r, fmt.Sprintf("/env/%s", key), http.StatusSeeOther)
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +0400204}
205
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400206func (s *EnvServer) createEnvForm(w http.ResponseWriter, r *http.Request) {
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400207 if err := tmplsParsed.form.Execute(w, nil); err != nil {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400208 http.Error(w, err.Error(), http.StatusInternalServerError)
209 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400210}
211
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400212func (s *EnvServer) createInvitation(w http.ResponseWriter, r *http.Request) {
213 invitations, err := s.readInvitations()
214 if err != nil {
215 http.Error(w, err.Error(), http.StatusInternalServerError)
216 return
217 }
218 token, err := installer.NewFixedLengthRandomNameGenerator(100).Generate() // TODO(giolekva): use cryptographic tokens
219 if err != nil {
220 http.Error(w, err.Error(), http.StatusInternalServerError)
221 return
222
223 }
224 invitations = append(invitations, invitation{token, StatusActive})
225 if err := s.writeInvitations(invitations); err != nil {
226 http.Error(w, err.Error(), http.StatusInternalServerError)
227 return
228 }
229 if _, err := w.Write([]byte("OK")); err != nil {
230 http.Error(w, err.Error(), http.StatusInternalServerError)
231 return
232 }
233}
234
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400235type createEnvReq struct {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400236 Name string
237 ContactEmail string `json:"contactEmail"`
238 Domain string `json:"domain"`
239 AdminPublicKey string `json:"adminPublicKey"`
240 SecretToken string `json:"secretToken"`
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400241}
242
243func (s *EnvServer) readInvitations() ([]invitation, error) {
244 r, err := s.repo.Reader("invitations")
245 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400246 if errors.Is(err, fs.ErrNotExist) {
247 return make([]invitation, 0), nil
248 }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400249 return nil, err
250 }
251 defer r.Close()
252 dec := json.NewDecoder(r)
253 invitations := make([]invitation, 0)
254 for {
255 var i invitation
256 if err := dec.Decode(&i); err == io.EOF {
257 break
258 }
259 invitations = append(invitations, i)
260 }
261 return invitations, nil
262}
263
264func (s *EnvServer) writeInvitations(invitations []invitation) error {
265 w, err := s.repo.Writer("invitations")
266 if err != nil {
267 return err
268 }
269 defer w.Close()
270 enc := json.NewEncoder(w)
271 for _, i := range invitations {
272 if err := enc.Encode(i); err != nil {
273 return err
274 }
275 }
276 return s.repo.CommitAndPush("Generated new invitation")
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400277}
278
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400279func extractRequest(r *http.Request) (createEnvReq, error) {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400280 var req createEnvReq
281 if err := func() error {
282 var err error
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400283 if err = r.ParseForm(); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400284 return err
285 }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400286 if req.SecretToken, err = getFormValue(r.PostForm, "secret-token"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400287 return err
288 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400289 if req.Domain, err = getFormValue(r.PostForm, "domain"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400290 return err
291 }
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400292 if req.ContactEmail, err = getFormValue(r.PostForm, "contact-email"); err != nil {
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400293 return err
294 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400295 if req.AdminPublicKey, err = getFormValue(r.PostForm, "admin-public-key"); err != nil {
296 return err
297 }
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400298 return nil
299 }(); err != nil {
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +0400300 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400301 return createEnvReq{}, err
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400302 }
303 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400304 return req, nil
305}
306
307func (s *EnvServer) acceptInvitation(token string) error {
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400308 invitations, err := s.readInvitations()
309 if err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400310 return err
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400311 }
312 found := false
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400313 for i := range invitations {
314 if invitations[i].Token == token && invitations[i].Status == StatusActive {
315 invitations[i].Status = StatusAccepted
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400316 found = true
317 break
318 }
319 }
320 if !found {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400321 return fmt.Errorf("Invitation not found")
322 }
323 return s.writeInvitations(invitations)
324}
325
326func (s *EnvServer) createEnv(w http.ResponseWriter, r *http.Request) {
gio1591fa72024-05-24 18:01:58 +0400327 if err := s.repo.Pull(); err != nil {
328 http.Error(w, err.Error(), http.StatusInternalServerError)
329 return
330 }
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400331 req, err := extractRequest(r)
332 if err != nil {
333 http.Error(w, err.Error(), http.StatusInternalServerError)
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400334 return
335 }
gio3cdee592024-04-17 10:15:56 +0400336 mgr, err := installer.NewInfraAppManager(s.repo, s.nsCreator)
337 if err != nil {
338 http.Error(w, err.Error(), http.StatusInternalServerError)
339 return
340 }
341 var infra installer.InfraConfig
gioe72b54f2024-04-22 10:44:41 +0400342 if err := soft.ReadYaml(s.repo, "config.yaml", &infra); err != nil {
Giorgi Lekveishvili724885f2023-11-29 16:18:42 +0400343 http.Error(w, err.Error(), http.StatusInternalServerError)
344 return
345 }
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400346 // if err := s.acceptInvitation(req.SecretToken); err != nil {
347 // http.Error(w, err.Error(), http.StatusInternalServerError)
348 // return
349 // }
Giorgi Lekveishvili081f18f2023-11-07 14:58:10 +0400350 if name, err := s.nameGenerator.Generate(); err != nil {
351 http.Error(w, err.Error(), http.StatusInternalServerError)
352 return
353 } else {
354 req.Name = name
355 }
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400356 var cidrs installer.EnvCIDRs
gioe72b54f2024-04-22 10:44:41 +0400357 if err := soft.ReadYaml(s.repo, "env-cidrs.yaml", &cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400358 http.Error(w, err.Error(), http.StatusInternalServerError)
359 return
360 }
361 startIP, err := findNextStartIP(cidrs)
362 if err != nil {
363 http.Error(w, err.Error(), http.StatusInternalServerError)
364 return
365 }
366 cidrs = append(cidrs, installer.EnvCIDR{req.Name, startIP})
gioe72b54f2024-04-22 10:44:41 +0400367 if err := soft.WriteYaml(s.repo, "env-cidrs.yaml", cidrs); err != nil {
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400368 http.Error(w, err.Error(), http.StatusInternalServerError)
369 return
370 }
Giorgi Lekveishvili5c1b06e2024-03-28 15:19:44 +0400371 if err := s.repo.CommitAndPush(fmt.Sprintf("Allocate CIDR for %s", req.Name)); err != nil {
372 http.Error(w, err.Error(), http.StatusInternalServerError)
373 return
374 }
gioe72b54f2024-04-22 10:44:41 +0400375 envNetwork, err := installer.NewEnvNetwork(startIP)
376 if err != nil {
377 http.Error(w, err.Error(), http.StatusInternalServerError)
378 return
379 }
380 env := installer.EnvConfig{
381 Id: req.Name,
382 InfraName: infra.Name,
383 Domain: req.Domain,
384 PrivateDomain: fmt.Sprintf("p.%s", req.Domain),
385 ContactEmail: req.ContactEmail,
386 AdminPublicKey: req.AdminPublicKey,
387 PublicIP: infra.PublicIP,
388 NameserverIP: infra.PublicIP,
389 NamespacePrefix: fmt.Sprintf("%s-", req.Name),
390 Network: envNetwork,
391 }
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400392 key := func() string {
393 for {
394 key, err := s.nameGenerator.Generate()
395 if err == nil {
396 return key
397 }
398 }
399 }()
400 infoUpdater := func(info string) {
401 s.envInfo[key] = template.HTML(markdown.ToHTML([]byte(info), nil, nil))
402 }
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +0400403 t, dns := tasks.NewCreateEnvTask(
gioe72b54f2024-04-22 10:44:41 +0400404 env,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400405 s.nsCreator,
gioe72b54f2024-04-22 10:44:41 +0400406 s.dnsFetcher,
407 s.httpClient,
408 s.dnsClient,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400409 s.repo,
gioe72b54f2024-04-22 10:44:41 +0400410 s.repoClient,
gio3cdee592024-04-17 10:15:56 +0400411 mgr,
Giorgi Lekveishviliab7ff6e2024-03-29 13:11:30 +0400412 infoUpdater,
Giorgi Lekveishvili46743d42023-12-10 15:47:23 +0400413 )
giod9c398e2024-06-06 13:33:03 +0400414 if err := s.Tasks.Add(key, t); err != nil {
415 panic(err)
416 }
417
Giorgi Lekveishvili1eec3e12023-12-18 21:12:29 +0400418 s.dns[key] = dns
Giorgi Lekveishvili77ee2dc2023-12-11 16:51:10 +0400419 go t.Start()
Giorgi Lekveishvilic85504d2023-12-20 19:29:47 +0400420 http.Redirect(w, r, fmt.Sprintf("/env/%s", key), http.StatusSeeOther)
Giorgi Lekveishvilib4a9c982023-06-22 15:17:02 +0400421}
Giorgi Lekveishvili9d5e3f52024-03-13 15:02:50 +0400422
423func findNextStartIP(cidrs installer.EnvCIDRs) (net.IP, error) {
424 m, err := netip.ParseAddr("10.0.0.0")
425 if err != nil {
426 return nil, err
427 }
428 for _, cidr := range cidrs {
429 i, err := netip.ParseAddr(cidr.IP.String())
430 if err != nil {
431 return nil, err
432 }
433 if i.Compare(m) > 0 {
434 m = i
435 }
436 }
437 sl := m.AsSlice()
438 sl[2]++
439 if sl[2] == 0b11111111 {
440 sl[2] = 0
441 sl[1]++
442 }
443 if sl[1] == 0b11111111 {
444 return nil, fmt.Errorf("Can not allocate")
445 }
446 ret, ok := netip.AddrFromSlice(sl)
447 if !ok {
448 return nil, fmt.Errorf("Must not reach")
449 }
450 return net.ParseIP(ret.String()), nil
451}