2021-12-07 21:39:19 +01:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2021 Kusakabe Si. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package gencfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/KusakabeSi/EtherGuard-VPN/conn"
|
|
|
|
"github.com/KusakabeSi/EtherGuard-VPN/device"
|
|
|
|
"github.com/KusakabeSi/EtherGuard-VPN/mtypes"
|
|
|
|
"github.com/KusakabeSi/EtherGuard-VPN/tap"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var gencfg_reader *bufio.Reader
|
|
|
|
|
|
|
|
func readFLn(promptF string, checkFn func(string) error, defaultAns func() string, args ...interface{}) string {
|
|
|
|
defaultans := defaultAns()
|
|
|
|
if defaultans != "" {
|
|
|
|
fmt.Printf(promptF+" ("+defaultans+") :", args...)
|
|
|
|
} else {
|
|
|
|
fmt.Printf(promptF+" :", args...)
|
|
|
|
}
|
|
|
|
text, err := gencfg_reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
text = strings.Replace(text, "\n", "", -1)
|
|
|
|
if text == "" {
|
|
|
|
text = defaultans
|
|
|
|
}
|
|
|
|
if err := checkFn(text); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return readFLn(promptF, checkFn, defaultAns, args...)
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseIDs(s string) ([]int, int, int, error) {
|
|
|
|
ret := make([]int, 0)
|
|
|
|
if len(s) <= 3 {
|
|
|
|
return ret, 0, 0, fmt.Errorf("Parse Error: %v", s)
|
|
|
|
}
|
|
|
|
if s[0] != '[' {
|
|
|
|
return ret, 0, 0, fmt.Errorf("Parse Error: %v", s)
|
|
|
|
}
|
|
|
|
if s[len(s)-1] != ']' {
|
|
|
|
return ret, 0, 0, fmt.Errorf("Parse Error: %v", s)
|
|
|
|
}
|
|
|
|
s = s[1 : len(s)-1]
|
|
|
|
as := strings.Split(s, ",")
|
|
|
|
min := math.MaxUint16
|
|
|
|
max := 0
|
|
|
|
for i, es := range as {
|
|
|
|
if strings.Contains(es, "~") {
|
|
|
|
esl := strings.SplitN(es, "~", 2)
|
|
|
|
si, err := strconv.ParseInt(esl[0], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return ret, min, max, err
|
|
|
|
}
|
|
|
|
ei, err := strconv.ParseInt(esl[1], 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return ret, min, max, err
|
|
|
|
}
|
|
|
|
if si >= ei {
|
|
|
|
return ret, min, max, fmt.Errorf("end %v must > start %v", ei, si)
|
|
|
|
}
|
|
|
|
if int(si) < 0 {
|
|
|
|
return ret, min, max, fmt.Errorf("node ID < 0 at element %v", i)
|
|
|
|
}
|
|
|
|
if min > int(si) {
|
|
|
|
min = int(si)
|
|
|
|
}
|
|
|
|
if int(si) < max {
|
|
|
|
return ret, min, max, fmt.Errorf("list out of order at the %vth element: %v", i, es)
|
|
|
|
} else if int(si) == max {
|
|
|
|
return ret, min, max, fmt.Errorf("duplicate id in the %vth element: %v", i, es)
|
|
|
|
}
|
|
|
|
max = int(ei)
|
|
|
|
for ; si <= ei; si++ {
|
|
|
|
ret = append(ret, int(si))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
si, err := strconv.ParseInt(es, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return ret, min, max, err
|
|
|
|
}
|
|
|
|
if int(si) < max {
|
|
|
|
return ret, min, max, fmt.Errorf("List out of order at the %vth element!", i)
|
|
|
|
} else if int(si) == max {
|
|
|
|
return ret, min, max, fmt.Errorf("duplicate id in the %vth element", i)
|
|
|
|
}
|
|
|
|
if min > int(si) {
|
|
|
|
min = int(si)
|
|
|
|
}
|
|
|
|
max = int(si)
|
|
|
|
ret = append(ret, int(si))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, min, max, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printExampleSMCfg() {
|
|
|
|
tconfig := SMCfg{}
|
|
|
|
toprint, _ := yaml.Marshal(tconfig)
|
|
|
|
fmt.Print(string(toprint))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenSuperCfg(SMCinfigPath string, printExample bool) (err error) {
|
|
|
|
SMCfg := SMCfg{}
|
|
|
|
if printExample {
|
|
|
|
printExampleSMCfg()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = mtypes.ReadYaml(SMCinfigPath, &SMCfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:46:15 +01:00
|
|
|
os.Chdir(filepath.Dir(SMCinfigPath))
|
|
|
|
|
|
|
|
err = os.MkdirAll(SMCfg.ConfigOutputDir, 0o700)
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-09 08:46:15 +01:00
|
|
|
files, err := os.ReadDir(SMCfg.ConfigOutputDir)
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(files) > 0 {
|
2021-12-09 08:46:15 +01:00
|
|
|
return fmt.Errorf(SMCfg.ConfigOutputDir + " not empty")
|
2021-12-07 21:39:19 +01:00
|
|
|
}
|
|
|
|
|
2021-12-09 08:46:15 +01:00
|
|
|
if SMCfg.SuperConfigTemplate != "" {
|
2021-12-07 21:39:19 +01:00
|
|
|
var sconfig mtypes.SuperConfig
|
2021-12-09 08:46:15 +01:00
|
|
|
err = mtypes.ReadYaml(SMCfg.SuperConfigTemplate, &sconfig)
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
2021-12-09 08:46:15 +01:00
|
|
|
fmt.Printf("Error read config: %v\t%v\n", SMCfg.SuperConfigTemplate, err)
|
2021-12-07 21:39:19 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:46:15 +01:00
|
|
|
if SMCfg.EdgeConfigTemplate != "" {
|
2021-12-07 21:39:19 +01:00
|
|
|
var econfig mtypes.EdgeConfig
|
2021-12-09 08:46:15 +01:00
|
|
|
err = mtypes.ReadYaml(SMCfg.EdgeConfigTemplate, &econfig)
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
2021-12-09 08:46:15 +01:00
|
|
|
fmt.Printf("Error read config: %v\t%v\n", SMCfg.EdgeConfigTemplate, err)
|
2021-12-07 21:39:19 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 05:20:58 +01:00
|
|
|
sconfig, _ := GetExampleSuperConf(SMCfg.SuperConfigTemplate, false)
|
2021-12-07 21:39:19 +01:00
|
|
|
|
2021-12-09 08:46:15 +01:00
|
|
|
if len(SMCfg.NetworkName) > 10 {
|
2021-12-07 21:39:19 +01:00
|
|
|
return fmt.Errorf("Name too long")
|
|
|
|
}
|
2021-12-15 07:30:12 +01:00
|
|
|
allowed := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"
|
2021-12-09 08:46:15 +01:00
|
|
|
for _, c := range []byte(SMCfg.NetworkName) {
|
2021-12-07 21:39:19 +01:00
|
|
|
if strings.Contains(allowed, string(c)) == false {
|
|
|
|
return fmt.Errorf("Name can only contain %v", allowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ListenPort := fmt.Sprintf("%v", SMCfg.Supernode.ListenPort)
|
|
|
|
|
|
|
|
API_Prefix := SMCfg.Supernode.EdgeAPI_Prefix
|
2021-12-09 08:46:15 +01:00
|
|
|
EndpointV4 := SMCfg.Supernode.EndpointV4
|
|
|
|
if EndpointV4 != "" {
|
2021-12-17 08:02:42 +01:00
|
|
|
_, _, err = conn.LookupIP(EndpointV4+":"+ListenPort, 4, 0)
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-12-09 08:46:15 +01:00
|
|
|
|
|
|
|
EndpointV6 := SMCfg.Supernode.EndpointV6
|
|
|
|
if EndpointV6 != "" {
|
|
|
|
if strings.Contains(EndpointV6, ":") && (EndpointV6[0] != '[' || EndpointV6[len(EndpointV6)-1] != ']') {
|
|
|
|
return fmt.Errorf("Invalid IPv6 format, please use [%v] instead", EndpointV6)
|
2021-12-07 21:39:19 +01:00
|
|
|
}
|
2021-12-17 08:02:42 +01:00
|
|
|
_, _, err = conn.LookupIP(EndpointV6+":"+ListenPort, 6, 0)
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if EndpointV4 == "" {
|
|
|
|
return fmt.Errorf("Muse provide at lease v4 v6 address")
|
|
|
|
}
|
|
|
|
|
|
|
|
EndpointEdgeAPIUrl := SMCfg.Supernode.Endpoint_EdgeAPI
|
|
|
|
|
2021-12-09 08:46:15 +01:00
|
|
|
sconfig.NodeName = SMCfg.NetworkName + "SP"
|
2021-12-07 21:39:19 +01:00
|
|
|
sconfig.API_Prefix = API_Prefix
|
|
|
|
sconfig.ListenPort, _ = strconv.Atoi(ListenPort)
|
|
|
|
sconfig.ListenPort_EdgeAPI = ListenPort
|
|
|
|
sconfig.ListenPort_ManageAPI = ListenPort
|
2021-12-09 08:46:15 +01:00
|
|
|
sconfig.EdgeTemplate = SMCfg.EdgeConfigTemplate
|
2021-12-07 21:39:19 +01:00
|
|
|
|
|
|
|
NodeIDs, _, ModeIDmax, err := ParseIDs(SMCfg.EdgeNode.NodeIDs)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-09 08:46:15 +01:00
|
|
|
MacPrefix := SMCfg.EdgeNode.MacPrefix
|
2021-12-07 21:39:19 +01:00
|
|
|
|
2021-12-09 08:46:15 +01:00
|
|
|
if MacPrefix != "" {
|
|
|
|
_, err = tap.GetMacAddr(MacPrefix, uint32(ModeIDmax))
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pbyte := mtypes.RandomBytes(4, []byte{0xaa, 0xbb, 0xcc, 0xdd})
|
|
|
|
pbyte[0] &^= 0b00000001
|
|
|
|
pbyte[0] |= 0b00000010
|
2021-12-09 21:23:02 +01:00
|
|
|
MacPrefix = fmt.Sprintf("%02X:%02X:%02X:%02X", pbyte[0], pbyte[1], pbyte[2], pbyte[3])
|
2021-12-07 21:39:19 +01:00
|
|
|
}
|
2021-12-09 08:46:15 +01:00
|
|
|
|
|
|
|
IPv4Block := SMCfg.EdgeNode.IPv4Range
|
|
|
|
if IPv4Block != "" {
|
|
|
|
_, _, err = tap.GetIP(4, IPv4Block, uint32(ModeIDmax))
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:46:15 +01:00
|
|
|
IPv6Block := SMCfg.EdgeNode.IPv6Range
|
|
|
|
if IPv6Block != "" {
|
|
|
|
_, _, err = tap.GetIP(6, IPv6Block, uint32(ModeIDmax))
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 08:46:15 +01:00
|
|
|
IPv6LLBlock := SMCfg.EdgeNode.IPv6LLRange
|
|
|
|
if IPv6LLBlock != "" {
|
|
|
|
_, _, err = tap.GetIP(6, IPv6LLBlock, uint32(ModeIDmax))
|
2021-12-07 21:39:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SuperPeerInfo := make([]mtypes.SuperPeerInfo, 0, ModeIDmax)
|
2021-12-09 08:46:15 +01:00
|
|
|
PrivKeyS4, PubKeyS4 := device.RandomKeyPair()
|
|
|
|
PrivKeyS6, PubKeyS6 := device.RandomKeyPair()
|
2021-12-07 21:39:19 +01:00
|
|
|
sconfig.PrivKeyV4 = PrivKeyS4.ToString()
|
|
|
|
sconfig.PrivKeyV6 = PrivKeyS6.ToString()
|
|
|
|
allec := make(map[mtypes.Vertex]mtypes.EdgeConfig)
|
2021-12-13 05:20:58 +01:00
|
|
|
peerceconf, _ := GetExampleEdgeConf(sconfig.EdgeTemplate, false)
|
2021-12-07 21:39:19 +01:00
|
|
|
for _, ii := range NodeIDs {
|
|
|
|
i := mtypes.Vertex(ii)
|
2021-12-09 08:46:15 +01:00
|
|
|
PSKeyE := device.RandomPSK()
|
|
|
|
PrivKeyE, PubKeyE := device.RandomKeyPair()
|
2021-12-07 21:39:19 +01:00
|
|
|
idstr := fmt.Sprintf("%0"+strconv.Itoa(len(strconv.Itoa(ModeIDmax)))+"d", i)
|
|
|
|
|
|
|
|
allec[i] = peerceconf
|
2021-12-11 05:29:19 +01:00
|
|
|
if EndpointV4 != "" {
|
|
|
|
peerceconf.DynamicRoute.SuperNode.EndpointV4 = EndpointV4 + ":" + ListenPort
|
|
|
|
}
|
|
|
|
if EndpointV6 != "" {
|
|
|
|
peerceconf.DynamicRoute.SuperNode.EndpointV6 = EndpointV6 + ":" + ListenPort
|
|
|
|
}
|
2021-12-07 21:39:19 +01:00
|
|
|
peerceconf.DynamicRoute.SuperNode.EndpointEdgeAPIUrl = EndpointEdgeAPIUrl
|
|
|
|
peerceconf.Interface.MacAddrPrefix = MacPrefix
|
|
|
|
peerceconf.Interface.IPv4CIDR = IPv4Block
|
|
|
|
peerceconf.Interface.IPv6CIDR = IPv6Block
|
|
|
|
peerceconf.Interface.IPv6LLPrefix = IPv6LLBlock
|
|
|
|
|
|
|
|
peerceconf.NodeID = i
|
2021-12-09 08:46:15 +01:00
|
|
|
peerceconf.NodeName = SMCfg.NetworkName + idstr
|
|
|
|
peerceconf.Interface.Name = SMCfg.NetworkName + idstr
|
2021-12-07 21:39:19 +01:00
|
|
|
peerceconf.DynamicRoute.SuperNode.PubKeyV4 = PubKeyS4.ToString()
|
|
|
|
peerceconf.DynamicRoute.SuperNode.PubKeyV6 = PubKeyS6.ToString()
|
|
|
|
peerceconf.DynamicRoute.SuperNode.PSKey = PSKeyE.ToString()
|
|
|
|
peerceconf.PrivKey = PrivKeyE.ToString()
|
|
|
|
|
|
|
|
SuperPeerInfo = append(SuperPeerInfo, mtypes.SuperPeerInfo{
|
|
|
|
NodeID: i,
|
2021-12-09 08:46:15 +01:00
|
|
|
Name: SMCfg.NetworkName + idstr,
|
2021-12-07 21:39:19 +01:00
|
|
|
PubKey: PubKeyE.ToString(),
|
|
|
|
PSKey: PSKeyE.ToString(),
|
|
|
|
AdditionalCost: peerceconf.DynamicRoute.AdditionalCost,
|
|
|
|
SkipLocalIP: peerceconf.DynamicRoute.SuperNode.SkipLocalIP,
|
|
|
|
})
|
|
|
|
mtypesBytes, _ := yaml.Marshal(peerceconf)
|
2021-12-09 08:46:15 +01:00
|
|
|
ioutil.WriteFile(filepath.Join(SMCfg.ConfigOutputDir, SMCfg.NetworkName+"_edge"+idstr+".yaml"), mtypesBytes, 0o600)
|
|
|
|
fmt.Println(filepath.Join(SMCfg.ConfigOutputDir, SMCfg.NetworkName+"_edge"+idstr+".yaml"))
|
2021-12-07 21:39:19 +01:00
|
|
|
}
|
|
|
|
sconfig.Peers = SuperPeerInfo
|
|
|
|
mtypesBytes, _ := yaml.Marshal(sconfig)
|
2021-12-09 08:46:15 +01:00
|
|
|
ioutil.WriteFile(filepath.Join(SMCfg.ConfigOutputDir, SMCfg.NetworkName+"_super.yaml"), mtypesBytes, 0o600)
|
|
|
|
fmt.Println(filepath.Join(SMCfg.ConfigOutputDir, SMCfg.NetworkName+"_super.yaml"))
|
2021-12-07 21:39:19 +01:00
|
|
|
return nil
|
|
|
|
}
|