This commit is contained in:
2026-02-04 14:19:57 +08:00
parent 0020d125c7
commit 33afd6f2b5
4 changed files with 274 additions and 3 deletions

View File

@@ -78,8 +78,16 @@ namespace CMLeonOS
ShowError("Network device is not ready");
}
dhcp.SendDiscoverPacket();
IPAddress = NetworkConfiguration.CurrentAddress.ToString();
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");
}
catch (Exception ex)

2
Lua.md
View File

@@ -1215,7 +1215,7 @@ print("Decoded:", decoded)
## 版本信息
- **Lua 版本**5.1
- **Lua 版本**5.2
- **UniLua 版本**:自定义实现
- **CMLeonOS 版本**:最新

127
NetworkConfigManager.cs Normal file
View 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
View File

@@ -4,7 +4,10 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using Sys = Cosmos.System;
using Cosmos.System.Network;
using Cosmos.System.Network.Config;
using Cosmos.System.Network.IPv4;
using Cosmos.System.Network.IPv4.UDP.DNS;
using EndPoint = Cosmos.System.Network.IPv4.EndPoint;
using System.Threading;
using System.Net;
@@ -197,6 +200,10 @@ namespace CMLeonOS
" tcpserver <port> - Start TCP server on specified port",
" tcpclient <ip> <port> - Connect to TCP server",
" 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",
" base64 encrypt <text> - Encode text to Base64",
" base64 decrypt <text> - Decode Base64 to text",
@@ -446,6 +453,18 @@ namespace CMLeonOS
case "hostname":
ProcessHostnameCommand(args);
break;
case "setdns":
SetDnsServer(args);
break;
case "setgateway":
SetGateway(args);
break;
case "ipconfig":
ShowNetworkConfig();
break;
case "nslookup":
NsLookup(args);
break;
case "cpass":
userSystem.ChangePassword();
break;
@@ -2326,8 +2345,6 @@ namespace CMLeonOS
try
{
HttpRequest request = new HttpRequest();
string domain = "";
string path = "/";
@@ -2361,6 +2378,7 @@ namespace CMLeonOS
Console.WriteLine($"Path: {path}");
Console.WriteLine();
HttpRequest request = new HttpRequest();
request.Domain = domain;
request.Path = path;
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}");
}
}
}
}