mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-16 12:03:03 +00:00
Add remote addr to gRPC transport layer conn (#382)
Co-authored-by: RPRX <63339210+rprx@users.noreply.github.com>
This commit is contained in:
parent
de54d4b08f
commit
36961ed882
@ -5,12 +5,15 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
"google.golang.org/grpc/peer"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common/buf"
|
"github.com/xtls/xray-core/common/buf"
|
||||||
"github.com/xtls/xray-core/common/net/cnc"
|
"github.com/xtls/xray-core/common/net/cnc"
|
||||||
"github.com/xtls/xray-core/common/signal/done"
|
"github.com/xtls/xray-core/common/signal/done"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HunkConn interface {
|
type HunkConn interface {
|
||||||
|
Context() context.Context
|
||||||
Send(*Hunk) error
|
Send(*Hunk) error
|
||||||
Recv() (*Hunk, error)
|
Recv() (*Hunk, error)
|
||||||
SendMsg(m interface{}) error
|
SendMsg(m interface{}) error
|
||||||
@ -35,11 +38,23 @@ func NewHunkReadWriter(hc HunkConn, cancel context.CancelFunc) *HunkReaderWriter
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewHunkConn(hc HunkConn, cancel context.CancelFunc) net.Conn {
|
func NewHunkConn(hc HunkConn, cancel context.CancelFunc) net.Conn {
|
||||||
|
var rAddr net.Addr
|
||||||
|
pr, ok := peer.FromContext(hc.Context())
|
||||||
|
if ok {
|
||||||
|
rAddr = pr.Addr
|
||||||
|
} else {
|
||||||
|
rAddr = &net.TCPAddr{
|
||||||
|
IP: []byte{0, 0, 0, 0},
|
||||||
|
Port: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wrc := NewHunkReadWriter(hc, cancel)
|
wrc := NewHunkReadWriter(hc, cancel)
|
||||||
return cnc.NewConnection(
|
return cnc.NewConnection(
|
||||||
cnc.ConnectionInput(wrc),
|
cnc.ConnectionInput(wrc),
|
||||||
cnc.ConnectionOutput(wrc),
|
cnc.ConnectionOutput(wrc),
|
||||||
cnc.ConnectionOnClose(wrc),
|
cnc.ConnectionOnClose(wrc),
|
||||||
|
cnc.ConnectionRemoteAddr(rAddr),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,15 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
"google.golang.org/grpc/peer"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common/buf"
|
"github.com/xtls/xray-core/common/buf"
|
||||||
"github.com/xtls/xray-core/common/net/cnc"
|
"github.com/xtls/xray-core/common/net/cnc"
|
||||||
"github.com/xtls/xray-core/common/signal/done"
|
"github.com/xtls/xray-core/common/signal/done"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MultiHunkConn interface {
|
type MultiHunkConn interface {
|
||||||
|
Context() context.Context
|
||||||
Send(*MultiHunk) error
|
Send(*MultiHunk) error
|
||||||
Recv() (*MultiHunk, error)
|
Recv() (*MultiHunk, error)
|
||||||
SendMsg(m interface{}) error
|
SendMsg(m interface{}) error
|
||||||
@ -30,11 +33,23 @@ func NewMultiHunkReadWriter(hc MultiHunkConn, cancel context.CancelFunc) *MultiH
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewMultiHunkConn(hc MultiHunkConn, cancel context.CancelFunc) net.Conn {
|
func NewMultiHunkConn(hc MultiHunkConn, cancel context.CancelFunc) net.Conn {
|
||||||
|
var rAddr net.Addr
|
||||||
|
pr, ok := peer.FromContext(hc.Context())
|
||||||
|
if ok {
|
||||||
|
rAddr = pr.Addr
|
||||||
|
} else {
|
||||||
|
rAddr = &net.TCPAddr{
|
||||||
|
IP: []byte{0, 0, 0, 0},
|
||||||
|
Port: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wrc := NewMultiHunkReadWriter(hc, cancel)
|
wrc := NewMultiHunkReadWriter(hc, cancel)
|
||||||
return cnc.NewConnection(
|
return cnc.NewConnection(
|
||||||
cnc.ConnectionInputMulti(wrc),
|
cnc.ConnectionInputMulti(wrc),
|
||||||
cnc.ConnectionOutputMulti(wrc),
|
cnc.ConnectionOutputMulti(wrc),
|
||||||
cnc.ConnectionOnClose(wrc),
|
cnc.ConnectionOnClose(wrc),
|
||||||
|
cnc.ConnectionRemoteAddr(rAddr),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user