merged with fixes
This commit is contained in:
parent
a9e758b712
commit
2f1818d656
176 changed files with 9000 additions and 907 deletions
28
internal/transport/metadata.go
Normal file
28
internal/transport/metadata.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Package transport holds neutral transport metadata shared across the
|
||||
// protocol/edge boundary and the RPC business layer. It must not import any
|
||||
// business package (internal/rpc, internal/app, ...), so that mtprotoedge can
|
||||
// produce transport facts without depending on the RPC implementation.
|
||||
package transport
|
||||
|
||||
import "context"
|
||||
|
||||
type ctxKey int
|
||||
|
||||
const (
|
||||
clientIPKey ctxKey = iota
|
||||
)
|
||||
|
||||
// WithClientIP records the client remote IP as neutral transport metadata.
|
||||
// An empty ip is ignored.
|
||||
func WithClientIP(ctx context.Context, ip string) context.Context {
|
||||
if ip == "" {
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, clientIPKey, ip)
|
||||
}
|
||||
|
||||
// ClientIPFrom returns the client remote IP carried in ctx, if set.
|
||||
func ClientIPFrom(ctx context.Context) (string, bool) {
|
||||
v, ok := ctx.Value(clientIPKey).(string)
|
||||
return v, ok && v != ""
|
||||
}
|
||||
27
internal/transport/metadata_test.go
Normal file
27
internal/transport/metadata_test.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package transport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWithClientIPRoundTrip(t *testing.T) {
|
||||
ctx := WithClientIP(context.Background(), "203.0.113.7")
|
||||
ip, ok := ClientIPFrom(ctx)
|
||||
if !ok || ip != "203.0.113.7" {
|
||||
t.Fatalf("round-trip mismatch: got %q ok=%v", ip, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithClientIPEmptyIgnored(t *testing.T) {
|
||||
ctx := WithClientIP(context.Background(), "")
|
||||
if _, ok := ClientIPFrom(ctx); ok {
|
||||
t.Fatalf("empty ip must not be stored")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIPFromNotSet(t *testing.T) {
|
||||
if _, ok := ClientIPFrom(context.Background()); ok {
|
||||
t.Fatalf("no ip set, ok must be false")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue