From 800581b2bf5dacb2134dfda751be08cbf78df978 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 26 Mar 2022 13:11:29 +0000 Subject: [PATCH 1/2] core: tcp keep-alive parameters for windows (#336) --- src/Simplex/Messaging/Client.hs | 2 +- src/Simplex/Messaging/Transport/KeepAlive.hs | 46 ++++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 407bfd40c..e6e87337f 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -114,7 +114,7 @@ smpDefaultConfig = defaultTransport = ("5223", transport @TLS), tcpTimeout = 4_000_000, tcpKeepAlive = Just defaultKeepAliveOpts, - smpPing = 1_200_000_000 -- 20min + smpPing = 600_000_000 -- 10min } data Request = Request diff --git a/src/Simplex/Messaging/Transport/KeepAlive.hs b/src/Simplex/Messaging/Transport/KeepAlive.hs index 3af71c45e..aa308492d 100644 --- a/src/Simplex/Messaging/Transport/KeepAlive.hs +++ b/src/Simplex/Messaging/Transport/KeepAlive.hs @@ -7,19 +7,6 @@ module Simplex.Messaging.Transport.KeepAlive where import Foreign.C (CInt (..)) import Network.Socket -_SOL_TCP :: CInt -_SOL_TCP = 6 - -#if defined(darwin_HOST_OS) -foreign import capi "netinet/tcp.h value TCP_KEEPALIVE" _TCP_KEEPIDLE :: CInt -#else -foreign import capi "netinet/tcp.h value TCP_KEEPIDLE" _TCP_KEEPIDLE :: CInt -#endif - -foreign import capi "netinet/tcp.h value TCP_KEEPINTVL" _TCP_KEEPINTVL :: CInt - -foreign import capi "netinet/tcp.h value TCP_KEEPCNT" _TCP_KEEPCNT :: CInt - data KeepAliveOpts = KeepAliveOpts { keepIdle :: Int, keepIntvl :: Int, @@ -34,6 +21,39 @@ defaultKeepAliveOpts = keepCnt = 4 } +_SOL_TCP :: CInt +_SOL_TCP = 6 + +#if defined(mingw32_HOST_OS) +-- Windows + +-- The values are copied from windows::Win32::Networking::WinSock +-- https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Networking/WinSock/index.html + +_TCP_KEEPIDLE :: CInt +_TCP_KEEPIDLE = 3 + +_TCP_KEEPINTVL :: CInt +_TCP_KEEPINTVL = 17 + +_TCP_KEEPCNT :: CInt +_TCP_KEEPCNT = 16 + +#else +-- Mac/Linux + +#if defined(darwin_HOST_OS) +foreign import capi "netinet/tcp.h value TCP_KEEPALIVE" _TCP_KEEPIDLE :: CInt +#else +foreign import capi "netinet/tcp.h value TCP_KEEPIDLE" _TCP_KEEPIDLE :: CInt +#endif + +foreign import capi "netinet/tcp.h value TCP_KEEPINTVL" _TCP_KEEPINTVL :: CInt + +foreign import capi "netinet/tcp.h value TCP_KEEPCNT" _TCP_KEEPCNT :: CInt + +#endif + setSocketKeepAlive :: Socket -> KeepAliveOpts -> IO () setSocketKeepAlive sock KeepAliveOpts {keepCnt, keepIdle, keepIntvl} = do setSocketOption sock KeepAlive 1 From 337d5bdfe7cffaef93496f7b1d18524395c47789 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Sat, 26 Mar 2022 19:57:18 +0400 Subject: [PATCH 2/2] add "SMP server on Linux" section to README (#337) --- README.md | 24 ++++++++++++++++++++++-- apps/smp-server/Main.hs | 5 ++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4e6594113..62fb440ab 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,9 @@ SimpleXMQ is implemented in Haskell - it benefits from robust software transacti ### SMP server -[SMP server](https://github.com/simplex-chat/simplexmq/blob/master/apps/smp-server/Main.hs) can be run on any Linux distribution without any dependencies, including low power/low memory devices. +[SMP server](https://github.com/simplex-chat/simplexmq/blob/master/apps/smp-server/Main.hs) can be run on any Linux distribution, including low power/low memory devices. OpenSSL library is required for initialization. -To initialize the server use `smp-server init` command - it will generate keys and certificates for TLS transport. The fingerprint of offline certificate is used as part of the server address to protect client/server connection against man-in-the-middle attacks: `smp://@[:5223]`. +To initialize the server use `smp-server init -n ` (or `smp-server init --ip ` for IP based address) command - it will generate keys and certificates for TLS transport. The fingerprint of offline certificate is used as part of the server address to protect client/server connection against man-in-the-middle attacks: `smp://@[:5223]`. SMP server uses in-memory persistence with an optional append-only log of created queues that allows to re-start the server without losing the connections. This log is compacted on every server restart, permanently removing suspended and removed queues. @@ -87,6 +87,26 @@ You can either run your own SMP server locally or deploy using [Linode StackScri It's the easiest to try SMP agent via a prototype [simplex-chat](https://github.com/simplex-chat/simplex-chat) terminal UI. +## Deploy SMP server on Linux + +You can run your SMP server as a Linux process, optionally using a service manager for booting and restarts. + +- For Ubuntu you can download a binary from [the latest release](https://github.com/simplex-chat/simplexmq/releases). + + If you're using other Linux distribution and the binary is incompatible with it, you can build from source using [Haskell stack](https://docs.haskellstack.org/en/stable/README/): + + ```shell + curl -sSL https://get.haskellstack.org/ | sh + ... + stack install + ``` + +- Initialize SMP server with `smp-server init [-l] -n ` or `smp-server init [-l] --ip ` - depending on how you initialize it, either FQDN or IP will be used for server's address. + +- Run `smp-server start` to start SMP server, or you can configure a service manager to run it as a service. + +See [this section](#smp-server) for more information. Run `smp-server -h` and `smp-server init -h` for explanation of commands and options. + [Linode](https://cloud.linode.com/stackscripts/748014) ## Deploy SMP server on Linode diff --git a/apps/smp-server/Main.hs b/apps/smp-server/Main.hs index 2a5afa049..163b312c8 100644 --- a/apps/smp-server/Main.hs +++ b/apps/smp-server/Main.hs @@ -131,8 +131,7 @@ cliCommandP = <*> strOption ( long "ip" <> help - "Server IP address used as Subject Alternative Name for TLS online certificate, \ - \also used as Common Name if FQDN is not supplied" + "Server IP address, used as Common Name for TLS online certificate if FQDN is not supplied" <> value "127.0.0.1" <> showDefault <> metavar "IP" @@ -140,7 +139,7 @@ cliCommandP = <*> (optional . strOption) ( long "fqdn" <> short 'n' - <> help "Server FQDN used as Common Name and Subject Alternative Name for TLS online certificate" + <> help "Server FQDN used as Common Name for TLS online certificate" <> showDefault <> metavar "FQDN" )