main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package cmd
  16. import (
  17. "context"
  18. "encoding/base64"
  19. "fmt"
  20. "os"
  21. "time"
  22. "github.com/mudler/edgevpn/api"
  23. "github.com/mudler/edgevpn/pkg/edgevpn"
  24. "github.com/urfave/cli"
  25. "gopkg.in/yaml.v2"
  26. )
  27. const Copyright string = ` edgevpn Copyright (C) 2021 Ettore Di Giacinto
  28. This program comes with ABSOLUTELY NO WARRANTY.
  29. This is free software, and you are welcome to redistribute it
  30. under certain conditions.`
  31. func MainFlags() []cli.Flag {
  32. return append([]cli.Flag{
  33. &cli.BoolFlag{
  34. Name: "g",
  35. Usage: "Generates a new configuration and prints it on screen",
  36. },
  37. &cli.BoolFlag{
  38. Name: "b",
  39. Usage: "Encodes the new config in base64, so it can be used as a token",
  40. },
  41. &cli.BoolFlag{
  42. Name: "api",
  43. Usage: "Starts also the API daemon locally for inspecting the network status",
  44. },
  45. &cli.StringFlag{
  46. Name: "api-listen",
  47. Value: ":8080",
  48. Usage: "API listening port",
  49. },
  50. &cli.StringFlag{
  51. Name: "address",
  52. Usage: "VPN virtual address",
  53. EnvVar: "ADDRESS",
  54. Value: "10.1.0.1/24",
  55. },
  56. &cli.StringFlag{
  57. Name: "router",
  58. Usage: "Sends all packets to this node",
  59. EnvVar: "ROUTER",
  60. },
  61. &cli.StringFlag{
  62. Name: "interface",
  63. Usage: "Interface name",
  64. Value: "edgevpn0",
  65. EnvVar: "IFACE",
  66. }}, CommonFlags...)
  67. }
  68. func Main() func(c *cli.Context) error {
  69. return func(c *cli.Context) error {
  70. if c.Bool("g") {
  71. // Generates a new config and exit
  72. newData := edgevpn.GenerateNewConnectionData()
  73. bytesData, err := yaml.Marshal(newData)
  74. if err != nil {
  75. fmt.Println(err)
  76. os.Exit(1)
  77. }
  78. if c.Bool("b") {
  79. fmt.Print(base64.StdEncoding.EncodeToString(bytesData))
  80. } else {
  81. fmt.Println(string(bytesData))
  82. }
  83. os.Exit(0)
  84. }
  85. e := edgevpn.New(cliToOpts(c)...)
  86. displayStart(e)
  87. ledger, err := e.Ledger()
  88. if err != nil {
  89. return err
  90. }
  91. if c.Bool("api") {
  92. go api.API(c.String("api-listen"), 5*time.Second, 20*time.Second, ledger)
  93. }
  94. if err := e.Start(context.Background()); err != nil {
  95. e.Logger().Fatal(err.Error())
  96. }
  97. return nil
  98. }
  99. }