Move Linux client & common packages into a public repo.

This commit is contained in:
Earl Lee
2020-02-05 14:16:58 -08:00
parent c955043dfe
commit a8d8b8719a
156 changed files with 17113 additions and 0 deletions

63
safesocket/basic_test.go Normal file
View File

@@ -0,0 +1,63 @@
// 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 (
"fmt"
"testing"
)
func TestBasics(t *testing.T) {
fmt.Printf("listening2...\n")
l, port, err := Listen("COOKIE", "Tailscale", "test", 0)
if err != nil {
t.Fatal(err)
}
fmt.Printf("listened.\n")
go func() {
fmt.Printf("accepting...\n")
s, err := l.Accept()
if err != nil {
t.Fatal(err)
}
fmt.Printf("accepted.\n")
l.Close()
s.Write([]byte("hello"))
fmt.Printf("server wrote.\n")
b := make([]byte, 1024)
n, err := s.Read(b)
if err != nil {
t.Fatal(err)
}
fmt.Printf("server read %d bytes.\n", n)
if string(b[:n]) != "world" {
t.Fatalf("got %#v, expected %#v\n", string(b[:n]), "world")
}
s.Close()
}()
fmt.Printf("connecting...\n")
c, err := Connect("COOKIE", "Tailscale", "test", port)
if err != nil {
t.Fatal(err)
}
fmt.Printf("connected.\n")
c.Write([]byte("world"))
fmt.Printf("client wrote.\n")
b := make([]byte, 1024)
n, err := c.Read(b)
if err != nil {
t.Fatal(err)
}
fmt.Printf("client read %d bytes.\n", n)
if string(b[:n]) != "hello" {
t.Fatalf("got %#v, expected %#v\n", string(b[:n]), "hello")
}
c.Close()
}

View File

@@ -0,0 +1,59 @@
// 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"
)
func path(vendor, name string, port uint16) string {
return fmt.Sprintf("127.0.0.1:%v", port)
}
func ConnCloseRead(c net.Conn) error {
return c.(*net.TCPConn).CloseRead()
}
func ConnCloseWrite(c net.Conn) error {
return c.(*net.TCPConn).CloseWrite()
}
// 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)
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?
// 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.
// TODO(apenwarr): handle magic cookie auth
func Listen(cookie, vendor, name 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)
if err != nil {
return nil, 0, err
}
return pipe, uint16(pipe.Addr().(*net.TCPAddr).Port), err
}

61
safesocket/unixsocket.go Normal file
View File

@@ -0,0 +1,61 @@
// 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.
// +build !windows
package safesocket
import (
"fmt"
"net"
"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()
}
func ConnCloseWrite(c net.Conn) error {
return c.(*net.UnixConn).CloseWrite()
}
// 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))
if err != nil {
return nil, err
}
return pipe, err
}
// TODO(apenwarr): handle magic cookie auth
func Listen(cookie, vendor, name 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
// the socket is still live, so let's not replace it. If it doesn't
// work, then replace it.
//
// Note that there's a race condition between these two steps. A
// "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)
if err == nil {
c.Close()
return nil, 0, fmt.Errorf("%v: address already in use", p)
}
_ = os.Remove(p)
pipe, err := net.Listen("unix", p)
if err != nil {
return nil, 0, err
}
os.Chmod(p, 0666)
return pipe, 0, err
}