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

Closed
Darthagnon opened this issue Mar 14, 2022 · 9 comments
Closed

Stun servers are hardcoded #21

Darthagnon opened this issue Mar 14, 2022 · 9 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Darthagnon
Copy link

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

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

@miroslavpejic85 miroslavpejic85 added enhancement New feature or request good first issue Good for newcomers labels Mar 14, 2022
@johngagefaulkner
Copy link

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

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

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 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.

@mubix
Copy link

mubix commented Jul 17, 2022

There are a couple servers like stunserver.org that are no longer valid STUN servers as the domains have been released. Currently there is malware hosted on that particular one. Can we limit the server list to "known good"? Pretty easy to compile without them but just trying to look out for other users.

@miroslavpejic85
Copy link
Owner

miroslavpejic85 commented Jul 17, 2022

Hi @mubix, thank you for this news.

Sure, I deleted all Hardcoded Stun Servers, only leaving as default

[{
	"Server": "stun.l.google.com",
	"Port": 19302
}]

Now you can set your own List of Stun Servers that will be saved in the same dir where the app was run as StunServers.json.

You can see the lists of Stun Servers by clicking on Stun tab, also add new, save - delete your own.

P2P-Stun-Servers

Special Thanks to @Darthagnon (this task) and @johngagefaulkner for the StunServer.cs

Thank you and enjoy!

@johngagefaulkner
Copy link

No thanks needed! I'm sorry I haven't responded, kids & career are working in tandem to hold my free time hostage!

@miroslavpejic85
Copy link
Owner

miroslavpejic85 commented Jul 17, 2022

@johngagefaulkner Deserved, you did a great job for the StunServer.cs ;)
No problem, I Understand you, I waited a while and in the end, having no news, I made the commit.
I wish you all the best and thank you for your valuable contribution.

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

No branches or pull requests

4 participants