GithubHelp home page GithubHelp logo

Comments (9)

miroslavpejic85 avatar miroslavpejic85 commented on May 22, 2024 3

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.

from p2p.

johngagefaulkner avatar johngagefaulkner commented on May 22, 2024 2

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));
}

from p2p.

miroslavpejic85 avatar miroslavpejic85 commented on May 22, 2024 2

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!

from p2p.

miroslavpejic85 avatar miroslavpejic85 commented on May 22, 2024 1

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

from p2p.

miroslavpejic85 avatar miroslavpejic85 commented on May 22, 2024 1

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

from p2p.

johngagefaulkner avatar johngagefaulkner commented on May 22, 2024

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.

from p2p.

mubix avatar mubix commented on May 22, 2024

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.

from p2p.

johngagefaulkner avatar johngagefaulkner commented on May 22, 2024

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

from p2p.

miroslavpejic85 avatar miroslavpejic85 commented on May 22, 2024

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

from p2p.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.