summaryrefslogtreecommitdiff
path: root/utils/network_windows.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-01-22 11:04:09 +0300
committerGitHub <[email protected]>2021-01-22 11:04:09 +0300
commit29d6020a9e8a3713b22269ed946547c96c24d3da (patch)
treeafe4d330ecb4180e1a9970c8e250bf4f8d92c15e /utils/network_windows.go
parent6807441b2bf1e821e335d67567af47567c9757f3 (diff)
parent4d60db85d1c0bfeddffe1de3e28d3464949c5f6d (diff)
Merge pull request #494 from spiral/feature/allow_https_listen_on_unix_socketsv2.0.0-beta11
feat(https): Allow https to listen on unix sockets
Diffstat (limited to 'utils/network_windows.go')
-rwxr-xr-xutils/network_windows.go54
1 files changed, 41 insertions, 13 deletions
diff --git a/utils/network_windows.go b/utils/network_windows.go
index ebe343a3..a07ac351 100755
--- a/utils/network_windows.go
+++ b/utils/network_windows.go
@@ -3,33 +3,61 @@
package utils
import (
- "errors"
"fmt"
"net"
"os"
"strings"
"syscall"
+
+ "github.com/valyala/tcplisten"
)
// CreateListener crates socket listener based on DSN definition.
func CreateListener(address string) (net.Listener, error) {
dsn := strings.Split(address, "://")
- if len(dsn) != 2 {
- return nil, errors.New("invalid DSN (tcp://:6001, unix://file.sock)")
- }
-
- if dsn[0] != "unix" && dsn[0] != "tcp" {
- return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)")
- }
- if dsn[0] == "unix" && fileExists(dsn[1]) {
- err := syscall.Unlink(dsn[1])
- if err != nil {
- return nil, fmt.Errorf("error during the unlink syscall: error %v", err)
+ switch len(dsn) {
+ case 1:
+ // assume, that there is no prefix here [127.0.0.1:8000]
+ return createTCPListener(dsn[0])
+ case 2:
+ // we got two part here, first part is the transport, second - address
+ // [tcp://127.0.0.1:8000] OR [unix:///path/to/unix.socket] OR [error://path]
+ // where error is wrong transport name
+ switch dsn[0] {
+ case "unix":
+ // check of file exist. If exist, unlink
+ if fileExists(dsn[1]) {
+ err := syscall.Unlink(dsn[1])
+ if err != nil {
+ return nil, fmt.Errorf("error during the unlink syscall: error %v", err)
+ }
+ }
+ return net.Listen(dsn[0], dsn[1])
+ case "tcp":
+ return createTCPListener(dsn[1])
+ // not an tcp or unix
+ default:
+ return nil, fmt.Errorf("invalid Protocol ([tcp://]:6001, unix://file.sock), address: %s", address)
}
+ // wrong number of split parts
+ default:
+ return nil, fmt.Errorf("wrong number of parsed protocol parts, address: %s", address)
}
+}
- return net.Listen(dsn[0], dsn[1])
+func createTCPListener(addr string) (net.Listener, error) {
+ cfg := tcplisten.Config{
+ ReusePort: true,
+ DeferAccept: true,
+ FastOpen: true,
+ Backlog: 0,
+ }
+ listener, err := cfg.NewListener("tcp4", addr)
+ if err != nil {
+ return nil, err
+ }
+ return listener, nil
}
// fileExists checks if a file exists and is not a directory before we