|
2 | 2 | using System; |
3 | 3 | using System.IO; |
4 | 4 | using System.Linq; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
5 | 7 | using System.Security.Cryptography; |
| 8 | +using System.Text; |
6 | 9 | using System.Threading.Tasks; |
7 | 10 |
|
8 | 11 | using NUnit.Framework; |
@@ -162,6 +165,78 @@ public void CanEstablishAndCommunicateOverHSConnectionOnionStyle() |
162 | 165 | { |
163 | 166 | Assert.DoesNotThrowAsync(EstablishAndCommunicateOverHSConnectionOnionStyle); |
164 | 167 | } |
| 168 | + |
| 169 | + [Test] |
| 170 | + [Retry(TestsRetryCount)] |
| 171 | + public void CanConnectToHiddenServiceUsingTorClient() |
| 172 | + { |
| 173 | + Assert.DoesNotThrowAsync(ConnectToHiddenServiceUsingTorClient); |
| 174 | + } |
| 175 | + |
| 176 | + private async Task ConnectToHiddenServiceUsingTorClient() |
| 177 | + { |
| 178 | + int descriptorUploadRetryLimit = 2; |
| 179 | + |
| 180 | + TorDirectory directory = await TorDirectory.BootstrapAsync(FallbackDirectorySelector.GetRandomFallbackDirectory(), new DirectoryInfo(Path.GetTempPath())); |
| 181 | + |
| 182 | + TorLogger.Log("Finished bootstraping"); |
| 183 | + |
| 184 | + SecureRandom random = new SecureRandom(); |
| 185 | + Ed25519KeyPairGenerator kpGen = new Ed25519KeyPairGenerator(); |
| 186 | + kpGen.Init(new Ed25519KeyGenerationParameters(random)); |
| 187 | + Ed25519PrivateKeyParameters masterPrivateKey = (Ed25519PrivateKeyParameters)kpGen.GenerateKeyPair().Private; |
| 188 | + |
| 189 | + TorServiceHost host = new TorServiceHost(directory, descriptorUploadRetryLimit, TestsRetryCount, FSharpOption<Ed25519PrivateKeyParameters>.Some(masterPrivateKey)); |
| 190 | + await host.StartAsync(); |
| 191 | + |
| 192 | + TorLogger.Log("Finished starting HS host"); |
| 193 | + |
| 194 | + var stringToSendAndReceive = |
| 195 | + "We are using tor!"; |
| 196 | + |
| 197 | + var serverSide = |
| 198 | + Task.Run(async () => { |
| 199 | + var stream = await host.AcceptClientAsync(); |
| 200 | + |
| 201 | + var httpResponse = |
| 202 | + "HTTP/1.1 200 OK\r\n" + |
| 203 | + "Server: NOnion\r\n" + |
| 204 | + $"Content-Length: {stringToSendAndReceive.Length}\r\n" + |
| 205 | + "Connection: close\r\n" + |
| 206 | + "Content-Type: text/plain" + |
| 207 | + "\r\n" + |
| 208 | + "\r\n" + |
| 209 | + stringToSendAndReceive + |
| 210 | + "\r\n"; |
| 211 | + |
| 212 | + await stream.SendDataAsync(Encoding.ASCII.GetBytes(httpResponse)); |
| 213 | + await stream.EndAsync(); |
| 214 | + }); |
| 215 | + |
| 216 | + var clientSide = |
| 217 | + Task.Run(async () => { |
| 218 | + var handler = new HttpClientHandler |
| 219 | + { |
| 220 | + Proxy = new WebProxy(new Uri("socks5://localhost:9050")) |
| 221 | + }; |
| 222 | + |
| 223 | + TestContext.Progress.WriteLine("Trying to connect to hidden service..."); |
| 224 | + using (handler) |
| 225 | + using (var httpClient = new HttpClient(handler)) |
| 226 | + { |
| 227 | + // Sometimes tor client takes a while to bootstrap and stalls |
| 228 | + // the request. |
| 229 | + httpClient.Timeout = TimeSpan.FromMinutes(20); |
| 230 | + var result = await httpClient.GetStringAsync("http://" + host.ExportUrl()); |
| 231 | + Assert.AreEqual(result, stringToSendAndReceive); |
| 232 | + } |
| 233 | + } |
| 234 | + ); |
| 235 | + |
| 236 | + await TaskUtils.WhenAllFailFast(serverSide, clientSide); |
| 237 | + |
| 238 | + ((IDisposable)host).Dispose(); |
| 239 | + } |
165 | 240 | } |
166 | 241 | } |
167 | 242 |
|
0 commit comments