mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 06:05:06 +03:00
Link refactoring, admin socket changes
This commit is contained in:
parent
c7ee7d9681
commit
7afa23be4c
32 changed files with 1206 additions and 1130 deletions
|
@ -7,10 +7,10 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/hjson/hjson-go"
|
||||
"github.com/hjson/hjson-go/v4"
|
||||
"golang.org/x/text/encoding/unicode"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
)
|
||||
|
||||
type CmdLineEnv struct {
|
||||
|
@ -21,7 +21,7 @@ type CmdLineEnv struct {
|
|||
|
||||
func newCmdLineEnv() CmdLineEnv {
|
||||
var cmdLineEnv CmdLineEnv
|
||||
cmdLineEnv.endpoint = defaults.GetDefaults().DefaultAdminListen
|
||||
cmdLineEnv.endpoint = config.GetDefaults().DefaultAdminListen
|
||||
return cmdLineEnv
|
||||
}
|
||||
|
||||
|
@ -58,31 +58,31 @@ func (cmdLineEnv *CmdLineEnv) parseFlagsAndArgs() {
|
|||
|
||||
func (cmdLineEnv *CmdLineEnv) setEndpoint(logger *log.Logger) {
|
||||
if cmdLineEnv.server == cmdLineEnv.endpoint {
|
||||
if config, err := os.ReadFile(defaults.GetDefaults().DefaultConfigFile); err == nil {
|
||||
if bytes.Equal(config[0:2], []byte{0xFF, 0xFE}) ||
|
||||
bytes.Equal(config[0:2], []byte{0xFE, 0xFF}) {
|
||||
if cfg, err := os.ReadFile(config.GetDefaults().DefaultConfigFile); err == nil {
|
||||
if bytes.Equal(cfg[0:2], []byte{0xFF, 0xFE}) ||
|
||||
bytes.Equal(cfg[0:2], []byte{0xFE, 0xFF}) {
|
||||
utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
|
||||
decoder := utf.NewDecoder()
|
||||
config, err = decoder.Bytes(config)
|
||||
cfg, err = decoder.Bytes(cfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
var dat map[string]interface{}
|
||||
if err := hjson.Unmarshal(config, &dat); err != nil {
|
||||
if err := hjson.Unmarshal(cfg, &dat); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if ep, ok := dat["AdminListen"].(string); ok && (ep != "none" && ep != "") {
|
||||
cmdLineEnv.endpoint = ep
|
||||
logger.Println("Found platform default config file", defaults.GetDefaults().DefaultConfigFile)
|
||||
logger.Println("Found platform default config file", config.GetDefaults().DefaultConfigFile)
|
||||
logger.Println("Using endpoint", cmdLineEnv.endpoint, "from AdminListen")
|
||||
} else {
|
||||
logger.Println("Configuration file doesn't contain appropriate AdminListen option")
|
||||
logger.Println("Falling back to platform default", defaults.GetDefaults().DefaultAdminListen)
|
||||
logger.Println("Falling back to platform default", config.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)
|
||||
logger.Println("Can't open config file from default location", config.GetDefaults().DefaultConfigFile)
|
||||
logger.Println("Falling back to platform default", config.GetDefaults().DefaultAdminListen)
|
||||
}
|
||||
} else {
|
||||
cmdLineEnv.endpoint = cmdLineEnv.server
|
||||
|
|
|
@ -174,17 +174,30 @@ func run() int {
|
|||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
table.SetHeader([]string{"Port", "Public Key", "IP Address", "Uptime", "RX", "TX", "Pr", "URI"})
|
||||
table.SetHeader([]string{"URI", "State", "Dir", "IP Address", "Uptime", "RX", "TX", "Pr", "Last Error"})
|
||||
for _, peer := range resp.Peers {
|
||||
state, lasterr, dir := "Up", "(none)", "Out"
|
||||
if !peer.Up {
|
||||
state, lasterr = "Down", fmt.Sprintf("%s (%s ago)", peer.LastError, peer.LastErrorTime.Round(time.Second))
|
||||
}
|
||||
if peer.Inbound {
|
||||
dir = "In"
|
||||
}
|
||||
uri, err := url.Parse(peer.URI)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
uri.RawQuery = ""
|
||||
table.Append([]string{
|
||||
fmt.Sprintf("%d", peer.Port),
|
||||
peer.PublicKey,
|
||||
uri.String(),
|
||||
state,
|
||||
dir,
|
||||
peer.IPAddress,
|
||||
(time.Duration(peer.Uptime) * time.Second).String(),
|
||||
peer.RXBytes.String(),
|
||||
peer.TXBytes.String(),
|
||||
fmt.Sprintf("%d", peer.Priority),
|
||||
peer.Remote,
|
||||
lasterr,
|
||||
})
|
||||
}
|
||||
table.Render()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue