2019-07-24 01:13:56 +00:00
|
|
|
package impl
|
2019-07-24 00:58:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/gbrlsnchs/jwt/v3"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2019-07-25 00:55:19 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2019-07-24 00:58:31 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
2019-09-16 13:46:05 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
|
|
|
"github.com/filecoin-project/go-lotus/build"
|
|
|
|
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
|
2019-07-24 00:58:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CommonAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
2019-08-20 17:19:24 +00:00
|
|
|
APISecret *dtypes.APIAlg
|
2019-07-24 00:58:31 +00:00
|
|
|
Host host.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
type jwtPayload struct {
|
|
|
|
Allow []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *CommonAPI) AuthVerify(ctx context.Context, token string) ([]string, error) {
|
|
|
|
var payload jwtPayload
|
|
|
|
if _, err := jwt.Verify([]byte(token), (*jwt.HMACSHA)(a.APISecret), &payload); err != nil {
|
|
|
|
return nil, xerrors.Errorf("JWT Verification failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload.Allow, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *CommonAPI) AuthNew(ctx context.Context, perms []string) ([]byte, error) {
|
|
|
|
p := jwtPayload{
|
|
|
|
Allow: perms, // TODO: consider checking validity
|
|
|
|
}
|
|
|
|
|
|
|
|
return jwt.Sign(&p, (*jwt.HMACSHA)(a.APISecret))
|
|
|
|
}
|
|
|
|
|
2019-07-25 00:55:19 +00:00
|
|
|
func (a *CommonAPI) NetConnectedness(ctx context.Context, pid peer.ID) (network.Connectedness, error) {
|
|
|
|
return a.Host.Network().Connectedness(pid), nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:58:31 +00:00
|
|
|
func (a *CommonAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) {
|
|
|
|
conns := a.Host.Network().Conns()
|
|
|
|
out := make([]peer.AddrInfo, len(conns))
|
|
|
|
|
|
|
|
for i, conn := range conns {
|
|
|
|
out[i] = peer.AddrInfo{
|
|
|
|
ID: conn.RemotePeer(),
|
|
|
|
Addrs: []ma.Multiaddr{
|
|
|
|
conn.RemoteMultiaddr(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *CommonAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error {
|
|
|
|
return a.Host.Connect(ctx, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *CommonAPI) NetAddrsListen(context.Context) (peer.AddrInfo, error) {
|
|
|
|
return peer.AddrInfo{
|
|
|
|
ID: a.Host.ID(),
|
|
|
|
Addrs: a.Host.Addrs(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 00:55:19 +00:00
|
|
|
func (a *CommonAPI) NetDisconnect(ctx context.Context, p peer.ID) error {
|
|
|
|
return a.Host.Network().ClosePeer(p)
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:58:31 +00:00
|
|
|
func (a *CommonAPI) ID(context.Context) (peer.ID, error) {
|
|
|
|
return a.Host.ID(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *CommonAPI) Version(context.Context) (api.Version, error) {
|
|
|
|
return api.Version{
|
2019-10-09 20:18:53 +00:00
|
|
|
Version: build.Version,
|
|
|
|
APIVersion: build.APIVersion,
|
2019-07-24 00:58:31 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ api.Common = &CommonAPI{}
|