-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNgrok.cs
More file actions
161 lines (132 loc) · 5.45 KB
/
Copy pathNgrok.cs
File metadata and controls
161 lines (132 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace TCPServerApp
{
class Ngrok
{
string ngrok_download_url;
string file_name = "ngrok.zip";
string ngrok_base_api = "http://127.0.0.1:4040/api";
public Process process;
public string AuthToken { get; set; }
public Ngrok()
{
//if the ngrok zip is there, just unzip
if (File.Exists(file_name)) UnZipArchive();
//If ngrok executable is not in root folder, download and unzip
if (!File.Exists("ngrok") && !File.Exists("ngrok.exe"))
{
if (string.IsNullOrEmpty(NgrokURL()))
{
Console.WriteLine("@@ Invalid System Architecture Detected - Cannot Run Program");
Console.WriteLine("@@ Press any key to exit: ");
Console.Read();
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
else DownloadNgrok().Wait();
}
}
public void Start(string TCPPort)
{
if (string.IsNullOrEmpty(AuthToken)) throw new System.ArgumentNullException("Invalid ngrok authentication token");
else
{
Console.WriteLine("Starting ngrok TCP tunnel");
ExecuteNgrok("authtoken " + AuthToken).WaitForExit();
Task.Run(
() => ExecuteNgrok("tcp " + TCPPort)
.WaitForExit() //keep tunnel service alive
);
}
}
public Process ExecuteNgrok(string arguments){
return process = RunProcess("./ngrok", arguments);
}
public void UnZipArchive(){
Console.WriteLine("Extracting Executable ... ");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
ZipFile.ExtractToDirectory(file_name, "./");
else RunProcess("unzip", file_name).WaitForExit();
File.Delete(file_name);
}
private Process RunProcess(string command, string arguments){
Process process = new Process();
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string result = process.StandardOutput.ReadToEnd();
Console.WriteLine(result);
return process;
}
public async Task<string> PublicUrl()
{
Console.WriteLine("Fetching Ngrok Tunnels ... ");
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string tunnels = await httpClient.GetStringAsync(ngrok_base_api + "/tunnels");
TunnelsResponse response = JsonConvert.DeserializeObject<TunnelsResponse>(tunnels);
var tunnel = response.tunnels[0];
Console.WriteLine("Retrieved Tunnel: " + tunnel.public_url);
return tunnel.public_url;
}
}
private async Task DownloadNgrok()
{
Console.WriteLine("Downloading Ngrok ... ");
using (HttpClient httpClient = new HttpClient())
{
var stream = await httpClient.GetStreamAsync(ngrok_download_url);
using (var fileStream = File.Create(file_name))
{
await stream.CopyToAsync(fileStream);
}
Console.WriteLine("Done With Download");
UnZipArchive(); //unzip the ngrok archive
}
}
private string NgrokURL()
{
var arch = RuntimeInformation.OSArchitecture == Architecture.X64 ? "amd64" : (RuntimeInformation.OSArchitecture == Architecture.X86 ? "386" : "");
if (string.IsNullOrEmpty(ngrok_download_url) && !string.IsNullOrEmpty(arch))
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
ngrok_download_url = string.Format("https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-darwin-{0}.zip", arch);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
ngrok_download_url = string.Format("https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-{0}.zip", arch);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
ngrok_download_url = string.Format("https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-windows-{0}.zip", arch);
}
}
return ngrok_download_url;
}
}
class TunnelsResponse
{
public List<Tunnel> tunnels { get; set; }
public string uri { get; set; }
}
class Tunnel
{
public string name { get; set; }
public string uri { get; set; }
public string public_url { get; set; }
public string proto { get; set; }
}
}