/
upload_command.go
104 lines (87 loc) · 1.95 KB
/
upload_command.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
package cli
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"github.com/nikola43/stardust/crypto"
)
type UploadCommand struct {
args *Args
}
const (
uploadCommand = "upload"
uploadDescription = "upload file to chain"
)
func newUploadCommand() Command {
return Command{
Name: uploadCommand,
Description: uploadDescription,
Exec: &UploadCommand{},
}
}
func (c *UploadCommand) ExecCommand(ctx context.Context, args []string) error {
c.args = &Args{args}
if len(args) == 0 {
return ErrorFromString(fmt.Sprintf("file not found"))
}
fi, err := os.Stat(args[0])
if err != nil {
return ErrorFromString(fmt.Sprintf("file not found"))
}
// get the size
size := fi.Size()
if size == 0 {
return ErrorFromString(fmt.Sprintf("file corupted"))
}
if len(args) < 2 {
return ErrorFromString(fmt.Sprintf("server ip"))
}
isValid := checkIPAddress(args[1])
if !isValid {
return ErrorFromString(fmt.Sprintf("ip invalid format"))
}
if len(args) < 3 {
return ErrorFromString(fmt.Sprintf("server port"))
}
dat, err := ioutil.ReadFile(args[0])
if err != nil {
fmt.Println(err)
return ErrorFromString(err.Error())
}
sCipher := &crypto.GCM{
Passphrase: "6368616e676520746869732070617373776f726420746f206120736563726574",
}
sCipher.Init()
b, err := sCipher.Encrypt([]byte(dat))
if err != nil {
fmt.Println("Encrypt")
panic(err)
}
conn, err := net.Dial("udp", args[1]+":"+args[2])
if err != nil {
fmt.Println(err)
panic(err)
}
if conn == nil {
return ErrorFromString("Invalid connection")
}
bytes, err := fmt.Fprintf(conn, string(b))
if err != nil {
fmt.Println(err)
panic(err)
}
fmt.Println(bytes)
//return ErrorFromString(fmt.Sprintf("%s: invalid subcommand passed", uploadCommand))
return nil
}
func checkIPAddress(ip string) bool {
if net.ParseIP(ip) == nil {
fmt.Printf("IP Address: %s - Invalid\n", ip)
return false
} else {
fmt.Printf("IP Address: %s - Valid\n", ip)
return true
}
}