-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (112 loc) · 3.25 KB
/
main.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
135
136
137
package main
import (
"fmt"
"net/netip"
"time"
"github.com/google/uuid"
"github.com/thefeli73/polemos/mtdaws"
"github.com/thefeli73/polemos/pcsdk"
"github.com/thefeli73/polemos/state"
)
// ConfigPath is a string of the location for the configfile
var ConfigPath string
func main() {
fmt.Println("Starting Polemos")
ConfigPath = "config.yaml"
// Initialize the config.Services map
var config state.Config
config.MTD.Services = make(map[state.CustomUUID]state.Service)
config = state.LoadConf(ConfigPath)
state.SaveConf(ConfigPath, config)
config = indexAllInstances(config)
state.SaveConf(ConfigPath, config)
// CREATE TUNNELS
createTunnels(config)
// START DOING MTD
mtdLoop(config)
}
func mtdLoop(config state.Config) {
for true {
//TODO: figure out migration (MTD)
config = movingTargetDefense(config)
state.SaveConf(ConfigPath, config)
fmt.Println("Sleeping for 1 minute")
time.Sleep(1*time.Minute)
//TODO: proxy commands
}
}
func movingTargetDefense(config state.Config) state.Config{
mtdaws.AWSMoveInstance(config)
return config
}
func indexAllInstances(config state.Config) state.Config {
fmt.Println("Indexing instances")
t := time.Now()
for _, service := range config.MTD.Services {
service.Active = false
}
//index AWS instances
awsNewInstanceCounter := 0
awsInactiveInstanceCounter := len(config.MTD.Services)
awsInstanceCounter := 0
awsInstances := mtdaws.GetInstances(config)
for _, instance := range awsInstances {
cloudID := mtdaws.GetCloudID(instance)
ip, err := netip.ParseAddr(instance.PublicIP)
if err != nil {
fmt.Println("Error converting ip:\t", err)
continue
}
var found bool
config, found = indexInstance(config, cloudID, ip)
if !found {
awsNewInstanceCounter++
} else {
awsInactiveInstanceCounter--
}
awsInstanceCounter++
}
// TODO: Purge instances in config that are not found in the cloud
fmt.Printf("Found %d active AWS instances (%d newly added, %d inactive) (took %s)\n",
awsInstanceCounter, awsNewInstanceCounter, awsInactiveInstanceCounter, time.Since(t).Round(100*time.Millisecond).String())
return config
}
func createTunnels(config state.Config) {
for serviceUUID, service := range config.MTD.Services {
if service.AdminEnabled && service.Active {
proxy := pcsdk.BuildProxy(netip.AddrPortFrom(service.EntryIP, config.MTD.ManagementPort))
err := proxy.Status()
if err != nil {
continue
}
// Reconfigure Proxy to new instance
err = proxy.Create(service.EntryPort, service.ServicePort, service.ServiceIP, serviceUUID)
if err != nil {
continue
}
}
}
}
func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) (state.Config, bool) {
found := false
var foundUUID state.CustomUUID
for u, service := range config.MTD.Services {
if service.CloudID == cloudID {
found = true
foundUUID = u
break;
}
}
if !found {
fmt.Println("New instance found:\t", cloudID)
u := uuid.New()
config.MTD.Services[state.CustomUUID(u)] = state.Service{CloudID: cloudID, ServiceIP: serviceIP, Active: true, AdminEnabled: true}
state.SaveConf(ConfigPath, config)
} else {
s := config.MTD.Services[foundUUID]
s.Active = true
config.MTD.Services[foundUUID] = s
state.SaveConf(ConfigPath, config)
}
return config, found
}