/
net.go
134 lines (119 loc) · 2.99 KB
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Package net provides net functionality for grpc
package net
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"sync"
"syscall"
"github.com/vdaas/vald/internal/errgroup"
)
const (
localIPv4 = "127.0.0.1"
localIPv6 = "::1"
localHost = "localhost"
defaultPort = 80
)
type Conn = net.Conn
type Dialer = net.Dialer
type ListenConfig = net.ListenConfig
type Listener = net.Listener
type Resolver = net.Resolver
var (
DefaultResolver = net.DefaultResolver
)
func Listen(network, address string) (Listener, error) {
return net.Listen(network, address)
}
func IsLocal(host string) bool {
return host == localHost ||
host == localIPv4 ||
host == localIPv6
}
func Dial(network string, addr string) (conn Conn, err error) {
return net.Dial(network, addr)
}
func Parse(addr string) (host string, port uint16, isIP bool, err error) {
host, port, err = SplitHostPort(addr)
isIP = IsIPv6(host) || IsIPv4(host)
if err != nil {
return host, defaultPort, isIP, err
}
return host, port, isIP, err
}
func IsIPv6(addr string) bool {
return net.ParseIP(addr) != nil && strings.Count(addr, ":") >= 2
}
func IsIPv4(addr string) bool {
return net.ParseIP(addr) != nil && strings.Count(addr, ":") < 2
}
func SplitHostPort(hostport string) (host string, port uint16, err error) {
switch {
case strings.HasPrefix(hostport, "::"):
hostport = localIPv6 + hostport
case strings.HasPrefix(hostport, ":"):
hostport = localIPv4 + hostport
}
host, portStr, err := net.SplitHostPort(hostport)
if err != nil {
host = hostport
port = defaultPort
}
p, err := strconv.Atoi(portStr)
if err != nil {
port = defaultPort
} else {
port = uint16(p)
}
return host, port, err
}
func ScanPorts(ctx context.Context, start, end uint16, host string) (ports []uint16, err error) {
var rl syscall.Rlimit
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rl)
if err != nil {
return nil, err
}
eg, egctx := errgroup.New(ctx)
eg.Limitation(int(rl.Max) / 2)
var mu sync.Mutex
for i := start; i <= end; i++ {
port := i
eg.Go(func() error {
select {
case <-egctx.Done():
return egctx.Err()
default:
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
return err
}
mu.Lock()
ports = append(ports, port)
mu.Unlock()
return conn.Close()
}
})
}
err = eg.Wait()
if err != nil {
return nil, err
}
return ports, nil
}