mirror of
https://github.com/luscis/openlan.git
synced 2025-10-02 07:12:08 +08:00
77 lines
1.7 KiB
Go
Executable File
77 lines
1.7 KiB
Go
Executable File
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Subnet struct {
|
|
Network string `json:"network,omitempty"`
|
|
Start string `json:"startAt,omitempty"`
|
|
End string `json:"endAt,omitempty"`
|
|
Netmask string `json:"netmask,omitempty"`
|
|
CIDR string `json:"cidr,omitempty"`
|
|
}
|
|
|
|
type MultiPath struct {
|
|
NextHop string `json:"nexthop"`
|
|
Weight int `json:"weight"`
|
|
}
|
|
|
|
func (mp *MultiPath) CompareEqual(b MultiPath) bool {
|
|
return mp.NextHop == b.NextHop
|
|
}
|
|
|
|
type PrefixRoute struct {
|
|
File string `json:"-"`
|
|
Network string `json:"network,omitempty"`
|
|
Prefix string `json:"prefix"`
|
|
NextHop string `json:"nexthop"`
|
|
MultiPath []MultiPath `json:"multipath,omitempty"`
|
|
Metric int `json:"metric"`
|
|
Mode string `json:"forward,omitempty"` // route or snat
|
|
NextGroup string `json:"nextgroup,omitempty"`
|
|
}
|
|
|
|
func (r *PrefixRoute) String() string {
|
|
elems := []string{}
|
|
if len(r.Prefix) > 0 {
|
|
elems = append(elems, fmt.Sprintf("Prefix: %s", r.Prefix))
|
|
}
|
|
if len(r.NextHop) > 0 {
|
|
elems = append(elems, fmt.Sprintf("Nexthop: %s", r.NextHop))
|
|
}
|
|
if len(r.Mode) > 0 {
|
|
elems = append(elems, fmt.Sprintf("Forward: %s", r.Mode))
|
|
}
|
|
if r.Metric > 0 {
|
|
elems = append(elems, fmt.Sprintf("Metric: %d", r.Metric))
|
|
}
|
|
return fmt.Sprintf("{%s}", strings.Join(elems, " "))
|
|
}
|
|
|
|
func (r *PrefixRoute) CorrectRoute(nexthop string) {
|
|
if r.Metric == 0 {
|
|
r.Metric = 660
|
|
}
|
|
if r.NextHop == "" {
|
|
r.NextHop = nexthop
|
|
}
|
|
if r.Mode == "" {
|
|
r.Mode = "snat"
|
|
}
|
|
|
|
}
|
|
|
|
func CorrectRoutes(routes []PrefixRoute, nexthop string) {
|
|
for i := range routes {
|
|
routes[i].CorrectRoute(nexthop)
|
|
}
|
|
}
|
|
|
|
type HostLease struct {
|
|
Network string `json:"network,omitempty"`
|
|
Hostname string `json:"hostname"`
|
|
Address string `json:"address"`
|
|
}
|