-
Notifications
You must be signed in to change notification settings - Fork 5
/
listener.go
80 lines (71 loc) · 1.48 KB
/
listener.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
package listener
import (
"fmt"
"strings"
"github.com/google/uuid"
"github.com/hideckies/hermit/pkg/common/utils"
)
type Listener struct {
Id uint
Uuid string
Name string
Protocol string
Addr string
Port uint16
Domains []string
Active bool
}
func NewListener(
id uint,
_uuid string,
name string,
protocol string,
addr string,
port uint16,
domains []string,
active bool,
) *Listener {
listenerUuid := _uuid
if _uuid == "" {
listenerUuid = uuid.NewString()
}
listenerName := name
if name == "" {
listenerName = utils.GenerateRandomAnimalName(false, strings.ToLower(protocol))
}
return &Listener{
Id: id,
Uuid: listenerUuid,
Name: listenerName,
Protocol: protocol,
Addr: addr,
Port: port,
Domains: domains,
Active: active,
}
}
func (lis *Listener) GetURL() string {
return fmt.Sprintf(fmt.Sprintf("%s://%s:%d", strings.ToLower(lis.Protocol), lis.Addr, lis.Port))
}
// This includes domain associated URLs such as "https://evil.com:12345"
func (lis *Listener) GetAllURLs() []string {
urls := []string{}
urls = append(urls, lis.GetURL())
if len(lis.Domains) > 0 {
for _, domain := range lis.Domains {
if domain == "" {
continue
}
domainURL := fmt.Sprintf("%s://%s:%d", strings.ToLower(lis.Protocol), domain, lis.Port)
urls = append(urls, domainURL)
}
}
return urls
}
func (lis *Listener) GetActiveString() string {
if lis.Active {
return "active"
} else {
return "inactive"
}
}