mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-17 04:29:52 +08:00

We now consider debugerrors harmful: we've run into cases where debugerror.Wrap() hid valuable error information (err == io.EOF?). I've removed them from the main code, but left them in some tests. Go errors are lacking, but unfortunately, this isn't the solution. It is possible that debugerros.New or debugerrors.Errorf should remain still (i.e. only remove debugerrors.Wrap) but we don't use these errors often enough to keep.
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package proxy
|
|
|
|
import (
|
|
ggio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
|
|
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
inet "github.com/ipfs/go-ipfs/p2p/net"
|
|
peer "github.com/ipfs/go-ipfs/p2p/peer"
|
|
dhtpb "github.com/ipfs/go-ipfs/routing/dht/pb"
|
|
)
|
|
|
|
// RequestHandler handles routing requests locally
|
|
type RequestHandler interface {
|
|
HandleRequest(ctx context.Context, p peer.ID, m *dhtpb.Message) *dhtpb.Message
|
|
}
|
|
|
|
// Loopback forwards requests to a local handler
|
|
type Loopback struct {
|
|
Handler RequestHandler
|
|
Local peer.ID
|
|
}
|
|
|
|
func (_ *Loopback) Bootstrap(ctx context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
// SendMessage intercepts local requests, forwarding them to a local handler
|
|
func (lb *Loopback) SendMessage(ctx context.Context, m *dhtpb.Message) error {
|
|
response := lb.Handler.HandleRequest(ctx, lb.Local, m)
|
|
if response != nil {
|
|
log.Warning("loopback handler returned unexpected message")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SendRequest intercepts local requests, forwarding them to a local handler
|
|
func (lb *Loopback) SendRequest(ctx context.Context, m *dhtpb.Message) (*dhtpb.Message, error) {
|
|
return lb.Handler.HandleRequest(ctx, lb.Local, m), nil
|
|
}
|
|
|
|
func (lb *Loopback) HandleStream(s inet.Stream) {
|
|
defer s.Close()
|
|
pbr := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
|
|
var incoming dhtpb.Message
|
|
if err := pbr.ReadMsg(&incoming); err != nil {
|
|
log.Debug(err)
|
|
return
|
|
}
|
|
ctx := context.TODO()
|
|
outgoing := lb.Handler.HandleRequest(ctx, s.Conn().RemotePeer(), &incoming)
|
|
|
|
pbw := ggio.NewDelimitedWriter(s)
|
|
|
|
if err := pbw.WriteMsg(outgoing); err != nil {
|
|
return // TODO logerr
|
|
}
|
|
}
|