1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/transport/internet/websocket/connection.go

204 lines
4.8 KiB
Go
Raw Normal View History

2016-12-22 18:30:46 -05:00
package websocket
2016-08-13 09:44:36 -04:00
import (
2021-03-19 18:36:11 -04:00
"context"
2016-08-13 09:44:36 -04:00
"io"
"net"
"time"
"github.com/gorilla/websocket"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/errors"
"github.com/v2fly/v2ray-core/v5/common/serial"
2016-08-13 09:44:36 -04:00
)
2021-05-19 17:28:52 -04:00
var _ buf.Writer = (*connection)(nil)
2017-04-20 05:00:15 -04:00
2017-02-16 10:53:31 -05:00
// connection is a wrapper for net.Conn over WebSocket connection.
type connection struct {
2018-12-27 10:36:48 -05:00
conn *websocket.Conn
reader io.Reader
remoteAddr net.Addr
2021-03-19 18:36:11 -04:00
shouldWait bool
delayedDialFinish context.Context
finishedDial context.CancelFunc
dialer DelayedDialer
}
type DelayedDialer interface {
Dial(earlyData []byte) (*websocket.Conn, error)
2016-08-13 09:44:36 -04:00
}
func newConnection(conn *websocket.Conn, remoteAddr net.Addr) *connection {
2017-10-20 16:45:14 -04:00
return &connection{
conn: conn,
remoteAddr: remoteAddr,
2017-10-20 16:45:14 -04:00
}
}
2021-03-19 18:36:11 -04:00
func newConnectionWithEarlyData(conn *websocket.Conn, remoteAddr net.Addr, earlyData io.Reader) *connection {
return &connection{
conn: conn,
remoteAddr: remoteAddr,
reader: earlyData,
}
}
func newConnectionWithDelayedDial(dialer DelayedDialer) *connection {
2021-12-22 09:51:11 -05:00
delayedDialContext, cancelFunc := context.WithCancel(context.Background())
2021-03-19 18:36:11 -04:00
return &connection{
shouldWait: true,
delayedDialFinish: delayedDialContext,
2021-12-22 09:51:11 -05:00
finishedDial: cancelFunc,
2021-03-19 18:36:11 -04:00
dialer: dialer,
}
}
func newRelayedConnectionWithDelayedDial(dialer DelayedDialerForwarded) *connectionForwarder {
2021-12-22 09:51:11 -05:00
delayedDialContext, cancelFunc := context.WithCancel(context.Background())
2021-03-19 18:36:11 -04:00
return &connectionForwarder{
shouldWait: true,
delayedDialFinish: delayedDialContext,
2021-12-22 09:51:11 -05:00
finishedDial: cancelFunc,
2021-03-19 18:36:11 -04:00
dialer: dialer,
}
}
func newRelayedConnection(conn io.ReadWriteCloser) *connectionForwarder {
return &connectionForwarder{
ReadWriteCloser: conn,
shouldWait: false,
}
}
2017-02-16 10:53:31 -05:00
// Read implements net.Conn.Read()
func (c *connection) Read(b []byte) (int, error) {
2017-02-09 05:42:17 -05:00
for {
reader, err := c.getReader()
2016-08-15 06:56:33 -04:00
if err != nil {
return 0, err
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
nBytes, err := reader.Read(b)
if errors.Cause(err) == io.EOF {
2017-02-09 06:31:26 -05:00
c.reader = nil
2017-02-09 05:42:17 -05:00
continue
2016-08-15 06:56:33 -04:00
}
2017-02-09 05:42:17 -05:00
return nBytes, err
2016-08-13 09:44:36 -04:00
}
}
2017-02-16 10:53:31 -05:00
func (c *connection) getReader() (io.Reader, error) {
2021-03-19 18:36:11 -04:00
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
return nil, newError("unable to read delayed dial websocket connection as it do not exist")
}
}
2017-02-09 05:42:17 -05:00
if c.reader != nil {
return c.reader, nil
2016-08-13 09:44:36 -04:00
}
2016-08-15 06:59:14 -04:00
2017-10-21 03:45:27 -04:00
_, reader, err := c.conn.NextReader()
2016-08-15 06:59:14 -04:00
if err != nil {
2017-02-09 05:42:17 -05:00
return nil, err
2016-08-15 06:59:14 -04:00
}
2017-02-09 05:42:17 -05:00
c.reader = reader
return reader, nil
}
2017-04-21 09:36:05 -04:00
// Write implements io.Writer.
2017-02-16 10:53:31 -05:00
func (c *connection) Write(b []byte) (int, error) {
2021-03-19 18:36:11 -04:00
if c.shouldWait {
var err error
c.conn, err = c.dialer.Dial(b)
c.finishedDial()
if err != nil {
return 0, newError("Unable to proceed with delayed write").Base(err)
}
c.remoteAddr = c.conn.RemoteAddr()
c.shouldWait = false
return len(b), nil
}
2017-10-21 03:45:27 -04:00
if err := c.conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
2016-08-15 06:59:14 -04:00
return 0, err
}
2017-02-09 05:42:17 -05:00
return len(b), nil
2016-08-15 06:59:14 -04:00
}
2017-04-23 07:41:52 -04:00
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
2018-12-27 10:36:48 -05:00
mb = buf.Compact(mb)
2018-12-27 11:00:34 -05:00
mb, err := buf.WriteMultiBuffer(c, mb)
buf.ReleaseMulti(mb)
return err
}
2017-02-16 10:53:31 -05:00
func (c *connection) Close() error {
2021-03-19 18:36:11 -04:00
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
return newError("unable to close delayed dial websocket connection as it do not exist")
}
}
2018-05-31 05:55:11 -04:00
var errors []interface{}
if err := c.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second*5)); err != nil {
errors = append(errors, err)
}
if err := c.conn.Close(); err != nil {
errors = append(errors, err)
}
if len(errors) > 0 {
return newError("failed to close connection").Base(newError(serial.Concat(errors...)))
}
return nil
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
2017-02-16 10:53:31 -05:00
func (c *connection) LocalAddr() net.Addr {
2021-03-19 18:36:11 -04:00
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
newError("websocket transport is not materialized when LocalAddr() is called").AtWarning().WriteToLog()
return &net.UnixAddr{
Name: "@placeholder",
Net: "unix",
}
}
}
2017-10-21 03:45:27 -04:00
return c.conn.LocalAddr()
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
2017-02-16 10:53:31 -05:00
func (c *connection) RemoteAddr() net.Addr {
return c.remoteAddr
2016-08-13 09:44:36 -04:00
}
2017-02-09 05:42:17 -05:00
2017-02-16 10:53:31 -05:00
func (c *connection) SetDeadline(t time.Time) error {
2017-02-09 05:42:17 -05:00
if err := c.SetReadDeadline(t); err != nil {
2017-01-04 07:29:41 -05:00
return err
}
2017-02-09 05:42:17 -05:00
return c.SetWriteDeadline(t)
2016-08-13 09:44:36 -04:00
}
2016-08-15 05:53:11 -04:00
2017-02-16 10:53:31 -05:00
func (c *connection) SetReadDeadline(t time.Time) error {
2021-03-19 18:36:11 -04:00
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
newError("websocket transport is not materialized when SetReadDeadline() is called").AtWarning().WriteToLog()
return nil
}
}
2017-10-21 03:45:27 -04:00
return c.conn.SetReadDeadline(t)
2016-08-13 09:44:36 -04:00
}
2017-02-16 10:53:31 -05:00
func (c *connection) SetWriteDeadline(t time.Time) error {
2021-03-19 18:36:11 -04:00
if c.shouldWait {
<-c.delayedDialFinish.Done()
if c.conn == nil {
newError("websocket transport is not materialized when SetWriteDeadline() is called").AtWarning().WriteToLog()
return nil
}
}
2017-10-21 03:45:27 -04:00
return c.conn.SetWriteDeadline(t)
2016-08-13 09:44:36 -04:00
}