2023-01-27 13:37:20 -08:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-06-03 14:42:20 -07:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-12 10:58:43 -08:00
|
|
|
"context"
|
2020-06-03 14:42:20 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-07-29 10:45:07 -07:00
|
|
|
"net"
|
2020-06-03 14:42:20 -07:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
2024-04-26 22:06:20 -07:00
|
|
|
"tailscale.com/net/netmon"
|
2020-06-03 14:42:20 -07:00
|
|
|
"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")
|
|
|
|
}
|
2025-01-07 05:34:07 -06:00
|
|
|
for _, hostTuple := range strings.Split(*meshWith, ",") {
|
|
|
|
if err := startMeshWithHost(s, hostTuple); err != nil {
|
2020-06-03 14:42:20 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-07 05:34:07 -06:00
|
|
|
func startMeshWithHost(s *derp.Server, hostTuple string) error {
|
|
|
|
var host string
|
|
|
|
var dialHost string
|
|
|
|
hostParts := strings.Split(hostTuple, "/")
|
|
|
|
if len(hostParts) > 2 {
|
|
|
|
return fmt.Errorf("too many components in host tuple %q", hostTuple)
|
|
|
|
}
|
|
|
|
host = hostParts[0]
|
|
|
|
if len(hostParts) == 2 {
|
|
|
|
dialHost = hostParts[1]
|
|
|
|
} else {
|
|
|
|
dialHost = hostParts[0]
|
|
|
|
}
|
|
|
|
|
2020-06-03 14:42:20 -07:00
|
|
|
logf := logger.WithPrefix(log.Printf, fmt.Sprintf("mesh(%q): ", host))
|
2024-04-26 22:06:20 -07:00
|
|
|
netMon := netmon.NewStatic() // good enough for cmd/derper; no need for netns fanciness
|
|
|
|
c, err := derphttp.NewClient(s.PrivateKey(), "https://"+host+"/derp", logf, netMon)
|
2020-06-03 14:42:20 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.MeshKey = s.MeshKey()
|
2023-10-25 11:59:06 -07:00
|
|
|
c.WatchConnectionChanges = true
|
2021-07-29 10:45:07 -07:00
|
|
|
|
2025-01-07 05:34:07 -06:00
|
|
|
logf("will dial %q for %q", dialHost, host)
|
|
|
|
if dialHost != host {
|
2021-07-29 10:45:07 -07:00
|
|
|
var d net.Dialer
|
2025-01-07 05:34:07 -06:00
|
|
|
c.SetURLDialer(func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
_, port, err := net.SplitHostPort(addr)
|
2025-01-06 15:39:41 -08:00
|
|
|
if err != nil {
|
2025-01-07 05:34:07 -06:00
|
|
|
logf("failed to split %q: %v", addr, err)
|
|
|
|
return nil, err
|
2025-01-06 15:39:41 -08:00
|
|
|
}
|
2025-01-07 05:34:07 -06:00
|
|
|
dialAddr := net.JoinHostPort(dialHost, port)
|
|
|
|
logf("dialing %q instead of %q", dialAddr, addr)
|
|
|
|
return d.DialContext(ctx, network, dialAddr)
|
|
|
|
})
|
|
|
|
}
|
2021-07-29 10:45:07 -07:00
|
|
|
|
2024-06-25 08:04:12 -07:00
|
|
|
add := func(m derp.PeerPresentMessage) { s.AddPacketForwarder(m.Key, c) }
|
|
|
|
remove := func(m derp.PeerGoneMessage) { s.RemovePacketForwarder(m.Peer, c) }
|
2021-02-12 10:58:43 -08:00
|
|
|
go c.RunWatchConnectionLoop(context.Background(), s.PublicKey(), logf, add, remove)
|
2020-06-03 14:42:20 -07:00
|
|
|
return nil
|
|
|
|
}
|