我正在运行安装了Ubuntu 14.04.1 LTS x64的VPS。我已经安装了mono-complete。我在尝试更大的事情之前尝试创建一个简单的HTTPS侦听器。这是我第一次使用Mono进行任何开发。
以下是我的测试应用程序的完整代码:
namespace jvs
{
using System;
using System.Net;
using System.Text;
using System.Linq;
using System.IO;
class Host
{
public HttpListener Server { get; set; }
public TextWriter Log { get; set; }
public Host()
{
this.Server = null;
this.Log = Console.Out;
}
public Host(TextWriter log)
{
this.Server = null;
this.Log = log;
}
public void Start()
{
this.Server = new HttpListener();
this.Server.Prefixes.Add("https://*:443/");
this.Server.AuthenticationSchemes = AuthenticationSchemes.Basic;
this.Server.Start();
this.Server.BeginGetContext(this.OnContextReceived, this.Server);
Log.WriteLine("Host started; listening on:");
foreach (var prefix in this.Server.Prefixes)
{
Log.WriteLine(" {0}", prefix.ToString());
}
}
public void Stop()
{
this.Server.Stop();
this.Server = null;
}
private void OnContextReceived(IAsyncResult ar)
{
// This function is NEVER called
var server = ar.AsyncState as HttpListener;
var context = this.Server.EndGetContext(ar);
var identity = context.User.Identity as HttpListenerBasicIdentity;
var response = context.Response;
Log.WriteLine("Connection received");
Log.WriteLine(" User: {0}\n Password: {1}", identity.Name, identity.Password);
server.BeginGetContext(this.OnContextReceived, server);
using (var ms = new MemoryStream())
using (var w = new StreamWriter(ms) { AutoFlush = true })
{
w.WriteLine(
@"<html>
<head>
<title>Vorpalnet</title>
</head>
<body>
<div style=""text-align: center; vertical-align: middle; width: 100%; height: 80%"">
it worked.
</div>
</body>
</html>");
var buffer = ms.ToArray();
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.OutputStream.Close();
}
}
public static void Main(string[] args)
{
Host host = new Host();
host.Start();
Console.Error.WriteLine("PID {0}", System.Diagnostics.Process.GetCurrentProcess().Id);
while (true) { }
}
}
}
编译代码并运行它(通过sudo
以允许使用端口443)后,一切似乎都没问题。但是,当我打开Firefox并导航到VPS地址时,我收到消息&#34;连接被中断。&#34;
此外,正如评论中所述,永远不会调用OnContextReceived
。
我已经生成了用于测试的自签名证书,并将它们配置为以下列方式使用:
$ makecert -r -n "CN=outpost" -sv outpost.pkv outpost.cer
$ httpcfg -add -port 443 -cert outpost.cer -pvk outpost.pvk
并验证:
$ httpcfg -list
Port: 443 Thumbprint: 02F57058BB31783710E6836E5646601A5AEBEB48
$ ll ~/.config/.mono/httplistener
total 16
drwxrwxr-x 2 vps vps 4096 Sep 17 16:42 ./
drwxrwxr-x 3 vps vps 4096 Sep 17 15:35 ../
-rw------- 1 vps vps 425 Sep 17 16:40 443.cer
-rw------- 1 vps vps 620 Sep 17 16:40 443.pvk
$ sudo ./host.exe
Host started; listening on:
https://*:443/
PID 15961
当它运行时,我打开了另一个会话并运行sudo netstat -antp | grep 443
;这是输出:
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 15961/cli
我尝试在本地连接到主机并获得 nothing 作为回报,这使得单声道不会拿起证书:
$ openssl s_client -connect outpost:443
CONNECTED(00000003)
2348096:error:140790E5:SSL routines:SSL23_WRITE:ssl handshake failure:s23_lib.c:177:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 318 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
---
我甚至通过host.exe.config 启用了网络跟踪日志记录,单声道似乎忽略了NO输出:
$ cat host.exe.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.Net" tracemode="includehex" maxdatasize="1024">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Cache">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Http">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.HttpListener">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.WebSockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<switches>
<add name="System.Net" value="Verbose"/>
<add name="System.Net.Cache" value="Verbose"/>
<add name="System.Net.Http" value="Verbose"/>
<add name="System.Net.HttpListener" value="Verbose"/>
<add name="System.Net.Sockets" value="Verbose"/>
<add name="System.Net.WebSockets" value="Verbose"/>
</switches>
<sharedListeners>
<add name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="network.log"
/>
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>
</configuration>
应该注意的是,使用普通HTTP(&#34; http:// *:80 /&#34;)根本没有 no 问题。一切正常。不幸的是,我将需要HTTPS来实现这个项目的最终目标。
此时我几乎把剩余的头发从头上拉了下来。我用Google搜索的所有内容都在Windows上解决了这个问题,但在Linux上没有任何用处。
这是单声道配置问题吗?我缺少一个步骤吗?我生成的证书错了吗?
答案 0 :(得分:1)
解决方案是:
$ sudo httpcfg -add -port 443 -cert outpost.cer -pvk outpost.pvk
原因:我通过host.exe
运行sudo
以获得使用端口443的权限。因此,证书是从/root/.config/.mono/httplistener
而不是/home/vps/.config/.mono/httplistener
提取的。通过httpcfg
运行sudo
会将证书放在正确的位置。
这么简单的问题令人头疼。