mirror of
https://github.com/juanfont/headscale.git
synced 2026-09-01 11:51:32 +09:00
all: adopt strings, errors, and os helpers
This commit is contained in:
@@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -16,19 +17,15 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Error is used to compare errors as per https://dave.cheney.net/2016/04/07/constant-errors
|
var (
|
||||||
type Error string
|
errMockOidcClientIDNotDefined = errors.New("MOCKOIDC_CLIENT_ID not defined")
|
||||||
|
errMockOidcClientSecretNotDefined = errors.New("MOCKOIDC_CLIENT_SECRET not defined")
|
||||||
func (e Error) Error() string { return string(e) }
|
errMockOidcPortNotDefined = errors.New("MOCKOIDC_PORT not defined")
|
||||||
|
errMockOidcUsersNotDefined = errors.New("MOCKOIDC_USERS not defined")
|
||||||
const (
|
|
||||||
errMockOidcClientIDNotDefined = Error("MOCKOIDC_CLIENT_ID not defined")
|
|
||||||
errMockOidcClientSecretNotDefined = Error("MOCKOIDC_CLIENT_SECRET not defined")
|
|
||||||
errMockOidcPortNotDefined = Error("MOCKOIDC_PORT not defined")
|
|
||||||
errMockOidcUsersNotDefined = Error("MOCKOIDC_USERS not defined")
|
|
||||||
refreshTTL = 60 * time.Minute
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const refreshTTL = 60 * time.Minute
|
||||||
|
|
||||||
var accessTTL = 2 * time.Minute
|
var accessTTL = 2 * time.Minute
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -384,13 +384,7 @@ func nodesToPtables(nodes []*v1.Node) (pterm.TableData, error) {
|
|||||||
expired = pterm.LightGreen("no")
|
expired = pterm.LightGreen("no")
|
||||||
}
|
}
|
||||||
|
|
||||||
var tagsBuilder strings.Builder
|
tags := strings.Join(node.GetTags(), "\n")
|
||||||
|
|
||||||
for _, tag := range node.GetTags() {
|
|
||||||
tagsBuilder.WriteString("\n" + tag)
|
|
||||||
}
|
|
||||||
|
|
||||||
tags := strings.TrimLeft(tagsBuilder.String(), "\n")
|
|
||||||
|
|
||||||
var user string
|
var user string
|
||||||
if node.GetUser() != nil {
|
if node.GetUser() != nil {
|
||||||
|
|||||||
+3
-4
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -283,7 +284,7 @@ func checkGitRepository(ctx context.Context) DoctorResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkRequiredFiles verifies required files exist.
|
// checkRequiredFiles verifies required files exist.
|
||||||
func checkRequiredFiles(ctx context.Context) DoctorResult {
|
func checkRequiredFiles(_ context.Context) DoctorResult {
|
||||||
requiredFiles := []string{
|
requiredFiles := []string{
|
||||||
"go.mod",
|
"go.mod",
|
||||||
"integration/",
|
"integration/",
|
||||||
@@ -293,9 +294,7 @@ func checkRequiredFiles(ctx context.Context) DoctorResult {
|
|||||||
var missingFiles []string
|
var missingFiles []string
|
||||||
|
|
||||||
for _, file := range requiredFiles {
|
for _, file := range requiredFiles {
|
||||||
cmd := exec.CommandContext(ctx, "test", "-e", file)
|
_, err := os.Stat(file)
|
||||||
|
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
missingFiles = append(missingFiles, file)
|
missingFiles = append(missingFiles, file)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,19 +22,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func loadDERPMapFromPath(path string) (*tailcfg.DERPMap, error) {
|
func loadDERPMapFromPath(path string) (*tailcfg.DERPMap, error) {
|
||||||
derpFile, err := os.Open(path)
|
b, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer derpFile.Close()
|
|
||||||
|
|
||||||
var derpMap tailcfg.DERPMap
|
var derpMap tailcfg.DERPMap
|
||||||
|
|
||||||
b, err := io.ReadAll(derpFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = yaml.Unmarshal(b, &derpMap)
|
err = yaml.Unmarshal(b, &derpMap)
|
||||||
|
|
||||||
return &derpMap, err
|
return &derpMap, err
|
||||||
|
|||||||
+2
-2
@@ -420,7 +420,7 @@ func (api headscaleV1APIServer) SetApprovedRoutes(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validateTag(tag string) error {
|
func validateTag(tag string) error {
|
||||||
if strings.Index(tag, "tag:") != 0 {
|
if !strings.HasPrefix(tag, "tag:") {
|
||||||
return errors.New("tag must start with the string 'tag:'")
|
return errors.New("tag must start with the string 'tag:'")
|
||||||
}
|
}
|
||||||
if strings.ToLower(tag) != tag {
|
if strings.ToLower(tag) != tag {
|
||||||
@@ -935,7 +935,7 @@ func (api headscaleV1APIServer) AuthReject(
|
|||||||
}
|
}
|
||||||
|
|
||||||
authReq.FinishAuth(types.AuthVerdict{
|
authReq.FinishAuth(types.AuthVerdict{
|
||||||
Err: fmt.Errorf("auth request rejected"),
|
Err: errors.New("auth request rejected"),
|
||||||
})
|
})
|
||||||
|
|
||||||
return &v1.AuthRejectResponse{}, nil
|
return &v1.AuthRejectResponse{}, nil
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -597,14 +596,7 @@ func groupSourcesByUser(
|
|||||||
// filterRuleKey generates a unique key for merging based on [tailcfg.FilterRule.SrcIPs]
|
// filterRuleKey generates a unique key for merging based on [tailcfg.FilterRule.SrcIPs]
|
||||||
// and [tailcfg.FilterRule.IPProto].
|
// and [tailcfg.FilterRule.IPProto].
|
||||||
func filterRuleKey(rule tailcfg.FilterRule) string {
|
func filterRuleKey(rule tailcfg.FilterRule) string {
|
||||||
srcKey := strings.Join(rule.SrcIPs, ",")
|
return fmt.Sprintf("%s|%v", strings.Join(rule.SrcIPs, ","), rule.IPProto)
|
||||||
|
|
||||||
protoStrs := make([]string, len(rule.IPProto))
|
|
||||||
for i, p := range rule.IPProto {
|
|
||||||
protoStrs[i] = strconv.Itoa(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
return srcKey + "|" + strings.Join(protoStrs, ",")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// mergeFilterRules merges rules with identical [tailcfg.FilterRule.SrcIPs] and
|
// mergeFilterRules merges rules with identical [tailcfg.FilterRule.SrcIPs] and
|
||||||
|
|||||||
@@ -162,10 +162,10 @@ func credentialsFromConfig() (string, string, bool) {
|
|||||||
return "", "", false
|
return "", "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.SplitN(string(decoded), ":", 2)
|
user, pass, ok := strings.Cut(string(decoded), ":")
|
||||||
if len(parts) != 2 {
|
if !ok {
|
||||||
return "", "", false
|
return "", "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
return parts[0], parts[1], true
|
return user, pass, true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user