mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-17 18:06:15 -05:00
simplify websocket dialer and hub
This commit is contained in:
parent
95c469c1fd
commit
60f3562ac1
@ -24,6 +24,12 @@ type connection struct {
|
|||||||
mergingWriter buf.Writer
|
mergingWriter buf.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newConnection(conn *websocket.Conn) *connection {
|
||||||
|
return &connection{
|
||||||
|
wsc: conn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Read implements net.Conn.Read()
|
// Read implements net.Conn.Read()
|
||||||
func (c *connection) Read(b []byte) (int, error) {
|
func (c *connection) Read(b []byte) (int, error) {
|
||||||
for {
|
for {
|
||||||
|
@ -2,6 +2,7 @@ package websocket
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"v2ray.com/core/app/log"
|
"v2ray.com/core/app/log"
|
||||||
@ -30,14 +31,13 @@ func dialWebsocket(ctx context.Context, dest net.Destination) (net.Conn, error)
|
|||||||
src := internet.DialerSourceFromContext(ctx)
|
src := internet.DialerSourceFromContext(ctx)
|
||||||
wsSettings := internet.TransportSettingsFromContext(ctx).(*Config)
|
wsSettings := internet.TransportSettingsFromContext(ctx).(*Config)
|
||||||
|
|
||||||
commonDial := func(network, addr string) (net.Conn, error) {
|
dialer := &websocket.Dialer{
|
||||||
|
NetDial: func(network, addr string) (net.Conn, error) {
|
||||||
return internet.DialSystem(ctx, src, dest)
|
return internet.DialSystem(ctx, src, dest)
|
||||||
}
|
},
|
||||||
|
|
||||||
dialer := websocket.Dialer{
|
|
||||||
NetDial: commonDial,
|
|
||||||
ReadBufferSize: 32 * 1024,
|
ReadBufferSize: 32 * 1024,
|
||||||
WriteBufferSize: 32 * 1024,
|
WriteBufferSize: 32 * 1024,
|
||||||
|
HandshakeTimeout: time.Second * 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol := "ws"
|
protocol := "ws"
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"v2ray.com/core/app/log"
|
"v2ray.com/core/app/log"
|
||||||
@ -20,18 +21,24 @@ type requestHandler struct {
|
|||||||
ln *Listener
|
ln *Listener
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var upgrader = &websocket.Upgrader{
|
||||||
|
ReadBufferSize: 32 * 1024,
|
||||||
|
WriteBufferSize: 32 * 1024,
|
||||||
|
HandshakeTimeout: time.Second * 8,
|
||||||
|
}
|
||||||
|
|
||||||
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
if request.URL.Path != h.path {
|
if request.URL.Path != h.path {
|
||||||
writer.WriteHeader(http.StatusNotFound)
|
writer.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn, err := converttovws(writer, request)
|
conn, err := upgrader.Upgrade(writer, request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Trace(newError("failed to convert to WebSocket connection").Base(err))
|
log.Trace(newError("failed to convert to WebSocket connection").Base(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
h.ln.addConn(h.ln.ctx, internet.Connection(conn))
|
h.ln.addConn(h.ln.ctx, newConnection(conn))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Listener struct {
|
type Listener struct {
|
||||||
@ -92,20 +99,6 @@ func (ln *Listener) listenws(address net.Address, port net.Port) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func converttovws(w http.ResponseWriter, r *http.Request) (*connection, error) {
|
|
||||||
var upgrader = websocket.Upgrader{
|
|
||||||
ReadBufferSize: 32 * 1024,
|
|
||||||
WriteBufferSize: 32 * 1024,
|
|
||||||
}
|
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &connection{wsc: conn}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ln *Listener) Addr() net.Addr {
|
func (ln *Listener) Addr() net.Addr {
|
||||||
return ln.listener.Addr()
|
return ln.listener.Addr()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user