2021-03-14 15:02:07 +00:00
|
|
|
package encoding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
2021-10-14 04:10:19 +00:00
|
|
|
xnet "github.com/xtls/xray-core/common/net"
|
2021-03-14 15:02:07 +00:00
|
|
|
"github.com/xtls/xray-core/common/net/cnc"
|
|
|
|
"github.com/xtls/xray-core/common/signal/done"
|
2022-05-18 07:29:01 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/peer"
|
2021-03-14 15:02:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MultiHunkConn interface {
|
2021-03-17 17:55:51 +00:00
|
|
|
Context() context.Context
|
2021-03-14 15:02:07 +00:00
|
|
|
Send(*MultiHunk) error
|
|
|
|
Recv() (*MultiHunk, error)
|
|
|
|
SendMsg(m interface{}) error
|
|
|
|
RecvMsg(m interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiHunkReaderWriter struct {
|
|
|
|
hc MultiHunkConn
|
|
|
|
cancel context.CancelFunc
|
|
|
|
done *done.Instance
|
|
|
|
|
|
|
|
buf [][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMultiHunkReadWriter(hc MultiHunkConn, cancel context.CancelFunc) *MultiHunkReaderWriter {
|
|
|
|
return &MultiHunkReaderWriter{hc, cancel, done.New(), nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMultiHunkConn(hc MultiHunkConn, cancel context.CancelFunc) net.Conn {
|
2021-03-17 17:55:51 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 04:10:19 +00:00
|
|
|
md, ok := metadata.FromIncomingContext(hc.Context())
|
|
|
|
if ok {
|
|
|
|
header := md.Get("x-real-ip")
|
|
|
|
if len(header) > 0 {
|
|
|
|
realip := xnet.ParseAddress(header[0])
|
|
|
|
if realip.Family().IsIP() {
|
|
|
|
rAddr = &net.TCPAddr{
|
|
|
|
IP: realip.IP(),
|
|
|
|
Port: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-14 15:02:07 +00:00
|
|
|
wrc := NewMultiHunkReadWriter(hc, cancel)
|
|
|
|
return cnc.NewConnection(
|
|
|
|
cnc.ConnectionInputMulti(wrc),
|
|
|
|
cnc.ConnectionOutputMulti(wrc),
|
|
|
|
cnc.ConnectionOnClose(wrc),
|
2021-03-17 17:55:51 +00:00
|
|
|
cnc.ConnectionRemoteAddr(rAddr),
|
2021-03-14 15:02:07 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MultiHunkReaderWriter) forceFetch() error {
|
|
|
|
hunk, err := h.hc.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newError("failed to fetch hunk from gRPC tunnel").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.buf = hunk.Data
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MultiHunkReaderWriter) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|
|
|
if h.done.Done() {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.forceFetch(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:57:14 +00:00
|
|
|
mb := make(buf.MultiBuffer, 0, len(h.buf))
|
2021-03-14 15:02:07 +00:00
|
|
|
for _, b := range h.buf {
|
2021-03-21 09:16:52 +00:00
|
|
|
if len(b) == 0 {
|
2021-03-14 15:02:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-21 09:16:52 +00:00
|
|
|
if cap(b) >= buf.Size {
|
|
|
|
mb = append(mb, buf.NewExisted(b))
|
|
|
|
} else {
|
|
|
|
nb := buf.New()
|
|
|
|
nb.Extend(int32(len(b)))
|
|
|
|
copy(nb.Bytes(), b)
|
|
|
|
|
|
|
|
mb = append(mb, nb)
|
|
|
|
}
|
2021-03-14 15:02:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return mb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MultiHunkReaderWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|
|
|
defer buf.ReleaseMulti(mb)
|
|
|
|
if h.done.Done() {
|
|
|
|
return io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
|
2021-03-21 09:16:52 +00:00
|
|
|
hunks := make([][]byte, 0, len(mb))
|
|
|
|
|
2021-03-14 15:02:07 +00:00
|
|
|
for _, b := range mb {
|
2021-03-21 09:16:52 +00:00
|
|
|
if b.Len() > 0 {
|
|
|
|
hunks = append(hunks, b.Bytes())
|
|
|
|
}
|
2021-03-14 15:02:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 09:16:52 +00:00
|
|
|
err := h.hc.Send(&MultiHunk{Data: hunks})
|
2021-03-14 15:02:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MultiHunkReaderWriter) Close() error {
|
|
|
|
if h.cancel != nil {
|
|
|
|
h.cancel()
|
|
|
|
}
|
|
|
|
if sc, match := h.hc.(StreamCloser); match {
|
|
|
|
return sc.CloseSend()
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.done.Close()
|
|
|
|
}
|