-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 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,69 @@ | ||
package splitrt | ||
|
||
import "strconv" | ||
|
||
var ( | ||
// RoutingTable is the routing table | ||
RoutingTable = "42111" | ||
|
||
// RulePriority1 is the first routing rule priority. It must be unique, | ||
// higher than the local rule, lower than the main and default rules, | ||
// lower than the second routing rule priority | ||
RulePriority1 = "2111" | ||
|
||
// RulePriority2 is the second routing rule priority. It must be unique, | ||
// higher than the local rule, lower than the main and default rules, | ||
// higher than the first routing rule priority | ||
RulePriority2 = "2112" | ||
|
||
// FirewallMark is the firewall mark used for split routing | ||
FirewallMark = RoutingTable | ||
) | ||
|
||
// Config is a split routing configuration | ||
type Config struct { | ||
RoutingTable string | ||
RulePriority1 string | ||
RulePriority2 string | ||
FirewallMark string | ||
} | ||
|
||
// Valid returns whether the split routing configuration is valid | ||
func (c *Config) Valid() bool { | ||
if c == nil || | ||
c.RoutingTable == "" || | ||
c.RulePriority1 == "" || | ||
c.RulePriority2 == "" || | ||
c.FirewallMark == "" { | ||
|
||
return false | ||
} | ||
|
||
// check rule priority values: must be > 0, < 32766, prio1 < prio2 | ||
prio1, err := strconv.ParseUint(c.RulePriority1, 10, 16) | ||
if err != nil { | ||
return false | ||
} | ||
prio2, err := strconv.ParseUint(c.RulePriority2, 10, 16) | ||
if err != nil { | ||
return false | ||
} | ||
if prio1 == 0 || prio2 == 0 || | ||
prio1 >= 32766 || prio2 >= 32766 || | ||
prio1 >= prio2 { | ||
|
||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// NewConfig returns a new split routing configuration | ||
func NewConfig() *Config { | ||
return &Config{ | ||
RoutingTable: RoutingTable, | ||
RulePriority1: RulePriority1, | ||
RulePriority2: RulePriority2, | ||
FirewallMark: FirewallMark, | ||
} | ||
} |
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,69 @@ | ||
package splitrt | ||
|
||
import "testing" | ||
|
||
// TestConfigValid tests Valid of Config | ||
func TestConfigValid(t *testing.T) { | ||
// test invalid | ||
for _, invalid := range []*Config{ | ||
nil, | ||
{}, | ||
{ | ||
RoutingTable: "42111", | ||
FirewallMark: "42111", | ||
RulePriority1: "0", | ||
RulePriority2: "1", | ||
}, | ||
{ | ||
RoutingTable: "42111", | ||
FirewallMark: "42111", | ||
RulePriority1: "32766", | ||
RulePriority2: "32767", | ||
}, | ||
{ | ||
RoutingTable: "42111", | ||
FirewallMark: "42111", | ||
RulePriority1: "2111", | ||
RulePriority2: "2111", | ||
}, | ||
{ | ||
RoutingTable: "42111", | ||
FirewallMark: "42111", | ||
RulePriority1: "2112", | ||
RulePriority2: "2111", | ||
}, | ||
} { | ||
want := false | ||
got := invalid.Valid() | ||
|
||
if got != want { | ||
t.Errorf("got %t, want %t for %v", got, want, invalid) | ||
} | ||
} | ||
|
||
// test valid | ||
for _, valid := range []*Config{ | ||
NewConfig(), | ||
{ | ||
RoutingTable: "42112", | ||
FirewallMark: "42112", | ||
RulePriority1: "2222", | ||
RulePriority2: "2223", | ||
}, | ||
} { | ||
want := true | ||
got := valid.Valid() | ||
|
||
if got != want { | ||
t.Errorf("got %t, want %t for %v", got, want, valid) | ||
} | ||
} | ||
} | ||
|
||
// TestNewConfig tests NewConfig | ||
func TestNewConfig(t *testing.T) { | ||
c := NewConfig() | ||
if !c.Valid() { | ||
t.Errorf("new config should be valid") | ||
} | ||
} |