forked from maxcnunes/waitforit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
63 lines (51 loc) · 1.18 KB
/
connection.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
package main
import (
"regexp"
"strconv"
)
const regexAddressConn string = `^([a-z]{3,}):\/\/([^:]+):?([0-9]+)?$`
const regexPathAddressConn string = `^([^\/]+)(\/?.*)$`
// Connection data
type Connection struct {
Type string
Scheme string
Port int
Host string
Path string
}
// BuildConn build a connection structure.
// This connection data can later be used as a common structure
// by the functions that will check if the target is available.
func BuildConn(cfg *Config) *Connection {
if cfg.Host != "" {
return &Connection{Type: "tcp", Host: cfg.Host, Port: cfg.Port}
}
address := cfg.Address
if address == "" {
return nil
}
match := regexp.MustCompile(regexAddressConn).FindAllStringSubmatch(address, -1)
if len(match) < 1 {
return nil
}
res := match[0]
port, err := strconv.Atoi(res[3])
if err != nil {
port = 80
}
hostAndPath := regexp.MustCompile(regexPathAddressConn).FindAllStringSubmatch(res[2], -1)[0]
conn := &Connection{
Type: res[1],
Port: port,
Host: hostAndPath[1],
Path: hostAndPath[2],
}
if conn.Type != "tcp" {
conn.Scheme = conn.Type
conn.Type = "tcp"
}
if conn.Scheme == "https" {
conn.Port = 443
}
return conn
}