C# 为什么';这条SSL隧道不是在工作吗?
我正在尝试在C#中构建一个SSL隧道,它在客户端模式下的工作方式类似于C# 为什么';这条SSL隧道不是在工作吗?,c#,.net,sockets,ssl,mono,C#,.net,Sockets,Ssl,Mono,我正在尝试在C#中构建一个SSL隧道,它在客户端模式下的工作方式类似于stunnel;i、 e.将未加密连接转发到加密连接 为什么这样不行 using System; using System.Net; using System.Net.Sockets; using System.Net.Security; using System.Collections; using System.Security; using System.Security.Authentication; using Sy
stunnel
;i、 e.将未加密连接转发到加密连接
为什么这样不行
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Collections;
using System.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Text;
using System.IO;
namespace Teststunnel
{
public class SslWrapper
{
public SslWrapper(IPEndPoint remote, int port)
{
listener = new TcpListener (port);
Console.Error.WriteLine ("Established listener");
listenport = port;
server = remote;
listener.Start ();
new Thread (mainEventLoop).Start();
}
private TcpListener listener;
private int listenport;
private IPEndPoint server;
private void mainEventLoop()
{
restart:
Console.Error.WriteLine ("Waiting for connections...");
TcpClient a = listener.AcceptTcpClient ();
Console.Error.WriteLine ("Connection received!");
tunnel (a);
goto restart;
}
private void tunnel(TcpClient nconn)
{
SslStream sstrm = new SslStream (
nconn.GetStream (),
false);
Console.Error.WriteLine (server.ToString());
try
{
sstrm.AuthenticateAsClient (server.ToString());
}
catch (AuthenticationException e)
{
Console.WriteLine("Exception: {0}", e.Message);
if (e.InnerException != null)
{
Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
}
Console.WriteLine ("Authentication failed - closing the connection.");
nconn.Close();
return;
}
}
}
}
在我的主函数中,我使用new-SslWrapper(new-IPEndPoint(IPAddress.Parse(“198.23.240.183”)、443、9999)调用它代码>。但是,当我尝试连接时,应用程序会因以下原因崩溃:
未处理的异常:
System.IO.IOException:身份验证或解密失败。-->Mono.Security.Protocol.Tls.TlsException:身份验证或解密失败。
位于:0中的Mono.Security.Protocol.Tls.RecordProtocol.ReadRecordBuffer(Int32 contentType,System.IO.Stream记录)[0x00000]处
位于:0中的Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback(IAsyncResult asyncResult)[0x00000]处
是的,我用的是单声道。但是,我确信我已经使用mozroot
导入了所有正确的SSL根证书。我做错了什么?这在Microsoft.NET上也是可以复制的。TcpClient a=listener.AcceptTcpClient()
使它看起来像是服务器端代码,sstrm.authenticatesclient
是用于客户端的。如果您控制双方,那么使用SSL在原则上可能会起作用,但这听起来是一个非常糟糕的主意。那么我该怎么做呢?这本质上是一个中间箱,因此它实际上既是服务器又是客户端。例如,它接收普通HTTP请求并将其转换为HTTPS请求。然后,您需要在与服务器的连接(而不是与客户端的连接)上以客户端身份进行身份验证。我没有看到您打开到服务器的TCP连接。是的,这就是问题所在。我完全不知道该怎么做。事情也是这样的:`client==>我的代码===>远程服务器