/
main.go
345 lines (288 loc) · 6.05 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
const SERVERURL = "http://localhost:3000/"
const OUTPUTDIR = "output/"
const RECEIVERPORT = "8080"
/*
type DownResp struct {
ip string
filename string
ok bool
}
type UpResp struct {
key string
ok bool
}
*/
type SendResp struct {
ip string
filename string
ok bool
}
type ExpectResp struct {
key string
ok bool
}
// **Endpoints**
// /expect
// Server stores IP and responds with keyword
// /send?key=KEY
// Server responds with IP
func send(path string, keyword string) error {
// Request server with key, get back IP
var body SendResp
resp, err := http.Get(SERVERURL + "send?key=" + keyword + "&filename=" + filepath.Base(path))
if err != nil {
return err
}
json.NewDecoder(resp.Body).Decode(&body)
if !body.ok {
return fmt.Errorf("invalid key (or server error)")
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
buf := &bytes.Buffer{}
writer := multipart.NewWriter(buf)
// Create a form field for the filename
filenameField, err := writer.CreateFormField("filename")
if err != nil {
return err
}
filenameField.Write([]byte(filepath.Base(path)))
// Create a form field for the file
fileField, err := writer.CreateFormFile("file", file.Name())
if err != nil {
return err
}
// Copy the file content to the form field
_, err = io.Copy(fileField, file)
if err != nil {
return err
}
writer.Close()
// Create a POST request with `buf` (mulipart form data)
_, err = http.NewRequest("POST", body.ip, buf)
if err != nil {
return err
}
fmt.Println("success. sent file to " + body.ip)
return nil
}
func handleFile(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("file")
if err != nil {
fmt.Println("Error retrieving the file:", err)
http.Error(w, "Error retrieving the file", http.StatusBadRequest)
return
}
defer file.Close()
out, err := os.Create(OUTPUTDIR + handler.Filename)
if err != nil {
fmt.Println("Error creating the file:", err)
http.Error(w, "Error creating the file", http.StatusInternalServerError)
return
}
defer out.Close()
_, err = io.Copy(out, r.Body)
if err != nil {
fmt.Println("Error copying the file:", err)
http.Error(w, "Error copying the file", http.StatusInternalServerError)
return
}
fmt.Println("success. recieved file.")
}
func expect(path string) error {
_, err := os.Stat(path)
// Does the file exist
if !os.IsNotExist(err) {
if err != nil {
return err
}
fmt.Println("this file already exists. do you want to overwrite? (y/N)")
var ans string
fmt.Scanln(&ans)
if ans != "y" && ans != "Y" {
return nil
}
}
resp, err := http.Get(SERVERURL + "expect?filename=" + path)
if err != nil {
return err
}
var body ExpectResp
json.NewDecoder(resp.Body).Decode(&body)
// Receive keyword from server and print
fmt.Println("your keyword is...: " + body.key)
http.HandleFunc("/file", handleFile)
http.ListenAndServe(":"+RECEIVERPORT, nil)
return nil
}
func main() {
if len(os.Args) < 2 {
fmt.Println("USAGE: sink COMMAND")
return
}
var err error
switch os.Args[1] {
case "expect":
if len(os.Args) < 3 {
fmt.Println("USAGE: sink expect OUTPUT_PATH")
return
}
err = expect(os.Args[2])
case "send":
if len(os.Args) < 4 {
fmt.Println("USAGE: sink send FILE_PATH KEYWORD")
return
}
err = send(os.Args[2], os.Args[3])
default:
fmt.Println("unknown command.")
return
}
if err != nil {
fmt.Println(err)
return
}
fmt.Println("exiting.")
}
/*
func main() {
if len(os.Args) < 3 {
fmt.Println("USAGE: sink COMMAND PATH/KEY")
return
}
key := os.Args[2]
var err error
switch os.Args[1] {
case "up":
var dir string
dir, err = os.Getwd()
if err != nil {
break
}
path := filepath.Join(dir, key)
_, err = os.Stat(path)
if err != nil {
break
}
err = up(&path)
case "down":
err = down(&key)
case "expect":
err = expect(&key)
case "send":
err = send(filepath.Base(keyword), &key)
default:
fmt.Println("unknown command.")
return
}
if err != nil {
fmt.Println(err)
return
}
fmt.Println("exiting.")
}*/
/*
func up(path *string) error {
// TODO merge this with line 49
resp, err := http.Post(
SERVERURL,
"text/plain",
// Grabs filename from the end of the path
bytes.NewBuffer(([]byte(filepath.Base(*path)))),
)
if err != nil {
return err
}
var body UpResp
json.NewDecoder(resp.Body).Decode(&body)
if !body.ok {
return fmt.Errorf("server error. sorry !")
}
fmt.Println("*** success. key is " + body.key + " ***")
client := sse.NewClient(serverURL)
events := client.Subscribe()
for {
select {
case event := <-events:
// Split the event data by newlines
dataLines := strings.Split(event.Data, "\n")
if len(dataLines) >= 2 {
// Extract the key and IP address
key := dataLines[0]
ipAddress := dataLines[1]
// Print the key
fmt.Println("Received key:", key)
// Send a POST request to the IP address
err := sendPOSTRequest(ipAddress)
if err != nil {
fmt.Println("Error sending POST request:", err)
}
}
}
}
fmt.Println("Do you want to remove this file? (y/N)")
var ans string
fmt.Scanln(&ans)
if ans == "y" || ans == "Y" {
err = os.Remove(*path)
if err != nil {
return err
}
}
return nil
}
func down(key *string) error {
// Request server with key
resp, err := http.Get(SERVERURL + "?key=" + *key)
if err != nil {
return err
}
// Server responds with IP of target
var body DownResp
json.NewDecoder(resp.Body).Decode(&body)
if !body.ok {
return fmt.Errorf("invalid key")
}
/*
resp, err = http.Get(body.ip)
if err != nil {
return err
}
defer resp.Body.Close()
*/
// THIS OR
/*
conn, err := net.Dial("tcp", body.ip)
if err != nil {
return err
}
defer conn.Close()
*/
/*
out, err := os.Create(OUTPUTDIR + body.filename)
if err != nil {
return err
}
// Download file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
fmt.Println("success. saved file to path " + "output")
return nil
}
*/