2020-02-05 22:16:58 +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 safesocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
safesocket: add ConnectionStrategy, provide control over fallbacks
fee2d9fad added support for cmd/tailscale to connect to IPNExtension.
It came in two parts: If no socket was provided, dial IPNExtension first,
and also, if dialing the socket failed, fall back to IPNExtension.
The second half of that support caused the integration tests to fail
when run on a machine that was also running IPNExtension.
The integration tests want to wait until the tailscaled instances
that they spun up are listening. They do that by dialing the new
instance. But when that dial failed, it was falling back to IPNExtension,
so it appeared (incorrectly) that tailscaled was running.
Hilarity predictably ensued.
If a user (or a test) explicitly provides a socket to dial,
it is a reasonable assumption that they have a specific tailscaled
in mind and don't want to fall back to IPNExtension.
It is certainly true of the integration tests.
Instead of adding a bool to Connect, split out the notion of a
connection strategy. For now, the implementation remains the same,
but with the details hidden a bit. Later, we can improve that.
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-12-08 21:55:55 +00:00
|
|
|
func connect(s *ConnectionStrategy) (net.Conn, error) {
|
|
|
|
pipe, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", s.port))
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pipe, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFlags(network, address string, c syscall.RawConn) error {
|
|
|
|
return c.Control(func(fd uintptr) {
|
|
|
|
syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET,
|
|
|
|
syscall.SO_REUSEADDR, 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(apenwarr): use named pipes instead of sockets?
|
2022-08-02 17:33:46 +01:00
|
|
|
//
|
|
|
|
// I tried to use winio.ListenPipe() here, but that code is a disaster,
|
|
|
|
// built on top of an API that's a disaster. So for now we'll hack it by
|
|
|
|
// just always using a TCP session on a fixed port on localhost. As a
|
|
|
|
// result, on Windows we ignore the vendor and name strings.
|
|
|
|
// NOTE(bradfitz): Jason did a new pipe package: https://go-review.googlesource.com/c/sys/+/299009
|
2020-02-25 16:46:26 +00:00
|
|
|
func listen(path string, port uint16) (_ net.Listener, gotPort uint16, _ error) {
|
2020-02-05 22:16:58 +00:00
|
|
|
lc := net.ListenConfig{
|
|
|
|
Control: setFlags,
|
|
|
|
}
|
2020-02-18 20:33:28 +00:00
|
|
|
pipe, err := lc.Listen(context.Background(), "tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return pipe, uint16(pipe.Addr().(*net.TCPAddr).Port), err
|
|
|
|
}
|