2023-06-06 10:41:30 +02:00
|
|
|
package derp
|
2021-10-22 16:55:35 +00:00
|
|
|
|
|
|
|
import (
|
2021-11-14 16:37:43 +01:00
|
|
|
"context"
|
2021-10-22 16:55:35 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
2023-06-06 10:23:39 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2021-10-22 16:55:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-03-27 10:48:39 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-10-22 16:55:35 +00:00
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func loadDERPMapFromPath(path string) (*tailcfg.DERPMap, error) {
|
|
|
|
derpFile, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer derpFile.Close()
|
|
|
|
var derpMap tailcfg.DERPMap
|
|
|
|
b, err := io.ReadAll(derpFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(b, &derpMap)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-22 16:55:35 +00:00
|
|
|
return &derpMap, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadDERPMapFromURL(addr url.URL) (*tailcfg.DERPMap, error) {
|
2023-06-06 10:41:30 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), types.HTTPReadTimeout)
|
2021-11-14 16:37:43 +01:00
|
|
|
defer cancel()
|
|
|
|
|
2022-09-04 11:33:00 +02:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, addr.String(), nil)
|
2021-11-14 16:37:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:55:35 +00:00
|
|
|
client := http.Client{
|
2023-06-06 10:41:30 +02:00
|
|
|
Timeout: types.HTTPReadTimeout,
|
2021-10-22 16:55:35 +00:00
|
|
|
}
|
2021-11-14 16:37:43 +01:00
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2021-10-22 16:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
2022-08-09 23:21:19 +02:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2021-10-22 16:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var derpMap tailcfg.DERPMap
|
|
|
|
err = json.Unmarshal(body, &derpMap)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-22 16:55:35 +00:00
|
|
|
return &derpMap, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// mergeDERPMaps naively merges a list of DERPMaps into a single
|
|
|
|
// DERPMap, it will _only_ look at the Regions, an integer.
|
|
|
|
// If a region exists in two of the given DERPMaps, the region
|
|
|
|
// form the _last_ DERPMap will be preserved.
|
2021-11-13 08:39:04 +00:00
|
|
|
// An empty DERPMap list will result in a DERPMap with no regions.
|
2021-10-22 16:55:35 +00:00
|
|
|
func mergeDERPMaps(derpMaps []*tailcfg.DERPMap) *tailcfg.DERPMap {
|
|
|
|
result := tailcfg.DERPMap{
|
|
|
|
OmitDefaultRegions: false,
|
|
|
|
Regions: map[int]*tailcfg.DERPRegion{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, derpMap := range derpMaps {
|
|
|
|
for id, region := range derpMap.Regions {
|
|
|
|
result.Regions[id] = region
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2023-06-06 10:23:39 +02:00
|
|
|
func GetDERPMap(cfg types.DERPConfig) *tailcfg.DERPMap {
|
2021-10-22 16:55:35 +00:00
|
|
|
derpMaps := make([]*tailcfg.DERPMap, 0)
|
|
|
|
|
|
|
|
for _, path := range cfg.Paths {
|
|
|
|
log.Debug().
|
|
|
|
Str("func", "GetDERPMap").
|
|
|
|
Str("path", path).
|
|
|
|
Msg("Loading DERPMap from path")
|
|
|
|
derpMap, err := loadDERPMapFromPath(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("func", "GetDERPMap").
|
|
|
|
Str("path", path).
|
|
|
|
Err(err).
|
|
|
|
Msg("Could not load DERP map from path")
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-22 16:55:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
derpMaps = append(derpMaps, derpMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range cfg.URLs {
|
|
|
|
derpMap, err := loadDERPMapFromURL(addr)
|
|
|
|
log.Debug().
|
|
|
|
Str("func", "GetDERPMap").
|
|
|
|
Str("url", addr.String()).
|
|
|
|
Msg("Loading DERPMap from path")
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("func", "GetDERPMap").
|
|
|
|
Str("url", addr.String()).
|
|
|
|
Err(err).
|
|
|
|
Msg("Could not load DERP map from path")
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-10-22 16:55:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
derpMaps = append(derpMaps, derpMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
derpMap := mergeDERPMaps(derpMaps)
|
|
|
|
|
|
|
|
log.Trace().Interface("derpMap", derpMap).Msg("DERPMap loaded")
|
|
|
|
|
|
|
|
if len(derpMap.Regions) == 0 {
|
|
|
|
log.Warn().
|
|
|
|
Msg("DERP map is empty, not a single DERP map datasource was loaded correctly or contained a region")
|
|
|
|
}
|
|
|
|
|
|
|
|
return derpMap
|
|
|
|
}
|