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

Add support for UNIX domain admin sockets to yggdrasilctl

This commit is contained in:
Neil Alexander 2018-07-07 11:25:01 +01:00
parent cd6030ec8f
commit 047b7d95a1
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -1,9 +1,11 @@
package main package main
import "errors"
import "flag" import "flag"
import "fmt" import "fmt"
import "strings" import "strings"
import "net" import "net"
import "net/url"
import "sort" import "sort"
import "encoding/json" import "encoding/json"
import "strconv" import "strconv"
@ -12,20 +14,34 @@ import "os"
type admin_info map[string]interface{} type admin_info map[string]interface{}
func main() { func main() {
server := flag.String("endpoint", "localhost:9001", "Admin socket endpoint") server := flag.String("endpoint", "tcp://localhost:9001", "Admin socket endpoint")
injson := flag.Bool("json", false, "Output in JSON format") injson := flag.Bool("json", false, "Output in JSON format")
flag.Parse() flag.Parse()
args := flag.Args() args := flag.Args()
if len(args) == 0 { if len(args) == 0 {
fmt.Println("usage:", os.Args[0], "[-endpoint=localhost:9001] [-json] command [key=value] [...]") fmt.Println("usage:", os.Args[0], "[-endpoint=proto://server] [-json] command [key=value] [...]")
fmt.Println("example:", os.Args[0], "getPeers") fmt.Println("example:", os.Args[0], "getPeers")
fmt.Println("example:", os.Args[0], "setTunTap name=auto mtu=1500 tap_mode=false") fmt.Println("example:", os.Args[0], "setTunTap name=auto mtu=1500 tap_mode=false")
fmt.Println("example:", os.Args[0], "-endpoint=localhost:9001 getDHT") fmt.Println("example:", os.Args[0], "-endpoint=tcp://localhost:9001 getDHT")
fmt.Println("example:", os.Args[0], "-endpoint=unix:///var/run/ygg.sock getDHT")
return return
} }
conn, err := net.Dial("tcp", *server) var conn net.Conn
u, err := url.Parse(*server)
if err == nil {
switch strings.ToLower(u.Scheme) {
case "unix":
conn, err = net.Dial("unix", (*server)[7:])
case "tcp":
conn, err = net.Dial("tcp", u.Host)
default:
err = errors.New("protocol not supported")
}
} else {
conn, err = net.Dial("tcp", *server)
}
if err != nil { if err != nil {
panic(err) panic(err)
} }