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
|
|
|
|
2019-10-18 22:05:40 -04:00
|
|
|
"github.com/gorilla/websocket"
|
2020-08-26 00:29:12 -04:00
|
|
|
"github.com/pires/go-proxyproto"
|
|
|
|
|
2017-01-03 09:16:48 -05:00
|
|
|
"v2ray.com/core/common"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2017-12-18 14:34:00 -05:00
|
|
|
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,
|
2019-01-10 04:40:51 -05:00
|
|
|
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) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:34:00 -05:00
|
|
|
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() {
|
2017-12-18 14:34:00 -05:00
|
|
|
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
|
2019-01-10 04:40:51 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (internet.Listener, error) {
|
2020-08-26 00:29:12 -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)
|
|
|
|
}
|
2020-08-26 01:01:35 -04:00
|
|
|
newError("listening TCP(for WS) on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx))
|
2020-08-26 00:29:12 -04:00
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
wsSettings := streamSettings.ProtocolSettings.(*Config)
|
2016-08-13 09:44:36 -04:00
|
|
|
|
2020-08-26 00:29:12 -04:00
|
|
|
if wsSettings.AcceptProxyProtocol {
|
|
|
|
policyFunc := func(upstream net.Addr) (proxyproto.Policy, error) { return proxyproto.REQUIRE, nil }
|
|
|
|
listener = &proxyproto.Listener{Listener: listener, Policy: policyFunc}
|
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
|
|
|
|
2020-08-26 00:29:12 -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
|
|
|
|
2018-09-10 07:23:27 -04:00
|
|
|
l := &Listener{
|
|
|
|
config: wsSettings,
|
|
|
|
addConn: addConn,
|
|
|
|
listener: listener,
|
2016-08-13 09:56:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-10 04:40:51 -05: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() {
|
2019-01-10 04:40:51 -05:00
|
|
|
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 {
|
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
|
|
|
}
|