2020-06-03 22:42:20 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-12 18:58:43 +00:00
|
|
|
"context"
|
2020-06-03 22:42:20 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func startMesh(s *derp.Server) error {
|
|
|
|
if *meshWith == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !s.HasMeshKey() {
|
|
|
|
return errors.New("--mesh-with requires --mesh-psk-file")
|
|
|
|
}
|
|
|
|
for _, host := range strings.Split(*meshWith, ",") {
|
|
|
|
if err := startMeshWithHost(s, host); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startMeshWithHost(s *derp.Server, host string) error {
|
|
|
|
logf := logger.WithPrefix(log.Printf, fmt.Sprintf("mesh(%q): ", host))
|
|
|
|
c, err := derphttp.NewClient(s.PrivateKey(), "https://"+host+"/derp", logf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.MeshKey = s.MeshKey()
|
2020-06-15 19:58:10 +01:00
|
|
|
add := func(k key.Public) { s.AddPacketForwarder(k, c) }
|
2020-06-25 03:45:27 +01:00
|
|
|
remove := func(k key.Public) { s.RemovePacketForwarder(k, c) }
|
2021-02-12 18:58:43 +00:00
|
|
|
go c.RunWatchConnectionLoop(context.Background(), s.PublicKey(), logf, add, remove)
|
2020-06-03 22:42:20 +01:00
|
|
|
return nil
|
|
|
|
}
|