Skip to content

Commit

Permalink
feat: support more router customization
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Oct 10, 2023
1 parent 92b13a2 commit ec371e0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ go build -o someguy

You can use `someguy` as a client or server.

`someguy start` runs a Delegated Routing V1 server that proxies requests to the [IPFS Amino DHT](https://blog.ipfs.tech/2023-09-amino-refactoring/) and the [cid.contact](https://cid.contact) indexer node. We plan on supporting additional custom endpoints.
`someguy start` runs a Delegated Routing V1 server that proxies requests to the [IPFS Amino DHT](https://blog.ipfs.tech/2023-09-amino-refactoring/) and the [cid.contact](https://cid.contact) indexer node.

If you don't want to run a server yourself, but want to query some other server, you can run `someguy ask` and choose any of the subcommands and ask for a provider, a peer, or even an IPNS record.

Expand Down
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,35 @@ func main() {
},
&cli.BoolFlag{
Name: "accelerated-dht",
Usage: "run the accelerated dht client",
Usage: "run the accelerated DHT client",
Value: true,
},
&cli.StringSliceFlag{
Name: "content-endpoints",
Usage: "other Delegated Routing V1 endpoints to proxy provider requests to",
Value: cli.NewStringSlice(cidContactEndpoint),
},
&cli.StringSliceFlag{
Name: "peer-endpoints",
Usage: "other Delegated Routing V1 endpoints to proxy peer requests to",
Value: cli.NewStringSlice(),
},
&cli.StringSliceFlag{
Name: "ipns-endpoints",
Usage: "other Delegated Routing V1 endpoints to proxy IPNS requests to",
Value: cli.NewStringSlice(),
},
},
Action: func(ctx *cli.Context) error {
return start(ctx.Context, ctx.Int("port"), ctx.Bool("accelerated-dht"))
return start(ctx.Context, ctx.Int("port"), ctx.Bool("accelerated-dht"), ctx.StringSlice("content-endpoints"), ctx.StringSlice("peer-endpoints"), ctx.StringSlice("ipns-endpoints"))
},
},
{
Name: "ask",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "endpoint",
Usage: "Delegated Routing V1 endpoint",
Usage: "the Delegated Routing V1 endpoint to ask",
Value: cidContactEndpoint,
},
&cli.BoolFlag{
Expand Down
53 changes: 39 additions & 14 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ import (
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
)

func start(ctx context.Context, port int, runAcceleratedDHTClient bool) error {
indexerClient, err := client.New(cidContactEndpoint)
if err != nil {
return err
}
indexerRouting := newWrappedDelegatedRouting(indexerClient)

func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentEndpoints, peerEndpoints, ipnsEndpoints []string) error {
h, err := newHost(runAcceleratedDHTClient)
if err != nil {
return err
Expand All @@ -52,14 +46,25 @@ func start(ctx context.Context, port int, runAcceleratedDHTClient bool) error {
dhtRouting = standardDHT
}

crRouters, err := getCombinedRouting(contentEndpoints, dhtRouting)
if err != nil {
return err
}

prRouters, err := getCombinedRouting(peerEndpoints, dhtRouting)
if err != nil {
return err
}

ipnsRouters, err := getCombinedRouting(ipnsEndpoints, dhtRouting)
if err != nil {
return err
}

proxy := &delegatedRoutingProxy{
cr: routinghelpers.Parallel{
Routers: []routing.Routing{indexerRouting, dhtRouting},
},
pr: routinghelpers.Parallel{
Routers: []routing.Routing{indexerRouting, dhtRouting},
},
vs: dhtRouting,
cr: crRouters,
pr: prRouters,
vs: ipnsRouters,
}

log.Printf("Listening on http://0.0.0.0:%d", port)
Expand Down Expand Up @@ -180,6 +185,26 @@ func (w *wrappedStandardAndAcceleratedDHTClient) Bootstrap(ctx context.Context)
return w.standard.Bootstrap(ctx)
}

func getCombinedRouting(endpoints []string, dht routing.Routing) (routing.Routing, error) {
if len(endpoints) == 0 {
return dht, nil
}

var routers []routing.Routing

for _, endpoint := range endpoints {
drclient, err := client.New(endpoint)
if err != nil {
return nil, err
}
routers = append(routers, newWrappedDelegatedRouting(drclient))
}

return routinghelpers.Parallel{
Routers: append(routers, dht),
}, nil
}

type wrappedDelegatedRouting struct {
routing.ValueStore
routing.PeerRouting
Expand Down

0 comments on commit ec371e0

Please sign in to comment.