|
@ -6,28 +6,92 @@ import ( |
|
|
"strings" |
|
|
"strings" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func optionList(options map[string]string, intendNum int) string { |
|
|
type Config struct { |
|
|
var result string |
|
|
Enable bool `yaml:"enable" json:"enable"` |
|
|
intend := strings.Repeat(" ", intendNum) |
|
|
Client []string `yaml:"client" json:"client"` |
|
|
|
|
|
Option map[string]string `yaml:"option" json:"option"` |
|
|
|
|
|
Route struct { |
|
|
|
|
|
Cidr string `yaml:"cidr" json:"cidr"` |
|
|
|
|
|
Option map[string]string `yaml:"option" json:"option"` |
|
|
|
|
|
} `yaml:"route" json:"route"` |
|
|
|
|
|
Prefix struct { |
|
|
|
|
|
Cidr string `yaml:"cidr" json:"cidr"` |
|
|
|
|
|
Option map[string]string `yaml:"option" json:"option"` |
|
|
|
|
|
} `yaml:"prefix" json:"prefix"` |
|
|
|
|
|
DNSSL struct { // DNS Search List
|
|
|
|
|
|
Suffix []string `yaml:"suffix" json:"suffix"` |
|
|
|
|
|
Option map[string]string `yaml:"option" json:"option"` |
|
|
|
|
|
} `yaml:"dnssl" json:"dnssl"` |
|
|
|
|
|
RDNSS struct { // Recursive DNS Server
|
|
|
|
|
|
IP []string `yaml:"ip" json:"ip"` |
|
|
|
|
|
Option map[string]string `yaml:"option" json:"option"` |
|
|
|
|
|
} `yaml:"rdnss" json:"rdnss"` |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func genSpace(num int) string { |
|
|
|
|
|
return strings.Repeat(" ", num) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func loadOption(options map[string]string, intend int) string { // load options into radvd config format
|
|
|
|
|
|
var ret string |
|
|
for option, value := range options { |
|
|
for option, value := range options { |
|
|
result += intend + option + " " + value + ";\n" |
|
|
ret += genSpace(intend) + option + " " + value + ";\n" |
|
|
} |
|
|
} |
|
|
return result |
|
|
return ret |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func loadPrefix(prefix string, options map[string]string) string { |
|
|
func loadClient(clients []string) string { |
|
|
result := " prefix " + prefix + " {\n" |
|
|
if len(clients) == 0 { // without client settings
|
|
|
result += optionList(options, 8) |
|
|
return "" |
|
|
return result + " };\n" |
|
|
} |
|
|
|
|
|
ret := genSpace(4) + "clients {\n" |
|
|
|
|
|
for _, client := range clients { |
|
|
|
|
|
ret += genSpace(8) + client + ";\n" |
|
|
|
|
|
} |
|
|
|
|
|
return ret + genSpace(4) + "};\n" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func loadPrefix(prefix string, option map[string]string) string { // load radvd prefix configure
|
|
|
|
|
|
if prefix == "" { // without prefix settings
|
|
|
|
|
|
return "" |
|
|
|
|
|
} |
|
|
|
|
|
header := genSpace(4) + "prefix " + prefix + " {\n" |
|
|
|
|
|
return header + loadOption(option, 8) + genSpace(4) + "};\n" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Load(options map[string]string, prefixes map[string]map[string]string) { |
|
|
func loadRoute(cidr string, option map[string]string) string { // load radvd route configure
|
|
|
radvdConfig := "interface eth0 {\n" |
|
|
if cidr == "" { // without route settings
|
|
|
radvdConfig += optionList(options, 4) |
|
|
return "" |
|
|
for prefix, prefixOptions := range prefixes { |
|
|
|
|
|
radvdConfig += loadPrefix(prefix, prefixOptions) |
|
|
|
|
|
} |
|
|
} |
|
|
radvdConfig += "};\n" |
|
|
header := genSpace(4) + "route " + cidr + " {\n" |
|
|
log.Debugf("Radvd configure -> \n%s", radvdConfig) |
|
|
return header + loadOption(option, 8) + genSpace(4) + "};\n" |
|
|
common.WriteFile("/etc/radvd.conf", radvdConfig, true) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func loadRdnss(ip []string, option map[string]string) string { |
|
|
|
|
|
if len(ip) == 0 { // without rdnss settings
|
|
|
|
|
|
return "" |
|
|
|
|
|
} |
|
|
|
|
|
header := genSpace(4) + "RDNSS " + strings.Join(ip, " ") + " {\n" |
|
|
|
|
|
return header + loadOption(option, 8) + genSpace(4) + "};\n" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func loadDnssl(suffix []string, option map[string]string) string { |
|
|
|
|
|
if len(suffix) == 0 { // without dnssl settings
|
|
|
|
|
|
return "" |
|
|
|
|
|
} |
|
|
|
|
|
header := genSpace(4) + "DNSSL " + strings.Join(suffix, " ") + " {\n" |
|
|
|
|
|
return header + loadOption(option, 8) + genSpace(4) + "};\n" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Load(Radvd *Config) { |
|
|
|
|
|
config := "interface eth0 {\n" |
|
|
|
|
|
config += loadOption(Radvd.Option, 4) |
|
|
|
|
|
config += loadPrefix(Radvd.Prefix.Cidr, Radvd.Prefix.Option) |
|
|
|
|
|
config += loadRoute(Radvd.Route.Cidr, Radvd.Route.Option) |
|
|
|
|
|
config += loadClient(Radvd.Client) |
|
|
|
|
|
config += loadRdnss(Radvd.RDNSS.IP, Radvd.RDNSS.Option) |
|
|
|
|
|
config += loadDnssl(Radvd.DNSSL.Suffix, Radvd.DNSSL.Option) |
|
|
|
|
|
config += "};\n" |
|
|
|
|
|
log.Debugf("Radvd configure -> \n%s", config) |
|
|
|
|
|
common.WriteFile("/etc/radvd.conf", config, true) |
|
|
} |
|
|
} |
|
|