2016-12-22 18:30:46 -05:00
|
|
|
package websocket
|
2016-08-13 09:44:36 -04:00
|
|
|
|
|
|
|
import (
|
2016-09-30 10:53:40 -04:00
|
|
|
"crypto/tls"
|
2016-08-13 09:44:36 -04:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-02-08 12:30:16 -05:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"v2ray.com/core/app/log"
|
2017-01-03 09:16:48 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2017-01-04 09:34:21 -05:00
|
|
|
"v2ray.com/core/transport/internet/internal"
|
2017-02-08 12:30:16 -05:00
|
|
|
v2tls "v2ray.com/core/transport/internet/tls"
|
2016-08-13 09:44:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrClosedListener = errors.New("Listener is closed.")
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConnectionWithError struct {
|
|
|
|
conn net.Conn
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2017-02-09 06:31:40 -05:00
|
|
|
type requestHandler struct {
|
|
|
|
path string
|
|
|
|
conns chan *ConnectionWithError
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Info("WebSocket|Listener: Failed to convert to WebSocket connection: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case h.conns <- &ConnectionWithError{conn: conn}:
|
|
|
|
default:
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Listener struct {
|
2016-08-13 09:44:36 -04:00
|
|
|
sync.Mutex
|
2017-02-17 18:28:50 -05:00
|
|
|
closed chan bool
|
2016-08-13 09:44:36 -04:00
|
|
|
awaitingConns chan *ConnectionWithError
|
2017-02-08 13:00:13 -05:00
|
|
|
listener net.Listener
|
2016-09-30 10:53:40 -04:00
|
|
|
tlsConfig *tls.Config
|
2016-10-02 17:43:58 -04:00
|
|
|
config *Config
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
|
2016-09-30 10:53:40 -04:00
|
|
|
func ListenWS(address v2net.Address, port v2net.Port, options internet.ListenOptions) (internet.Listener, error) {
|
2017-01-12 06:54:34 -05:00
|
|
|
networkSettings, err := options.Stream.GetEffectiveTransportSettings()
|
2016-10-02 17:43:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wsSettings := networkSettings.(*Config)
|
2016-08-13 09:44:36 -04:00
|
|
|
|
2017-02-09 06:31:40 -05:00
|
|
|
l := &Listener{
|
2017-02-17 18:28:50 -05:00
|
|
|
closed: make(chan bool),
|
2016-08-13 09:44:36 -04:00
|
|
|
awaitingConns: make(chan *ConnectionWithError, 32),
|
2016-10-02 17:43:58 -04:00
|
|
|
config: wsSettings,
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
2016-10-16 08:22:21 -04:00
|
|
|
if options.Stream != nil && options.Stream.HasSecuritySettings() {
|
2016-10-02 17:43:58 -04:00
|
|
|
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
|
|
|
|
if err != nil {
|
2017-02-10 05:57:59 -05:00
|
|
|
return nil, errors.Base(err).Message("WebSocket: Failed to create apply TLS config.")
|
2016-10-02 17:43:58 -04:00
|
|
|
}
|
2016-10-16 08:22:21 -04:00
|
|
|
tlsConfig, ok := securitySettings.(*v2tls.Config)
|
|
|
|
if ok {
|
|
|
|
l.tlsConfig = tlsConfig.GetTLSConfig()
|
|
|
|
}
|
2016-09-30 10:53:40 -04:00
|
|
|
}
|
2016-08-13 09:44:36 -04:00
|
|
|
|
2016-10-02 17:43:58 -04:00
|
|
|
err = l.listenws(address, port)
|
2016-08-13 09:44:36 -04:00
|
|
|
|
|
|
|
return l, err
|
|
|
|
}
|
|
|
|
|
2017-02-09 06:31:40 -05:00
|
|
|
func (ln *Listener) listenws(address v2net.Address, port v2net.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-02-08 13:00:13 -05:00
|
|
|
return errors.Base(err).Message("WebSocket|Listener: Failed to listen TCP ", netAddr)
|
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-02-08 13:00:13 -05:00
|
|
|
return errors.Base(err).Message("WebSocket|Listener: Failed to listen TLS ", netAddr)
|
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-02-09 06:31:40 -05:00
|
|
|
http.Serve(listener, &requestHandler{
|
2017-02-10 05:57:59 -05:00
|
|
|
path: ln.config.GetNormailzedPath(),
|
2017-02-09 06:31:40 -05:00
|
|
|
conns: ln.awaitingConns,
|
|
|
|
})
|
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-02-16 10:53:31 -05:00
|
|
|
func converttovws(w http.ResponseWriter, r *http.Request) (*connection, error) {
|
2016-08-13 09:44:36 -04:00
|
|
|
var upgrader = websocket.Upgrader{
|
2017-01-27 07:50:38 -05:00
|
|
|
ReadBufferSize: 32 * 1024,
|
|
|
|
WriteBufferSize: 32 * 1024,
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-16 10:53:31 -05:00
|
|
|
return &connection{wsc: conn}, nil
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
|
2017-02-09 06:31:40 -05:00
|
|
|
func (ln *Listener) Accept() (internet.Connection, error) {
|
2017-02-17 18:28:50 -05:00
|
|
|
for {
|
2016-08-13 09:44:36 -04:00
|
|
|
select {
|
2017-02-17 18:28:50 -05:00
|
|
|
case <-ln.closed:
|
|
|
|
return nil, ErrClosedListener
|
2017-02-09 06:31:40 -05:00
|
|
|
case connErr, open := <-ln.awaitingConns:
|
2016-08-13 09:44:36 -04:00
|
|
|
if !open {
|
|
|
|
return nil, ErrClosedListener
|
|
|
|
}
|
|
|
|
if connErr.err != nil {
|
|
|
|
return nil, connErr.err
|
|
|
|
}
|
2017-02-09 06:31:40 -05:00
|
|
|
return internal.NewConnection(internal.ConnectionID{}, connErr.conn, ln, internal.ReuseConnection(ln.config.IsConnectionReuse())), nil
|
2016-08-13 09:44:36 -04:00
|
|
|
case <-time.After(time.Second * 2):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 06:31:40 -05:00
|
|
|
func (ln *Listener) Put(id internal.ConnectionID, conn net.Conn) {
|
|
|
|
ln.Lock()
|
|
|
|
defer ln.Unlock()
|
2017-02-17 18:28:50 -05:00
|
|
|
select {
|
|
|
|
case <-ln.closed:
|
2016-08-13 09:44:36 -04:00
|
|
|
return
|
2017-02-17 18:28:50 -05:00
|
|
|
default:
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
select {
|
2017-02-09 06:31:40 -05:00
|
|
|
case ln.awaitingConns <- &ConnectionWithError{conn: conn}:
|
2016-08-13 09:44:36 -04:00
|
|
|
default:
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-09 06:31:40 -05:00
|
|
|
func (ln *Listener) Close() error {
|
|
|
|
ln.Lock()
|
|
|
|
defer ln.Unlock()
|
2017-02-17 18:28:50 -05:00
|
|
|
select {
|
|
|
|
case <-ln.closed:
|
|
|
|
return ErrClosedListener
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
close(ln.closed)
|
2017-02-09 06:31:40 -05:00
|
|
|
ln.listener.Close()
|
|
|
|
close(ln.awaitingConns)
|
|
|
|
for connErr := range ln.awaitingConns {
|
2016-08-13 09:44:36 -04:00
|
|
|
if connErr.conn != nil {
|
2017-01-28 10:28:18 -05:00
|
|
|
connErr.conn.Close()
|
2016-08-13 09:44:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|