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"
|
2018-04-20 16:45:44 -04:00
|
|
|
|
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,
|
2017-10-20 16:45:14 -04:00
|
|
|
HandshakeTimeout: time.Second * 8,
|
|
|
|
}
|
|
|
|
|
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
|
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-02-08 09:39:46 -05:00
|
|
|
func ListenWS(ctx context.Context, address net.Address, port net.Port, addConn internet.ConnHandler) (internet.Listener, error) {
|
2018-09-07 08:50:25 -04:00
|
|
|
networkSettings := internet.StreamSettingsFromContext(ctx)
|
|
|
|
wsSettings := networkSettings.ProtocolSettings.(*Config)
|
2016-08-13 09:44:36 -04:00
|
|
|
|
2018-09-10 07:23:27 -04:00
|
|
|
var tlsConfig *tls.Config
|
2017-12-16 18:53:17 -05:00
|
|
|
if config := v2tls.ConfigFromContext(ctx); 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
|
|
|
|
2018-09-10 07:23:27 -04:00
|
|
|
listener, err := listenTCP(ctx, address, port, tlsConfig)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-13 09:44:36 -04:00
|
|
|
go func() {
|
2018-09-10 07:23:27 -04:00
|
|
|
if err := l.serve(); err != nil {
|
|
|
|
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) (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),
|
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ln *Listener) serve() error {
|
|
|
|
return http.Serve(ln.listener, &requestHandler{
|
|
|
|
path: ln.config.GetNormalizedPath(),
|
|
|
|
ln: ln,
|
|
|
|
})
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|