close
The Wayback Machine - https://web.archive.org/web/20220513031841/https://github.com/miroslavpejic85/p2p/issues/21
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stun servers are hardcoded #21

Open
Darthagnon opened this issue Mar 14, 2022 · 5 comments
Open

Stun servers are hardcoded #21

Darthagnon opened this issue Mar 14, 2022 · 5 comments
Labels
enhancement good first issue

Comments

@Darthagnon
Copy link

@Darthagnon Darthagnon commented Mar 14, 2022

I noticed in 171947c that the stun servers are hardcoded. Wouldn't it be better practice to leave them easily editable in a config file (possibly eventually in the GUI)?

@miroslavpejic85
Copy link
Owner

@miroslavpejic85 miroslavpejic85 commented Mar 14, 2022

Hello @Darthagnon, thanks, that's a great idea.

@miroslavpejic85 miroslavpejic85 added enhancement good first issue labels Mar 14, 2022
@johngagefaulkner
Copy link

@johngagefaulkner johngagefaulkner commented Apr 5, 2022

Not sure if anyone has made any progress on this but I converted the long list of URLs and Ports for the Stun Servers to a .JSON file: StunServers.json.txt

Note: Don't forget to remove the .txt extension from the end of the file name before using it with the code below.

I'm working on converting all the code over to .NET 6 for a personal project but I also wanted/needed to test my code on the existing/working .NET Framework v4.8 codebase so I've included the model(s) for both. Each class also contains methods to import the .JSON file from either a local file, a URL, or using a JSON string that has otherwise already been imported.

  • Here's the model class for .NET 6:
using System.IO;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

public class StunServer
{
    public string? Url { get; set; }
    public int Port { get; set; }

    /// <summary>
    /// Loads list of Stun Servers from a local file path.
    /// </summary>
    /// <param name="filePath">Full path to the local Stun Server list (.JSON). Example: C:\Users\Administrator\Downloads\StunServers.json</param>
    /// <returns>An array of 'StunServer' objects.</returns>
    public static StunServer[]? GetStunServersFromFile(string filePath)
    {
        string _json = File.ReadAllText(filePath);
        return JsonSerializer.Deserialize<StunServer[]>(_json);
    }

    /// <summary>
    /// Loads list of Stun Servers from an HTTP/HTTPS URL.
    /// </summary>
    /// <param name="fileUrl">Full URL to the Stun Server list (.JSON). Example: "https://raw.github.com/username/repo/files/StunServers.json"</param>
    /// <returns>An array of 'StunServer' objects.</returns>
    public static async Task<StunServer[]?> GetStunServersFromUrl(string fileUrl)
    {
        string _json;
        using (HttpClient _client = new())
        {
            _json = await _client.GetStringAsync(fileUrl);
        }
        return JsonSerializer.Deserialize<StunServer[]>(_json);
    }

    /// <summary>
    /// Loads list of Stun Servers from a pre-populated JSON string.
    /// </summary>
    /// <param name="json">A string, in JSON format, containing an array of Stun Server objects.</param>
    /// <returns>An array of 'StunServer' objects.</returns>
    public static StunServer[]? GetStunServersFromJson(string json)
    {
        return JsonSerializer.Deserialize<StunServer[]>(json);
    }
}
  • And the .NET Framework v4.8 model class:
// .NET Framework v4.8
// Requires Newtonsoft.Json

using System.IO;
using System.Net;
using Newtonsoft.Json;

public class StunServer
{
    public string Url { get; set; }
    public int Port { get; set; }

    /// <summary>
    /// Loads list of Stun Servers from a local file path.
    /// </summary>
    /// <param name="filePath">Full path to the local Stun Server list (.JSON). Example: C:\Users\Administrator\Downloads\StunServers.json</param>
    /// <returns>An array of 'StunServer' objects.</returns>
    public static StunServer[] GetStunServersFromFile(string filePath)
    {
        string _json = System.IO.File.ReadAllText(filePath);
        return JsonConvert.DeserializeObject<StunServer[]>(_json);
    }

    /// <summary>
    /// Loads list of Stun Servers from an HTTP/HTTPS URL.
    /// </summary>
    /// <param name="fileUrl">Full URL to the Stun Server list (.JSON). Example: "https://raw.github.com/username/repo/files/StunServers.json"</param>
    /// <returns>An array of 'StunServer' objects.</returns>
    public static StunServer[] GetStunServersFromUrl(string fileUrl)
    {
        string _json;
        using (System.Net.WebClient wc = new System.Net.WebClient())
        {
            _json = wc.DownloadString(fileUrl);
        }
        return JsonConvert.DeserializeObject<StunServer[]>(_json);
    }

    /// <summary>
    /// Loads list of Stun Servers from a pre-populated JSON string.
    /// </summary>
    /// <param name="json">A string, in JSON format, containing an array of Stun Server objects.</param>
    /// <returns>An array of 'StunServer' objects.</returns>
    public static StunServer[] GetStunServersFromJson(string json)
    {
        return JsonConvert.DeserializeObject<StunServer[]>(json);
    }
}

Finally if, for whatever reason, you still want/need to generate a list of Tuples from the StunServer object array:

List<Tuple<string, int>> stunServers = new List<Tuple<string, int>>();
foreach (var _server in p2p.Models.StunServer.GetStunServersFromFile(@"C:\Dev\StunServers.json"))
{
    stunServers.Add(new Tuple<string, int>(_server.Url, _server.Port));
}

@miroslavpejic85
Copy link
Owner

@miroslavpejic85 miroslavpejic85 commented Apr 5, 2022

Hey @johngagefaulkner,
Thank you so much for sharing your great work, let's see if there are any other proposals for this implementation.

@johngagefaulkner
Copy link

@johngagefaulkner johngagefaulkner commented Apr 5, 2022

Hey @johngagefaulkner,

Thank you so much for sharing your great work, let's see if there are any other proposals for this implementation.

Fair warning, I haven't been to sleep in almost 2 days now (we have a newborn) so please forgive me if this suggestion doesn't make any sense, BUT...

I don't think it'd be a bad idea for you, as the author of the project, to be in control of the list of available Stun Servers and their ports. You could always host it on this GitHub repo, hard-code the URL into the project, and download it (for local cache) on each user's machine. I'm not all too familiar with the process but I'd imagine not all 290+ in the list are needed so you could have a shorter list containing only the ones you deem necessary which would reduce bandwidth costs for GitHub. If the user isn't able to use any of what's available in that file, they can click a button to "try additional servers" which would pull the full list. Once one, or both, of the lists are cached locally, you wouldn't need to check for an updated list again unless there were issues using whatever each user had locally.

Let me know if you're interested and need any help.

@miroslavpejic85
Copy link
Owner

@miroslavpejic85 miroslavpejic85 commented Apr 9, 2022

Hey @johngagefaulkner, Many congratulations for the newborn ❤️
The idea is to not depend from a hosted file but that everyone can edit their own.
The StunServers.json can be in the same path of the portable app, generated itself when the app start if it doesn't exist, loaded in to dataGridView if exist ;) For me the json file is ok.
Can you also add a new tab in the app to save, delete the StunServer lists from the json using dataGridView?

StunServersJson

You can try to implement this and send me a PR, that would be great.
Many thanks and have a nice weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue
Projects
None yet
Development

No branches or pull requests

3 participants