From a4e5d38461010fd4b8c76f98df82b4166a5766d5 Mon Sep 17 00:00:00 2001 From: Pavel Makarenko Date: Fri, 13 Jan 2017 18:45:28 +0300 Subject: [PATCH] Disable HTTP & WebSocket transports on demand. (#128) * Added .idea folder into .gitignore * Added --http-transport flag * Fixed test & Removed some dumps * Added support of yes/no --- .gitignore | 7 ++++--- cmd/tile38-server/main.go | 30 +++++++++++++++++++++++++----- controller/controller.go | 8 ++++---- controller/server/server.go | 24 +++++++++++++++++------- tests/mock_test.go | 2 +- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 3d40845a..c7c2978c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,9 @@ tile38-* !cmd/tile38-* data*/ -coverage.out +coverage.out packages/ -# Ignore VS Code folder -.vscode/ \ No newline at end of file +# Ignore IDE folders +.idea/ +.vscode/ diff --git a/cmd/tile38-server/main.go b/cmd/tile38-server/main.go index f7d8f26f..7e6d1345 100644 --- a/cmd/tile38-server/main.go +++ b/cmd/tile38-server/main.go @@ -32,6 +32,9 @@ var ( quiet bool ) +// TODO: Set to false in 2.* +var httpTransport bool = true + // Fire up a webhook test server by using the --webhook-http-consumer-port // for example // $ ./tile38-server --webhook-http-consumer-port 9999 @@ -106,6 +109,17 @@ func main() { case "--dev", "-dev": devMode = true continue + case "--http-transport": + i++ + if i < len(os.Args) { + switch strings.ToLower(os.Args[i]) { + case "1", "true", "yes": + httpTransport = true + case "0", "false", "no": + httpTransport = false + } + continue + } } nargs = append(nargs, os.Args[i]) } @@ -118,6 +132,7 @@ func main() { flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent.") flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging.") flag.Parse() + var logw io.Writer = os.Stderr if quiet { logw = ioutil.Discard @@ -138,6 +153,11 @@ func main() { gitsha = "" } + httpTransportEnabled := "Enabled" + if !httpTransport { + httpTransportEnabled = "Disabled" + } + // _____ _ _ ___ ___ // |_ _|_| |___|_ | . | // | | | | | -_|_ | . | @@ -148,12 +168,12 @@ func main() { | | | |____ | _ | Tile38 %s%s %d bit (%s/%s) | | | %sPort: %d, PID: %d - |____ | _ | - | | | tile38.com - |_______|_______| -`+"\n", core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, port, os.Getpid()) + |____ | _ | HTTP & WebSocket transports: %s + | | | + |_______|_______| tile38.com +`+"\n", core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, port, os.Getpid(), httpTransportEnabled) - if err := controller.ListenAndServe(host, port, dir); err != nil { + if err := controller.ListenAndServe(host, port, dir, httpTransport); err != nil { log.Fatal(err) } } diff --git a/controller/controller.go b/controller/controller.go index f94ec04c..68b24142 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -100,10 +100,10 @@ type Controller struct { } // ListenAndServe starts a new tile38 server -func ListenAndServe(host string, port int, dir string) error { - return ListenAndServeEx(host, port, dir, nil) +func ListenAndServe(host string, port int, dir string, http bool) error { + return ListenAndServeEx(host, port, dir, nil, http) } -func ListenAndServeEx(host string, port int, dir string, ln *net.Listener) error { +func ListenAndServeEx(host string, port int, dir string, ln *net.Listener, http bool) error { log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA) c := &Controller{ host: host, @@ -221,7 +221,7 @@ func ListenAndServeEx(host string, port int, dir string, ln *net.Listener) error delete(c.conns, conn) c.mu.Unlock() } - return server.ListenAndServe(host, port, protected, handler, opened, closed, ln) + return server.ListenAndServe(host, port, protected, handler, opened, closed, ln, http) } func (c *Controller) watchMemory() { diff --git a/controller/server/server.go b/controller/server/server.go index f83cd8d1..e031304f 100644 --- a/controller/server/server.go +++ b/controller/server/server.go @@ -21,21 +21,21 @@ Tile38 is running in protected mode because protected mode is enabled, no host address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Tile38 you may adopt one of the -following solutions: +following solutions: 1) Disable protected mode by sending the command 'CONFIG SET protected-mode no' - from the loopback interface by connecting to Tile38 from the same host + from the loopback interface by connecting to Tile38 from the same host the server is running, however MAKE SURE Tile38 is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change - permanent. + permanent. 2) Alternatively you can just disable the protected mode by editing the Tile38 configuration file, and setting the 'protected-mode' option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the - '--protected-mode no' option. -4) Use a host address or an authentication password. + '--protected-mode no' option. +4) Use a host address or an authentication password. -NOTE: You only need to do one of the above things in order for the server +NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside. `) + "\r\n") @@ -55,6 +55,7 @@ func ListenAndServe( opened func(conn *Conn), closed func(conn *Conn), lnp *net.Listener, + http bool, ) error { ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)) if err != nil { @@ -70,7 +71,7 @@ func ListenAndServe( log.Error(err) return err } - go handleConn(&Conn{Conn: conn}, protected, handler, opened, closed) + go handleConn(&Conn{Conn: conn}, protected, handler, opened, closed, http) } } @@ -87,6 +88,7 @@ func handleConn( handler func(conn *Conn, msg *Message, rd *AnyReaderWriter, w io.Writer, websocket bool) error, opened func(conn *Conn), closed func(conn *Conn), + http bool, ) { opened(conn) defer closed(conn) @@ -110,6 +112,14 @@ func handleConn( rd := NewAnyReaderWriter(conn) for { msg, err := rd.ReadMessage() + + // Just closing connection if we have deprecated HTTP or WS connection, + // And --http-transport = false + if !http && (msg.ConnType == WebSocket || msg.ConnType == HTTP) { + conn.Close() + return + } + if err != nil { if err == io.EOF { return diff --git a/tests/mock_test.go b/tests/mock_test.go index 3f387744..bbf13a68 100644 --- a/tests/mock_test.go +++ b/tests/mock_test.go @@ -51,7 +51,7 @@ func mockOpenServer() (*mockServer, error) { s := &mockServer{port: port} tlog.Default = tlog.New(logOutput, nil) go func() { - if err := controller.ListenAndServe("localhost", port, dir); err != nil { + if err := controller.ListenAndServe("localhost", port, dir, true); err != nil { log.Fatal(err) } }()