mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
网络
This commit is contained in:
@@ -78,8 +78,16 @@ namespace CMLeonOS
|
|||||||
ShowError("Network device is not ready");
|
ShowError("Network device is not ready");
|
||||||
}
|
}
|
||||||
dhcp.SendDiscoverPacket();
|
dhcp.SendDiscoverPacket();
|
||||||
|
|
||||||
IPAddress = NetworkConfiguration.CurrentAddress.ToString();
|
IPAddress = NetworkConfiguration.CurrentAddress.ToString();
|
||||||
Console.WriteLine($"Local IP: {IPAddress}");
|
Console.WriteLine($"Local IP: {IPAddress}");
|
||||||
|
|
||||||
|
string gateway = NetworkConfigManager.Instance.GetGateway();
|
||||||
|
Console.WriteLine($"Gateway: {gateway}");
|
||||||
|
|
||||||
|
string dns = NetworkConfigManager.Instance.GetDNS();
|
||||||
|
Console.WriteLine($"DNS Server: {dns}");
|
||||||
|
|
||||||
ShowSuccess("Network started");
|
ShowSuccess("Network started");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
2
Lua.md
2
Lua.md
@@ -1215,7 +1215,7 @@ print("Decoded:", decoded)
|
|||||||
|
|
||||||
## 版本信息
|
## 版本信息
|
||||||
|
|
||||||
- **Lua 版本**:5.1
|
- **Lua 版本**:5.2
|
||||||
- **UniLua 版本**:自定义实现
|
- **UniLua 版本**:自定义实现
|
||||||
- **CMLeonOS 版本**:最新
|
- **CMLeonOS 版本**:最新
|
||||||
|
|
||||||
|
|||||||
127
NetworkConfigManager.cs
Normal file
127
NetworkConfigManager.cs
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace CMLeonOS
|
||||||
|
{
|
||||||
|
public class NetworkConfigManager
|
||||||
|
{
|
||||||
|
private static NetworkConfigManager instance;
|
||||||
|
private string configFilePath = @"0:\system\network.dat";
|
||||||
|
|
||||||
|
private string dns = "1.1.1.1";
|
||||||
|
private string gateway = "192.168.0.1";
|
||||||
|
|
||||||
|
private NetworkConfigManager()
|
||||||
|
{
|
||||||
|
LoadConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static NetworkConfigManager Instance
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (instance == null)
|
||||||
|
{
|
||||||
|
instance = new NetworkConfigManager();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LoadConfig()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (File.Exists(configFilePath))
|
||||||
|
{
|
||||||
|
string[] lines = File.ReadAllLines(configFilePath);
|
||||||
|
|
||||||
|
foreach (string line in lines)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
string[] parts = line.Split('=');
|
||||||
|
if (parts.Length == 2)
|
||||||
|
{
|
||||||
|
string key = parts[0].Trim().ToLower();
|
||||||
|
string value = parts[1].Trim();
|
||||||
|
|
||||||
|
if (key == "dns")
|
||||||
|
{
|
||||||
|
dns = value;
|
||||||
|
}
|
||||||
|
else if (key == "gateway")
|
||||||
|
{
|
||||||
|
gateway = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error loading network config: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SaveConfig()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string directory = Path.GetDirectoryName(configFilePath);
|
||||||
|
if (!Directory.Exists(directory))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(directory);
|
||||||
|
}
|
||||||
|
|
||||||
|
using (StreamWriter writer = new StreamWriter(configFilePath))
|
||||||
|
{
|
||||||
|
writer.WriteLine("# CMLeonOS Network Configuration");
|
||||||
|
writer.WriteLine($"# Generated on {DateTime.Now}");
|
||||||
|
writer.WriteLine();
|
||||||
|
writer.WriteLine($"DNS={dns}");
|
||||||
|
writer.WriteLine($"Gateway={gateway}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error saving network config: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetDNS()
|
||||||
|
{
|
||||||
|
return dns;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetGateway()
|
||||||
|
{
|
||||||
|
return gateway;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetDNS(string dnsIp)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(dnsIp))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("DNS address cannot be empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
dns = dnsIp;
|
||||||
|
SaveConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetGateway(string gatewayIp)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(gatewayIp))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Gateway address cannot be empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
gateway = gatewayIp;
|
||||||
|
SaveConfig();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
140
Shell.cs
140
Shell.cs
@@ -4,7 +4,10 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Sys = Cosmos.System;
|
using Sys = Cosmos.System;
|
||||||
|
using Cosmos.System.Network;
|
||||||
|
using Cosmos.System.Network.Config;
|
||||||
using Cosmos.System.Network.IPv4;
|
using Cosmos.System.Network.IPv4;
|
||||||
|
using Cosmos.System.Network.IPv4.UDP.DNS;
|
||||||
using EndPoint = Cosmos.System.Network.IPv4.EndPoint;
|
using EndPoint = Cosmos.System.Network.IPv4.EndPoint;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@@ -197,6 +200,10 @@ namespace CMLeonOS
|
|||||||
" tcpserver <port> - Start TCP server on specified port",
|
" tcpserver <port> - Start TCP server on specified port",
|
||||||
" tcpclient <ip> <port> - Connect to TCP server",
|
" tcpclient <ip> <port> - Connect to TCP server",
|
||||||
" wget <url> - Download file from URL",
|
" wget <url> - Download file from URL",
|
||||||
|
" setdns <ip> - Set DNS server",
|
||||||
|
" setgateway <ip> - Set gateway",
|
||||||
|
" ipconfig - Show network configuration",
|
||||||
|
" nslookup <domain> - DNS lookup",
|
||||||
" whoami - Show current username",
|
" whoami - Show current username",
|
||||||
" base64 encrypt <text> - Encode text to Base64",
|
" base64 encrypt <text> - Encode text to Base64",
|
||||||
" base64 decrypt <text> - Decode Base64 to text",
|
" base64 decrypt <text> - Decode Base64 to text",
|
||||||
@@ -446,6 +453,18 @@ namespace CMLeonOS
|
|||||||
case "hostname":
|
case "hostname":
|
||||||
ProcessHostnameCommand(args);
|
ProcessHostnameCommand(args);
|
||||||
break;
|
break;
|
||||||
|
case "setdns":
|
||||||
|
SetDnsServer(args);
|
||||||
|
break;
|
||||||
|
case "setgateway":
|
||||||
|
SetGateway(args);
|
||||||
|
break;
|
||||||
|
case "ipconfig":
|
||||||
|
ShowNetworkConfig();
|
||||||
|
break;
|
||||||
|
case "nslookup":
|
||||||
|
NsLookup(args);
|
||||||
|
break;
|
||||||
case "cpass":
|
case "cpass":
|
||||||
userSystem.ChangePassword();
|
userSystem.ChangePassword();
|
||||||
break;
|
break;
|
||||||
@@ -2326,8 +2345,6 @@ namespace CMLeonOS
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HttpRequest request = new HttpRequest();
|
|
||||||
|
|
||||||
string domain = "";
|
string domain = "";
|
||||||
string path = "/";
|
string path = "/";
|
||||||
|
|
||||||
@@ -2361,6 +2378,7 @@ namespace CMLeonOS
|
|||||||
Console.WriteLine($"Path: {path}");
|
Console.WriteLine($"Path: {path}");
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|
||||||
|
HttpRequest request = new HttpRequest();
|
||||||
request.Domain = domain;
|
request.Domain = domain;
|
||||||
request.Path = path;
|
request.Path = path;
|
||||||
request.Method = "GET";
|
request.Method = "GET";
|
||||||
@@ -2661,5 +2679,123 @@ namespace CMLeonOS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SetDnsServer(string args)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(args))
|
||||||
|
{
|
||||||
|
ShowError("Usage: setdns <ip_address>");
|
||||||
|
ShowError("Example: setdns 8.8.8.8");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string dnsIp = args.Trim();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NetworkConfigManager.Instance.SetDNS(dnsIp);
|
||||||
|
ShowSuccess($"DNS server set to: {dnsIp}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ShowError($"Error setting DNS: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetGateway(string args)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(args))
|
||||||
|
{
|
||||||
|
ShowError("Usage: setgateway <ip_address>");
|
||||||
|
ShowError("Example: setgateway 192.168.1.1");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string gatewayIp = args.Trim();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NetworkConfigManager.Instance.SetGateway(gatewayIp);
|
||||||
|
ShowSuccess($"Gateway set to: {gatewayIp}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ShowError($"Error setting gateway: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ShowNetworkConfig()
|
||||||
|
{
|
||||||
|
Console.WriteLine("====================================");
|
||||||
|
Console.WriteLine(" Network Configuration");
|
||||||
|
Console.WriteLine("====================================");
|
||||||
|
Console.WriteLine();
|
||||||
|
|
||||||
|
Console.WriteLine($"Network Device: {Kernel.NetworkDevice?.Name ?? "Not available"}");
|
||||||
|
Console.WriteLine($"Local IP: {Kernel.IPAddress}");
|
||||||
|
|
||||||
|
string gateway = NetworkConfigManager.Instance.GetGateway();
|
||||||
|
Console.WriteLine($"Gateway: {gateway}");
|
||||||
|
|
||||||
|
string dns = NetworkConfigManager.Instance.GetDNS();
|
||||||
|
Console.WriteLine($"DNS Server: {dns}");
|
||||||
|
|
||||||
|
Console.WriteLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void NsLookup(string args)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(args))
|
||||||
|
{
|
||||||
|
ShowError("Usage: nslookup <domain>");
|
||||||
|
ShowError("Example: nslookup github.com");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string domain = args.Trim();
|
||||||
|
|
||||||
|
string dnsStr = NetworkConfigManager.Instance.GetDNS();
|
||||||
|
if (string.IsNullOrWhiteSpace(dnsStr))
|
||||||
|
{
|
||||||
|
ShowError("DNS server not configured. Use 'setdns' command first.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Address dns = Address.Parse(dnsStr);
|
||||||
|
|
||||||
|
Console.WriteLine("====================================");
|
||||||
|
Console.WriteLine(" DNS Lookup");
|
||||||
|
Console.WriteLine("====================================");
|
||||||
|
Console.WriteLine();
|
||||||
|
Console.WriteLine($"Domain: {domain}");
|
||||||
|
Console.WriteLine($"DNS Server: {dnsStr}");
|
||||||
|
Console.WriteLine();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var dnsClient = new DnsClient())
|
||||||
|
{
|
||||||
|
dnsClient.Connect(dns);
|
||||||
|
Console.WriteLine("Sending DNS query...");
|
||||||
|
|
||||||
|
dnsClient.SendAsk(domain);
|
||||||
|
|
||||||
|
Address result = dnsClient.Receive();
|
||||||
|
|
||||||
|
if (result != null)
|
||||||
|
{
|
||||||
|
ShowSuccess($"Resolved: {domain} -> {result}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ShowError("DNS query failed or timed out");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ShowError($"DNS lookup error: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user