Guest User

Untitled

a guest
Oct 6th, 2025
111
0
362 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. public class ApiClient
  2. {
  3.     private static readonly HttpClient _httpClient;
  4.     private string ServerAddress => IP;
  5.     private string Address => pathService.OdbcClientAddress;
  6.     private readonly IPathService pathService; // ignore this
  7.     public string IP => pathService.OdbcClientIP;
  8.     public static readonly SemaphoreSlim MDnsResolveLock = new SemaphoreSlim(1, 1);
  9.  
  10.     public ApiClient(IPathService pathService)
  11.     {
  12.         this.pathService = pathService;
  13.     }
  14.  
  15.     static ApiClient()
  16.     {
  17.         byte[] trustedCertBytes;
  18.         var assembly = Assembly.GetExecutingAssembly();
  19.         using (var stream = assembly.GetManifestResourceStream("MyApp.cert.cer")!)
  20.         {
  21.             trustedCertBytes = new byte[stream.Length];
  22.             stream.ReadExactly(trustedCertBytes);
  23.         }
  24.  
  25.         // Use certificate pinning
  26.         var handler = new HttpClientHandler
  27.         {
  28.             ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
  29.             {
  30.                 return cert!.GetRawCertData().SequenceEqual(trustedCertBytes);
  31.             }
  32.         };
  33.  
  34.         _httpClient = new HttpClient(handler);
  35.         _httpClient.Timeout = TimeSpan.FromSeconds(20);
  36.         _httpClient.DefaultRequestHeaders.Add("x-api-key", Constants.API_KEY);
  37.     }
  38.  
  39.     public async Task<Result> GetIP()
  40.     {
  41.         await MDnsResolveLock.WaitAsync();
  42.        
  43.         try
  44.         {
  45.             if (OperatingSystem.IsWindows())
  46.             {
  47.                 if (Address.Any(char.IsLetter))
  48.                     pathService.OdbcClientIP = Address + ".local";
  49.                 else
  50.                     pathService.OdbcClientIP = Address;
  51.                 pathService.MDnsResolved = true; // MDnsResolved just checks if it was already resolved, otherwise it skips this GetIP method. When there is an exception then it marks it as false
  52.                 return Result.Success; // Self-made class Result
  53.             }
  54.  
  55.             if (pathService.MDnsResolved) return Result.Success;
  56.             if (Address.Any(char.IsLetter))
  57.             { // The address was written as a computer name instead of an IP, so look for IP (Android can't reoslve on its own)
  58.                 for (int i = 0; i < 5; i++)
  59.                 {
  60.                     string? address = await ResolveMdnsHostAsync(Address);
  61.                     if (address != null)
  62.                     {
  63.                         pathService.OdbcClientIP = address;
  64.                         pathService.MDnsResolved = true;
  65.                         break;
  66.                     }
  67.                     await Task.Delay(200);
  68.                 }
  69.             }
  70.             else
  71.             {
  72.                 pathService.OdbcClientIP = Address;
  73.                 pathService.MDnsResolved = true;
  74.             }
  75.             return Result.Success;
  76.         }
  77.         catch (Exception ex)
  78.         {
  79.             pathService.MDnsResolved = false;
  80.             return Result.Failure(ex.Message);
  81.         }
  82.         finally
  83.         {
  84.             MDnsResolveLock.Release();
  85.         }
  86.     }
  87.  
  88.     private static async Task<string?> ResolveMdnsHostAsync(string hostname)
  89.     {
  90.         if (hostname.EndsWith(".local", StringComparison.OrdinalIgnoreCase))
  91.             hostname = hostname.Substring(0, hostname.Length - 6);
  92.  
  93.         // Search all networks hosts using mDNS
  94.         var results = await ZeroconfResolver.ResolveAsync("_https._tcp.local.");
  95.  
  96.         // Find host by hostname
  97.         var host = results.FirstOrDefault(h =>
  98.             h.DisplayName.Equals(hostname, StringComparison.OrdinalIgnoreCase));
  99.  
  100.         return host?.IPAddress;
  101.     }
  102.  
  103. ...
  104. }
Advertisement
Add Comment
Please, Sign In to add comment