mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-23 18:15:26 +00:00
c883e79884
This commit rewrites the `routes list` command to use ptables to present a slightly nicer list, including a new field if the route is enabled or not (which is quite useful). In addition, it reworks the enable command to support enabling multiple routes (not only one route as per removed TODO). This allows users to actually take advantage of exit-nodes and subnet relays.
129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
package headscale
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"gopkg.in/check.v1"
|
|
"gorm.io/datatypes"
|
|
"inet.af/netaddr"
|
|
"tailscale.com/tailcfg"
|
|
)
|
|
|
|
func (s *Suite) TestGetRoutes(c *check.C) {
|
|
n, err := h.CreateNamespace("test")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
_, err = h.GetMachine("test", "testmachine")
|
|
c.Assert(err, check.NotNil)
|
|
|
|
route, err := netaddr.ParseIPPrefix("10.0.0.0/24")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
hi := tailcfg.Hostinfo{
|
|
RoutableIPs: []netaddr.IPPrefix{route},
|
|
}
|
|
hostinfo, err := json.Marshal(hi)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
m := Machine{
|
|
ID: 0,
|
|
MachineKey: "foo",
|
|
NodeKey: "bar",
|
|
DiscoKey: "faa",
|
|
Name: "test_get_route_machine",
|
|
NamespaceID: n.ID,
|
|
Registered: true,
|
|
RegisterMethod: "authKey",
|
|
AuthKeyID: uint(pak.ID),
|
|
HostInfo: datatypes.JSON(hostinfo),
|
|
}
|
|
h.db.Save(&m)
|
|
|
|
r, err := h.GetAdvertisedNodeRoutes("test", "testmachine")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(len(*r), check.Equals, 1)
|
|
|
|
err = h.EnableNodeRoute("test", "testmachine", "192.168.0.0/24")
|
|
c.Assert(err, check.NotNil)
|
|
|
|
err = h.EnableNodeRoute("test", "testmachine", "10.0.0.0/24")
|
|
c.Assert(err, check.IsNil)
|
|
}
|
|
|
|
func (s *Suite) TestGetEnableRoutes(c *check.C) {
|
|
n, err := h.CreateNamespace("test")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
_, err = h.GetMachine("test", "testmachine")
|
|
c.Assert(err, check.NotNil)
|
|
|
|
route, err := netaddr.ParseIPPrefix(
|
|
"10.0.0.0/24",
|
|
)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
route2, err := netaddr.ParseIPPrefix(
|
|
"150.0.10.0/25",
|
|
)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
hi := tailcfg.Hostinfo{
|
|
RoutableIPs: []netaddr.IPPrefix{route, route2},
|
|
}
|
|
hostinfo, err := json.Marshal(hi)
|
|
c.Assert(err, check.IsNil)
|
|
|
|
m := Machine{
|
|
ID: 0,
|
|
MachineKey: "foo",
|
|
NodeKey: "bar",
|
|
DiscoKey: "faa",
|
|
Name: "test_enable_route_machine",
|
|
NamespaceID: n.ID,
|
|
Registered: true,
|
|
RegisterMethod: "authKey",
|
|
AuthKeyID: uint(pak.ID),
|
|
HostInfo: datatypes.JSON(hostinfo),
|
|
}
|
|
h.db.Save(&m)
|
|
|
|
availableRoutes, err := h.GetAdvertisedNodeRoutes("test", "testmachine")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(len(*availableRoutes), check.Equals, 2)
|
|
|
|
enabledRoutes, err := h.GetEnabledNodeRoutes("test", "testmachine")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(len(enabledRoutes), check.Equals, 0)
|
|
|
|
err = h.EnableNodeRoute("test", "testmachine", "192.168.0.0/24")
|
|
c.Assert(err, check.NotNil)
|
|
|
|
err = h.EnableNodeRoute("test", "testmachine", "10.0.0.0/24")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
enabledRoutes1, err := h.GetEnabledNodeRoutes("test", "testmachine")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(len(enabledRoutes1), check.Equals, 1)
|
|
|
|
// Adding it twice will just let it pass through
|
|
err = h.EnableNodeRoute("test", "testmachine", "10.0.0.0/24")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
enabledRoutes2, err := h.GetEnabledNodeRoutes("test", "testmachine")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(len(enabledRoutes2), check.Equals, 1)
|
|
|
|
err = h.EnableNodeRoute("test", "testmachine", "150.0.10.0/25")
|
|
c.Assert(err, check.IsNil)
|
|
|
|
enabledRoutes3, err := h.GetEnabledNodeRoutes("test", "testmachine")
|
|
c.Assert(err, check.IsNil)
|
|
c.Assert(len(enabledRoutes3), check.Equals, 2)
|
|
}
|