2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-04-30 04:03:00 +00:00
|
|
|
|
2022-08-02 18:34:03 +00:00
|
|
|
//go:build go1.19
|
2022-04-30 04:03:00 +00:00
|
|
|
|
|
|
|
package tailscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
"net/netip"
|
2022-04-30 04:03:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Routes contains the lists of subnet routes that are currently advertised by a device,
|
|
|
|
// as well as the subnets that are enabled to be routed by the device.
|
|
|
|
type Routes struct {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
AdvertisedRoutes []netip.Prefix `json:"advertisedRoutes"`
|
|
|
|
EnabledRoutes []netip.Prefix `json:"enabledRoutes"`
|
2022-04-30 04:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Routes retrieves the list of subnet routes that have been enabled for a device.
|
|
|
|
// The routes that are returned are not necessarily advertised by the device,
|
|
|
|
// they have only been preapproved.
|
|
|
|
func (c *Client) Routes(ctx context.Context, deviceID string) (routes *Routes, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("tailscale.Routes: %w", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-05-01 04:45:51 +00:00
|
|
|
path := fmt.Sprintf("%s/api/v2/device/%s/routes", c.baseURL(), deviceID)
|
2022-04-30 04:03:00 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, resp, err := c.sendRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If status code was not successful, return the error.
|
|
|
|
// TODO: Change the check for the StatusCode to include other 2XX success codes.
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, handleErrorResponse(b, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sr Routes
|
|
|
|
err = json.Unmarshal(b, &sr)
|
|
|
|
return &sr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type postRoutesParams struct {
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Routes []netip.Prefix `json:"routes"`
|
2022-04-30 04:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRoutes updates the list of subnets that are enabled for a device.
|
2022-07-26 03:55:44 +00:00
|
|
|
// Subnets must be parsable by net/netip.ParsePrefix.
|
2022-04-30 04:03:00 +00:00
|
|
|
// Subnets do not have to be currently advertised by a device, they may be pre-enabled.
|
|
|
|
// Returns the updated list of enabled and advertised subnet routes in a *Routes object.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func (c *Client) SetRoutes(ctx context.Context, deviceID string, subnets []netip.Prefix) (routes *Routes, err error) {
|
2022-04-30 04:03:00 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("tailscale.SetRoutes: %w", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
params := &postRoutesParams{Routes: subnets}
|
|
|
|
data, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-01 04:45:51 +00:00
|
|
|
path := fmt.Sprintf("%s/api/v2/device/%s/routes", c.baseURL(), deviceID)
|
2022-04-30 04:03:00 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", path, bytes.NewBuffer(data))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, resp, err := c.sendRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If status code was not successful, return the error.
|
|
|
|
// TODO: Change the check for the StatusCode to include other 2XX success codes.
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, handleErrorResponse(b, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var srr *Routes
|
|
|
|
if err := json.Unmarshal(b, &srr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return srr, err
|
|
|
|
}
|