proxy.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "context"
  16. "time"
  17. "github.com/libp2p/go-libp2p"
  18. "github.com/libp2p/go-libp2p/core/metrics"
  19. "github.com/mudler/edgevpn/api"
  20. "github.com/mudler/edgevpn/pkg/node"
  21. "github.com/mudler/edgevpn/pkg/services"
  22. "github.com/urfave/cli/v2"
  23. )
  24. func Proxy() *cli.Command {
  25. return &cli.Command{
  26. Name: "proxy",
  27. Usage: "Starts a local http proxy server to egress nodes",
  28. Description: `Start a proxy locally, providing an ingress point for the network.`,
  29. UsageText: "edgevpn proxy",
  30. Flags: append(CommonFlags,
  31. &cli.StringFlag{
  32. Name: "listen",
  33. Value: ":8080",
  34. Usage: "Listening address",
  35. EnvVars: []string{"PROXYLISTEN"},
  36. },
  37. &cli.BoolFlag{
  38. Name: "debug",
  39. },
  40. &cli.IntFlag{
  41. Name: "interval",
  42. Usage: "proxy announce time interval",
  43. EnvVars: []string{"PROXYINTERVAL"},
  44. Value: 120,
  45. },
  46. &cli.IntFlag{
  47. Name: "dead-interval",
  48. Usage: "interval (in seconds) wether detect egress nodes offline",
  49. EnvVars: []string{"PROXYDEADINTERVAL"},
  50. Value: 600,
  51. },
  52. ),
  53. Action: func(c *cli.Context) error {
  54. o, _, ll := cliToOpts(c)
  55. o = append(o, services.Proxy(
  56. time.Duration(c.Int("interval"))*time.Second,
  57. time.Duration(c.Int("dead-interval"))*time.Second,
  58. c.String("listen"))...)
  59. bwc := metrics.NewBandwidthCounter()
  60. o = append(o, node.WithLibp2pAdditionalOptions(libp2p.BandwidthReporter(bwc)))
  61. e, err := node.New(o...)
  62. if err != nil {
  63. return err
  64. }
  65. displayStart(ll)
  66. go handleStopSignals()
  67. ctx := context.Background()
  68. // Start the node to the network, using our ledger
  69. if err := e.Start(ctx); err != nil {
  70. return err
  71. }
  72. return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, bwc, c.Bool("debug"))
  73. },
  74. }
  75. }