-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MINOR: Add support for set-fc-mark, set-fc-tos
And also set-bc-mark and set-bc-tos. Those actions reaplce set-mark and set-tos, which are now deprecated in HAProxy.
- Loading branch information
Showing
19 changed files
with
658 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2024 HAProxy Technologies | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
//nolint:dupl | ||
package actions | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strings" | ||
|
||
"github.com/haproxytech/config-parser/v5/common" | ||
"github.com/haproxytech/config-parser/v5/types" | ||
) | ||
|
||
type SetBcMark struct { | ||
Expr common.Expression | ||
Cond string | ||
CondTest string | ||
Comment string | ||
} | ||
|
||
func (f *SetBcMark) Parse(parts []string, parserType types.ParserType, comment string) error { | ||
if comment != "" { | ||
f.Comment = comment | ||
} | ||
if len(parts) < 3 { | ||
return fmt.Errorf("not enough params") | ||
} | ||
var command []string | ||
switch parserType { | ||
case types.HTTP: | ||
command = parts[2:] | ||
case types.TCP: | ||
command = parts[3:] | ||
} | ||
command, condition := common.SplitRequest(command) | ||
|
||
if len(command) > 0 { | ||
if len(command) == 1 && !validateUnsignedNumber(command[0], math.MaxUint32) { | ||
return fmt.Errorf("number '%s' is not a valid mark value", command[0]) | ||
} | ||
expr := common.Expression{} | ||
err := expr.Parse(command) | ||
if err != nil { | ||
return fmt.Errorf("not enough params") | ||
} | ||
f.Expr = expr | ||
} | ||
if len(condition) > 1 { | ||
f.Cond = condition[0] | ||
f.CondTest = strings.Join(condition[1:], " ") | ||
} | ||
return nil | ||
} | ||
|
||
func (f *SetBcMark) String() string { | ||
return common.SmartJoin("set-bc-mark", f.Expr.String(), f.Cond, f.CondTest) | ||
} | ||
|
||
func (f *SetBcMark) GetComment() string { | ||
return f.Comment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2024 HAProxy Technologies | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
//nolint:dupl | ||
package actions | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strings" | ||
|
||
"github.com/haproxytech/config-parser/v5/common" | ||
"github.com/haproxytech/config-parser/v5/types" | ||
) | ||
|
||
type SetBcTos struct { | ||
Expr common.Expression | ||
Cond string | ||
CondTest string | ||
Comment string | ||
} | ||
|
||
func (f *SetBcTos) Parse(parts []string, parserType types.ParserType, comment string) error { | ||
if comment != "" { | ||
f.Comment = comment | ||
} | ||
if len(parts) < 3 { | ||
return fmt.Errorf("not enough params") | ||
} | ||
var command []string | ||
switch parserType { | ||
case types.HTTP: | ||
command = parts[2:] | ||
case types.TCP: | ||
command = parts[3:] | ||
} | ||
command, condition := common.SplitRequest(command) | ||
|
||
if len(command) > 0 { | ||
if len(command) == 1 && !validateUnsignedNumber(command[0], math.MaxUint8) { | ||
return fmt.Errorf("number '%s' is not a valid TOS value", command[0]) | ||
} | ||
expr := common.Expression{} | ||
err := expr.Parse(command) | ||
if err != nil { | ||
return fmt.Errorf("not enough params") | ||
} | ||
f.Expr = expr | ||
} | ||
if len(condition) > 1 { | ||
f.Cond = condition[0] | ||
f.CondTest = strings.Join(condition[1:], " ") | ||
} | ||
return nil | ||
} | ||
|
||
func (f *SetBcTos) String() string { | ||
return common.SmartJoin("set-bc-tos", f.Expr.String(), f.Cond, f.CondTest) | ||
} | ||
|
||
func (f *SetBcTos) GetComment() string { | ||
return f.Comment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2024 HAProxy Technologies | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
//nolint:dupl | ||
package actions | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strings" | ||
|
||
"github.com/haproxytech/config-parser/v5/common" | ||
"github.com/haproxytech/config-parser/v5/types" | ||
) | ||
|
||
type SetFcMark struct { | ||
Expr common.Expression | ||
Cond string | ||
CondTest string | ||
Comment string | ||
} | ||
|
||
func (f *SetFcMark) Parse(parts []string, parserType types.ParserType, comment string) error { | ||
if comment != "" { | ||
f.Comment = comment | ||
} | ||
if len(parts) < 3 { | ||
return fmt.Errorf("not enough params") | ||
} | ||
var command []string | ||
switch parserType { | ||
case types.HTTP: | ||
command = parts[2:] | ||
case types.TCP: | ||
command = parts[3:] | ||
} | ||
command, condition := common.SplitRequest(command) | ||
|
||
if len(command) > 0 { | ||
if len(command) == 1 && !validateUnsignedNumber(command[0], math.MaxUint32) { | ||
return fmt.Errorf("number '%s' is not a valid mark value", command[0]) | ||
} | ||
expr := common.Expression{} | ||
err := expr.Parse(command) | ||
if err != nil { | ||
return fmt.Errorf("not enough params") | ||
} | ||
f.Expr = expr | ||
} | ||
if len(condition) > 1 { | ||
f.Cond = condition[0] | ||
f.CondTest = strings.Join(condition[1:], " ") | ||
} | ||
return nil | ||
} | ||
|
||
func (f *SetFcMark) String() string { | ||
return common.SmartJoin("set-fc-mark", f.Expr.String(), f.Cond, f.CondTest) | ||
} | ||
|
||
func (f *SetFcMark) GetComment() string { | ||
return f.Comment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright 2024 HAProxy Technologies | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package actions | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/haproxytech/config-parser/v5/common" | ||
"github.com/haproxytech/config-parser/v5/types" | ||
) | ||
|
||
type SetFcTos struct { | ||
Expr common.Expression | ||
Cond string | ||
CondTest string | ||
Comment string | ||
} | ||
|
||
func (f *SetFcTos) Parse(parts []string, parserType types.ParserType, comment string) error { | ||
if comment != "" { | ||
f.Comment = comment | ||
} | ||
if len(parts) < 3 { | ||
return fmt.Errorf("not enough params") | ||
} | ||
var command []string | ||
switch parserType { | ||
case types.HTTP: | ||
command = parts[2:] | ||
case types.TCP: | ||
command = parts[3:] | ||
} | ||
command, condition := common.SplitRequest(command) | ||
|
||
if len(command) > 0 { | ||
if len(command) == 1 && !validateUnsignedNumber(command[0], math.MaxUint8) { | ||
return fmt.Errorf("number '%s' is not a valid TOS value", command[0]) | ||
} | ||
expr := common.Expression{} | ||
err := expr.Parse(command) | ||
if err != nil { | ||
return fmt.Errorf("not enough params") | ||
} | ||
f.Expr = expr | ||
} | ||
if len(condition) > 1 { | ||
f.Cond = condition[0] | ||
f.CondTest = strings.Join(condition[1:], " ") | ||
} | ||
return nil | ||
} | ||
|
||
func (f *SetFcTos) String() string { | ||
return common.SmartJoin("set-fc-tos", f.Expr.String(), f.Cond, f.CondTest) | ||
} | ||
|
||
func (f *SetFcTos) GetComment() string { | ||
return f.Comment | ||
} | ||
|
||
// Test if the given string is an unsigned integer between zero and "max". | ||
// The number can be in decimal or hexadecimal (0x). | ||
// If the parsing failed, assume the string was an Expr and return true. | ||
func validateUnsignedNumber(text string, max int64) bool { | ||
var n int64 | ||
var err error | ||
if strings.HasPrefix(text, "0x") { | ||
n, err = strconv.ParseInt(text, 16, 64) | ||
} else { | ||
n, err = strconv.ParseInt(text, 10, 64) | ||
} | ||
if err != nil { | ||
// Assume it was an expression, not a number. | ||
return true | ||
} | ||
return n >= 0 && n <= max | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.