2020-08-25 11:51:00 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-03-10 12:15:54 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-20 15:51:28 +00:00
|
|
|
"crypto/tls"
|
2020-11-23 11:05:27 +00:00
|
|
|
"time"
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-08-25 11:51:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2021-05-31 12:09:04 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
2020-09-15 10:51:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2021-03-12 13:07:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/rpc/client"
|
2020-09-15 10:51:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-08-25 11:51:00 +00:00
|
|
|
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-03-12 13:07:52 +00:00
|
|
|
CallOption func(*callOptions)
|
2020-08-25 11:51:00 +00:00
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
Option func(*clientOptions)
|
2020-08-25 11:51:00 +00:00
|
|
|
|
|
|
|
callOptions struct {
|
2020-09-10 11:29:11 +00:00
|
|
|
version *pkg.Version
|
2020-11-16 09:31:25 +00:00
|
|
|
xHeaders []*pkg.XHeader
|
2020-08-25 11:51:00 +00:00
|
|
|
ttl uint32
|
|
|
|
epoch uint64
|
2021-03-10 12:15:54 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2021-05-31 12:09:04 +00:00
|
|
|
session *session.Token
|
2020-09-15 10:46:56 +00:00
|
|
|
bearer *token.BearerToken
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clientOptions struct {
|
2021-03-12 13:07:52 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2020-11-23 11:05:27 +00:00
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
rawOpts []client.Option
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
2020-09-15 10:51:36 +00:00
|
|
|
|
|
|
|
v2SessionReqInfo struct {
|
|
|
|
addr *refs.Address
|
|
|
|
verb v2session.ObjectSessionVerb
|
|
|
|
|
|
|
|
exp, nbf, iat uint64
|
|
|
|
}
|
2020-08-25 11:51:00 +00:00
|
|
|
)
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
func (c *clientImpl) defaultCallOptions() *callOptions {
|
|
|
|
return &callOptions{
|
2020-09-10 11:29:11 +00:00
|
|
|
version: pkg.SDKVersion(),
|
2021-03-12 13:07:52 +00:00
|
|
|
ttl: 2,
|
|
|
|
key: c.opts.key,
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:31:25 +00:00
|
|
|
func WithXHeader(x *pkg.XHeader) CallOption {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *callOptions) {
|
|
|
|
opts.xHeaders = append(opts.xHeaders, x)
|
|
|
|
}
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func WithTTL(ttl uint32) CallOption {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *callOptions) {
|
|
|
|
opts.ttl = ttl
|
|
|
|
}
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 12:15:54 +00:00
|
|
|
// WithKey sets client's key for the next request.
|
|
|
|
func WithKey(key *ecdsa.PrivateKey) CallOption {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *callOptions) {
|
|
|
|
opts.key = key
|
|
|
|
}
|
2021-03-10 12:15:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 11:51:00 +00:00
|
|
|
func WithEpoch(epoch uint64) CallOption {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *callOptions) {
|
|
|
|
opts.epoch = epoch
|
|
|
|
}
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
func WithSession(token *session.Token) CallOption {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *callOptions) {
|
|
|
|
opts.session = token
|
|
|
|
}
|
2020-09-15 10:51:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 10:46:56 +00:00
|
|
|
func WithBearer(token *token.BearerToken) CallOption {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *callOptions) {
|
|
|
|
opts.bearer = token
|
|
|
|
}
|
2020-09-15 10:46:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
func v2MetaHeaderFromOpts(options *callOptions) *v2session.RequestMetaHeader {
|
2020-08-25 11:51:00 +00:00
|
|
|
meta := new(v2session.RequestMetaHeader)
|
2020-09-10 11:29:11 +00:00
|
|
|
meta.SetVersion(options.version.ToV2())
|
2020-08-25 11:51:00 +00:00
|
|
|
meta.SetTTL(options.ttl)
|
|
|
|
meta.SetEpoch(options.epoch)
|
|
|
|
|
|
|
|
xhdrs := make([]*v2session.XHeader, len(options.xHeaders))
|
|
|
|
for i := range options.xHeaders {
|
2020-11-16 09:31:25 +00:00
|
|
|
xhdrs[i] = options.xHeaders[i].ToV2()
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
meta.SetXHeaders(xhdrs)
|
|
|
|
|
2020-09-15 10:46:56 +00:00
|
|
|
if options.bearer != nil {
|
|
|
|
meta.SetBearerToken(options.bearer.ToV2())
|
|
|
|
}
|
|
|
|
|
2020-10-27 13:27:16 +00:00
|
|
|
meta.SetSessionToken(options.session.ToV2())
|
|
|
|
|
2020-08-25 11:51:00 +00:00
|
|
|
return meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultClientOptions() *clientOptions {
|
|
|
|
return &clientOptions{
|
2021-05-20 15:51:28 +00:00
|
|
|
rawOpts: make([]client.Option, 0, 4),
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 15:10:51 +00:00
|
|
|
func WithAddress(addr string) Option {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithNetworkAddress(addr))
|
|
|
|
}
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 15:10:51 +00:00
|
|
|
func WithGRPCConnection(grpcConn *grpc.ClientConn) Option {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithGRPCConn(grpcConn))
|
|
|
|
}
|
2020-08-25 11:51:00 +00:00
|
|
|
}
|
2020-11-23 11:05:27 +00:00
|
|
|
|
|
|
|
// WithDialTimeout returns option to set connection timeout to the remote node.
|
|
|
|
func WithDialTimeout(dur time.Duration) Option {
|
2021-03-12 13:07:52 +00:00
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithDialTimeout(dur))
|
|
|
|
}
|
2020-11-23 11:05:27 +00:00
|
|
|
}
|
2021-03-10 12:15:54 +00:00
|
|
|
|
2021-05-20 15:51:28 +00:00
|
|
|
// WithTLSConfig returns option to set connection's TLS config to the remote node.
|
|
|
|
func WithTLSConfig(cfg *tls.Config) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.rawOpts = append(opts.rawOpts, client.WithTLSCfg(cfg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
// WithDefaultPrivateKey returns option to set default private key
|
|
|
|
// used for the work.
|
|
|
|
func WithDefaultPrivateKey(key *ecdsa.PrivateKey) Option {
|
|
|
|
return func(opts *clientOptions) {
|
|
|
|
opts.key = key
|
2021-03-10 12:15:54 +00:00
|
|
|
}
|
|
|
|
}
|