-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathderpnet_file.c
261 lines (213 loc) · 5.29 KB
/
derpnet_file.c
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
#define _CRT_SECURE_NO_DEPRECATE
#define DERPNET_STATIC
#include "derpnet.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
static void PrintHelpAndExit(char* argv0)
{
printf(
"USAGE: %s s other_user filename\n"
"Sends file to other user:\n"
" - other_key = PUBLIC key of user to send to\n"
" - filename = file to send\n"
"\n"
"USAGE: %s r\n"
"Receives one file from first sender\n"
"\n"
, argv0, argv0);
exit(0);
}
static void PrintKey(const DerpKey* Key)
{
for (size_t i=0; i<32; i++)
{
printf("%02hhx", Key->Bytes[i]);
}
}
static DerpKey HexToKey(const char* Hex)
{
DERPNET_ASSERT(strlen(Hex) == 64);
DerpKey Key;
for (size_t i=0; i<32; i++)
{
sscanf(&Hex[2*i], "%02hhx", &Key.Bytes[i]);
}
return Key;
}
// choose any hostname from https://login.tailscale.com/derpmap/default
// both peers do not need to be connected to the same server
// but they must be connected to the same region
#define DERP_SERVER_HOST "derp1f.tailscale.com"
int main(int argc, char* argv[])
{
if (argc < 2)
{
PrintHelpAndExit(argv[0]);
}
if (strcmp(argv[1], "s") == 0)
{
if (argc != 4)
{
PrintHelpAndExit(argv[0]);
}
DerpKey MySecretKey;
DerpNet_CreateNewKey(&MySecretKey);
DerpKey OtherUser = HexToKey(argv[2]);
const char* FileName = argv[3];
FILE* File = fopen(FileName, "rb");
if (!File)
{
printf("ERROR: cannot open '%s' file for reading!\n", FileName);
}
DerpNet Net;
printf("Connecting to server... ");
if (!DerpNet_Open(&Net, DERP_SERVER_HOST, &MySecretKey))
{
printf("ERROR!\n");
exit(1);
}
printf("OK!\n");
printf("Sending filename... ");
if (!DerpNet_Send(&Net, &OtherUser, FileName, strlen(FileName)))
{
printf("ERROR!\n");
exit(1);
}
printf("OK!\n");
clock_t ClockStart = clock();
clock_t ClockNext = ClockStart + CLOCKS_PER_SEC;
size_t TotalSize = 0;
for (;;)
{
clock_t ClockNow = clock();
if (ClockNow >= ClockNext)
{
double Speed = TotalSize / ((double)(ClockNow - ClockStart) / CLOCKS_PER_SEC);
printf("\rSending: %zu KB - %.2f KB/s", TotalSize / 1024, Speed / 1024);
ClockNext = ClockNow + CLOCKS_PER_SEC;
}
char Buffer[4096];
size_t BufferSize = fread(Buffer, 1, sizeof(Buffer), File);
if (BufferSize == 0)
{
break;
}
if (!DerpNet_Send(&Net, &OtherUser, Buffer, BufferSize))
{
printf("ERROR!\n");
exit(1);
}
TotalSize += BufferSize;
}
// send 0 sized message for end of file
if (!DerpNet_Send(&Net, &OtherUser, NULL, 0))
{
printf("ERROR!\n");
exit(1);
}
printf("\nDone!\n");
double Time = (double)(clock() - ClockStart) / CLOCKS_PER_SEC;
double Speed = TotalSize / Time;
printf("\rSent %zu KB in %.1f seconds = %.2f KB/s\n", TotalSize / 1024, Time, Speed / 1024);
fclose(File);
DerpNet_Close(&Net);
}
else if (strcmp(argv[1], "r") == 0)
{
if (argc != 2)
{
PrintHelpAndExit(argv[0]);
}
DerpKey MySecretKey;
DerpNet_CreateNewKey(&MySecretKey);
DerpKey MyPublicKey;
DerpNet_GetPublicKey(&MySecretKey, &MyPublicKey);
printf("My PUBLIC key is: ");
PrintKey(&MyPublicKey);
printf("\n");
DerpNet Net;
printf("Connecting to server... ");
if (!DerpNet_Open(&Net, DERP_SERVER_HOST, &MySecretKey))
{
printf("ERROR!\n");
exit(1);
}
printf("OK!\n");
printf("Waiting for filename... ");
DerpKey ExpectedUser;
FILE* File;
{
uint8_t* ReceiveData;
uint32_t ReceiveSize;
DerpKey ReceiveUser;
int ReceiveResult = DerpNet_Recv(&Net, &ReceiveUser, &ReceiveData, &ReceiveSize, true);
if (ReceiveResult <= 0)
{
printf("ERROR!\n");
exit(1);
}
char FileName[256];
snprintf(FileName, sizeof(FileName), "%.*s", (int)ReceiveSize, (char*)ReceiveData);
if (strchr(FileName, '/') || strchr(FileName, '\\'))
{
printf("ERROR: filename contains bad characters!");
exit(1);
}
printf("receiving '%s' file\n", FileName);
File = fopen(FileName, "wb");
if (!File)
{
printf("ERROR: cannot open '%s' file for writing!\n", FileName);
exit(1);
}
ExpectedUser = ReceiveUser;
}
clock_t ClockStart = clock();
clock_t ClockNext = ClockStart + CLOCKS_PER_SEC;
size_t TotalSize = 0;
for (;;)
{
uint8_t* ReceiveData;
uint32_t ReceiveSize;
DerpKey ReceiveUser;
int ReceiveResult = DerpNet_Recv(&Net, &ReceiveUser, &ReceiveData, &ReceiveSize, true);
if (ReceiveResult <= 0)
{
printf("ERROR!\n");
exit(1);
}
if (memcmp(&ReceiveUser, &ExpectedUser, sizeof(ReceiveUser)) == 0)
{
if (fwrite(ReceiveData, 1, ReceiveSize, File) != ReceiveSize)
{
printf("ERROR writing to file\n");
exit(1);
}
// received 0 sized message, end of file
if (ReceiveSize == 0)
{
break;
}
TotalSize += ReceiveSize;
clock_t ClockNow = clock();
if (ClockNow >= ClockNext)
{
double Speed = TotalSize / ((double)(ClockNow - ClockStart) / CLOCKS_PER_SEC);
printf("\rReceiving: %zu KB - %.2f KB/s", TotalSize / 1024, Speed / 1024);
ClockNext = ClockNow + CLOCKS_PER_SEC;
}
}
}
printf("\nDone!\n");
double Time = (double)(clock() - ClockStart) / CLOCKS_PER_SEC;
double Speed = TotalSize / Time;
printf("\rReceived %zu KB in %.1f seconds = %.2f KB/s\n", TotalSize / 1024, Time, Speed / 1024);
fclose(File);
DerpNet_Close(&Net);
}
else
{
PrintHelpAndExit(argv[0]);
}
}