mirror of
https://github.com/amnezia-vpn/amneziawg-go.git
synced 2025-09-01 23:53:09 +02:00
chore: improve on hyphen parsing
This commit is contained in:
parent
675b7b7531
commit
0ab6960fcc
1 changed files with 12 additions and 8 deletions
|
@ -26,8 +26,8 @@ func NewMagicHeader(min, max uint32) (MagicHeader, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseMagicHeader(key, value string) (MagicHeader, error) {
|
func ParseMagicHeader(key, value string) (MagicHeader, error) {
|
||||||
splitLimits := strings.Split(value, "-")
|
hyphenIdx := strings.Index(value, "-")
|
||||||
if len(splitLimits) != 2 {
|
if hyphenIdx == -1 {
|
||||||
// if there is no hyphen, we treat it as single magic header value
|
// if there is no hyphen, we treat it as single magic header value
|
||||||
magicHeader, err := strconv.ParseUint(value, 10, 32)
|
magicHeader, err := strconv.ParseUint(value, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -35,23 +35,27 @@ func ParseMagicHeader(key, value string) (MagicHeader, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewMagicHeader(uint32(magicHeader), uint32(magicHeader))
|
return NewMagicHeader(uint32(magicHeader), uint32(magicHeader))
|
||||||
} else if len(splitLimits[0]) == 0 || len(splitLimits[1]) == 0 {
|
}
|
||||||
|
|
||||||
|
minStr := value[:hyphenIdx]
|
||||||
|
maxStr := value[hyphenIdx+1:]
|
||||||
|
if len(minStr) == 0 || len(maxStr) == 0 {
|
||||||
return MagicHeader{}, fmt.Errorf("invalid value for key: %s; value: %s; expected format: min-max", key, value)
|
return MagicHeader{}, fmt.Errorf("invalid value for key: %s; value: %s; expected format: min-max", key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
min, err := strconv.ParseUint(splitLimits[0], 10, 32)
|
min, err := strconv.ParseUint(minStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MagicHeader{}, fmt.Errorf("parse min key: %s; value: %s; %w", key, splitLimits[0], err)
|
return MagicHeader{}, fmt.Errorf("parse min key: %s; value: %s; %w", key, minStr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
max, err := strconv.ParseUint(splitLimits[1], 10, 32)
|
max, err := strconv.ParseUint(maxStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MagicHeader{}, fmt.Errorf("parse max key: %s; value: %s; %w", key, splitLimits[1], err)
|
return MagicHeader{}, fmt.Errorf("parse max key: %s; value: %s; %w", key, maxStr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
magicHeader, err := NewMagicHeader(uint32(min), uint32(max))
|
magicHeader, err := NewMagicHeader(uint32(min), uint32(max))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MagicHeader{}, fmt.Errorf("new magicHeader key: %s; value: %s-%s; %w", key, splitLimits[0], splitLimits[1], err)
|
return MagicHeader{}, fmt.Errorf("new magicHeader key: %s; value: %s-%s; %w", key, minStr, maxStr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return magicHeader, nil
|
return magicHeader, nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue