2020-02-05 14:16:58 -08: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.
|
|
|
|
|
|
|
|
// The tailscale command is the Tailscale command-line client. It interacts
|
2020-02-14 10:19:22 -08:00
|
|
|
// with the tailscaled node agent.
|
2020-02-10 13:32:16 -08:00
|
|
|
package main // import "tailscale.com/cmd/tailscale"
|
2020-02-05 14:16:58 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-28 00:08:57 -08:00
|
|
|
"flag"
|
2020-02-05 14:16:58 -08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-02-28 00:08:57 -08:00
|
|
|
"strings"
|
2020-02-05 14:16:58 -08:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/apenwarr/fixconsole"
|
|
|
|
"github.com/pborman/getopt/v2"
|
2020-02-28 00:08:57 -08:00
|
|
|
"github.com/peterbourgon/ff/v2/ffcli"
|
2020-02-17 19:33:01 -08:00
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
2020-02-05 14:16:58 -08:00
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/logpolicy"
|
|
|
|
"tailscale.com/safesocket"
|
|
|
|
)
|
|
|
|
|
2020-02-15 18:14:50 -08:00
|
|
|
// globalStateKey is the ipn.StateKey that tailscaled loads on
|
|
|
|
// startup.
|
|
|
|
//
|
|
|
|
// We have to support multiple state keys for other OSes (Windows in
|
|
|
|
// particular), but right now Unix daemons run with a single
|
|
|
|
// node-global state. To keep open the option of having per-user state
|
|
|
|
// later, the global state key doesn't look like a username.
|
|
|
|
const globalStateKey = "_daemon"
|
|
|
|
|
2020-02-14 10:19:22 -08:00
|
|
|
// pump receives backend messages on conn and pushes them into bc.
|
|
|
|
func pump(ctx context.Context, bc *ipn.BackendClient, conn net.Conn) {
|
2020-02-05 14:16:58 -08:00
|
|
|
defer log.Printf("Control connection done.\n")
|
2020-02-14 10:19:22 -08:00
|
|
|
defer conn.Close()
|
2020-02-05 14:16:58 -08:00
|
|
|
for ctx.Err() == nil {
|
2020-02-14 10:19:22 -08:00
|
|
|
msg, err := ipn.ReadMsg(conn)
|
2020-02-05 14:16:58 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ReadMsg: %v\n", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
bc.GotNotifyMsg(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
err := fixconsole.FixConsoleIfNeeded()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("fixConsoleOutput: %v\n", err)
|
|
|
|
}
|
2020-02-14 10:19:22 -08:00
|
|
|
|
2020-02-28 00:08:57 -08:00
|
|
|
upf := flag.NewFlagSet("up", flag.ExitOnError)
|
|
|
|
upf.StringVar(&upArgs.socket, "socket", "/run/tailscale/tailscaled.sock", "path to tailscaled's unix socket")
|
|
|
|
upf.StringVar(&upArgs.server, "login-server", "https://login.tailscale.com", "base URL of control server")
|
|
|
|
upf.BoolVar(&upArgs.acceptRoutes, "accept-routes", false, "accept routes advertised by other Tailscale nodes")
|
|
|
|
upf.BoolVar(&upArgs.noSingleRoutes, "no-single-routes", false, "don't install routes to single nodes")
|
|
|
|
upf.BoolVar(&upArgs.noPacketFilter, "no-packet-filter", false, "disable packet filter")
|
|
|
|
upf.StringVar(&upArgs.advertiseRoutes, "advertise-routes", "", "routes to advertise to other nodes (comma-separated, e.g. 10.0.0.0/8,192.168.0.0/24)")
|
|
|
|
upCmd := &ffcli.Command{
|
|
|
|
Name: "up",
|
|
|
|
ShortUsage: "up [flags]",
|
|
|
|
ShortHelp: "Connect to your Tailscale network",
|
|
|
|
|
|
|
|
LongHelp: strings.TrimSpace(`
|
|
|
|
"tailscale up" connects this machine to your Tailscale network,
|
|
|
|
triggering authentication if necessary.
|
|
|
|
|
|
|
|
The flags passed to this command set tailscaled options that are
|
|
|
|
specific to this machine, such as whether to advertise some routes to
|
|
|
|
other nodes in the Tailscale network. If you don't specify any flags,
|
|
|
|
options are reset to their default.
|
|
|
|
`),
|
|
|
|
FlagSet: upf,
|
|
|
|
Exec: runUp,
|
|
|
|
}
|
|
|
|
|
|
|
|
netcheckCmd := &ffcli.Command{
|
|
|
|
Name: "netcheck",
|
|
|
|
ShortUsage: "netcheck",
|
|
|
|
ShortHelp: "Print an analysis of local network conditions",
|
|
|
|
Exec: runNetcheck,
|
2020-02-25 14:05:17 -08:00
|
|
|
}
|
2020-02-28 00:08:57 -08:00
|
|
|
|
|
|
|
rootCmd := &ffcli.Command{
|
|
|
|
Name: "tailscale",
|
|
|
|
ShortUsage: "tailscale subcommand [flags]",
|
|
|
|
ShortHelp: "The easiest, most secure way to use WireGuard.",
|
|
|
|
LongHelp: strings.TrimSpace(`
|
|
|
|
This CLI is still under active development. Commands and flags will
|
|
|
|
change in the future.
|
|
|
|
`),
|
|
|
|
Subcommands: []*ffcli.Command{
|
|
|
|
upCmd,
|
|
|
|
netcheckCmd,
|
|
|
|
},
|
|
|
|
Exec: func(context.Context, []string) error { return flag.ErrHelp },
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rootCmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil && err != flag.ErrHelp {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var upArgs = struct {
|
|
|
|
socket string
|
|
|
|
server string
|
|
|
|
acceptRoutes bool
|
|
|
|
noSingleRoutes bool
|
|
|
|
noPacketFilter bool
|
|
|
|
advertiseRoutes string
|
|
|
|
}{}
|
|
|
|
|
|
|
|
func runUp(ctx context.Context, args []string) error {
|
2020-02-18 19:21:02 -08:00
|
|
|
pol := logpolicy.New("tailnode.log.tailscale.io")
|
2020-02-28 00:08:57 -08:00
|
|
|
if len(args) > 0 {
|
2020-02-05 14:16:58 -08:00
|
|
|
log.Fatalf("too many non-flag arguments: %#v", getopt.Args()[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
defer pol.Close()
|
|
|
|
|
2020-02-17 19:33:01 -08:00
|
|
|
var adv []wgcfg.CIDR
|
2020-02-28 00:08:57 -08:00
|
|
|
if upArgs.advertiseRoutes != "" {
|
|
|
|
advroutes := strings.Split(upArgs.advertiseRoutes, ",")
|
|
|
|
for _, s := range advroutes {
|
|
|
|
cidr, err := wgcfg.ParseCIDR(s)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%q is not a valid CIDR prefix: %v", s, err)
|
|
|
|
}
|
|
|
|
adv = append(adv, *cidr)
|
2020-02-17 19:33:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 16:04:31 -08:00
|
|
|
// TODO(apenwarr): fix different semantics between prefs and uflags
|
|
|
|
// TODO(apenwarr): allow setting/using CorpDNS
|
2020-02-20 11:07:00 -08:00
|
|
|
prefs := ipn.NewPrefs()
|
2020-02-28 00:08:57 -08:00
|
|
|
prefs.ControlURL = upArgs.server
|
2020-02-20 11:07:00 -08:00
|
|
|
prefs.WantRunning = true
|
2020-02-28 00:08:57 -08:00
|
|
|
prefs.RouteAll = upArgs.acceptRoutes
|
|
|
|
prefs.AllowSingleHosts = !upArgs.noSingleRoutes
|
|
|
|
prefs.UsePacketFilter = !upArgs.noPacketFilter
|
2020-02-20 11:07:00 -08:00
|
|
|
prefs.AdvertiseRoutes = adv
|
2020-02-13 16:04:31 -08:00
|
|
|
|
2020-02-28 00:08:57 -08:00
|
|
|
c, err := safesocket.Connect(upArgs.socket, 0)
|
2020-02-05 14:16:58 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("safesocket.Connect: %v\n", err)
|
|
|
|
}
|
|
|
|
clientToServer := func(b []byte) {
|
|
|
|
ipn.WriteMsg(c, b)
|
|
|
|
}
|
|
|
|
|
2020-02-28 00:08:57 -08:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-02-15 18:14:50 -08:00
|
|
|
defer cancel()
|
2020-02-05 14:16:58 -08:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-interrupt
|
|
|
|
c.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
bc := ipn.NewBackendClient(log.Printf, clientToServer)
|
2020-02-15 18:14:50 -08:00
|
|
|
bc.SetPrefs(prefs)
|
2020-02-05 14:16:58 -08:00
|
|
|
opts := ipn.Options{
|
2020-02-18 21:03:22 -08:00
|
|
|
StateKey: globalStateKey,
|
2020-02-05 14:16:58 -08:00
|
|
|
Notify: func(n ipn.Notify) {
|
|
|
|
if n.ErrMessage != nil {
|
|
|
|
log.Fatalf("backend error: %v\n", *n.ErrMessage)
|
|
|
|
}
|
|
|
|
if s := n.State; s != nil {
|
|
|
|
switch *s {
|
|
|
|
case ipn.NeedsLogin:
|
|
|
|
bc.StartLoginInteractive()
|
|
|
|
case ipn.NeedsMachineAuth:
|
2020-02-28 00:08:57 -08:00
|
|
|
fmt.Fprintf(os.Stderr, "\nTo authorize your machine, visit (as admin):\n\n\t%s/admin/machines\n\n", upArgs.server)
|
2020-02-05 14:16:58 -08:00
|
|
|
case ipn.Starting, ipn.Running:
|
|
|
|
// Done full authentication process
|
2020-02-15 18:14:50 -08:00
|
|
|
fmt.Fprintf(os.Stderr, "\ntailscaled is authenticated, nothing more to do.\n\n")
|
2020-02-05 14:16:58 -08:00
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if url := n.BrowseToURL; url != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "\nTo authenticate, visit:\n\n\t%s\n\n", *url)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-02-15 18:14:50 -08:00
|
|
|
// We still have to Start right now because it's the only way to
|
|
|
|
// set up notifications and whatnot. This causes a bunch of churn
|
|
|
|
// every time the CLI touches anything.
|
|
|
|
//
|
|
|
|
// TODO(danderson): redo the frontend/backend API to assume
|
|
|
|
// ephemeral frontends that read/modify/write state, once
|
|
|
|
// Windows/Mac state is moved into backend.
|
2020-02-05 14:16:58 -08:00
|
|
|
bc.Start(opts)
|
|
|
|
pump(ctx, bc, c)
|
2020-02-28 00:08:57 -08:00
|
|
|
|
|
|
|
return nil
|
2020-02-05 14:16:58 -08:00
|
|
|
}
|