forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c45b24c
commit efc8c23
Showing
3 changed files
with
119 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
|
||
"v2ray.com/core/common/net" | ||
"v2ray.com/core/transport/ray" | ||
) | ||
|
||
// Dial provides an easy way for upstream caller to create net.Conn through V2Ray. | ||
// It dispatches the request to the given destination by the given V2Ray instance. | ||
// Since it is under a proxy context, the LocalAddr() and RemoteAddr() in returned net.Conn | ||
// will not show real addresses being used for communication. | ||
func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, error) { | ||
r, err := v.Dispatcher().Dispatch(ctx, dest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ray.NewConnection(r, &net.TCPAddr{IP: []byte{0, 0, 0, 0}}, &net.TCPAddr{IP: []byte{0, 0, 0, 0}}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package ray | ||
|
||
import ( | ||
"io" | ||
"net" | ||
"time" | ||
|
||
"v2ray.com/core/common/buf" | ||
) | ||
|
||
type connection struct { | ||
stream Ray | ||
closed bool | ||
localAddr net.Addr | ||
remoteAddr net.Addr | ||
|
||
reader *buf.BufferedReader | ||
writer buf.Writer | ||
} | ||
|
||
// NewConnection wraps a Ray into net.Conn. | ||
func NewConnection(stream InboundRay, localAddr net.Addr, remoteAddr net.Addr) net.Conn { | ||
return &connection{ | ||
stream: stream, | ||
localAddr: localAddr, | ||
remoteAddr: remoteAddr, | ||
reader: buf.NewBufferedReader(stream.InboundOutput()), | ||
writer: stream.InboundInput(), | ||
} | ||
} | ||
|
||
// Read implements net.Conn.Read(). | ||
func (c *connection) Read(b []byte) (int, error) { | ||
if c.closed { | ||
return 0, io.EOF | ||
} | ||
return c.reader.Read(b) | ||
} | ||
|
||
// ReadMultiBuffer implements buf.Reader. | ||
func (c *connection) ReadMultiBuffer() (buf.MultiBuffer, error) { | ||
return c.reader.ReadMultiBuffer() | ||
} | ||
|
||
// Write implements net.Conn.Write(). | ||
func (c *connection) Write(b []byte) (int, error) { | ||
if c.closed { | ||
return 0, io.ErrClosedPipe | ||
} | ||
|
||
l := len(b) | ||
mb := buf.NewMultiBufferCap(l/buf.Size + 1) | ||
mb.Write(b) | ||
return l, c.writer.WriteMultiBuffer(mb) | ||
} | ||
|
||
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error { | ||
if c.closed { | ||
return io.ErrClosedPipe | ||
} | ||
|
||
return c.writer.WriteMultiBuffer(mb) | ||
} | ||
|
||
// Close implements net.Conn.Close(). | ||
func (c *connection) Close() error { | ||
c.closed = true | ||
c.stream.InboundInput().Close() | ||
c.stream.InboundOutput().CloseError() | ||
return nil | ||
} | ||
|
||
// LocalAddr implements net.Conn.LocalAddr(). | ||
func (c *connection) LocalAddr() net.Addr { | ||
return c.localAddr | ||
} | ||
|
||
// RemoteAddr implements net.Conn.RemoteAddr(). | ||
func (c *connection) RemoteAddr() net.Addr { | ||
return c.remoteAddr | ||
} | ||
|
||
// SetDeadline implements net.Conn.SetDeadline(). | ||
func (c *connection) SetDeadline(t time.Time) error { | ||
return nil | ||
} | ||
|
||
// SetReadDeadline implements net.Conn.SetReadDeadline(). | ||
func (c *connection) SetReadDeadline(t time.Time) error { | ||
return nil | ||
} | ||
|
||
// SetWriteDeadline implement net.Conn.SetWriteDeadline(). | ||
func (c *connection) SetWriteDeadline(t time.Time) error { | ||
return nil | ||
} |