proxy.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "time"
  19. "github.com/libp2p/go-libp2p"
  20. "github.com/libp2p/go-libp2p-core/metrics"
  21. "github.com/mudler/edgevpn/api"
  22. "github.com/mudler/edgevpn/pkg/node"
  23. "github.com/mudler/edgevpn/pkg/services"
  24. "github.com/urfave/cli"
  25. )
  26. func Proxy() cli.Command {
  27. return cli.Command{
  28. Name: "proxy",
  29. Usage: "Starts a local http proxy server to egress nodes",
  30. Description: `Start a proxy locally, providing an ingress point for the network.`,
  31. UsageText: "edgevpn proxy",
  32. Flags: append(CommonFlags,
  33. &cli.StringFlag{
  34. Name: "listen",
  35. Value: ":8080",
  36. Usage: "Listening address",
  37. EnvVar: "PROXYLISTEN",
  38. },
  39. &cli.BoolFlag{
  40. Name: "debug",
  41. },
  42. &cli.IntFlag{
  43. Name: "interval",
  44. Usage: "proxy announce time interval",
  45. EnvVar: "PROXYINTERVAL",
  46. Value: 120,
  47. },
  48. &cli.IntFlag{
  49. Name: "dead-interval",
  50. Usage: "interval (in seconds) wether detect egress nodes offline",
  51. EnvVar: "PROXYDEADINTERVAL",
  52. Value: 600,
  53. },
  54. ),
  55. Action: func(c *cli.Context) error {
  56. o, _, ll := cliToOpts(c)
  57. o = append(o, services.Proxy(
  58. time.Duration(c.Int("interval"))*time.Second,
  59. time.Duration(c.Int("dead-interval"))*time.Second,
  60. c.String("listen"))...)
  61. bwc := metrics.NewBandwidthCounter()
  62. o = append(o, node.WithLibp2pAdditionalOptions(libp2p.BandwidthReporter(bwc)))
  63. e, err := node.New(o...)
  64. if err != nil {
  65. return err
  66. }
  67. displayStart(ll)
  68. ctx := context.Background()
  69. // Start the node to the network, using our ledger
  70. if err := e.Start(ctx); err != nil {
  71. return err
  72. }
  73. return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, bwc, c.Bool("debug"))
  74. },
  75. }
  76. }