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"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/apenwarr/fixconsole"
|
|
|
|
"github.com/pborman/getopt/v2"
|
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-18 12:33:28 -08:00
|
|
|
socket := getopt.StringLong("socket", 0, "/run/tailscale/tailscaled.sock", "path of tailscaled's unix socket")
|
2020-02-05 14:16:58 -08:00
|
|
|
server := getopt.StringLong("server", 's', "https://login.tailscale.com", "URL to tailcontrol server")
|
|
|
|
nuroutes := getopt.BoolLong("no-single-routes", 'N', "disallow (non-subnet) routes to single nodes")
|
2020-02-15 18:14:50 -08:00
|
|
|
routeall := getopt.BoolLong("remote-routes", 'R', "accept routes advertised by remote nodes")
|
|
|
|
nopf := getopt.BoolLong("no-packet-filter", 'F', "disable packet filter")
|
2020-02-17 19:33:01 -08:00
|
|
|
advroutes := getopt.ListLong("routes", 'r', "routes to advertise to other nodes (comma-separated, e.g. 10.0.0.0/8,192.168.1.0/24)")
|
2020-02-05 14:16:58 -08:00
|
|
|
getopt.Parse()
|
2020-02-15 18:14:50 -08:00
|
|
|
pol := logpolicy.New("tailnode.log.tailscale.io", "tailscale")
|
2020-02-05 14:16:58 -08:00
|
|
|
if len(getopt.Args()) > 0 {
|
|
|
|
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
|
|
|
|
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-13 16:04:31 -08:00
|
|
|
// TODO(apenwarr): fix different semantics between prefs and uflags
|
|
|
|
// TODO(apenwarr): allow setting/using CorpDNS
|
2020-02-15 18:14:50 -08:00
|
|
|
prefs := ipn.Prefs{
|
2020-02-18 21:03:22 -08:00
|
|
|
ControlURL: *server,
|
2020-02-15 18:14:50 -08:00
|
|
|
WantRunning: true,
|
|
|
|
RouteAll: *routeall,
|
|
|
|
AllowSingleHosts: !*nuroutes,
|
|
|
|
UsePacketFilter: !*nopf,
|
2020-02-17 19:33:01 -08:00
|
|
|
AdvertiseRoutes: adv,
|
2020-02-15 18:14:50 -08:00
|
|
|
}
|
2020-02-13 16:04:31 -08:00
|
|
|
|
2020-02-18 12:33:28 -08:00
|
|
|
c, err := safesocket.Connect(*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)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
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:
|
|
|
|
fmt.Fprintf(os.Stderr, "\nTo authorize your machine, visit (as admin):\n\n\t%s/admin/machines\n\n", *server)
|
|
|
|
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)
|
|
|
|
}
|