config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright © 2022 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 vpn
  16. import (
  17. "time"
  18. "github.com/ipfs/go-log"
  19. "github.com/songgao/water"
  20. )
  21. type Config struct {
  22. Interface *water.Interface
  23. InterfaceName string
  24. InterfaceAddress string
  25. RouterAddress string
  26. InterfaceMTU int
  27. MTU int
  28. DeviceType water.DeviceType
  29. LedgerAnnounceTime time.Duration
  30. Logger log.StandardLogger
  31. NetLinkBootstrap bool
  32. // Frame timeout
  33. Timeout time.Duration
  34. Concurrency int
  35. ChannelBufferSize int
  36. MaxStreams int
  37. lowProfile bool
  38. }
  39. type Option func(cfg *Config) error
  40. // Apply applies the given options to the config, returning the first error
  41. // encountered (if any).
  42. func (cfg *Config) Apply(opts ...Option) error {
  43. for _, opt := range opts {
  44. if opt == nil {
  45. continue
  46. }
  47. if err := opt(cfg); err != nil {
  48. return err
  49. }
  50. }
  51. return nil
  52. }
  53. func WithMaxStreams(i int) func(cfg *Config) error {
  54. return func(cfg *Config) error {
  55. cfg.MaxStreams = i
  56. return nil
  57. }
  58. }
  59. var LowProfile Option = func(cfg *Config) error {
  60. cfg.lowProfile = true
  61. return nil
  62. }
  63. func WithInterface(i *water.Interface) func(cfg *Config) error {
  64. return func(cfg *Config) error {
  65. cfg.Interface = i
  66. return nil
  67. }
  68. }
  69. func NetLinkBootstrap(b bool) func(cfg *Config) error {
  70. return func(cfg *Config) error {
  71. cfg.NetLinkBootstrap = b
  72. return nil
  73. }
  74. }
  75. func WithTimeout(s string) Option {
  76. return func(cfg *Config) error {
  77. d, err := time.ParseDuration(s)
  78. cfg.Timeout = d
  79. return err
  80. }
  81. }
  82. func Logger(l log.StandardLogger) func(cfg *Config) error {
  83. return func(cfg *Config) error {
  84. cfg.Logger = l
  85. return nil
  86. }
  87. }
  88. func WithRouterAddress(i string) func(cfg *Config) error {
  89. return func(cfg *Config) error {
  90. cfg.RouterAddress = i
  91. return nil
  92. }
  93. }
  94. func WithLedgerAnnounceTime(t time.Duration) func(cfg *Config) error {
  95. return func(cfg *Config) error {
  96. cfg.LedgerAnnounceTime = t
  97. return nil
  98. }
  99. }
  100. func WithConcurrency(i int) Option {
  101. return func(cfg *Config) error {
  102. cfg.Concurrency = i
  103. return nil
  104. }
  105. }
  106. func WithChannelBufferSize(i int) Option {
  107. return func(cfg *Config) error {
  108. cfg.ChannelBufferSize = i
  109. return nil
  110. }
  111. }
  112. func WithInterfaceMTU(i int) func(cfg *Config) error {
  113. return func(cfg *Config) error {
  114. cfg.InterfaceMTU = i
  115. return nil
  116. }
  117. }
  118. func WithPacketMTU(i int) func(cfg *Config) error {
  119. return func(cfg *Config) error {
  120. cfg.MTU = i
  121. return nil
  122. }
  123. }
  124. func WithInterfaceType(d water.DeviceType) func(cfg *Config) error {
  125. return func(cfg *Config) error {
  126. cfg.DeviceType = d
  127. return nil
  128. }
  129. }
  130. func WithInterfaceName(i string) func(cfg *Config) error {
  131. return func(cfg *Config) error {
  132. cfg.InterfaceName = i
  133. return nil
  134. }
  135. }
  136. func WithInterfaceAddress(i string) func(cfg *Config) error {
  137. return func(cfg *Config) error {
  138. cfg.InterfaceAddress = i
  139. return nil
  140. }
  141. }