async Task <Connection> NewConnection() { try { var r = await ConnectHelper.Connect(this, server, timeout); r.ThrowIfFailed(); var ws = new WebSocket(r.Stream, true); if (timeout > 0) { ws.AddToManaged(timeout / 2, timeout); } var conn = new Connection(ws, enc) { PerSessionIV = per_session_iv }; ws.Closed += (_) => { // when websocket is closed lock (pool) pool.Remove(conn); // remove from pool if it's in }; return(conn); } catch (Exception e) { Logger.error("connecting to server: " + e.Message); throw; } }
public override async Task <ConnectResult> ProtectedConnect(ConnectArgument arg) { var dest = arg.Dest; var baseResult = await ConnectHelper.Connect(this, server.WithDefaultPort(80), connect_timeout); if (!baseResult.Ok) { return(baseResult); } try { var dataStream = baseResult.Stream; var asStream = MyStream.ToStream(dataStream); var sw = new StringWriter(new StringBuilder(1024)); var destStr = dest.ToString(); HttpClient.WriteHttpRequestHeader(sw, "CONNECT", destStr, new Dictionary <string, string> { ["Host"] = destStr }); await dataStream.WriteAsync(NaiveUtils.GetUTF8Bytes(sw.ToString())); var responseStr = await NaiveUtils.ReadStringUntil(asStream, NaiveUtils.DoubleCRLFBytes); var sr = new StringReader(responseStr); var response = HttpClient.ReadHttpResponseHeader(sr); if (response.StatusCode != "200") { throw new Exception($"remote server response '{response.StatusCode} {response.ReasonPhrase}'"); } return(CreateConnectResultWithStream(dataStream)); } catch (Exception) { MyStream.CloseWithTimeout(baseResult.Stream); throw; } }
public override async Task <ConnectResult> ProtectedConnect(ConnectArgument arg) { var dest = arg.Dest; var baseResult = await ConnectHelper.Connect(this, server, connect_timeout); if (!baseResult.Ok) { return(baseResult); } try { var dataStream = getEncryptionStream(baseResult.Stream); var bytes = dest.ToSocks5Bytes(); await dataStream.WriteAsync(bytes); return(CreateConnectResultWithStream(dataStream)); } catch (Exception) { MyStream.CloseWithTimeout(baseResult.Stream); throw; } }
public override Task <ConnectResult> ProtectedConnect(ConnectArgument arg) { var dest = force_dest.IsDefault ? arg.Dest : force_dest; return(ConnectHelper.Connect(this, arg.Dest, connect_timeout)); }