main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package cmd
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "os"
  6. "time"
  7. "github.com/mudler/edgevpn/api"
  8. "github.com/mudler/edgevpn/pkg/edgevpn"
  9. "github.com/urfave/cli"
  10. "gopkg.in/yaml.v2"
  11. )
  12. const Copyright string = ` edgevpn Copyright (C) 2021 Ettore Di Giacinto
  13. This program comes with ABSOLUTELY NO WARRANTY.
  14. This is free software, and you are welcome to redistribute it
  15. under certain conditions.`
  16. func MainFlags() []cli.Flag {
  17. return append([]cli.Flag{
  18. &cli.BoolFlag{
  19. Name: "g",
  20. Usage: "Generates a new configuration and prints it on screen",
  21. },
  22. &cli.BoolFlag{
  23. Name: "b",
  24. Usage: "Encodes the new config in base64, so it can be used as a token",
  25. },
  26. &cli.BoolFlag{
  27. Name: "api",
  28. Usage: "Starts also the API daemon locally for inspecting the network status",
  29. },
  30. &cli.StringFlag{
  31. Name: "api-listen",
  32. Value: ":8080",
  33. Usage: "API listening port",
  34. },
  35. &cli.StringFlag{
  36. Name: "address",
  37. Usage: "VPN virtual address",
  38. EnvVar: "ADDRESS",
  39. Value: "10.1.0.1/24",
  40. },
  41. &cli.StringFlag{
  42. Name: "interface",
  43. Usage: "Interface name",
  44. Value: "edgevpn0",
  45. EnvVar: "IFACE",
  46. }}, CommonFlags...)
  47. }
  48. func Main() func(c *cli.Context) error {
  49. return func(c *cli.Context) error {
  50. if c.Bool("g") {
  51. // Generates a new config and exit
  52. newData := edgevpn.GenerateNewConnectionData()
  53. bytesData, err := yaml.Marshal(newData)
  54. if err != nil {
  55. fmt.Println(err)
  56. os.Exit(1)
  57. }
  58. if c.Bool("b") {
  59. fmt.Print(base64.StdEncoding.EncodeToString(bytesData))
  60. } else {
  61. fmt.Println(string(bytesData))
  62. }
  63. os.Exit(0)
  64. }
  65. e := edgevpn.New(cliToOpts(c)...)
  66. displayStart(e)
  67. ledger, err := e.Ledger()
  68. if err != nil {
  69. return err
  70. }
  71. if c.Bool("api") {
  72. go api.API(c.String("api-listen"), 5*time.Second, 20*time.Second, ledger)
  73. }
  74. if err := e.Start(); err != nil {
  75. e.Logger().Fatal(err.Error())
  76. }
  77. return nil
  78. }
  79. }