mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-27 12:05:26 +00:00
Merge pull request #643 from iSchluff/fix/dns-name-panic
This commit is contained in:
commit
8dda44105e
14
utils.go
14
utils.go
@ -325,11 +325,17 @@ func GenerateRandomStringURLSafe(n int) (string, error) {
|
|||||||
// number generator fails to function correctly, in which
|
// number generator fails to function correctly, in which
|
||||||
// case the caller should not continue.
|
// case the caller should not continue.
|
||||||
func GenerateRandomStringDNSSafe(n int) (string, error) {
|
func GenerateRandomStringDNSSafe(n int) (string, error) {
|
||||||
str, err := GenerateRandomStringURLSafe(n)
|
var str string
|
||||||
|
var err error
|
||||||
|
for len(str) < n {
|
||||||
|
str, err = GenerateRandomStringURLSafe(n)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
str = strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(str, "_", ""), "-", ""))
|
||||||
|
}
|
||||||
|
|
||||||
str = strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(str, "_", ""), "-", ""))
|
return str[:n], nil
|
||||||
|
|
||||||
return str[:n], err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsStringInSlice(slice []string, str string) bool {
|
func IsStringInSlice(slice []string, str string) bool {
|
||||||
|
@ -34,7 +34,7 @@ func (s *Suite) TestGetUsedIps(c *check.C) {
|
|||||||
MachineKey: "foo",
|
MachineKey: "foo",
|
||||||
NodeKey: "bar",
|
NodeKey: "bar",
|
||||||
DiscoKey: "faa",
|
DiscoKey: "faa",
|
||||||
Hostname: "testmachine",
|
Hostname: "testmachine",
|
||||||
NamespaceID: namespace.ID,
|
NamespaceID: namespace.ID,
|
||||||
RegisterMethod: RegisterMethodAuthKey,
|
RegisterMethod: RegisterMethodAuthKey,
|
||||||
AuthKeyID: uint(pak.ID),
|
AuthKeyID: uint(pak.ID),
|
||||||
@ -82,7 +82,7 @@ func (s *Suite) TestGetMultiIp(c *check.C) {
|
|||||||
MachineKey: "foo",
|
MachineKey: "foo",
|
||||||
NodeKey: "bar",
|
NodeKey: "bar",
|
||||||
DiscoKey: "faa",
|
DiscoKey: "faa",
|
||||||
Hostname: "testmachine",
|
Hostname: "testmachine",
|
||||||
NamespaceID: namespace.ID,
|
NamespaceID: namespace.ID,
|
||||||
RegisterMethod: RegisterMethodAuthKey,
|
RegisterMethod: RegisterMethodAuthKey,
|
||||||
AuthKeyID: uint(pak.ID),
|
AuthKeyID: uint(pak.ID),
|
||||||
@ -172,7 +172,7 @@ func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
|
|||||||
MachineKey: "foo",
|
MachineKey: "foo",
|
||||||
NodeKey: "bar",
|
NodeKey: "bar",
|
||||||
DiscoKey: "faa",
|
DiscoKey: "faa",
|
||||||
Hostname: "testmachine",
|
Hostname: "testmachine",
|
||||||
NamespaceID: namespace.ID,
|
NamespaceID: namespace.ID,
|
||||||
RegisterMethod: RegisterMethodAuthKey,
|
RegisterMethod: RegisterMethodAuthKey,
|
||||||
AuthKeyID: uint(pak.ID),
|
AuthKeyID: uint(pak.ID),
|
||||||
@ -185,3 +185,15 @@ func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
|
|||||||
c.Assert(len(ips2), check.Equals, 1)
|
c.Assert(len(ips2), check.Equals, 1)
|
||||||
c.Assert(ips2[0].String(), check.Equals, expected.String())
|
c.Assert(ips2[0].String(), check.Equals, expected.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Suite) TestGenerateRandomStringDNSSafe(c *check.C) {
|
||||||
|
for i := 0; i < 100000; i++ {
|
||||||
|
str, err := GenerateRandomStringDNSSafe(8)
|
||||||
|
if err != nil {
|
||||||
|
c.Error(err)
|
||||||
|
}
|
||||||
|
if len(str) != 8 {
|
||||||
|
c.Error("invalid length", len(str), str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user