storj/pkg/kademlia/config.go
Bryan White 5d20cf8829
Node Identity (#193)
* peertls: don't log errors for double close

understood that this part of the code is undergoing heavy change
right now, but just want to make sure this fix gets incorporated
somewhere

* git cleanup: node-id stuff

* cleanup

* rename identity_util.go

* wip `CertificateAuthority` refactor

* refactoring

* gitignore update

* wip

* Merge remote-tracking branch 'storj/doubleclose' into node-id3

* storj/doubleclose:
  peertls: don't log errors for double close

* add peertls tests & gomports

* wip:

+ refactor
+ style changes
+ cleanup
+ [wip] add version to CA and identity configs
+ [wip] heavy client setup

* refactor

* wip:

+ refactor
+ style changes
+ add `CAConfig.Load`
+ add `CAConfig.Save`

* wip:

+ add `LoadOrCreate` and `Create` to CA and Identity configs
+ add overwrite to CA and identity configs
+ heavy client setup
+ refactor
+ style changes
+ cleanup

* wip

* fixing things

* fixing things

* wip hc setup

* hc setup:

+ refactor
+ bugfixing

* improvements based on reveiw feedback

* goimports

* improvements:

+ responding to review feedback
+ refactor

* feedback-based improvements

* feedback-based improvements

* feedback-based improvements

* feedback-based improvements

* feedback-based improvements

* feedback-based improvements

* cleanup

* refactoring CA and Identity structs

* Merge branch 'master' into node-id3

* move version field to setup config structs for CA and identity

* fix typo

* responding to revieiw feedback

* responding to revieiw feedback

* responding to revieiw feedback

* responding to revieiw feedback

* responding to revieiw feedback

* responding to revieiw feedback

* Merge branch 'master' into node-id3

* fix gateway setup finally

* go imports

* fix `FullCertificateAuthority.GenerateIdentity`

* cleanup overlay tests

* bugfixing

* update ca/identity setup

* go imports

* fix peertls test copy/paste fail

* responding to review feedback

* setup tweaking

* update farmer setup
2018-08-13 10:39:45 +02:00

93 lines
2.3 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package kademlia
import (
"context"
"net"
"github.com/zeebo/errs"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/provider"
proto "storj.io/storj/protos/overlay"
)
var (
// Error defines a Kademlia error
Error = errs.Class("kademlia error")
mon = monkit.Package()
)
//CtxKey Used as kademlia key
type CtxKey int
const (
ctxKeyKad CtxKey = iota
)
// Config defines all of the things that are needed to start up Kademlia
// server endpoints (and not necessarily client code).
type Config struct {
BootstrapAddr string `help:"the kademlia node to bootstrap against" default:"bootstrap-dev.storj.io:8080"`
// TODO(jt): remove this! kademlia should just use the grpc server
TODOListenAddr string `help:"the host/port for kademlia to listen on. TODO(jt): this should be removed!" default:"127.0.0.1:7776"`
}
// Run implements provider.Responsibility
func (c Config) Run(ctx context.Context, server *provider.Provider) (
err error) {
defer mon.Task()(&ctx)(&err)
// TODO(jt): don't split the host/port
host, port, err := net.SplitHostPort(c.BootstrapAddr)
if err != nil {
return Error.Wrap(err)
}
// TODO(jt): an intro node shouldn't require an ID, and should only be an
// address
in, err := GetIntroNode("", host, port)
if err != nil {
return err
}
// TODO(jt): don't split the host/port
host, port, err = net.SplitHostPort(c.TODOListenAddr)
if err != nil {
return Error.Wrap(err)
}
// TODO(jt): kademlia should register on server.GRPC() instead of listening
// itself
kad, err := NewKademlia(server.Identity().ID, []proto.Node{*in}, host, port)
if err != nil {
return err
}
defer func() { _ = kad.Disconnect() }()
// TODO(jt): ListenAndServe should probably be blocking and we should kick
// it off in a goroutine here
err = kad.ListenAndServe()
if err != nil {
return err
}
// TODO(jt): Bootstrap should probably be blocking and we should kick it off
// in a goroutine here
err = kad.Bootstrap(ctx)
if err != nil {
return err
}
return server.Run(context.WithValue(ctx, ctxKeyKad, kad))
}
// LoadFromContext loads an existing Kademlia from the Provider context
// stack if one exists.
func LoadFromContext(ctx context.Context) *Kademlia {
if v, ok := ctx.Value(ctxKeyKad).(*Kademlia); ok {
return v
}
return nil
}