2
0
Ettore Di Giacinto 3 жил өмнө
parent
commit
c0aea2315f
4 өөрчлөгдсөн 94 нэмэгдсэн , 45 устгасан
  1. 3 1
      README.md
  2. 1 0
      go.mod
  3. 7 0
      go.sum
  4. 83 44
      main.go

+ 3 - 1
README.md

@@ -2,7 +2,9 @@
 
 Fully Decentralized. Immutable. Portable. Easy to use Statically compiled VPN
 
-EdgeVPN uses libp2p to connect and create a blockchain between nodes. It keeps the routing table stored in the ledger, while connections are dynamically established via p2p.
+EdgeVPN uses libp2p to build an immutable trusted blockchain addressable p2p network.
+
+It connect and creates a small blockchain between nodes. It keeps the routing table stored in the ledger, while connections are dynamically established via p2p.
 
 ## Usage
 

+ 1 - 0
go.mod

@@ -17,6 +17,7 @@ require (
 	github.com/pkg/errors v0.9.1
 	github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091
 	github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
+	github.com/urfave/cli v1.22.5 // indirect
 	github.com/vishvananda/netlink v1.1.0
 	github.com/xlzd/gotp v0.0.0-20181030022105-c8557ba2c119
 	go.opencensus.io v0.23.0 // indirect

+ 7 - 0
go.sum

@@ -123,7 +123,9 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
 github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
 github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
 github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
+github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
 github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
+github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
 github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
 github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
 github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
@@ -902,7 +904,9 @@ github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqn
 github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
 github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
+github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
 github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
+github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
 github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
@@ -928,6 +932,7 @@ github.com/shurcooL/notifications v0.0.0-20181007000457-627ab5aea122/go.mod h1:b
 github.com/shurcooL/octicon v0.0.0-20181028054416-fa4f57f9efb2/go.mod h1:eWdoE5JD4R5UVWDucdOPg1g2fqQRq78IQa9zlOV1vpQ=
 github.com/shurcooL/reactions v0.0.0-20181006231557-f2e0b4ca5b82/go.mod h1:TCR1lToEk4d2s07G3XGfz2QrgHXg4RJBvjrOozvoWfk=
 github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
+github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
 github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4=
 github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
@@ -979,6 +984,8 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1
 github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
 github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
 github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
+github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
+github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
 github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
 github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
 github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=

+ 83 - 44
main.go

@@ -22,6 +22,7 @@ import (
 
 	"github.com/ipfs/go-log/v2"
 	"github.com/songgao/water"
+	"github.com/urfave/cli"
 
 	internal "github.com/mudler/edgevpn/internal"
 	"go.uber.org/zap"
@@ -30,68 +31,109 @@ import (
 	edgevpn "github.com/mudler/edgevpn/pkg/edgevpn"
 )
 
-func main() {
-	help := flag.Bool("h", false, "Display Help")
-	genKeys := flag.Bool("g", false, "Generate pub/priv keys")
+const copyRight string = `	edgevpn  Copyright (C) 2021 Ettore Di Giacinto
+This program comes with ABSOLUTELY NO WARRANTY.
+This is free software, and you are welcome to redistribute it
+under certain conditions.`
 
+func main() {
 	l, _ := zap.NewProduction()
 	defer l.Sync() // flushes buffer, if any
 
+	app := &cli.App{
+		Name:        "edgevpn",
+		Version:     internal.Version,
+		Author:      "Ettore Di Giacinto",
+		Usage:       "edgevpn --config /etc/edgevpn/config.yaml",
+		Description: "edgevpn uses libp2p to build an immutable trusted blockchain addressable p2p network",
+		Copyright:   copyRight,
+		Flags: []cli.Flag{
+			&cli.BoolFlag{
+				Name:  "g",
+				Usage: "Generates a new configuration and prints it on screen",
+			},
+			&cli.StringFlag{
+				Name:   "config",
+				Usage:  "Specify a path to a edgevpn config file",
+				EnvVar: "EDGEVPNCONFIG",
+			},
+			&cli.StringFlag{
+				Name:   "token",
+				Usage:  "Specify an edgevpn token in place of a config file",
+				EnvVar: "EDGEVPNTOKEN",
+			},
+			&cli.StringFlag{
+				Name:   "address",
+				Usage:  "VPN virtual address",
+				EnvVar: "ADDRESS",
+				Value:  "10.1.0.1/24",
+			},
+			&cli.StringFlag{
+				Name:   "interface",
+				Usage:  "Interface name",
+				Value:  "edgevpn0",
+				EnvVar: "IFACE",
+			},
+		},
+
+		Action: func(c *cli.Context) error {
+			if c.Bool("g") {
+				// Generates a new config and exit
+				newData, err := edgevpn.GenerateNewConnectionData()
+				if err != nil {
+					fmt.Println(err)
+					os.Exit(1)
+				}
+
+				bytesData, err := yaml.Marshal(newData)
+				if err != nil {
+					fmt.Println(err)
+					os.Exit(1)
+				}
+
+				fmt.Println(string(bytesData))
+				os.Exit(0)
+			}
+
+			start(l, c)
+			return nil
+		},
+	}
+
+	err := app.Run(os.Args)
+	if err != nil {
+		l.Sugar().Fatal(err)
+	}
+}
+func start(l *zap.Logger, c *cli.Context) {
+	config := c.String("config")
+	address := c.String("address")
+	iface := c.String("interface")
+	token := c.String("token")
+
 	opts := []edgevpn.Option{
 		edgevpn.Logger(l),
 		edgevpn.LogLevel(log.LevelInfo),
 		edgevpn.MaxMessageSize(2 << 20), // 2MB
 		edgevpn.WithInterfaceMTU(1450),
 		edgevpn.WithPacketMTU(1420),
-		edgevpn.WithInterfaceAddress(os.Getenv("ADDRESS")),
-		edgevpn.WithInterfaceName(os.Getenv("IFACE")),
+		edgevpn.WithInterfaceAddress(address),
+		edgevpn.WithInterfaceName(iface),
 		edgevpn.WithMaxBlockChainSize(1000),
 		edgevpn.WithInterfaceType(water.TUN),
 		edgevpn.NetLinkBootstrap(true),
-		edgevpn.FromBase64(os.Getenv("EDGEVPNTOKEN")),
+		edgevpn.FromBase64(token),
+		edgevpn.FromYaml(config),
 	}
 
-	opts = append(opts, edgevpn.FromYaml(os.Getenv("EDGEVPNCONFIG")))
-
 	flag.Parse()
 
 	e := edgevpn.New(opts...)
 
-	if *help {
-		fmt.Println("edgevpn uses libp2p to build an immutable trusted p2p network")
-		fmt.Println("")
-		fmt.Println()
-		fmt.Println("Usage: Run './edgevpn in two different terminals. Let them connect to the bootstrap nodes, announce themselves and connect to the peers")
-		flag.PrintDefaults()
-		return
-	}
-
-	if *genKeys {
-		newData, err := edgevpn.GenerateNewConnectionData()
-		if err != nil {
-			fmt.Println(err)
-			os.Exit(1)
-		}
-
-		bytesData, err := yaml.Marshal(newData)
-		if err != nil {
-			fmt.Println(err)
-			os.Exit(1)
-		}
-
-		fmt.Println(string(bytesData))
-		os.Exit(0)
-	}
-
-	l.Sugar().Info(`
-	edgevpn  Copyright (C) 2021 Ettore Di Giacinto
-	This program comes with ABSOLUTELY NO WARRANTY.
-	This is free software, and you are welcome to redistribute it
-	under certain conditions.
-	`)
+	l.Sugar().Info(copyRight)
 
 	l.Sugar().Infof("Version: %s commit: %s", internal.Version, internal.Commit)
-	if os.Getenv("EDGEVPNCONFIG") == "" && os.Getenv("EDGEVPNTOKEN") == "" {
+	if config == "" && token == "" {
 		l.Sugar().Fatal("EDGEVPNCONFIG or EDGEVPNTOKEN not supplied. config file is required")
 	}
 	l.Sugar().Info("Start")
@@ -99,7 +141,4 @@ func main() {
 	if err := e.Start(); err != nil {
 		l.Sugar().Fatal(err.Error())
 	}
-
-	for {
-	}
 }