This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
258 lines (225 loc) · 6.77 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*cloud-proxy is a utility for creating multiple instances
and starting socks proxies via SSH after creation.*/
package main
import (
"crypto/rand"
"encoding/hex"
"flag"
"fmt"
"log"
"math/big"
"os"
"os/exec"
"os/signal"
"strings"
"text/template"
"time"
"golang.org/x/crypto/sha3"
)
var (
sshLocation = flag.String("key-location", "~/.ssh/id_rsa", "SSH key location")
count = flag.Int("count", 5, "Amount of droplets to deploy")
name = flag.String("name", "cloud-proxy", "Droplet name prefix")
doRegionFlag = flag.String("doRegions", "*", "Comma separated list of regions to deploy droplets to, defaults to all.")
awsRegionFlag = flag.String("awsRegions", "*", "Comma separated list of regions to deploy droplets to, defaults to all.")
force = flag.Bool("force", false, "Bypass built-in protections that prevent you from deploying more than 50 droplets")
startPort = flag.Int("start-tcp", 55555, "TCP port to start first proxy on and increment from")
awsProvider = flag.Bool("aws", false, "Use AWS as provider")
doProvider = flag.Bool("do", false, "Use DigitalOcean as provider")
showversion = flag.Bool("v", false, "Print version and exit")
version = "2.0.0"
)
type digitaloceanInfo struct {
Name string
Region string
}
type AwsInfo struct {
AMI map[string]string // AMI["sa-east-1"] = "cloud-proxy-ami-sa-east-1"
SecurityGroup map[string]string // SecurityGroup["sa-east-1"] = "cloud-proxy-securitygroup-sa-east-1"
Instances []Instance
}
type Instance struct {
Name string
Region string
AMI string
SecurityGroup string
}
func main() {
flag.Parse()
if *showversion {
fmt.Println(version)
os.Exit(0)
}
if !*doProvider && !*awsProvider {
log.Fatal("Need at least one provider")
}
providers := []string{}
doInfos := []digitaloceanInfo{}
awsInfo := AwsInfo{}
if *doProvider {
providers = append(providers, "digitalocean")
}
if *awsProvider {
providers = append(providers, "aws")
awsInfo.AMI = make(map[string]string)
awsInfo.SecurityGroup = make(map[string]string)
}
if *count > 50 && !*force {
log.Fatalln("-count greater than 50")
}
for x := 0; x < *count; x++ {
var region string
providerIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(providers))))
if err != nil {
log.Fatal(err)
}
switch providers[providerIndex.Int64()] {
case "digitalocean":
if *doRegionFlag == "*" {
region = randomRegion(doRegions)
} else {
numRegions := strings.Split(*doRegionFlag, ",")
if len(numRegions) > 1 {
region = randomRegion(numRegions)
} else {
region = numRegions[0]
}
}
computerName := fmt.Sprintf("%s-%s", *name, namePostfix())
cs := digitaloceanInfo{}
cs.Name = computerName
cs.Region = region
doInfos = append(doInfos, cs)
case "aws":
if *awsRegionFlag == "*" {
region = randomRegion(awsRegions)
} else {
numRegions := strings.Split(*awsRegionFlag, ",")
if len(numRegions) > 1 {
region = randomRegion(numRegions)
} else {
region = numRegions[0]
}
}
computerName := fmt.Sprintf("%s-%s-%s", *name, namePostfix(), region)
amiName := fmt.Sprintf("%s-ami-%s", *name, region)
securityGroupName := fmt.Sprintf("%s-securitygroup-%s", *name, region)
awsInfo.AMI[region] = amiName
awsInfo.SecurityGroup[region] = securityGroupName
awsInfo.Instances = append(awsInfo.Instances, Instance{Name: computerName, Region: region, AMI: amiName, SecurityGroup: securityGroupName})
}
}
createTerraformFile("do-infrastructure.tf", doTemplate, doInfos)
createTerraformFile("aws-infrastructure.tf", awsTemplate, awsInfo)
executeTerraform([]string{"apply", "-var-file=secrets.tfvars", "-auto-approve"})
computerUsers := make(map[string]string)
for _, instance := range awsInfo.Instances {
name := fmt.Sprintf("%s-IP", instance.Name)
computerUsers[name] = "ec2-user"
}
for _, computer := range doInfos {
name := fmt.Sprintf("%s-IP", computer.Name)
computerUsers[name] = "root"
}
printConfigs(*startPort, printProxyChains, printSocksd)
tunnelProcesses := createTunnels(computerUsers)
log.Println("Please CTRL-C to destroy infrastructure")
// Catch CTRL-C and delete droplets.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
executeTerraform([]string{"destroy", "-var-file=secrets.tfvars", "-auto-approve"})
for _, process := range tunnelProcesses {
if err := process.Kill(); err != nil {
log.Fatal(err)
}
}
}
func printConfigs(port int, configs ...func(int)) {
for _, config := range configs {
config(port)
}
}
func printProxyChains(port int) {
log.Println("proxychains config")
for x := 0; x < *count; x++ {
fmt.Printf("socks5 127.0.0.1 %d\n", port)
port++
}
}
func printSocksd(port int) {
log.Println("socksd config")
fmt.Printf("\"upstreams\": [\n")
for x := 0; x < *count; x++ {
fmt.Printf("{\"type\": \"socks5\", \"address\": \"127.0.0.1:%d\"}", port)
port++
if x < (*count - 1) {
fmt.Printf(",\n")
}
}
fmt.Printf("\n]\n")
}
func randomRegion(regions []string) string {
regionIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(regions))))
if err != nil {
log.Fatal(err)
}
return regions[regionIndex.Int64()]
}
func createTerraformFile(fileName, templateData string, data interface{}) {
fh, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
t := template.Must(template.New("tmpl").Parse(templateData))
if err := t.Execute(fh, data); err != nil {
log.Fatal(err)
}
}
func createTunnels(computerUsers map[string]string) []*os.Process {
time.Sleep(10 * time.Second)
output, err := exec.Command("terraform", "output").Output()
if err != nil {
log.Fatal(err)
}
tunnelProcesses := []*os.Process{}
for _, line := range strings.Split(string(output), "\n") {
splitOutput := strings.Split(line, "=")
if len(splitOutput) != 2 {
continue
}
ip := strings.TrimSpace(splitOutput[1])
computerName := strings.TrimSpace(splitOutput[0])
port := fmt.Sprintf("%d", *startPort)
var host string
if user, ok := computerUsers[computerName]; ok {
host = fmt.Sprintf("%s@%s", user, ip)
}
fmt.Printf("creating tunnel to %s on %s\n", host, port)
cmd := exec.Command("ssh", "-D", port, "-N", "-o", "StrictHostKeyChecking=no", "-i", *sshLocation, host)
cmd.Stderr = os.Stderr
cmd.Start()
tunnelProcesses = append(tunnelProcesses, cmd.Process)
*startPort++
}
return tunnelProcesses
}
func executeTerraform(args []string) {
cmd := exec.Command("terraform", args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Start()
cmd.Wait()
}
func namePostfix() string {
var buffer [32]byte
if _, err := rand.Read(buffer[:]); err != nil {
log.Fatal(err)
}
hash := sha3.New256()
if _, err := hash.Write(buffer[:]); err != nil {
log.Fatal(err)
}
output := hash.Sum(nil)
return hex.EncodeToString(output[0:8])
}