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"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2017-10-20 16:45:14 -04:00
|
|
|
"time"
|
2016-08-13 09:44:36 -04:00
|
|
|
|
2017-02-08 12:30:16 -05:00
|
|
|
"github.com/gorilla/websocket"
|
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"
|
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()
|
|
|
|
if len(forwardedAddrs) > 0 && forwardedAddrs[0].Family().Either(net.AddressFamilyIPv4, net.AddressFamilyIPv6) {
|
|
|
|
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
|
2017-02-26 08:38:41 -05:00
|
|
|
listener net.Listener
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
config *Config
|
2018-02-08 09:39:46 -05:00
|
|
|
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) {
|
2017-02-23 19:05:16 -05:00
|
|
|
networkSettings := internet.TransportSettingsFromContext(ctx)
|
2016-10-02 17:43:58 -04:00
|
|
|
wsSettings := networkSettings.(*Config)
|
2016-08-13 09:44:36 -04:00
|
|
|
|
2017-02-09 06:31:40 -05:00
|
|
|
l := &Listener{
|
2017-05-08 18:01:15 -04:00
|
|
|
config: wsSettings,
|
|
|
|
addConn: addConn,
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
2017-12-16 18:53:17 -05:00
|
|
|
if config := v2tls.ConfigFromContext(ctx); config != nil {
|
|
|
|
l.tlsConfig = config.GetTLSConfig()
|
2016-09-30 10:53:40 -04:00
|
|
|
}
|
2016-08-13 09:44:36 -04:00
|
|
|
|
2017-02-23 19:05:16 -05:00
|
|
|
err := l.listenws(address, port)
|
2016-08-13 09:44:36 -04:00
|
|
|
|
|
|
|
return l, err
|
|
|
|
}
|
|
|
|
|
2017-08-29 06:56:57 -04:00
|
|
|
func (ln *Listener) listenws(address net.Address, port net.Port) error {
|
2017-02-08 13:00:13 -05:00
|
|
|
netAddr := address.String() + ":" + strconv.Itoa(int(port.Value()))
|
|
|
|
var listener net.Listener
|
2017-02-09 06:31:40 -05:00
|
|
|
if ln.tlsConfig == nil {
|
2017-02-08 13:00:13 -05:00
|
|
|
l, err := net.Listen("tcp", netAddr)
|
2016-08-15 06:17:29 -04:00
|
|
|
if err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to listen TCP ", netAddr).Base(err)
|
2016-08-15 06:17:29 -04:00
|
|
|
}
|
2017-02-08 13:00:13 -05:00
|
|
|
listener = l
|
|
|
|
} else {
|
2017-02-09 06:31:40 -05:00
|
|
|
l, err := tls.Listen("tcp", netAddr, ln.tlsConfig)
|
2016-08-15 06:17:29 -04:00
|
|
|
if err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to listen TLS ", netAddr).Base(err)
|
2016-08-13 09:56:51 -04:00
|
|
|
}
|
2017-02-08 13:00:13 -05:00
|
|
|
listener = l
|
2016-08-13 09:56:51 -04:00
|
|
|
}
|
2017-02-09 06:31:40 -05:00
|
|
|
ln.listener = listener
|
2016-08-13 09:56:51 -04:00
|
|
|
|
2016-08-13 09:44:36 -04:00
|
|
|
go func() {
|
2017-10-21 03:45:27 -04:00
|
|
|
err := http.Serve(listener, &requestHandler{
|
2017-02-26 08:38:41 -05:00
|
|
|
path: ln.config.GetNormailzedPath(),
|
|
|
|
ln: ln,
|
2017-02-09 06:31:40 -05:00
|
|
|
})
|
2017-10-21 03:45:27 -04:00
|
|
|
if err != nil {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("failed to serve http for WebSocket").Base(err).AtWarning().WriteToLog()
|
2017-10-21 03:45:27 -04:00
|
|
|
}
|
2016-08-13 09:44:36 -04:00
|
|
|
}()
|
|
|
|
|
2017-02-08 13:00:13 -05:00
|
|
|
return nil
|
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() {
|
2017-01-12 06:54:34 -05:00
|
|
|
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_WebSocket, ListenWS))
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|