public async Task OpenManagerTunnel(WSTunnelConfig config, Func <Task> disconnectTunnel, Func <Guid, Task> disconnectSocket) { managerWebSocket = new ClientWebSocket(); serverUri = new Uri(config.ServerUri); this.config = config; log.LogInformation($"Подключение к {serverUri}"); try { if (!string.IsNullOrWhiteSpace(config.Proxy?.Server)) { var proxy = new WebProxy(config.Proxy.Server, config.Proxy.Port); proxy.UseDefaultCredentials = config.Proxy.UseDefaultCredentials; if (!string.IsNullOrWhiteSpace(config.Proxy.Login)) { proxy.Credentials = new NetworkCredential(config.Proxy.Login, config.Proxy.Passwd); } managerWebSocket.Options.Proxy = proxy; } await managerWebSocket.ConnectAsync(serverUri, CancellationToken.None); } catch (Exception ex) { log.LogError(ex, $"{ex.Message}"); throw; } log.LogInformation($"Есть контакт"); RunManagerReceiver(disconnectTunnel, disconnectSocket); }
public static async Task Start() { try { log.LogInformation("Инициализация"); wsTunnelConfig = ConfigWatcher.GetSection("wstunnel").ConvertValue <WSTunnelConfig>(); client = new WsClient(); await client.OpenManagerTunnel(wsTunnelConfig, DisconnectTunnel, DisconnectSocket); await client.Auth(); var tunnelGuid = await client.OpenDataTunnel(DataReceiver, DisconnecDataTunnel); dataConnections.TryAdd(tunnelGuid, new WSDataConnectionInfo { TunnelId = tunnelGuid }); var testObj = new EchoReq { ReqId = Guid.NewGuid(), Timestamp = DateTime.UtcNow }; var buffer = Encoding.UTF8.GetBytes(testObj.ToJson()); await client.SendData(tunnelGuid, Guid.Empty, buffer, buffer.Length); log.LogInformation($"Послали echo - {testObj.ReqId}"); server = new Socks5Server(); server.Start(wsTunnelConfig.Port, RequireNewConnection, TransferData, CloseSocket); } catch (Exception e) { log.LogError($"Ошибка инициализации: {e.Message}"); server?.Dispose(); client?.Dispose(); } }