5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 08:21:36 +00:00

Try to use default config file location to find AdminListen when yggdrasilctl is not given an endpoint

This commit is contained in:
Neil Alexander 2018-12-10 11:09:10 +00:00
parent cd11d2eccd
commit d29b5a074a
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net" "net"
"net/url" "net/url"
@ -14,6 +15,9 @@ import (
"strconv" "strconv"
"strings" "strings"
"golang.org/x/text/encoding/unicode"
"github.com/neilalexander/hjson-go"
"github.com/yggdrasil-network/yggdrasil-go/src/defaults" "github.com/yggdrasil-network/yggdrasil-go/src/defaults"
) )
@ -48,6 +52,37 @@ func main() {
return return
} }
if *server == defaultEndpoint {
if config, err := ioutil.ReadFile(defaults.GetDefaults().DefaultConfigFile); err == nil {
if bytes.Compare(config[0:2], []byte{0xFF, 0xFE}) == 0 ||
bytes.Compare(config[0:2], []byte{0xFE, 0xFF}) == 0 {
utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
decoder := utf.NewDecoder()
config, err = decoder.Bytes(config)
if err != nil {
panic(err)
}
}
var dat map[string]interface{}
if err := hjson.Unmarshal(config, &dat); err != nil {
panic(err)
}
if ep, ok := dat["AdminListen"].(string); ok && (ep != "none" && ep != "") {
defaultEndpoint = ep
logger.Println("Found platform default config file", defaults.GetDefaults().DefaultConfigFile)
logger.Println("Using endpoint", defaultEndpoint, "from AdminListen")
} else {
logger.Println("Configuration file doesn't contain appropriate AdminListen option")
logger.Println("Falling back to platform default", defaults.GetDefaults().DefaultAdminListen)
}
} else {
logger.Println("Can't open config file from default location", defaults.GetDefaults().DefaultConfigFile)
logger.Println("Falling back to platform default", defaults.GetDefaults().DefaultAdminListen)
}
} else {
logger.Println("Using endpoint", *server, "from command line")
}
var conn net.Conn var conn net.Conn
u, err := url.Parse(*server) u, err := url.Parse(*server)
if err == nil { if err == nil {
@ -59,7 +94,7 @@ func main() {
logger.Println("Connecting to TCP socket", u.Host) logger.Println("Connecting to TCP socket", u.Host)
conn, err = net.Dial("tcp", u.Host) conn, err = net.Dial("tcp", u.Host)
default: default:
logger.Println("Unknown protocol", u.Scheme, "- please check your endpoint") logger.Println("Unknown protocol or malformed address - check your endpoint")
err = errors.New("protocol not supported") err = errors.New("protocol not supported")
} }
} else { } else {