1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/transport/internet/websocket/hub.go

177 lines
4.7 KiB
Go
Raw Normal View History

2019-02-01 14:08:21 -05:00
// +build !confonly
2016-12-22 18:30:46 -05:00
package websocket
2016-08-13 09:44:36 -04:00
import (
2021-04-05 14:34:22 -04:00
"bytes"
2017-02-23 19:05:16 -05:00
"context"
2016-09-30 10:53:40 -04:00
"crypto/tls"
2021-04-05 14:34:22 -04:00
"encoding/base64"
"io"
2016-08-13 09:44:36 -04:00
"net/http"
2021-04-05 14:34:22 -04:00
"strings"
2016-08-13 09:44:36 -04:00
"sync"
2017-10-20 16:45:14 -04:00
"time"
2018-07-13 06:17:52 -04:00
"github.com/gorilla/websocket"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/net"
http_proto "github.com/v2fly/v2ray-core/v4/common/protocol/http"
"github.com/v2fly/v2ray-core/v4/common/session"
"github.com/v2fly/v2ray-core/v4/transport/internet"
v2tls "github.com/v2fly/v2ray-core/v4/transport/internet/tls"
2016-08-13 09:44:36 -04:00
)
2017-02-09 06:31:40 -05:00
type requestHandler struct {
2021-04-05 14:34:22 -04:00
path string
ln *Listener
earlyDataEnabled bool
2017-02-09 06:31:40 -05:00
}
2017-10-20 16:45:14 -04:00
var upgrader = &websocket.Upgrader{
2017-12-15 20:04:51 -05:00
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
HandshakeTimeout: time.Second * 4,
2020-06-28 03:54:53 -04:00
CheckOrigin: func(r *http.Request) bool {
return true
},
2017-10-20 16:45:14 -04:00
}
2017-02-09 06:31:40 -05:00
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
2021-04-05 14:34:22 -04:00
var earlyData io.Reader
if !h.earlyDataEnabled {
if request.URL.Path != h.path {
writer.WriteHeader(http.StatusNotFound)
return
}
} else {
if strings.HasPrefix(request.URL.RequestURI(), h.path) {
earlyDataStr := request.URL.RequestURI()[len(h.path):]
earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr)))
} else {
writer.WriteHeader(http.StatusNotFound)
return
}
2017-02-09 06:31:40 -05:00
}
2021-04-05 14:34:22 -04:00
2017-10-20 16:45:14 -04:00
conn, err := upgrader.Upgrade(writer, request, nil)
2017-02-09 06:31:40 -05:00
if err != nil {
2017-12-19 15:28:12 -05:00
newError("failed to convert to WebSocket connection").Base(err).WriteToLog()
2017-02-09 06:31:40 -05:00
return
}
forwardedAddrs := http_proto.ParseXForwardedFor(request.Header)
remoteAddr := conn.RemoteAddr()
2018-11-16 05:13:36 -05:00
if len(forwardedAddrs) > 0 && forwardedAddrs[0].Family().IsIP() {
2020-10-29 03:30:38 -04:00
remoteAddr = &net.TCPAddr{
IP: forwardedAddrs[0].IP(),
Port: int(0),
}
}
2021-04-05 14:34:22 -04:00
if earlyData == nil {
h.ln.addConn(newConnection(conn, remoteAddr))
} else {
h.ln.addConn(newConnectionWithEarlyData(conn, remoteAddr, earlyData))
}
2017-02-09 06:31:40 -05:00
}
type Listener struct {
2016-08-13 09:44:36 -04:00
sync.Mutex
server http.Server
2018-09-10 07:23:27 -04:00
listener net.Listener
config *Config
addConn internet.ConnHandler
2020-10-29 03:30:38 -04:00
locker *internet.FileLocker // for unix domain socket
2016-08-13 09:44:36 -04:00
}
func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (internet.Listener, error) {
2020-10-29 03:30:38 -04:00
l := &Listener{
addConn: addConn,
}
wsSettings := streamSettings.ProtocolSettings.(*Config)
2020-10-29 03:30:38 -04:00
l.config = wsSettings
if l.config != nil {
if streamSettings.SocketSettings == nil {
streamSettings.SocketSettings = &internet.SocketConfig{}
}
streamSettings.SocketSettings.AcceptProxyProtocol = l.config.AcceptProxyProtocol
}
var listener net.Listener
var err error
if port == net.Port(0) { // unix
2020-10-29 03:30:38 -04:00
listener, err = internet.ListenSystem(ctx, &net.UnixAddr{
Name: address.Domain(),
Net: "unix",
}, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to listen unix domain socket(for WS) on ", address).Base(err)
}
newError("listening unix domain socket(for WS) on ", address).WriteToLog(session.ExportIDToError(ctx))
locker := ctx.Value(address.Domain())
if locker != nil {
l.locker = locker.(*internet.FileLocker)
}
} else { // tcp
2020-10-29 03:30:38 -04:00
listener, err = internet.ListenSystem(ctx, &net.TCPAddr{
IP: address.IP(),
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to listen TCP(for WS) on ", address, ":", port).Base(err)
}
newError("listening TCP(for WS) on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx))
}
2016-08-13 09:44:36 -04:00
2020-10-29 03:30:38 -04:00
if streamSettings.SocketSettings != nil && streamSettings.SocketSettings.AcceptProxyProtocol {
2020-08-26 01:01:35 -04:00
newError("accepting PROXY protocol").AtWarning().WriteToLog(session.ExportIDToError(ctx))
2016-09-30 10:53:40 -04:00
}
2016-08-13 09:44:36 -04:00
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
if tlsConfig := config.GetTLSConfig(); tlsConfig != nil {
listener = tls.NewListener(listener, tlsConfig)
}
2018-09-10 07:23:27 -04:00
}
2016-08-13 09:44:36 -04:00
2020-10-29 03:30:38 -04:00
l.listener = listener
2021-04-05 14:34:22 -04:00
var useEarlyData = false
if wsSettings.MaxEarlyData != 0 {
useEarlyData = true
}
2016-08-13 09:56:51 -04:00
l.server = http.Server{
Handler: &requestHandler{
2021-04-05 14:34:22 -04:00
path: wsSettings.GetNormalizedPath(),
ln: l,
earlyDataEnabled: useEarlyData,
},
ReadHeaderTimeout: time.Second * 4,
MaxHeaderBytes: 2048,
}
2016-08-13 09:44:36 -04:00
go func() {
if err := l.server.Serve(l.listener); err != nil {
2018-09-10 07:23:27 -04:00
newError("failed to serve http for WebSocket").Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
2017-10-21 03:45:27 -04:00
}
2016-08-13 09:44:36 -04:00
}()
2018-09-10 07:23:27 -04:00
return l, err
}
2017-10-21 03:45:27 -04:00
// Addr implements net.Listener.Addr().
2017-02-09 06:31:40 -05:00
func (ln *Listener) Addr() net.Addr {
return ln.listener.Addr()
2016-08-13 09:44:36 -04:00
}
2017-10-21 03:45:27 -04:00
// Close implements net.Listener.Close().
2017-02-09 06:31:40 -05:00
func (ln *Listener) Close() error {
2020-10-29 03:30:38 -04:00
if ln.locker != nil {
ln.locker.Release()
}
2017-02-27 09:13:22 -05:00
return ln.listener.Close()
2016-08-13 09:44:36 -04:00
}
func init() {
2018-08-06 07:48:35 -04:00
common.Must(internet.RegisterTransportListener(protocolName, ListenWS))
2016-08-13 09:44:36 -04:00
}