2020-06-03 21:42:20 +00: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 21:42:20 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-07-29 17:45:07 +00:00
|
|
|
"net"
|
2020-06-03 21:42:20 +00:00
|
|
|
"strings"
|
2021-07-29 17:45:07 +00:00
|
|
|
"time"
|
2020-06-03 21:42:20 +00:00
|
|
|
|
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
|
|
|
"tailscale.com/types/key"
|
|
|
|
"tailscale.com/types/logger"
|
2022-11-18 18:13:14 +00:00
|
|
|
"tailscale.com/util/strs"
|
2020-06-03 21:42:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2021-07-29 17:45:07 +00:00
|
|
|
|
|
|
|
// For meshed peers within a region, connect via VPC addresses.
|
|
|
|
c.SetURLDialer(func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var d net.Dialer
|
|
|
|
var r net.Resolver
|
2022-11-18 18:13:14 +00:00
|
|
|
if base, ok := strs.CutSuffix(host, ".tailscale.com"); ok && port == "443" {
|
2021-07-29 17:45:07 +00:00
|
|
|
subCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
vpcHost := base + "-vpc.tailscale.com"
|
|
|
|
ips, _ := r.LookupIP(subCtx, "ip", vpcHost)
|
|
|
|
if len(ips) > 0 {
|
|
|
|
vpcAddr := net.JoinHostPort(ips[0].String(), port)
|
2021-07-29 21:29:05 +00:00
|
|
|
c, err := d.DialContext(subCtx, network, vpcAddr)
|
2021-07-29 17:45:07 +00:00
|
|
|
if err == nil {
|
|
|
|
log.Printf("connected to %v (%v) instead of %v", vpcHost, ips[0], base)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
log.Printf("failed to connect to %v (%v): %v; trying non-VPC route", vpcHost, ips[0], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d.DialContext(ctx, network, addr)
|
|
|
|
})
|
|
|
|
|
2021-10-28 22:42:50 +00:00
|
|
|
add := func(k key.NodePublic) { s.AddPacketForwarder(k, c) }
|
|
|
|
remove := func(k key.NodePublic) { 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 21:42:20 +00:00
|
|
|
return nil
|
|
|
|
}
|