safesocket: simplify API.
On unix, we want to provide a full path to the desired unix socket. On windows, currently we want to provide a TCP port, but someday we'll also provide a "path-ish" object for a named pipe. For now, simplify the API down to exactly a path and a TCP port. Signed-off-by: David Anderson <dave@natulte.net>
This commit is contained in:
committed by
Dave Anderson
parent
b72e6446e2
commit
4460bd638b
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestBasics(t *testing.T) {
|
||||
l, port, err := Listen("COOKIE", "Tailscale", "test", 0)
|
||||
l, port, err := Listen("test", 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -42,7 +42,7 @@ func TestBasics(t *testing.T) {
|
||||
}()
|
||||
|
||||
go func() {
|
||||
c, err := Connect("COOKIE", "Tailscale", "test", port)
|
||||
c, err := Connect("test", port)
|
||||
if err != nil {
|
||||
errs <- err
|
||||
return
|
||||
|
||||
@@ -24,9 +24,8 @@ func ConnCloseWrite(c net.Conn) error {
|
||||
}
|
||||
|
||||
// TODO(apenwarr): handle magic cookie auth
|
||||
func Connect(cookie, vendor, name string, port uint16) (net.Conn, error) {
|
||||
p := path(vendor, name, port)
|
||||
pipe, err := net.Dial("tcp", p)
|
||||
func Connect(path string, port uint16) (net.Conn, error) {
|
||||
pipe, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -46,12 +45,11 @@ func setFlags(network, address string, c syscall.RawConn) error {
|
||||
// just always using a TCP session on a fixed port on localhost. As a
|
||||
// result, on Windows we ignore the vendor and name strings.
|
||||
// TODO(apenwarr): handle magic cookie auth
|
||||
func Listen(cookie, vendor, name string, port uint16) (net.Listener, uint16, error) {
|
||||
func Listen(path string, port uint16) (net.Listener, uint16, error) {
|
||||
lc := net.ListenConfig{
|
||||
Control: setFlags,
|
||||
}
|
||||
p := path(vendor, name, port)
|
||||
pipe, err := lc.Listen(context.Background(), "tcp", p)
|
||||
pipe, err := lc.Listen(context.Background(), "tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
@@ -12,10 +12,6 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func path(vendor, name string) string {
|
||||
return fmt.Sprintf("%s-%s.sock", vendor, name)
|
||||
}
|
||||
|
||||
func ConnCloseRead(c net.Conn) error {
|
||||
return c.(*net.UnixConn).CloseRead()
|
||||
}
|
||||
@@ -25,8 +21,8 @@ func ConnCloseWrite(c net.Conn) error {
|
||||
}
|
||||
|
||||
// TODO(apenwarr): handle magic cookie auth
|
||||
func Connect(cookie, vendor, name string, port uint16) (net.Conn, error) {
|
||||
pipe, err := net.Dial("unix", path(vendor, name))
|
||||
func Connect(path string, port uint16) (net.Conn, error) {
|
||||
pipe, err := net.Dial("unix", path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -34,7 +30,7 @@ func Connect(cookie, vendor, name string, port uint16) (net.Conn, error) {
|
||||
}
|
||||
|
||||
// TODO(apenwarr): handle magic cookie auth
|
||||
func Listen(cookie, vendor, name string, port uint16) (net.Listener, uint16, error) {
|
||||
func Listen(path string, port uint16) (net.Listener, uint16, error) {
|
||||
// Unix sockets hang around in the filesystem even after nobody
|
||||
// is listening on them. (Which is really unfortunate but long-
|
||||
// entrenched semantics.) Try connecting first; if it works, then
|
||||
@@ -45,17 +41,16 @@ func Listen(cookie, vendor, name string, port uint16) (net.Listener, uint16, err
|
||||
// "proper" daemon usually uses a dance involving pidfiles to first
|
||||
// ensure that no other instances of itself are running, but that's
|
||||
// beyond the scope of our simple socket library.
|
||||
p := path(vendor, name)
|
||||
c, err := net.Dial("unix", p)
|
||||
c, err := net.Dial("unix", path)
|
||||
if err == nil {
|
||||
c.Close()
|
||||
return nil, 0, fmt.Errorf("%v: address already in use", p)
|
||||
return nil, 0, fmt.Errorf("%v: address already in use", path)
|
||||
}
|
||||
_ = os.Remove(p)
|
||||
pipe, err := net.Listen("unix", p)
|
||||
_ = os.Remove(path)
|
||||
pipe, err := net.Listen("unix", path)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
os.Chmod(p, 0666)
|
||||
os.Chmod(path, 0666)
|
||||
return pipe, 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user