-
Notifications
You must be signed in to change notification settings - Fork 0
/
KernelCurl.cs
281 lines (245 loc) · 8.8 KB
/
KernelCurl.cs
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
using Cosmos.HAL;
using Cosmos.System.Network;
using Cosmos.System.Network.Config;
using Cosmos.System.Network.IPv4;
using Cosmos.System.Network.IPv4.TCP;
using Cosmos.System.Network.IPv4.UDP.DHCP;
using Cosmos.System.Network.IPv4.UDP.DNS;
using System;
using System.Text;
namespace Quantum
{
class HTTPListener
{
public static void Execute(string arg, string name)
{
try
{
var dnsClient = new DnsClient();
var tcpClient = new TcpClient(80);
dnsClient.Connect(DNSConfig.Server(0));
dnsClient.SendAsk(arg);
Address address = dnsClient.Receive();
dnsClient.Close();
tcpClient.Connect(address, 80);
string httpget = "GET / HTTP/1.1\r\n" +
"User-Agent: Wget (CosmosOS)\r\n" +
"Accept: */*\r\n" +
"Accept-Encoding: identity\r\n" +
"Host: " + arg + "\r\n" +
"Connection: Keep-Alive\r\n\r\n";
tcpClient.Send(Encoding.ASCII.GetBytes(httpget));
var ep = new EndPoint(Address.Zero, 0);
byte[] data = tcpClient.Receive(ref ep);
VFS.Touch(Kernel.dir() + name);
VFS.ByteArrayToFile(Kernel.dir() + name, data);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Kernel.print(ex.ToString());
Console.ResetColor();
}
Kernel.print("curl: success");
}
}
class Ping
{
public static void Execute(string domain)
{
int PacketSent = 0;
int PacketReceived = 0;
int PacketLost = 0;
int PercentLoss;
Address source;
Address destination = Address.Parse(domain);
if (destination != null)
{
source = IPConfig.FindNetwork(destination);
}
else //Make a DNS request if it's not an IP
{
var xClient = new DnsClient();
xClient.Connect(DNSConfig.Server(0));
xClient.SendAsk(domain);
destination = xClient.Receive();
xClient.Close();
if (destination == null)
{
Kernel.err("curl: failed to get DNS respone");
return;
}
source = IPConfig.FindNetwork(destination);
}
try
{
Kernel.print("Sending ping to " + destination.ToString());
var xClient = new ICMPClient();
xClient.Connect(destination);
for (int i = 0; i < 4; i++)
{
xClient.SendEcho();
PacketSent++;
var endpoint = new EndPoint(Address.Zero, 0);
int second = xClient.Receive(ref endpoint, 4000);
if (second == -1)
{
Kernel.print("Destination host unreachable.");
PacketLost++;
}
else
{
if (second < 1)
{
Kernel.print("Reply received from " + endpoint.Address.ToString() + " time < 1s");
}
else if (second >= 1)
{
Kernel.print("Reply received from " + endpoint.Address.ToString() + " time " + second + "s");
}
PacketReceived++;
}
}
xClient.Close();
}
catch
{
Kernel.err("curl: ping process error");
return;
}
PercentLoss = 25 * PacketLost;
Kernel.print();
Kernel.print("Ping statistics for " + destination.ToString() + ":");
Kernel.print(" Packets: Sent = " + PacketSent + ", Received = " + PacketReceived + ", Lost = " + PacketLost + " (" + PercentLoss + "% loss)");
}
}
class DNS
{
public static void Execute(string arg)
{
var xClient = new DnsClient();
string domainname;
xClient.Connect(DNSConfig.Server(0));
Kernel.print("DNS used : " + DNSConfig.Server(0).ToString());
xClient.SendAsk(arg);
domainname = arg;
Address address = xClient.Receive();
xClient.Close();
if (address == null)
{
Kernel.err("curl: bad domain");
return;
}
else
{
Kernel.print(domainname + " is " + address.ToString());
}
}
}
class ICP
{
public static void Empty()
{
if (NetworkStack.ConfigEmpty())
{
Kernel.print("No network configuration detected! Use ipconfig /help");
}
foreach (NetworkDevice device in NetworkConfig.Keys)
{
switch (device.CardType)
{
case CardType.Ethernet:
Kernel.print("Ethernet Card : " + device.NameID + " - " + device.Name);
break;
case CardType.Wireless:
Kernel.print("Wireless Card : " + device.NameID + " - " + device.Name);
break;
}
if (NetworkConfig.CurrentConfig.Key == device)
{
Kernel.print(" (current)");
}
else
{
Kernel.print();
}
Kernel.print("MAC Address : " + device.MACAddress.ToString());
Kernel.print("IP Address : " + NetworkConfig.Get(device).IPAddress.ToString());
Kernel.print("Subnet mask : " + NetworkConfig.Get(device).SubnetMask.ToString());
Kernel.print("Default Gateway : " + NetworkConfig.Get(device).DefaultGateway.ToString());
Kernel.print("DNS Nameservers : ");
foreach (Address dnsnameserver in DNSConfig.DNSNameservers)
{
Kernel.print(" " + dnsnameserver.ToString());
}
}
}
}
class FTP
{
public static void Execute()
{
Kernel.print("Soon");
}
}
class KernelCurl
{
public static void Init()
{
Kernel.print("NetworkStack: maybe success.");
using (var xClient = new DHCPClient())
{
Kernel.print("Waiting...");
Kernel.print("Success!");
}
Kernel.print("DHCPCLient: sendDiscoverPacket");
Kernel.print("curl: Success.");
}
public static void start(string[] parts)
{
string opinion = parts[1];
switch (opinion)
{
case "ip":
{
Kernel.print(NetworkConfig.CurrentConfig.Value.IPAddress.ToString());
break;
}
case "url":
{
HTTPListener.Execute(parts[2], parts[3]);
break;
}
case "ping":
{
Ping.Execute(parts[2]);
break;
}
case "dns":
{
DNS.Execute(parts[2]);
break;
}
case "ftp":
{
Kernel.print("Port: 21");
FTP.Execute();
break;
}
case "ipconfig":
{
if (parts.Length == 2)
{
ICP.Empty();
}
break;
}
default:
{
Kernel.err("curl: unknown command: " + opinion);
break;
}
}
}
}
}