internal void Refresh() { if (string.IsNullOrEmpty(_transmuterTorOptions.TorrcFile) || !File.Exists(_transmuterTorOptions.TorrcFile)) { if (!string.IsNullOrEmpty(_transmuterTorOptions.TorrcFile)) { _logger.LogWarning("Torrc file is not found"); } Services = Array.Empty <TorService>(); return; } List <TorService> result = new List <TorService>(); try { var torrcContent = File.ReadAllText(_transmuterTorOptions.TorrcFile); if (!Torrc.TryParse(torrcContent, out var torrc)) { _logger.LogWarning("Torrc file could not be parsed"); Services = Array.Empty <TorService>(); return; } var services = torrc.ServiceDirectories.SelectMany(d => d.ServicePorts.Select(p => (Directory: new DirectoryInfo(d.DirectoryPath), VirtualPort: p.VirtualPort))) .Select(d => (ServiceName: d.Directory.Name, ReadingLines: System.IO.File.ReadAllLines(Path.Combine(d.Directory.FullName, "hostname")), VirtualPort: d.VirtualPort)) .ToArray(); foreach (var service in services) { try { var onionHost = (service.ReadingLines)[0].Trim(); var torService = new TorService() { Name = service.ServiceName, OnionHost = $"http://{onionHost}", VirtualPort = service.VirtualPort }; result.Add(torService); } catch (Exception ex) { _logger.LogWarning(ex, $"Error while reading hidden service {service.ServiceName} configuration"); } } } catch (Exception ex) { _logger.LogWarning(ex, $"Error while reading torrc file"); } Services = result.ToArray(); }
public static bool TryParse(string str, out Torrc value) { value = null; List<HiddenServiceDir> serviceDirectories = new List<HiddenServiceDir>(); var lines = str.Split(new char[] { '\n' }); HiddenServiceDir currentDirectory = null; foreach (var line in lines) { if (HiddenServiceDir.TryParse(line, out var dir)) { serviceDirectories.Add(dir); currentDirectory = dir; } else if (HiddenServicePortDefinition.TryParse(line, out var portDef) && currentDirectory != null) { currentDirectory.ServicePorts.Add(portDef); } } value = new Torrc() { ServiceDirectories = serviceDirectories }; return true; }