-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (163 loc) · 4.14 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
)
const VersionMajor = 2
const VersionMinor = 0
type fileEntity struct {
filePath string
baseDir string
}
func main() {
hostPtr := flag.String("connect", "", "Host address")
listenPtr := flag.Bool("listen", false, "Listen for connection")
portPtr := flag.Int("port", 3214, "Connection port")
helpPtr := flag.Bool("help", false, "Show help")
versionPtr := flag.Bool("version", false, "Show version")
dirPtr := flag.String("dir", "", "Directory for receiving files")
flag.Parse()
dir, err := filepath.Abs(*dirPtr)
if err != nil {
fmt.Println(Colored("✘ Unable to check dir %s", ColorRed, *dirPtr))
return
}
if helpPtr != nil && *helpPtr == true {
flag.Usage()
return
}
if versionPtr != nil && *versionPtr == true {
fmt.Printf("Version: %d.%d\n", VersionMajor, VersionMinor)
return
}
var files []fileEntity
args := flag.Args()
if len(args) > 0 {
for _, arg := range args {
var argAbs string
var stat os.FileInfo
if argAbs, err = filepath.Abs(arg); err == nil {
stat, err = os.Stat(argAbs)
}
if err != nil {
fmt.Println(Colored("✘ Unable to open %s", ColorRed, arg))
return
}
if stat.IsDir() {
if dirFiles, err := scanDir(argAbs); err == nil {
files = append(files, dirFiles...)
}
if err != nil {
fmt.Println(Colored("✘ Unable to scan dir %s", ColorRed, arg))
return
}
} else {
e := fileEntity{
filePath: argAbs,
baseDir: filepath.Dir(argAbs),
}
files = append(files, e)
}
}
}
conn := NewConnection()
address := fmt.Sprintf("%s:%d", *hostPtr, *portPtr)
if hostPtr != nil && *hostPtr != "" {
fmt.Println(Colored("☛ Connecting to %s", ColorCyan, address))
if _, err := conn.Connect(address); err == nil {
defer safeDisconnect(conn)
fmt.Println(Colored("⇄ Connected", ColorCyan))
proto := NewProto(conn)
sendFiles(proto, files)
receiveFiles(proto, dir)
fmt.Println(Colored("⇵ Transfer done", ColorCyan))
} else {
fmt.Println(Colored("✘ Unable to connect to %s", ColorRed, address))
}
} else if listenPtr != nil && *listenPtr {
fmt.Println(Colored("☛ Listening...", ColorCyan))
if _, err := conn.Listen(address); err == nil {
defer safeDisconnect(conn)
fmt.Println(Colored("⇄ Connected", ColorCyan))
proto := NewProto(conn)
receiveFiles(proto, dir)
sendFiles(proto, files)
fmt.Println(Colored("⇵ Transfer done", ColorCyan))
} else {
fmt.Println(Colored("✘ Unable to listen on %s", ColorRed, address))
}
} else {
flag.Usage()
}
}
func sendFiles(proto *Proto, files []fileEntity) {
for _, file := range files {
var progress *TtyProgress
err := proto.SendFile(file.baseDir, file.filePath, func(relDir string, size int64) {
progress = NewProgress(relDir, filepath.Base(file.filePath), size, Receiving)
}, func(total int64) {
progress.Draw(total)
})
if err != nil {
progress.Failed(err)
fmt.Println()
return
} else {
progress.Done()
fmt.Println()
}
}
_ = proto.SendDone()
}
func receiveFiles(proto *Proto, dir string) {
for {
var progress *TtyProgress
hasMore, err := proto.ReadFile(dir, func(relDir string, name string, size int64) {
progress = NewProgress(relDir, name, size, Sending)
}, func(total int64) {
progress.Draw(total)
})
if !hasMore {
break
}
if err == ErrInvalidMD5Sum {
progress.Warning(err)
fmt.Println()
} else if err != nil {
if progress != nil {
progress.Failed(err)
} else {
fmt.Print(Colored("✘ Receive file error: %s", ColorRed, err.Error()))
}
fmt.Println()
return
} else {
progress.Done()
fmt.Println()
}
}
}
func safeDisconnect(conn *Connection) {
err := conn.Disconnect()
if err != nil {
fmt.Println(Colored("✘ Disconnection failure", ColorRed))
return
}
fmt.Println(Colored("↮ Disconnected", ColorCyan))
}
func scanDir(base string) ([]fileEntity, error) {
var files []fileEntity
err := filepath.Walk(base, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
e := fileEntity{
filePath: path,
baseDir: base,
}
files = append(files, e)
}
return nil
})
return files, err
}