Esempio n. 1
0
        private void OutputDataReceivedEventHandler(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                NebulaModel.Logger.Log.Debug($"Ngrok Stdout: {e.Data}");

                var json = MiniJson.Deserialize(e.Data) as Dictionary <string, object>;
                if (json != null)
                {
                    var lvl = json["lvl"] as string;
                    if (lvl == "info")
                    {
                        var msg = json["msg"] as string;
                        if (msg == "starting web service")
                        {
                            _ngrokAPIAddress = json["addr"] as string;
                        }
                        else if (msg == "started tunnel")
                        {
                            var addr = json["addr"] as string;
                            var url  = json["url"] as string;
                            if (
                                (addr == $"//localhost:{_port}" || addr == $"//127.0.0.1:{_port}" || addr == $"//0.0.0.0:{_port}") &&
                                url.StartsWith("tcp://")
                                )
                            {
                                NgrokAddress = url.Replace("tcp://", "");
                                _ngrokAddressObtainedSource.TrySetResult(true);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public async Task <string> GetTunnelAddressFromAPI()
        {
            if (!IsNgrokActive())
            {
                throw new Exception($"Not able to get Ngrok tunnel address from API because Ngrok is not started (or exited prematurely)! LastErrorCode: {NgrokLastErrorCode}");
            }

            if (_ngrokAPIAddress == null)
            {
                throw new Exception($"Not able to get Ngrok tunnel address because Ngrok API address is not (yet) known!");
            }

            using (var client = new HttpClient())
            {
                var response = await client.GetAsync($"http://{_ngrokAPIAddress}/api/tunnels");

                if (response.IsSuccessStatusCode)
                {
                    var body = await response.Content.ReadAsStringAsync();

                    var json = MiniJson.Deserialize(body) as Dictionary <string, object>;

                    var tunnels = json["tunnels"] as List <object>;

                    string publicUrl = null;
                    foreach (Dictionary <string, object> tunnel in tunnels)
                    {
                        if (tunnel["proto"] as string == "tcp" && (tunnel["config"] as Dictionary <string, object>)["addr"] as string == $"localhost:{_port}")
                        {
                            publicUrl = tunnel["public_url"] as string;
                            break;
                        }
                    }

                    if (publicUrl == null)
                    {
                        throw new Exception("Not able to get Ngrok tunnel address because no matching tunnel was found in API response");
                    }

                    return(publicUrl.Replace("tcp://", ""));
                }
                else
                {
                    throw new Exception("Could not access the ngrok API");
                }
            }
        }
Esempio n. 3
0
 public static JsonArray FromJson(string _jsonString)
 {
     return(MiniJson.Deserialize(_jsonString) as JsonArray);
 }
Esempio n. 4
0
 public static JsonObject FromJson(string _jsonString)
 {
     return(MiniJson.Deserialize(_jsonString) as JsonObject);
 }