Browse Source

Allow to connect also with token

Ettore Di Giacinto 4 years ago
parent
commit
65c53b6f6f
3 changed files with 33 additions and 3 deletions
  1. 8 1
      README.md
  2. 4 2
      main.go
  3. 21 0
      pkg/edgevpn/options.go

+ 8 - 1
README.md

@@ -9,7 +9,7 @@ EdgeVPN uses libp2p to connect and create a blockchain between nodes. It keeps t
 Generate a config, and send it over all the nodes you wish to connect:
 Generate a config, and send it over all the nodes you wish to connect:
 
 
 ```bash
 ```bash
-./edgevpn -g > config.yaml
+edgevpn -g > config.yaml
 ```
 ```
 
 
 Run edgevpn on multiple hosts:
 Run edgevpn on multiple hosts:
@@ -26,6 +26,13 @@ EDGEVPNCONFIG=config.yaml IFACE=edgevpn0 ADDRESS=10.1.0.13/24 ./edgevpn
 
 
 ... and that's it! the `ADDRESS` is a _virtual_ unique IP for each node, and it is actually the ip where the node will be reachable to from the vpn, while `IFACE` is the interface name.
 ... and that's it! the `ADDRESS` is a _virtual_ unique IP for each node, and it is actually the ip where the node will be reachable to from the vpn, while `IFACE` is the interface name.
 
 
+You can also encode the config in base64, and pass it to edgevpn with `EDGEVPNTOKEN` instead:
+
+```bash
+EDGEVPNTOKEN=$(edgevpn -g | base64 -w0)
+IFACE=edgevpn0 ADDRESS=10.1.0.13/24 ./edgevpn
+```
+
 *Note*: It might take up time to build the connection between nodes. Wait at least 5 mins, it depends on the network behind the hosts.
 *Note*: It might take up time to build the connection between nodes. Wait at least 5 mins, it depends on the network behind the hosts.
 
 
 ## Architecture
 ## Architecture

+ 4 - 2
main.go

@@ -48,9 +48,11 @@ func main() {
 		edgevpn.WithMaxBlockChainSize(1000),
 		edgevpn.WithMaxBlockChainSize(1000),
 		edgevpn.WithInterfaceType(water.TUN),
 		edgevpn.WithInterfaceType(water.TUN),
 		edgevpn.NetLinkBootstrap(true),
 		edgevpn.NetLinkBootstrap(true),
+		edgevpn.FromBase64(os.Getenv("EDGEVPNTOKEN")),
 	}
 	}
 
 
 	opts = append(opts, edgevpn.FromYaml(os.Getenv("EDGEVPNCONFIG")))
 	opts = append(opts, edgevpn.FromYaml(os.Getenv("EDGEVPNCONFIG")))
+
 	flag.Parse()
 	flag.Parse()
 
 
 	e := edgevpn.New(opts...)
 	e := edgevpn.New(opts...)
@@ -89,8 +91,8 @@ func main() {
 	`)
 	`)
 
 
 	l.Sugar().Infof("Version: %s commit: %s", internal.Version, internal.Commit)
 	l.Sugar().Infof("Version: %s commit: %s", internal.Version, internal.Commit)
-	if os.Getenv("EDGEVPNCONFIG") == "" {
-		l.Sugar().Fatal("EDGEVPNCONFIG not supplied. config file is required")
+	if os.Getenv("EDGEVPNCONFIG") == "" && os.Getenv("EDGEVPNTOKEN") == "" {
+		l.Sugar().Fatal("EDGEVPNCONFIG or EDGEVPNTOKEN not supplied. config file is required")
 	}
 	}
 	l.Sugar().Info("Start")
 	l.Sugar().Info("Start")
 
 

+ 21 - 0
pkg/edgevpn/options.go

@@ -1,6 +1,7 @@
 package edgevpn
 package edgevpn
 
 
 import (
 import (
+	"encoding/base64"
 	"io/ioutil"
 	"io/ioutil"
 
 
 	"github.com/ipfs/go-log/v2"
 	"github.com/ipfs/go-log/v2"
@@ -244,6 +245,26 @@ func FromYaml(path string) func(cfg *Config) error {
 		if err := yaml.Unmarshal(data, &t); err != nil {
 		if err := yaml.Unmarshal(data, &t); err != nil {
 			return errors.Wrap(err, "parsing yaml")
 			return errors.Wrap(err, "parsing yaml")
 		}
 		}
+
+		t.copy(cfg)
+		return nil
+	}
+}
+
+func FromBase64(bb string) func(cfg *Config) error {
+	return func(cfg *Config) error {
+		if len(bb) == 0 {
+			return nil
+		}
+		configDec, err := base64.StdEncoding.DecodeString(bb)
+		if err != nil {
+			return err
+		}
+		t := YAMLConnectionConfig{}
+
+		if err := yaml.Unmarshal(configDec, &t); err != nil {
+			return errors.Wrap(err, "parsing yaml")
+		}
 		t.copy(cfg)
 		t.copy(cfg)
 		return nil
 		return nil
 	}
 	}