diff --git a/Kernel.cs b/Kernel.cs index 9a4d4ad..4724331 100644 --- a/Kernel.cs +++ b/Kernel.cs @@ -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) diff --git a/Lua.md b/Lua.md index ca6e47b..ca4de1c 100644 --- a/Lua.md +++ b/Lua.md @@ -1215,7 +1215,7 @@ print("Decoded:", decoded) ## 版本信息 -- **Lua 版本**:5.1 +- **Lua 版本**:5.2 - **UniLua 版本**:自定义实现 - **CMLeonOS 版本**:最新 diff --git a/NetworkConfigManager.cs b/NetworkConfigManager.cs new file mode 100644 index 0000000..04035bd --- /dev/null +++ b/NetworkConfigManager.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/Shell.cs b/Shell.cs index bf0063f..9986b0f 100644 --- a/Shell.cs +++ b/Shell.cs @@ -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 - Start TCP server on specified port", " tcpclient - Connect to TCP server", " wget - Download file from URL", + " setdns - Set DNS server", + " setgateway - Set gateway", + " ipconfig - Show network configuration", + " nslookup - DNS lookup", " whoami - Show current username", " base64 encrypt - Encode text to Base64", " base64 decrypt - 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 "); + 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 "); + 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 "); + 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}"); + } + } } } \ No newline at end of file