2017-01-04 07:29:41 -05:00
|
|
|
package internal
|
2016-04-27 17:01:31 -04:00
|
|
|
|
|
|
|
import (
|
2016-06-14 16:54:08 -04:00
|
|
|
"io"
|
2016-04-27 17:01:31 -04:00
|
|
|
"net"
|
2017-01-02 01:43:02 -05:00
|
|
|
"sync"
|
2016-04-27 17:01:31 -04:00
|
|
|
"time"
|
|
|
|
|
2017-01-04 07:29:41 -05:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2016-06-11 19:30:56 -04:00
|
|
|
)
|
|
|
|
|
2017-01-04 07:29:41 -05:00
|
|
|
// ConnectionID is the ID of a connection.
|
|
|
|
type ConnectionID struct {
|
|
|
|
Local v2net.Address
|
|
|
|
Remote v2net.Address
|
|
|
|
RemotePort v2net.Port
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnectionID creates a new ConnectionId.
|
|
|
|
func NewConnectionID(source v2net.Address, dest v2net.Destination) ConnectionID {
|
|
|
|
return ConnectionID{
|
|
|
|
Local: source,
|
|
|
|
Remote: dest.Address,
|
|
|
|
RemotePort: dest.Port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Reuser struct {
|
|
|
|
userEnabled bool
|
|
|
|
appEnable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReuseConnection(reuse bool) *Reuser {
|
|
|
|
return &Reuser{
|
|
|
|
userEnabled: reuse,
|
|
|
|
appEnable: reuse,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection is an implementation of net.Conn with re-usability.
|
2016-05-02 17:53:16 -04:00
|
|
|
type Connection struct {
|
2017-01-02 01:43:02 -05:00
|
|
|
sync.RWMutex
|
2017-01-04 07:29:41 -05:00
|
|
|
id ConnectionID
|
2016-05-02 17:53:16 -04:00
|
|
|
conn net.Conn
|
2017-01-04 07:29:41 -05:00
|
|
|
listener ConnectionRecyler
|
|
|
|
reuser *Reuser
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2017-01-04 07:29:41 -05:00
|
|
|
func NewConnection(id ConnectionID, conn net.Conn, manager ConnectionRecyler, reuser *Reuser) *Connection {
|
2016-06-14 16:54:08 -04:00
|
|
|
return &Connection{
|
2016-11-24 17:16:05 -05:00
|
|
|
id: id,
|
2016-06-14 16:54:08 -04:00
|
|
|
conn: conn,
|
|
|
|
listener: manager,
|
2017-01-04 07:29:41 -05:00
|
|
|
reuser: reuser,
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) Read(b []byte) (int, error) {
|
2017-01-02 01:43:02 -05:00
|
|
|
conn := v.underlyingConn()
|
|
|
|
if conn == nil {
|
2016-06-14 16:54:08 -04:00
|
|
|
return 0, io.EOF
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2017-01-02 01:43:02 -05:00
|
|
|
return conn.Read(b)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) Write(b []byte) (int, error) {
|
2017-01-02 01:43:02 -05:00
|
|
|
conn := v.underlyingConn()
|
|
|
|
if conn == nil {
|
2016-06-14 16:54:08 -04:00
|
|
|
return 0, io.ErrClosedPipe
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
2017-01-02 01:43:02 -05:00
|
|
|
return conn.Write(b)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2017-01-04 07:29:41 -05:00
|
|
|
// Close implements net.Conn.Close(). If the connection is reusable, the underlying connection will be recycled.
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) Close() error {
|
2017-01-02 01:43:02 -05:00
|
|
|
if v == nil {
|
|
|
|
return io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
if v.conn == nil {
|
2016-06-14 16:54:08 -04:00
|
|
|
return io.ErrClosedPipe
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.Reusable() {
|
|
|
|
v.listener.Put(v.id, v.conn)
|
2016-05-29 16:33:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
err := v.conn.Close()
|
|
|
|
v.conn = nil
|
2016-06-14 16:54:08 -04:00
|
|
|
return err
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) LocalAddr() net.Addr {
|
2017-01-02 01:43:02 -05:00
|
|
|
conn := v.underlyingConn()
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return conn.LocalAddr()
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) RemoteAddr() net.Addr {
|
2017-01-02 01:43:02 -05:00
|
|
|
conn := v.underlyingConn()
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return conn.RemoteAddr()
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SetDeadline(t time.Time) error {
|
2017-01-02 01:43:02 -05:00
|
|
|
conn := v.underlyingConn()
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return conn.SetDeadline(t)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SetReadDeadline(t time.Time) error {
|
2017-01-02 01:43:02 -05:00
|
|
|
conn := v.underlyingConn()
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return conn.SetReadDeadline(t)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SetWriteDeadline(t time.Time) error {
|
2017-01-02 01:43:02 -05:00
|
|
|
conn := v.underlyingConn()
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return conn.SetWriteDeadline(t)
|
2016-04-27 17:01:31 -04:00
|
|
|
}
|
2016-05-29 16:33:04 -04:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SetReusable(reusable bool) {
|
2017-01-02 01:43:02 -05:00
|
|
|
if v == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-04 07:29:41 -05:00
|
|
|
v.reuser.appEnable = reusable
|
2016-05-29 16:33:04 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) Reusable() bool {
|
2017-01-02 01:43:02 -05:00
|
|
|
if v == nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-01-04 07:29:41 -05:00
|
|
|
return v.reuser.userEnabled && v.reuser.appEnable
|
2016-05-29 16:33:04 -04:00
|
|
|
}
|
2016-06-11 19:30:56 -04:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Connection) SysFd() (int, error) {
|
2017-01-02 01:43:02 -05:00
|
|
|
conn := v.underlyingConn()
|
|
|
|
if conn == nil {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
2017-01-04 07:29:41 -05:00
|
|
|
return GetSysFd(conn)
|
2017-01-02 01:43:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Connection) underlyingConn() net.Conn {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v.RLock()
|
|
|
|
defer v.RUnlock()
|
|
|
|
|
|
|
|
return v.conn
|
2016-06-11 19:30:56 -04:00
|
|
|
}
|