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

126 lines
3.0 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 (
2017-02-23 19:05:16 -05:00
"context"
2016-09-30 10:53:40 -04:00
"crypto/tls"
2016-08-13 09:44:36 -04:00
"net/http"
"sync"
2017-10-20 16:45:14 -04:00
"time"
2018-07-13 06:17:52 -04:00
"github.com/gorilla/websocket"
2017-01-03 09:16:48 -05:00
"v2ray.com/core/common"
"v2ray.com/core/common/net"
http_proto "v2ray.com/core/common/protocol/http"
2018-09-10 07:23:27 -04:00
"v2ray.com/core/common/session"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/transport/internet"
2017-02-08 12:30:16 -05:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-08-13 09:44:36 -04:00
)
2017-02-09 06:31:40 -05:00
type requestHandler struct {
2017-02-26 08:38:41 -05:00
path string
ln *Listener
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,
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) {
if request.URL.Path != h.path {
writer.WriteHeader(http.StatusNotFound)
return
}
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() {
remoteAddr.(*net.TCPAddr).IP = forwardedAddrs[0].IP()
}
2018-02-08 09:39:46 -05:00
h.ln.addConn(newConnection(conn, remoteAddr))
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
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) {
wsSettings := streamSettings.ProtocolSettings.(*Config)
2016-08-13 09:44:36 -04:00
2018-09-10 07:23:27 -04:00
var tlsConfig *tls.Config
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
2018-09-10 07:23:27 -04:00
tlsConfig = config.GetTLSConfig()
2016-09-30 10:53:40 -04:00
}
2016-08-13 09:44:36 -04:00
listener, err := listenTCP(ctx, address, port, tlsConfig, streamSettings.SocketSettings)
2018-09-10 07:23:27 -04:00
if err != nil {
return nil, err
}
2016-08-13 09:44:36 -04:00
2018-09-10 07:23:27 -04:00
l := &Listener{
config: wsSettings,
addConn: addConn,
listener: listener,
2016-08-13 09:56:51 -04:00
}
l.server = http.Server{
Handler: &requestHandler{
path: wsSettings.GetNormalizedPath(),
ln: l,
},
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
}
func listenTCP(ctx context.Context, address net.Address, port net.Port, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (net.Listener, error) {
2018-09-15 15:35:32 -04:00
listener, err := internet.ListenSystem(ctx, &net.TCPAddr{
2018-09-10 07:23:27 -04:00
IP: address.IP(),
Port: int(port),
}, sockopt)
2018-09-10 07:23:27 -04:00
if err != nil {
return nil, newError("failed to listen TCP on", address, ":", port).Base(err)
}
if tlsConfig != nil {
return tls.NewListener(listener, tlsConfig), nil
}
return listener, nil
}
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 {
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
}