mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-04 12:33:04 +02:00
WithUserAgent() pool option.
This commit is contained in:
17
pool.go
17
pool.go
@ -30,6 +30,7 @@ type SimplePool struct {
|
|||||||
signatureChecker func(Event) bool
|
signatureChecker func(Event) bool
|
||||||
penaltyBoxMu sync.Mutex
|
penaltyBoxMu sync.Mutex
|
||||||
penaltyBox map[string][2]float64
|
penaltyBox map[string][2]float64
|
||||||
|
userAgent string
|
||||||
}
|
}
|
||||||
|
|
||||||
type DirectedFilters struct {
|
type DirectedFilters struct {
|
||||||
@ -120,10 +121,20 @@ func (h WithEventMiddleware) ApplyPoolOption(pool *SimplePool) {
|
|||||||
pool.eventMiddleware = append(pool.eventMiddleware, h)
|
pool.eventMiddleware = append(pool.eventMiddleware, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithUserAgent sets the user-agent header for all relay connections in the pool.
|
||||||
|
func WithUserAgent(userAgent string) withUserAgentOpt { return withUserAgentOpt(userAgent) }
|
||||||
|
|
||||||
|
type withUserAgentOpt string
|
||||||
|
|
||||||
|
func (h withUserAgentOpt) ApplyPoolOption(pool *SimplePool) {
|
||||||
|
pool.userAgent = string(h)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ PoolOption = (WithAuthHandler)(nil)
|
_ PoolOption = (WithAuthHandler)(nil)
|
||||||
_ PoolOption = (WithEventMiddleware)(nil)
|
_ PoolOption = (WithEventMiddleware)(nil)
|
||||||
_ PoolOption = WithPenaltyBox()
|
_ PoolOption = WithPenaltyBox()
|
||||||
|
_ PoolOption = WithUserAgent("")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
|
func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
|
||||||
@ -146,7 +157,6 @@ func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// try to connect
|
// try to connect
|
||||||
var err error
|
|
||||||
// we use this ctx here so when the pool dies everything dies
|
// we use this ctx here so when the pool dies everything dies
|
||||||
ctx, cancel := context.WithTimeout(pool.Context, time.Second*15)
|
ctx, cancel := context.WithTimeout(pool.Context, time.Second*15)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@ -156,7 +166,10 @@ func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) {
|
|||||||
opts = append(opts, WithSignatureChecker(pool.signatureChecker))
|
opts = append(opts, WithSignatureChecker(pool.signatureChecker))
|
||||||
}
|
}
|
||||||
|
|
||||||
if relay, err = RelayConnect(ctx, nm, opts...); err != nil {
|
relay = NewRelay(context.Background(), url, opts...)
|
||||||
|
relay.RequestHeader.Set("User-Agent", pool.userAgent)
|
||||||
|
|
||||||
|
if err := relay.Connect(ctx); err != nil {
|
||||||
if pool.penaltyBox != nil {
|
if pool.penaltyBox != nil {
|
||||||
// putting relay in penalty box
|
// putting relay in penalty box
|
||||||
pool.penaltyBoxMu.Lock()
|
pool.penaltyBoxMu.Lock()
|
||||||
|
4
relay.go
4
relay.go
@ -66,6 +66,7 @@ func NewRelay(ctx context.Context, url string, opts ...RelayOption) *Relay {
|
|||||||
ok, _ := e.CheckSignature()
|
ok, _ := e.CheckSignature()
|
||||||
return ok
|
return ok
|
||||||
},
|
},
|
||||||
|
RequestHeader: make(http.Header, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@ -160,9 +161,6 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.RequestHeader == nil {
|
|
||||||
r.RequestHeader = make(http.Header, 1)
|
|
||||||
}
|
|
||||||
if r.RequestHeader.Get("User-Agent") == "" {
|
if r.RequestHeader.Get("User-Agent") == "" {
|
||||||
r.RequestHeader.Set("User-Agent", "github.com/nbd-wtf/go-nostr")
|
r.RequestHeader.Set("User-Agent", "github.com/nbd-wtf/go-nostr")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user