2021-03-14 15:02:07 +00:00
|
|
|
package encoding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
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 HunkConn interface {
|
2021-03-17 17:55:51 +00:00
|
|
|
Context() context.Context
|
2021-03-14 15:02:07 +00:00
|
|
|
Send(*Hunk) error
|
|
|
|
Recv() (*Hunk, error)
|
|
|
|
SendMsg(m interface{}) error
|
|
|
|
RecvMsg(m interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type StreamCloser interface {
|
|
|
|
CloseSend() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type HunkReaderWriter struct {
|
|
|
|
hc HunkConn
|
|
|
|
cancel context.CancelFunc
|
|
|
|
done *done.Instance
|
|
|
|
|
|
|
|
buf []byte
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHunkReadWriter(hc HunkConn, cancel context.CancelFunc) *HunkReaderWriter {
|
|
|
|
return &HunkReaderWriter{hc, cancel, done.New(), nil, 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHunkConn(hc HunkConn, 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 := NewHunkReadWriter(hc, cancel)
|
|
|
|
return cnc.NewConnection(
|
|
|
|
cnc.ConnectionInput(wrc),
|
|
|
|
cnc.ConnectionOutput(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 *HunkReaderWriter) forceFetch() error {
|
|
|
|
hunk, err := h.hc.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to fetch hunk from gRPC tunnel").Base(err)
|
2021-03-14 15:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.buf = hunk.Data
|
|
|
|
h.index = 0
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HunkReaderWriter) Read(buf []byte) (int, error) {
|
|
|
|
if h.done.Done() {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.index >= len(h.buf) {
|
|
|
|
if err := h.forceFetch(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n := copy(buf, h.buf[h.index:])
|
|
|
|
h.index += n
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HunkReaderWriter) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|
|
|
if h.done.Done() {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
if h.index >= len(h.buf) {
|
|
|
|
if err := h.forceFetch(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 09:16:52 +00:00
|
|
|
if cap(h.buf) >= buf.Size {
|
2021-03-14 15:02:07 +00:00
|
|
|
b := h.buf
|
|
|
|
h.index = len(h.buf)
|
|
|
|
return buf.MultiBuffer{buf.NewExisted(b)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := buf.New()
|
|
|
|
_, err := b.ReadFrom(h)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.MultiBuffer{b}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HunkReaderWriter) Write(buf []byte) (int, error) {
|
|
|
|
if h.done.Done() {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
|
|
|
|
err := h.hc.Send(&Hunk{Data: buf[:]})
|
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return 0, errors.New("failed to send data over gRPC tunnel").Base(err)
|
2021-03-14 15:02:07 +00:00
|
|
|
}
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HunkReaderWriter) Close() error {
|
|
|
|
if h.cancel != nil {
|
|
|
|
h.cancel()
|
|
|
|
}
|
|
|
|
if sc, match := h.hc.(StreamCloser); match {
|
|
|
|
return sc.CloseSend()
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.done.Close()
|
|
|
|
}
|