mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-07 08:44:07 +01:00
Prepare regexps on compile time (#1327)
This commit is contained in:
parent
0ca06b566a
commit
63d211c698
@ -7,12 +7,12 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"regexp"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
"github.com/hashicorp/go-version"
|
"github.com/hashicorp/go-version"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
|
nbversion "github.com/netbirdio/netbird/version"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -289,12 +289,7 @@ func isNetworkManagerSupportedVersion() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseVersion(inputVersion string) (*version.Version, error) {
|
func parseVersion(inputVersion string) (*version.Version, error) {
|
||||||
reg, err := regexp.Compile(version.SemverRegexpRaw)
|
if inputVersion == "" || !nbversion.SemverRegexp.MatchString(inputVersion) {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if inputVersion == "" || !reg.MatchString(inputVersion) {
|
|
||||||
return nil, fmt.Errorf("couldn't parse the provided version: Not SemVer")
|
return nil, fmt.Errorf("couldn't parse the provided version: Not SemVer")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,6 +86,8 @@ func (s SimpleRecord) Len() uint16 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var invalidHostMatcher = regexp.MustCompile(invalidHostLabel)
|
||||||
|
|
||||||
// GetParsedDomainLabel returns a domain label with max 59 characters,
|
// GetParsedDomainLabel returns a domain label with max 59 characters,
|
||||||
// parsed for old Hosts.txt requirements, and converted to ASCII and lowercase
|
// parsed for old Hosts.txt requirements, and converted to ASCII and lowercase
|
||||||
func GetParsedDomainLabel(name string) (string, error) {
|
func GetParsedDomainLabel(name string) (string, error) {
|
||||||
@ -99,8 +101,6 @@ func GetParsedDomainLabel(name string) (string, error) {
|
|||||||
return "", fmt.Errorf("unable to convert host label to ASCII, error: %v", err)
|
return "", fmt.Errorf("unable to convert host label to ASCII, error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
invalidHostMatcher := regexp.MustCompile(invalidHostLabel)
|
|
||||||
|
|
||||||
validHost := strings.ToLower(invalidHostMatcher.ReplaceAllString(ascii, "-"))
|
validHost := strings.ToLower(invalidHostMatcher.ReplaceAllString(ascii, "-"))
|
||||||
if len(validHost) > 58 {
|
if len(validHost) > 58 {
|
||||||
validHost = validHost[:59]
|
validHost = validHost[:59]
|
||||||
|
@ -1596,9 +1596,10 @@ func (am *DefaultAccountManager) GetAllConnectedPeers() (map[string]struct{}, er
|
|||||||
return am.peersUpdateManager.GetAllConnectedPeers(), nil
|
return am.peersUpdateManager.GetAllConnectedPeers(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var invalidDomainRegexp = regexp.MustCompile(`^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$`)
|
||||||
|
|
||||||
func isDomainValid(domain string) bool {
|
func isDomainValid(domain string) bool {
|
||||||
re := regexp.MustCompile(`^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$`)
|
return invalidDomainRegexp.MatchString(domain)
|
||||||
return re.MatchString(domain)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDNSDomain returns the configured dnsDomain
|
// GetDNSDomain returns the configured dnsDomain
|
||||||
|
@ -33,6 +33,8 @@ func NewAccessControl(audience, userIDClaim string, getUser GetUser) *AccessCont
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tokenPathRegexp = regexp.MustCompile(`^.*/api/users/.*/tokens.*$`)
|
||||||
|
|
||||||
// Handler method of the middleware which forbids all modify requests for non admin users
|
// Handler method of the middleware which forbids all modify requests for non admin users
|
||||||
// It also adds
|
// It also adds
|
||||||
func (a *AccessControl) Handler(h http.Handler) http.Handler {
|
func (a *AccessControl) Handler(h http.Handler) http.Handler {
|
||||||
@ -55,13 +57,7 @@ func (a *AccessControl) Handler(h http.Handler) http.Handler {
|
|||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodDelete, http.MethodPost, http.MethodPatch, http.MethodPut:
|
case http.MethodDelete, http.MethodPost, http.MethodPatch, http.MethodPut:
|
||||||
|
|
||||||
ok, err := regexp.MatchString(`^.*/api/users/.*/tokens.*$`, r.URL.Path)
|
if tokenPathRegexp.MatchString(r.URL.Path) {
|
||||||
if err != nil {
|
|
||||||
log.Debugf("regex failed")
|
|
||||||
util.WriteError(status.Errorf(status.Internal, ""), w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if ok {
|
|
||||||
log.Debugf("valid Path")
|
log.Debugf("valid Path")
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -381,15 +380,10 @@ func createPostRequest(ctx context.Context, endpoint string, payloadStr string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getMinMaxVersion(inputList []string) (string, string) {
|
func getMinMaxVersion(inputList []string) (string, string) {
|
||||||
reg, err := regexp.Compile(version.SemverRegexpRaw)
|
|
||||||
if err != nil {
|
|
||||||
return "", ""
|
|
||||||
}
|
|
||||||
|
|
||||||
versions := make([]*version.Version, 0)
|
versions := make([]*version.Version, 0)
|
||||||
|
|
||||||
for _, raw := range inputList {
|
for _, raw := range inputList {
|
||||||
if raw != "" && reg.MatchString(raw) {
|
if raw != "" && nbversion.SemverRegexp.MatchString(raw) {
|
||||||
v, err := version.NewVersion(raw)
|
v, err := version.NewVersion(raw)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
versions = append(versions, v)
|
versions = append(versions, v)
|
||||||
|
@ -267,8 +267,9 @@ func validateGroups(list []string, groups map[string]*Group) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var domainMatcher = regexp.MustCompile(domainPattern)
|
||||||
|
|
||||||
func validateDomain(domain string) error {
|
func validateDomain(domain string) error {
|
||||||
domainMatcher := regexp.MustCompile(domainPattern)
|
|
||||||
if !domainMatcher.MatchString(domain) {
|
if !domainMatcher.MatchString(domain) {
|
||||||
return errors.New("domain should consists of only letters, numbers, and hyphens with no leading, trailing hyphens, or spaces")
|
return errors.New("domain should consists of only letters, numbers, and hyphens with no leading, trailing hyphens, or spaces")
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,19 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
v "github.com/hashicorp/go-version"
|
||||||
|
)
|
||||||
|
|
||||||
// will be replaced with the release version when using goreleaser
|
// will be replaced with the release version when using goreleaser
|
||||||
var version = "development"
|
var version = "development"
|
||||||
|
|
||||||
|
var (
|
||||||
|
VersionRegexp = regexp.MustCompile("^" + v.VersionRegexpRaw + "$")
|
||||||
|
SemverRegexp = regexp.MustCompile("^" + v.SemverRegexpRaw + "$")
|
||||||
|
)
|
||||||
|
|
||||||
// NetbirdVersion returns the Netbird version
|
// NetbirdVersion returns the Netbird version
|
||||||
func NetbirdVersion() string {
|
func NetbirdVersion() string {
|
||||||
return version
|
return version
|
||||||
|
Loading…
Reference in New Issue
Block a user