v2ray-core/transport/internet/websocket/hub.go

128 lines
2.7 KiB
Go
Raw Normal View History

2016-12-22 23:30:46 +00:00
package websocket
2016-08-13 13:44:36 +00:00
import (
2017-02-24 00:05:16 +00:00
"context"
2016-09-30 14:53:40 +00:00
"crypto/tls"
2016-08-13 13:44:36 +00:00
"net"
"net/http"
"strconv"
"sync"
"time"
2017-02-08 17:30:16 +00:00
"github.com/gorilla/websocket"
"v2ray.com/core/app/log"
2017-01-03 14:16:48 +00:00
"v2ray.com/core/common"
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
2017-02-08 17:30:16 +00:00
v2tls "v2ray.com/core/transport/internet/tls"
2016-08-13 13:44:36 +00:00
)
2017-02-09 11:31:40 +00:00
type requestHandler struct {
2017-02-26 13:38:41 +00:00
path string
ln *Listener
2017-02-09 11:31:40 +00:00
}
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != h.path {
writer.WriteHeader(http.StatusNotFound)
return
}
conn, err := converttovws(writer, request)
if err != nil {
2017-04-09 11:30:46 +00:00
log.Trace(newError("failed to convert to WebSocket connection").Base(err))
2017-02-09 11:31:40 +00:00
return
}
select {
2017-02-27 14:13:22 +00:00
case <-h.ln.ctx.Done():
conn.Close()
case h.ln.conns <- internet.Connection(conn):
2017-02-26 13:38:41 +00:00
case <-time.After(time.Second * 5):
2017-02-09 11:31:40 +00:00
conn.Close()
}
}
type Listener struct {
2016-08-13 13:44:36 +00:00
sync.Mutex
2017-02-27 14:13:22 +00:00
ctx context.Context
2017-02-26 13:38:41 +00:00
listener net.Listener
tlsConfig *tls.Config
config *Config
conns chan<- internet.Connection
2016-08-13 13:44:36 +00:00
}
2017-02-26 13:38:41 +00:00
func ListenWS(ctx context.Context, address v2net.Address, port v2net.Port, conns chan<- internet.Connection) (internet.Listener, error) {
2017-02-24 00:05:16 +00:00
networkSettings := internet.TransportSettingsFromContext(ctx)
2016-10-02 21:43:58 +00:00
wsSettings := networkSettings.(*Config)
2016-08-13 13:44:36 +00:00
2017-02-09 11:31:40 +00:00
l := &Listener{
2017-02-27 14:13:22 +00:00
ctx: ctx,
2017-02-26 13:38:41 +00:00
config: wsSettings,
conns: conns,
2016-08-13 13:44:36 +00:00
}
2017-02-24 00:05:16 +00:00
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
2016-10-16 12:22:21 +00:00
tlsConfig, ok := securitySettings.(*v2tls.Config)
if ok {
l.tlsConfig = tlsConfig.GetTLSConfig()
}
2016-09-30 14:53:40 +00:00
}
2016-08-13 13:44:36 +00:00
2017-02-24 00:05:16 +00:00
err := l.listenws(address, port)
2016-08-13 13:44:36 +00:00
return l, err
}
2017-02-09 11:31:40 +00:00
func (ln *Listener) listenws(address v2net.Address, port v2net.Port) error {
2017-02-08 18:00:13 +00:00
netAddr := address.String() + ":" + strconv.Itoa(int(port.Value()))
var listener net.Listener
2017-02-09 11:31:40 +00:00
if ln.tlsConfig == nil {
2017-02-08 18:00:13 +00:00
l, err := net.Listen("tcp", netAddr)
2016-08-15 10:17:29 +00:00
if err != nil {
2017-04-08 23:43:25 +00:00
return newError("failed to listen TCP ", netAddr).Base(err)
2016-08-15 10:17:29 +00:00
}
2017-02-08 18:00:13 +00:00
listener = l
} else {
2017-02-09 11:31:40 +00:00
l, err := tls.Listen("tcp", netAddr, ln.tlsConfig)
2016-08-15 10:17:29 +00:00
if err != nil {
2017-04-08 23:43:25 +00:00
return newError("failed to listen TLS ", netAddr).Base(err)
2016-08-13 13:56:51 +00:00
}
2017-02-08 18:00:13 +00:00
listener = l
2016-08-13 13:56:51 +00:00
}
2017-02-09 11:31:40 +00:00
ln.listener = listener
2016-08-13 13:56:51 +00:00
2016-08-13 13:44:36 +00:00
go func() {
2017-02-09 11:31:40 +00:00
http.Serve(listener, &requestHandler{
2017-02-26 13:38:41 +00:00
path: ln.config.GetNormailzedPath(),
ln: ln,
2017-02-09 11:31:40 +00:00
})
2016-08-13 13:44:36 +00:00
}()
2017-02-08 18:00:13 +00:00
return nil
2016-08-13 13:44:36 +00:00
}
2017-02-16 15:53:31 +00:00
func converttovws(w http.ResponseWriter, r *http.Request) (*connection, error) {
2016-08-13 13:44:36 +00:00
var upgrader = websocket.Upgrader{
2017-01-27 12:50:38 +00:00
ReadBufferSize: 32 * 1024,
WriteBufferSize: 32 * 1024,
2016-08-13 13:44:36 +00:00
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return nil, err
}
2017-02-16 15:53:31 +00:00
return &connection{wsc: conn}, nil
2016-08-13 13:44:36 +00:00
}
2017-02-09 11:31:40 +00:00
func (ln *Listener) Addr() net.Addr {
return ln.listener.Addr()
2016-08-13 13:44:36 +00:00
}
2017-02-09 11:31:40 +00:00
func (ln *Listener) Close() error {
2017-02-27 14:13:22 +00:00
return ln.listener.Close()
2016-08-13 13:44:36 +00:00
}
func init() {
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_WebSocket, ListenWS))
2016-08-13 13:44:36 +00:00
}