2020-07-15 07:56:48 -07: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 cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-09-10 19:55:09 -07:00
|
|
|
"errors"
|
2020-07-15 07:56:48 -07:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-08-28 20:49:14 +08:00
|
|
|
"sync"
|
2020-07-15 07:56:48 -07:00
|
|
|
|
|
|
|
"github.com/peterbourgon/ff/v2/ffcli"
|
|
|
|
"github.com/tailscale/wireguard-go/wgcfg"
|
2020-08-10 08:10:15 -07:00
|
|
|
"inet.af/netaddr"
|
2020-07-15 07:56:48 -07:00
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/tailcfg"
|
2020-08-03 11:26:07 -06:00
|
|
|
"tailscale.com/version"
|
2020-09-10 19:55:09 -07:00
|
|
|
"tailscale.com/version/distro"
|
2020-07-15 07:56:48 -07:00
|
|
|
"tailscale.com/wgengine/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
var 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 are specific to this machine. If you don't
|
|
|
|
specify any flags, options are reset to their default.
|
|
|
|
`),
|
|
|
|
FlagSet: (func() *flag.FlagSet {
|
|
|
|
upf := flag.NewFlagSet("up", flag.ExitOnError)
|
|
|
|
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")
|
2020-07-23 16:09:33 -04:00
|
|
|
upf.BoolVar(&upArgs.acceptDNS, "accept-dns", true, "accept DNS configuration from the admin panel")
|
2020-07-15 07:56:48 -07:00
|
|
|
upf.BoolVar(&upArgs.singleRoutes, "host-routes", true, "install host routes to other Tailscale nodes")
|
|
|
|
upf.BoolVar(&upArgs.shieldsUp, "shields-up", false, "don't allow incoming connections")
|
2020-08-28 20:49:14 +08:00
|
|
|
upf.BoolVar(&upArgs.forceReauth, "force-reauth", false, "force reauthentication")
|
2020-07-15 07:56:48 -07:00
|
|
|
upf.StringVar(&upArgs.advertiseTags, "advertise-tags", "", "ACL tags to request (comma-separated, e.g. eng,montreal,ssh)")
|
|
|
|
upf.StringVar(&upArgs.authKey, "authkey", "", "node authorization key")
|
2020-07-15 15:50:02 -04:00
|
|
|
upf.StringVar(&upArgs.hostname, "hostname", "", "hostname to use instead of the one provided by the OS")
|
2020-08-03 11:26:07 -06:00
|
|
|
if runtime.GOOS == "linux" || isBSD(runtime.GOOS) || version.OS() == "macOS" {
|
2020-07-15 07:56:48 -07:00
|
|
|
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)")
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "linux" {
|
2020-09-10 19:55:09 -07:00
|
|
|
upf.BoolVar(&upArgs.snat, "snat-subnet-routes", true, "source NAT traffic to local routes advertised with --advertise-routes")
|
|
|
|
upf.StringVar(&upArgs.netfilterMode, "netfilter-mode", defaultNetfilterMode(), "netfilter mode (one of on, nodivert, off)")
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
|
|
|
return upf
|
|
|
|
})(),
|
|
|
|
Exec: runUp,
|
|
|
|
}
|
|
|
|
|
2020-09-10 19:55:09 -07:00
|
|
|
func defaultNetfilterMode() string {
|
|
|
|
if distro.Get() == distro.Synology {
|
|
|
|
return "off"
|
|
|
|
}
|
|
|
|
return "on"
|
|
|
|
}
|
|
|
|
|
2020-07-15 07:56:48 -07:00
|
|
|
var upArgs struct {
|
|
|
|
server string
|
|
|
|
acceptRoutes bool
|
2020-07-23 16:09:33 -04:00
|
|
|
acceptDNS bool
|
2020-07-15 07:56:48 -07:00
|
|
|
singleRoutes bool
|
|
|
|
shieldsUp bool
|
2020-08-28 20:49:14 +08:00
|
|
|
forceReauth bool
|
2020-07-15 07:56:48 -07:00
|
|
|
advertiseRoutes string
|
|
|
|
advertiseTags string
|
|
|
|
snat bool
|
|
|
|
netfilterMode string
|
|
|
|
authKey string
|
2020-07-15 15:50:02 -04:00
|
|
|
hostname string
|
|
|
|
}
|
|
|
|
|
2020-07-15 07:56:48 -07:00
|
|
|
// parseIPOrCIDR parses an IP address or a CIDR prefix. If the input
|
|
|
|
// is an IP address, it is returned in CIDR form with a /32 mask for
|
|
|
|
// IPv4 or a /128 mask for IPv6.
|
|
|
|
func parseIPOrCIDR(s string) (wgcfg.CIDR, bool) {
|
|
|
|
if strings.Contains(s, "/") {
|
|
|
|
ret, err := wgcfg.ParseCIDR(s)
|
|
|
|
if err != nil {
|
|
|
|
return wgcfg.CIDR{}, false
|
|
|
|
}
|
|
|
|
return ret, true
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, ok := wgcfg.ParseIP(s)
|
|
|
|
if !ok {
|
|
|
|
return wgcfg.CIDR{}, false
|
|
|
|
}
|
|
|
|
if ip.Is4() {
|
2020-07-16 08:39:38 -07:00
|
|
|
return wgcfg.CIDR{IP: ip, Mask: 32}, true
|
2020-07-15 07:56:48 -07:00
|
|
|
} else {
|
2020-07-16 08:39:38 -07:00
|
|
|
return wgcfg.CIDR{IP: ip, Mask: 128}, true
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isBSD(s string) bool {
|
|
|
|
return s == "dragonfly" || s == "freebsd" || s == "netbsd" || s == "openbsd"
|
|
|
|
}
|
|
|
|
|
2020-08-10 08:10:15 -07:00
|
|
|
func warnf(format string, args ...interface{}) {
|
2020-07-15 07:56:48 -07:00
|
|
|
fmt.Printf("Warning: "+format+"\n", args...)
|
|
|
|
}
|
|
|
|
|
2020-08-03 11:26:07 -06:00
|
|
|
// checkIPForwarding prints warnings if IP forwarding is not
|
2020-07-15 07:56:48 -07:00
|
|
|
// enabled, or if we were unable to verify the state of IP forwarding.
|
|
|
|
func checkIPForwarding() {
|
|
|
|
var key string
|
|
|
|
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
key = "net.ipv4.ip_forward"
|
2020-08-03 11:26:07 -06:00
|
|
|
} else if isBSD(runtime.GOOS) || version.OS() == "macOS" {
|
2020-07-15 07:56:48 -07:00
|
|
|
key = "net.inet.ip.forwarding"
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := exec.Command("sysctl", "-n", key).Output()
|
|
|
|
if err != nil {
|
2020-08-10 08:10:15 -07:00
|
|
|
warnf("couldn't check %s (%v).\nSubnet routes won't work without IP forwarding.", key, err)
|
2020-07-15 07:56:48 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
on, err := strconv.ParseBool(string(bytes.TrimSpace(bs)))
|
|
|
|
if err != nil {
|
2020-08-10 08:10:15 -07:00
|
|
|
warnf("couldn't parse %s (%v).\nSubnet routes won't work without IP forwarding.", key, err)
|
2020-07-15 07:56:48 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !on {
|
2020-08-10 08:10:15 -07:00
|
|
|
warnf("%s is disabled. Subnet routes won't work.", key)
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runUp(ctx context.Context, args []string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
log.Fatalf("too many non-flag arguments: %q", args)
|
|
|
|
}
|
|
|
|
|
2020-09-10 19:55:09 -07:00
|
|
|
if distro.Get() == distro.Synology {
|
|
|
|
notSupported := "not yet supported on Synology; see https://github.com/tailscale/tailscale/issues/451"
|
|
|
|
if upArgs.advertiseRoutes != "" {
|
|
|
|
return errors.New("--advertise-routes is " + notSupported)
|
|
|
|
}
|
|
|
|
if upArgs.acceptRoutes {
|
|
|
|
return errors.New("--accept-routes is " + notSupported)
|
|
|
|
}
|
|
|
|
if upArgs.netfilterMode != "off" {
|
|
|
|
return errors.New("--netfilter-mode values besides \"off\" " + notSupported)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 07:56:48 -07:00
|
|
|
var routes []wgcfg.CIDR
|
|
|
|
if upArgs.advertiseRoutes != "" {
|
|
|
|
advroutes := strings.Split(upArgs.advertiseRoutes, ",")
|
|
|
|
for _, s := range advroutes {
|
|
|
|
cidr, ok := parseIPOrCIDR(s)
|
2020-08-10 08:10:15 -07:00
|
|
|
ipp, err := netaddr.ParseIPPrefix(s) // parse it with other pawith both packages
|
|
|
|
if !ok || err != nil {
|
|
|
|
fatalf("%q is not a valid IP address or CIDR prefix", s)
|
|
|
|
}
|
|
|
|
if ipp != ipp.Masked() {
|
|
|
|
fatalf("%s has non-address bits set; expected %s", ipp, ipp.Masked())
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
|
|
|
routes = append(routes, cidr)
|
|
|
|
}
|
2020-08-10 08:10:15 -07:00
|
|
|
checkIPForwarding()
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var tags []string
|
|
|
|
if upArgs.advertiseTags != "" {
|
|
|
|
tags = strings.Split(upArgs.advertiseTags, ",")
|
2020-10-28 07:59:57 -07:00
|
|
|
for i, tag := range tags {
|
|
|
|
if strings.HasPrefix(tag, "tag:") {
|
|
|
|
// Accept fully-qualified tags (starting with
|
|
|
|
// "tag:"), as we do in the ACL file.
|
2020-11-04 08:04:00 -08:00
|
|
|
if err := tailcfg.CheckTag(tag); err != nil {
|
2020-10-28 07:59:57 -07:00
|
|
|
fatalf("tag: %q: %v", tag, err)
|
|
|
|
}
|
2020-11-04 08:04:00 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := tailcfg.CheckTagSuffix(tag); err != nil {
|
2020-10-28 07:59:57 -07:00
|
|
|
fatalf("tag: %q: %v", tag, err)
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
2020-10-28 07:59:57 -07:00
|
|
|
tags[i] = "tag:" + tag
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:05:08 -04:00
|
|
|
if len(upArgs.hostname) > 256 {
|
2020-08-10 08:10:15 -07:00
|
|
|
fatalf("hostname too long: %d bytes (max 256)", len(upArgs.hostname))
|
2020-07-15 15:50:02 -04:00
|
|
|
}
|
|
|
|
|
2020-07-15 07:56:48 -07:00
|
|
|
// TODO(apenwarr): fix different semantics between prefs and uflags
|
|
|
|
prefs := ipn.NewPrefs()
|
|
|
|
prefs.ControlURL = upArgs.server
|
|
|
|
prefs.WantRunning = true
|
|
|
|
prefs.RouteAll = upArgs.acceptRoutes
|
2020-07-23 16:09:33 -04:00
|
|
|
prefs.CorpDNS = upArgs.acceptDNS
|
2020-07-15 07:56:48 -07:00
|
|
|
prefs.AllowSingleHosts = upArgs.singleRoutes
|
|
|
|
prefs.ShieldsUp = upArgs.shieldsUp
|
|
|
|
prefs.AdvertiseRoutes = routes
|
|
|
|
prefs.AdvertiseTags = tags
|
|
|
|
prefs.NoSNAT = !upArgs.snat
|
2020-07-15 15:50:02 -04:00
|
|
|
prefs.Hostname = upArgs.hostname
|
ipn, ipnserver, cmd/tailscale: add "server mode" support on Windows
This partially (but not yet fully) migrates Windows to tailscaled's
StateStore storage system.
This adds a new bool Pref, ForceDaemon, defined as:
// ForceDaemon specifies whether a platform that normally
// operates in "client mode" (that is, requires an active user
// logged in with the GUI app running) should keep running after the
// GUI ends and/or the user logs out.
//
// The only current applicable platform is Windows. This
// forced Windows to go into "server mode" where Tailscale is
// running even with no users logged in. This might also be
// used for macOS in the future. This setting has no effect
// for Linux/etc, which always operate in daemon mode.
Then, when ForceDaemon becomes true, we now write use the StateStore
to track which user started it in server mode, and store their prefs
under that key.
The ipnserver validates the connections/identities and informs that
LocalBackend which userid is currently in charge.
The GUI can then enable/disable server mode at runtime, without using
the CLI.
But the "tailscale up" CLI was also fixed, so Windows users can use
authkeys or ACL tags, etc.
Updates #275
2020-10-12 14:28:21 -07:00
|
|
|
prefs.ForceDaemon = (runtime.GOOS == "windows")
|
|
|
|
|
2020-07-15 07:56:48 -07:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
switch upArgs.netfilterMode {
|
|
|
|
case "on":
|
|
|
|
prefs.NetfilterMode = router.NetfilterOn
|
|
|
|
case "nodivert":
|
|
|
|
prefs.NetfilterMode = router.NetfilterNoDivert
|
2020-08-10 08:10:15 -07:00
|
|
|
warnf("netfilter=nodivert; add iptables calls to ts-* chains manually.")
|
2020-07-15 07:56:48 -07:00
|
|
|
case "off":
|
|
|
|
prefs.NetfilterMode = router.NetfilterOff
|
2020-08-10 08:10:15 -07:00
|
|
|
warnf("netfilter=off; configure iptables yourself.")
|
2020-07-15 07:56:48 -07:00
|
|
|
default:
|
2020-08-10 08:10:15 -07:00
|
|
|
fatalf("invalid value --netfilter-mode: %q", upArgs.netfilterMode)
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c, bc, ctx, cancel := connect(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var printed bool
|
2020-08-28 20:49:14 +08:00
|
|
|
var loginOnce sync.Once
|
|
|
|
startLoginInteractive := func() { loginOnce.Do(func() { bc.StartLoginInteractive() }) }
|
2020-07-15 07:56:48 -07:00
|
|
|
|
|
|
|
bc.SetPrefs(prefs)
|
ipn, ipnserver, cmd/tailscale: add "server mode" support on Windows
This partially (but not yet fully) migrates Windows to tailscaled's
StateStore storage system.
This adds a new bool Pref, ForceDaemon, defined as:
// ForceDaemon specifies whether a platform that normally
// operates in "client mode" (that is, requires an active user
// logged in with the GUI app running) should keep running after the
// GUI ends and/or the user logs out.
//
// The only current applicable platform is Windows. This
// forced Windows to go into "server mode" where Tailscale is
// running even with no users logged in. This might also be
// used for macOS in the future. This setting has no effect
// for Linux/etc, which always operate in daemon mode.
Then, when ForceDaemon becomes true, we now write use the StateStore
to track which user started it in server mode, and store their prefs
under that key.
The ipnserver validates the connections/identities and informs that
LocalBackend which userid is currently in charge.
The GUI can then enable/disable server mode at runtime, without using
the CLI.
But the "tailscale up" CLI was also fixed, so Windows users can use
authkeys or ACL tags, etc.
Updates #275
2020-10-12 14:28:21 -07:00
|
|
|
|
2020-07-15 07:56:48 -07:00
|
|
|
opts := ipn.Options{
|
2020-09-29 20:51:25 -07:00
|
|
|
StateKey: ipn.GlobalDaemonStateKey,
|
2020-07-15 07:56:48 -07:00
|
|
|
AuthKey: upArgs.authKey,
|
|
|
|
Notify: func(n ipn.Notify) {
|
|
|
|
if n.ErrMessage != nil {
|
2020-08-10 08:10:15 -07:00
|
|
|
fatalf("backend error: %v\n", *n.ErrMessage)
|
2020-07-15 07:56:48 -07:00
|
|
|
}
|
|
|
|
if s := n.State; s != nil {
|
|
|
|
switch *s {
|
|
|
|
case ipn.NeedsLogin:
|
|
|
|
printed = true
|
2020-08-28 20:49:14 +08:00
|
|
|
startLoginInteractive()
|
2020-07-15 07:56:48 -07:00
|
|
|
case ipn.NeedsMachineAuth:
|
|
|
|
printed = true
|
|
|
|
fmt.Fprintf(os.Stderr, "\nTo authorize your machine, visit (as admin):\n\n\t%s/admin/machines\n\n", upArgs.server)
|
|
|
|
case ipn.Starting, ipn.Running:
|
|
|
|
// Done full authentication process
|
|
|
|
if printed {
|
|
|
|
// Only need to print an update if we printed the "please click" message earlier.
|
|
|
|
fmt.Fprintf(os.Stderr, "Success.\n")
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if url := n.BrowseToURL; url != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "\nTo authenticate, visit:\n\n\t%s\n\n", *url)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
ipn, ipnserver, cmd/tailscale: add "server mode" support on Windows
This partially (but not yet fully) migrates Windows to tailscaled's
StateStore storage system.
This adds a new bool Pref, ForceDaemon, defined as:
// ForceDaemon specifies whether a platform that normally
// operates in "client mode" (that is, requires an active user
// logged in with the GUI app running) should keep running after the
// GUI ends and/or the user logs out.
//
// The only current applicable platform is Windows. This
// forced Windows to go into "server mode" where Tailscale is
// running even with no users logged in. This might also be
// used for macOS in the future. This setting has no effect
// for Linux/etc, which always operate in daemon mode.
Then, when ForceDaemon becomes true, we now write use the StateStore
to track which user started it in server mode, and store their prefs
under that key.
The ipnserver validates the connections/identities and informs that
LocalBackend which userid is currently in charge.
The GUI can then enable/disable server mode at runtime, without using
the CLI.
But the "tailscale up" CLI was also fixed, so Windows users can use
authkeys or ACL tags, etc.
Updates #275
2020-10-12 14:28:21 -07:00
|
|
|
|
|
|
|
// On Windows, we still run in mostly the "legacy" way that
|
|
|
|
// predated the server's StateStore. That is, we send an empty
|
|
|
|
// StateKey and send the prefs directly. Although the Windows
|
|
|
|
// supports server mode, though, the transition to StateStore
|
|
|
|
// is only half complete. Only server mode uses it, and the
|
|
|
|
// Windows service (~tailscaled) is the one that computes the
|
|
|
|
// StateKey based on the connection idenity. So for now, just
|
|
|
|
// do as the Windows GUI's always done:
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// The Windows service will set this as needed based
|
|
|
|
// on our connection's identity.
|
|
|
|
opts.StateKey = ""
|
|
|
|
opts.Prefs = prefs
|
|
|
|
}
|
|
|
|
|
2020-07-15 07:56:48 -07: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.
|
|
|
|
bc.Start(opts)
|
2020-08-28 20:49:14 +08:00
|
|
|
if upArgs.forceReauth {
|
|
|
|
printed = true
|
|
|
|
startLoginInteractive()
|
|
|
|
}
|
2020-07-15 07:56:48 -07:00
|
|
|
pump(ctx, bc, c)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|