-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
483 lines (389 loc) · 12.3 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package main
import (
"bufio"
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/ec2instanceconnect"
"golang.org/x/crypto/ssh"
"github.com/miekg/dns"
)
func keygen() (string, string) {
savePrivateFileTo := "./.deployr/key"
savePublicFileTo := "./.deployr/key.pub"
bitSize := 4096
err := os.MkdirAll("./.deployr", 0755)
if err != nil {
log.Fatal("Failed to create .deployr directory: ", err)
}
privateKey, err := generatePrivateKey(bitSize)
if err != nil {
log.Fatal(err.Error())
}
publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey)
if err != nil {
log.Fatal(err.Error())
}
privateKeyBytes := encodePrivateKeyToPEM(privateKey)
err = writeKeyToFile(privateKeyBytes, savePrivateFileTo)
if err != nil {
log.Fatal(err.Error())
}
err = writeKeyToFile([]byte(publicKeyBytes), savePublicFileTo)
if err != nil {
log.Fatal(err.Error())
}
// fmt.Println("public key " + string(publicKeyBytes))
// fmt.Println("private key " + string(privateKeyBytes))
return string(publicKeyBytes), string(privateKeyBytes)
}
// generatePrivateKey creates a RSA Private Key of specified byte size
func generatePrivateKey(bitSize int) (*rsa.PrivateKey, error) {
// Private Key generation
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
return nil, err
}
// Validate Private Key
err = privateKey.Validate()
if err != nil {
return nil, err
}
log.Println("Private Key generated")
return privateKey, nil
}
// encodePrivateKeyToPEM encodes Private Key from RSA to PEM format
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte {
// Get ASN.1 DER format
privDER := x509.MarshalPKCS1PrivateKey(privateKey)
// pem.Block
privBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: privDER,
}
// Private key in PEM format
privatePEM := pem.EncodeToMemory(&privBlock)
return privatePEM
}
// generatePublicKey take a rsa.PublicKey and return bytes suitable for writing to .pub file
// returns in the format "ssh-rsa ..."
func generatePublicKey(privatekey *rsa.PublicKey) ([]byte, error) {
publicRsaKey, err := ssh.NewPublicKey(privatekey)
if err != nil {
return nil, err
}
pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey)
log.Println("Public key generated")
return pubKeyBytes, nil
}
// writePemToFile writes keys to a file
func writeKeyToFile(keyBytes []byte, saveFileTo string) error {
err := ioutil.WriteFile(saveFileTo, keyBytes, 0600)
if err != nil {
return err
}
log.Printf("Key saved to: %s", saveFileTo)
return nil
}
func main() {
DeployrConfig()
err := os.MkdirAll(os.Getenv("HOME")+"/.aws", 0755)
if err != nil {
fmt.Println("error while making dr")
log.Fatal("Error occored")
}
filePath := os.Getenv("HOME") + "/.aws/credentials"
file, err := os.Create(filePath)
if err != nil {
fmt.Println("error while making file")
log.Fatal(err)
}
defer file.Close()
credentialsFileContent := fmt.Sprintf(`[deployr]
aws_access_key_id = %s
aws_secret_access_key = %s`, deployrcfg.AwsAcess, deployrcfg.AwsSecret)
_, err = file.WriteString(credentialsFileContent)
if err != nil {
fmt.Println("error while writing to file")
log.Fatal(err)
}
// Create config file
configPath := os.Getenv("HOME") + "/.aws/config"
configFile, err := os.Create(configPath)
if err != nil {
fmt.Println("Error while creating config file:", err)
log.Fatal(err)
}
defer configFile.Close()
configfilecontent := fmt.Sprintf(`[profile deployr]
region = %s
`, deployrcfg.Region)
// Write region to config file
_, err = configFile.WriteString(configfilecontent)
if err != nil {
fmt.Println("Error while writing to config file:", err)
log.Fatal(err)
}
cfg, erre := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile("deployr"))
if erre != nil {
fmt.Println("error while writing loading config")
log.Fatal(err)
}
fmt.Println("credentials ok")
svc := ec2.NewFromConfig(cfg)
// create security group
createGroupOutput, err := svc.CreateSecurityGroup(context.TODO(), &ec2.CreateSecurityGroupInput{
GroupName: aws.String("deployr-sg"),
Description: aws.String("Security group for deployr instance with SSH, HTTP, and HTTPS access"),
})
if err != nil {
log.Fatalf("Failed to create security group: %v", err)
}
securityGroupID := aws.ToString(createGroupOutput.GroupId)
fmt.Printf("Created security group with ID: %s\n", securityGroupID)
// Authorize inbound rules
_, err = svc.AuthorizeSecurityGroupIngress(context.TODO(), &ec2.AuthorizeSecurityGroupIngressInput{
GroupId: aws.String(securityGroupID),
IpPermissions: []types.IpPermission{
{
IpProtocol: aws.String("tcp"),
FromPort: aws.Int32(22),
ToPort: aws.Int32(22),
IpRanges: []types.IpRange{
{
CidrIp: aws.String("0.0.0.0/0"),
Description: aws.String("Allow SSH access from anywhere"),
},
},
},
{
IpProtocol: aws.String("tcp"),
FromPort: aws.Int32(80),
ToPort: aws.Int32(80),
IpRanges: []types.IpRange{
{
CidrIp: aws.String("0.0.0.0/0"),
Description: aws.String("Allow HTTP traffic from anywhere"),
},
},
},
{
IpProtocol: aws.String("tcp"),
FromPort: aws.Int32(443),
ToPort: aws.Int32(443),
IpRanges: []types.IpRange{
{
CidrIp: aws.String("0.0.0.0/0"),
Description: aws.String("Allow HTTPS traffic from anywhere"),
},
},
},
},
})
if err != nil {
log.Fatalf("Failed to add security group rules: %v", err)
}
resp, instanceErr := svc.RunInstances(context.TODO(),
&ec2.RunInstancesInput{
ImageId: aws.String(deployrcfg.Ami),
InstanceType: types.InstanceTypeT2Micro,
MinCount: aws.Int32(1),
MaxCount: aws.Int32(1),
TagSpecifications: []types.TagSpecification{
{
ResourceType: types.ResourceTypeInstance,
Tags: []types.Tag{
{
Key: aws.String("deployr"),
Value: aws.String("deployr"),
},
},
},
},
SecurityGroupIds: []string{securityGroupID},
},
)
instanceId := resp.Instances[0].InstanceId
fmt.Println("Instance id of provisiond response --> " + *instanceId)
instancePubDns, instancePubIp, _ := GetPublicDNSByInstanceID(svc, *instanceId)
fmt.Printf("\033[32m"+`
---------------------------
Please point your IP to:
%s
---------------------------
`+"\033[0m", instancePubIp)
resolveConfimation(deployrcfg.Domain, instancePubIp)
fmt.Println("the public dns is ")
fmt.Println(instancePubDns)
if instanceErr != nil {
fmt.Println("error while running instance")
log.Fatal(instanceErr.Error())
}
if resp == nil {
log.Fatal("RunInstances response is nil")
}
time.Sleep(10 * time.Second)
icsvc := ec2instanceconnect.NewFromConfig(cfg)
pubkey, _ := keygen()
respp, erroricsvc := icsvc.SendSSHPublicKey(context.TODO(), &ec2instanceconnect.SendSSHPublicKeyInput{
InstanceId: instanceId,
InstanceOSUser: aws.String("ec2-user"),
SSHPublicKey: aws.String(pubkey),
})
if erroricsvc != nil {
fmt.Println("error in sending public key ")
log.Fatal(erroricsvc.Error())
}
fmt.Println(respp.Success)
pkey, err := ioutil.ReadFile("./.deployr/key")
signer, signError := ssh.ParsePrivateKey([]byte(pkey))
if signError != nil {
fmt.Println("error signing jeyt s")
log.Fatal(signError.Error())
}
config := &ssh.ClientConfig{
User: "ec2-user",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
fmt.Printf("Connected to host %s. Fingerprint: %s\n",
hostname, ssh.FingerprintSHA256(key))
if err := addHostToKnownHosts(hostname, key); err != nil {
return fmt.Errorf("failed to add host to known_hosts: %w", err)
}
return nil
},
Timeout: 10 * time.Second,
}
clientresp, clienterr := ssh.Dial("tcp", instancePubDns+":22", config)
if clienterr != nil {
fmt.Println("error dialing to the ssh client")
log.Fatal(clienterr.Error())
}
session, err := clientresp.NewSession()
if err != nil {
log.Fatal("Failed to create session: ", err)
}
defer session.Close()
// Once a Session is created, you can execute a single command on
// the remote side using the Run method.
command := fmt.Sprintf(`sudo sh -c 'if [ ! -d /.deployr ]; then mkdir /.deployr; fi && curl -o /.deployr/deployr.sh https://gist.githubusercontent.com/adityacodes30/68f9887074b0e203e0986ae03a00d842/raw/95743327ad67cfae66e979ecbf794defbd903294/gistfile1.sh && sudo chmod +x /.deployr/deployr.sh && sudo /bin/bash /.deployr/deployr.sh %s %s %s'`, deployrcfg.Target, deployrcfg.Domain, deployrcfg.Email)
var b bytes.Buffer
session.Stdout = &b
if err := session.Run(command); err != nil {
log.Fatal("Failed to run: " + err.Error())
}
fmt.Println(b.String())
}
func GetPublicDNSByInstanceID(ec2Client *ec2.Client, instanceID string) (string, string, error) {
for {
// Describe the instance using its ID
resp, err := ec2Client.DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{
InstanceIds: []string{instanceID},
})
if err != nil {
return "", "", fmt.Errorf("failed to describe instance: %w", err)
}
// Check if instances were found
if len(resp.Reservations) == 0 || len(resp.Reservations[0].Instances) == 0 {
return "", "", fmt.Errorf("no instances found with ID: %s", instanceID)
}
// Get the instance and its current state
instance := resp.Reservations[0].Instances[0]
state := instance.State.Name
fmt.Printf("Instance state: %s\n", state)
// If the instance is running and has a public DNS, return it
if state == types.InstanceStateNameRunning && aws.ToString(instance.PublicDnsName) != "" {
return aws.ToString(instance.PublicDnsName), aws.ToString(instance.PublicIpAddress), nil
}
// Wait before the next poll
fmt.Println("waiting for public dns to be assigned")
time.Sleep(2 * time.Second)
}
}
func addHostToKnownHosts(host string, key ssh.PublicKey) error {
knownHostsPath := os.Getenv("HOME") + "/.ssh/known_hosts"
// Create the formatted host key entry
hostKeyEntry := fmt.Sprintf("%s %s %s\n",
host,
key.Type(),
base64.StdEncoding.EncodeToString(key.Marshal()))
// Append the host key to the known_hosts file
return ioutil.WriteFile(knownHostsPath, []byte(hostKeyEntry), 0644)
}
func PollForIpPoint(domain string, ipAddr string) bool {
counter := 0
dnsResolver := "8.8.8.8:53"
for {
ip, err := resolveWithDNS(domain, dnsResolver)
if err != nil {
fmt.Printf("Error looking up host: %v\n", err)
return false
}
if len(ip) > 0 && ip[0] == ipAddr {
fmt.Println("Domain resolved to the correct IP!")
return true
}
counter++
if counter >= 1000 {
fmt.Println("Reached maximum retries. DNS propagation failed.")
return false
}
fmt.Println("Waiting for public DNS to be propagated...")
time.Sleep(20 * time.Second)
}
}
func resolveWithDNS(domain string, resolver string) ([]string, error) {
c := dns.Client{}
m := dns.Msg{}
m.SetQuestion(dns.Fqdn(domain), dns.TypeA)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, _, err := c.ExchangeContext(ctx, &m, resolver)
if err != nil {
return nil, err
}
var ips []string
for _, ans := range resp.Answer {
if a, ok := ans.(*dns.A); ok {
ips = append(ips, a.A.String())
}
}
return ips, nil
}
func resolveConfimation(domain string, ip string) {
fmt.Print("\n\033[32mPlease type 'confirm' if your DNS has propagated:\033[0m\n\n")
fmt.Printf("You can check that by if the IP on this website matches --> \033[33m%s\033[0m\n \n", ip)
fmt.Printf("Paste this in your browser --> \033[34m: https://www.nslookup.io/domains/%s/dns-records/\033[0m\n", domain)
fmt.Printf("\033[34m\033]8;;https://www.nslookup.io/domains/%s/dns-records/\033\\Or Click here to directly go to the website\033]8;;\033\\\033[0m\n\n", domain)
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ") // Prompt for input
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if strings.ToLower(input) == "confirm" {
fmt.Println("DNS propagation confirmed. Proceeding...")
break
} else {
fmt.Println("Invalid input. Please type 'confirm'.")
}
}
time.Sleep(10 * time.Second)
}