options.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 node
  16. import (
  17. "encoding/base64"
  18. "io/ioutil"
  19. "time"
  20. "github.com/ipfs/go-log"
  21. "github.com/libp2p/go-libp2p"
  22. dht "github.com/libp2p/go-libp2p-kad-dht"
  23. "github.com/mudler/edgevpn/pkg/blockchain"
  24. discovery "github.com/mudler/edgevpn/pkg/discovery"
  25. "github.com/mudler/edgevpn/pkg/protocol"
  26. "github.com/mudler/edgevpn/pkg/utils"
  27. "github.com/pkg/errors"
  28. "github.com/xlzd/gotp"
  29. "gopkg.in/yaml.v2"
  30. )
  31. // WithLibp2pOptions Overrides defaults options
  32. func WithLibp2pOptions(i ...libp2p.Option) func(cfg *Config) error {
  33. return func(cfg *Config) error {
  34. cfg.Options = i
  35. return nil
  36. }
  37. }
  38. func WithSealer(i Sealer) Option {
  39. return func(cfg *Config) error {
  40. cfg.Sealer = i
  41. return nil
  42. }
  43. }
  44. func WithLibp2pAdditionalOptions(i ...libp2p.Option) func(cfg *Config) error {
  45. return func(cfg *Config) error {
  46. cfg.AdditionalOptions = append(cfg.AdditionalOptions, i...)
  47. return nil
  48. }
  49. }
  50. func WithNetworkService(ns ...NetworkService) func(cfg *Config) error {
  51. return func(cfg *Config) error {
  52. cfg.NetworkServices = append(cfg.NetworkServices, ns...)
  53. return nil
  54. }
  55. }
  56. func WithInterfaceAddress(i string) func(cfg *Config) error {
  57. return func(cfg *Config) error {
  58. cfg.InterfaceAddress = i
  59. return nil
  60. }
  61. }
  62. func WithBlacklist(i ...string) func(cfg *Config) error {
  63. return func(cfg *Config) error {
  64. cfg.Blacklist = i
  65. return nil
  66. }
  67. }
  68. func Logger(l log.StandardLogger) func(cfg *Config) error {
  69. return func(cfg *Config) error {
  70. cfg.Logger = l
  71. return nil
  72. }
  73. }
  74. func WithStore(s blockchain.Store) func(cfg *Config) error {
  75. return func(cfg *Config) error {
  76. cfg.Store = s
  77. return nil
  78. }
  79. }
  80. // Handlers adds a handler to the list that is called on each received message
  81. func Handlers(h ...Handler) func(cfg *Config) error {
  82. return func(cfg *Config) error {
  83. cfg.Handlers = append(cfg.Handlers, h...)
  84. return nil
  85. }
  86. }
  87. // WithStreamHandler adds a handler to the list that is called on each received message
  88. func WithStreamHandler(id protocol.Protocol, h StreamHandler) func(cfg *Config) error {
  89. return func(cfg *Config) error {
  90. cfg.StreamHandlers[id] = h
  91. return nil
  92. }
  93. }
  94. // DiscoveryService Adds the service given as argument to the discovery services
  95. func DiscoveryService(s ...ServiceDiscovery) func(cfg *Config) error {
  96. return func(cfg *Config) error {
  97. cfg.ServiceDiscovery = append(cfg.ServiceDiscovery, s...)
  98. return nil
  99. }
  100. }
  101. func ListenAddresses(ss ...string) func(cfg *Config) error {
  102. return func(cfg *Config) error {
  103. for _, s := range ss {
  104. a := &discovery.AddrList{}
  105. err := a.Set(s)
  106. if err != nil {
  107. return err
  108. }
  109. cfg.ListenAddresses = append(cfg.ListenAddresses, *a)
  110. }
  111. return nil
  112. }
  113. }
  114. func Insecure(b bool) func(cfg *Config) error {
  115. return func(cfg *Config) error {
  116. cfg.Insecure = b
  117. return nil
  118. }
  119. }
  120. func ExchangeKeys(s string) func(cfg *Config) error {
  121. return func(cfg *Config) error {
  122. cfg.ExchangeKey = s
  123. return nil
  124. }
  125. }
  126. func RoomName(s string) func(cfg *Config) error {
  127. return func(cfg *Config) error {
  128. cfg.RoomName = s
  129. return nil
  130. }
  131. }
  132. func SealKeyInterval(i int) func(cfg *Config) error {
  133. return func(cfg *Config) error {
  134. cfg.SealKeyInterval = i
  135. return nil
  136. }
  137. }
  138. func SealKeyLength(i int) func(cfg *Config) error {
  139. return func(cfg *Config) error {
  140. cfg.SealKeyLength = i
  141. return nil
  142. }
  143. }
  144. func LibP2PLogLevel(l log.LogLevel) func(cfg *Config) error {
  145. return func(cfg *Config) error {
  146. log.SetAllLoggers(l)
  147. return nil
  148. }
  149. }
  150. func MaxMessageSize(i int) func(cfg *Config) error {
  151. return func(cfg *Config) error {
  152. cfg.MaxMessageSize = i
  153. return nil
  154. }
  155. }
  156. func WithPeerGater(d Gater) Option {
  157. return func(cfg *Config) error {
  158. cfg.PeerGater = d
  159. return nil
  160. }
  161. }
  162. func WithLedgerAnnounceTime(t time.Duration) func(cfg *Config) error {
  163. return func(cfg *Config) error {
  164. cfg.LedgerAnnounceTime = t
  165. return nil
  166. }
  167. }
  168. func WithLedgerInterval(t time.Duration) func(cfg *Config) error {
  169. return func(cfg *Config) error {
  170. cfg.LedgerSyncronizationTime = t
  171. return nil
  172. }
  173. }
  174. func WithDiscoveryInterval(t time.Duration) func(cfg *Config) error {
  175. return func(cfg *Config) error {
  176. cfg.DiscoveryInterval = t
  177. return nil
  178. }
  179. }
  180. func WithDiscoveryBootstrapPeers(a discovery.AddrList) func(cfg *Config) error {
  181. return func(cfg *Config) error {
  182. cfg.DiscoveryBootstrapPeers = a
  183. return nil
  184. }
  185. }
  186. type OTPConfig struct {
  187. Interval int `yaml:"interval"`
  188. Key string `yaml:"key"`
  189. Length int `yaml:"length"`
  190. }
  191. type OTP struct {
  192. DHT OTPConfig `yaml:"dht"`
  193. Crypto OTPConfig `yaml:"crypto"`
  194. }
  195. type YAMLConnectionConfig struct {
  196. OTP OTP `yaml:"otp"`
  197. RoomName string `yaml:"room"`
  198. Rendezvous string `yaml:"rendezvous"`
  199. MDNS string `yaml:"mdns"`
  200. MaxMessageSize int `yaml:"max_message_size"`
  201. }
  202. // Base64 returns the base64 string representation of the connection
  203. func (y YAMLConnectionConfig) Base64() string {
  204. bytesData, _ := yaml.Marshal(y)
  205. return base64.StdEncoding.EncodeToString(bytesData)
  206. }
  207. // YAML returns the connection config as yaml string
  208. func (y YAMLConnectionConfig) YAML() string {
  209. bytesData, _ := yaml.Marshal(y)
  210. return string(bytesData)
  211. }
  212. func (y YAMLConnectionConfig) copy(mdns, dht bool, cfg *Config, opts ...dht.Option) {
  213. d := discovery.NewDHT(opts...)
  214. d.RefreshDiscoveryTime = cfg.DiscoveryInterval
  215. d.OTPInterval = y.OTP.DHT.Interval
  216. d.OTPKey = y.OTP.DHT.Key
  217. d.KeyLength = y.OTP.DHT.Length
  218. d.RendezvousString = y.Rendezvous
  219. d.BootstrapPeers = cfg.DiscoveryBootstrapPeers
  220. m := &discovery.MDNS{DiscoveryServiceTag: y.MDNS}
  221. cfg.ExchangeKey = y.OTP.Crypto.Key
  222. cfg.RoomName = y.RoomName
  223. cfg.SealKeyInterval = y.OTP.Crypto.Interval
  224. // cfg.ServiceDiscovery = []ServiceDiscovery{d, m}
  225. if mdns {
  226. cfg.ServiceDiscovery = append(cfg.ServiceDiscovery, m)
  227. }
  228. if dht {
  229. cfg.ServiceDiscovery = append(cfg.ServiceDiscovery, d)
  230. }
  231. cfg.SealKeyLength = y.OTP.Crypto.Length
  232. cfg.MaxMessageSize = y.MaxMessageSize
  233. }
  234. const defaultKeyLength = 32
  235. func GenerateNewConnectionData(i ...int) *YAMLConnectionConfig {
  236. defaultInterval := 9000
  237. maxMessSize := 20 << 20 // 20MB
  238. if len(i) >= 2 {
  239. defaultInterval = i[0]
  240. maxMessSize = i[1]
  241. } else if len(i) == 1 {
  242. defaultInterval = i[0]
  243. }
  244. return &YAMLConnectionConfig{
  245. MaxMessageSize: maxMessSize,
  246. RoomName: gotp.RandomSecret(defaultKeyLength),
  247. Rendezvous: utils.RandStringRunes(defaultKeyLength),
  248. MDNS: utils.RandStringRunes(defaultKeyLength),
  249. OTP: OTP{
  250. DHT: OTPConfig{
  251. Key: gotp.RandomSecret(defaultKeyLength),
  252. Interval: defaultInterval,
  253. Length: defaultKeyLength,
  254. },
  255. Crypto: OTPConfig{
  256. Key: gotp.RandomSecret(defaultKeyLength),
  257. Interval: defaultInterval,
  258. Length: defaultKeyLength,
  259. },
  260. },
  261. }
  262. }
  263. func FromYaml(enablemDNS, enableDHT bool, path string, d ...dht.Option) func(cfg *Config) error {
  264. return func(cfg *Config) error {
  265. if len(path) == 0 {
  266. return nil
  267. }
  268. t := YAMLConnectionConfig{}
  269. data, err := ioutil.ReadFile(path)
  270. if err != nil {
  271. return errors.Wrap(err, "reading yaml file")
  272. }
  273. if err := yaml.Unmarshal(data, &t); err != nil {
  274. return errors.Wrap(err, "parsing yaml")
  275. }
  276. t.copy(enablemDNS, enableDHT, cfg, d...)
  277. return nil
  278. }
  279. }
  280. func FromBase64(enablemDNS, enableDHT bool, bb string, d ...dht.Option) func(cfg *Config) error {
  281. return func(cfg *Config) error {
  282. if len(bb) == 0 {
  283. return nil
  284. }
  285. configDec, err := base64.StdEncoding.DecodeString(bb)
  286. if err != nil {
  287. return err
  288. }
  289. t := YAMLConnectionConfig{}
  290. if err := yaml.Unmarshal(configDec, &t); err != nil {
  291. return errors.Wrap(err, "parsing yaml")
  292. }
  293. t.copy(enablemDNS, enableDHT, cfg, d...)
  294. return nil
  295. }
  296. }