What is the name of the domain?
testtest.wvusd.homes
What is the error number?
404
What is the error message?
Jul 17 00:52:57 something cloudflared[2316]: 2024-07-17T00:52:57Z INF Registered tunnel connection connIndex=0 connection=6994e...
Jul 17 00:52:57 something cloudflared[2316]: 2024-07-17T00:52:57Z INF Registered tunnel connection connIndex=1 connection=2bde...
Jul 17 00:52:57 something cloudflared[2316]: 2024-07-17T00:52:57Z INF Registered tunnel connection connIndex=2 connection=c08e...
Jul 17 00:52:58 something cloudflared[2316]: 2024-07-17T00:52:58Z INF Registered tunnel connection connIndex=3 connection=61b9b...
Jul 17 00:52:59 something cloudflared[2316]: 2024-07-17T00:52:59Z INF Updated to new configuration config="{\"ingress\":[{\"host\...
Jul 17 00:57:05 something cloudflared[2316]: 2024/07/17 00:57:05 Unsolicited response received on idle HTTP channel starting with "HTTP/1.1 200 OK\r\nServer: Microsoft-NetCore/2.0\r\nDate: Wed, 17 Jul 2024 00:57:05 GMT\r\nConnection: close\r\n\r\n"
Jul 17 00:57:20 something cloudflared[2316]: 2024/07/17 00:57:20 Unsolicited response received on idle HTTP channel starting with "HTTP/1.1 200 OK\r\nServer: Microsoft-NetCore/2.0\r\nDate: Wed, 17 Jul 2024 00:57:20 GMT\r\nConnection: close\r\n\r\n"
Jul 17 01:07:14 something cloudflared[2316]: 2024/07/17 01:07:14 Unsolicited response received on idle HTTP channel starting with "HTTP/1.1 200 OK\r\nServer: Microsoft-NetCore/2.0\r\nDate: Wed, 17 Jul 2024 01:07:14 GMT\r\nConnection: close\r\n\r\n"
What is the issue you’re encountering
It will always show 404 error when using C# HttpListener to setup a server on Ubuntu 22 with Cloudflare tunnel
What steps have you taken to resolve the issue?
ufw disable
sudo systemctl stop cloudflared
sudo systemctl start cloudflared
and restart the computer
What are the steps to reproduce the issue?
Use this sample code:
using System.Net;
using System.Text;
using YHttpListenerFramework;
using static YHttpListenerFramework.YHttpListener;
namespace SimpleServer
{
internal class Program
{
static void Main(string[] args)
{
{
OneListenerTask l = new OneListenerTask("http://127.0.0.1:3325/");
TypeEvent e = new TypeEvent();
e.WebsiteVisitedEvent += HandlePost;
l.AddTypeEvent("GET", e);
l.Start();
}
Console.WriteLine("Listening...");
while (true)
{
Thread.Sleep(1000);
}
}
private static Task HandlePost(HttpListenerContext context)
{
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
// 设置Content-Type为image/png
context.Response.ContentType = "image/png";
// 示例:返回一个空的PNG响应
byte[] imageBytes;
try
{
imageBytes = File.ReadAllBytes("1.png");
}
catch
{
imageBytes = Encoding.UTF8.GetBytes("No image");
}
// 写入响应
context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);
// 关闭响应
context.Response.Close();
return Task.CompletedTask;
}
}
}
The framework:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace YHttpListenerFramework
{
public class YHttpListener
{
public class OneListenerTask
{
private HttpListener httpListener = new HttpListener();
private bool started = false;
private int _sleepTime = 0;
private Task listenTask;
private CancellationTokenSource tokenSource = new CancellationTokenSource();
private Dictionary<string, TypeEvent> typeEvent = new Dictionary<string, TypeEvent>();
public delegate void WebsiteVisitedEventHandler(HttpListenerContext context);
public event WebsiteVisitedEventHandler WebsiteVisitedEvent;
public delegate void HttpListenerExceptionHandler(HttpListenerException ex);
public event HttpListenerExceptionHandler HttpListenerExceptionEvent;
public OneListenerTask(string website)
{
try { httpListener.Prefixes.Add(website); }
catch (Exception ex) { throw ex; }
TypeEvent e = new TypeEvent();
e.WebsiteVisitedEvent +=
(HttpListenerContext context) =>
{
string method = "";
foreach (var t in typeEvent)
{
method += t.Key + ", ";
}
// if(method.Length > 2) Do not need this because the "method" at least will be "OPTIONS"
method = method.Substring(0, method.Length - 2);
context.Response.Headers.Add("Allow", method);
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
return Task.CompletedTask;
};
AddTypeEvent("OPTIONS", e);
}
public void AddTypeEvent(string type, TypeEvent e)
{
typeEvent.Add(type.ToUpper(), e);
}
public bool RemoveTypeEvent(string type)
{
return typeEvent.Remove(type);
}
public void Start()
{
if (!started) { started = true; }
else return;
httpListener.Start();
listenTask = Task.Run(ListenTask, tokenSource.Token);
//listenTask = new Task(ListenTask, tokenSource.Token);
//listenTask.Start();
}
private void ListenTask()
{
HttpListenerContext context = null;
while (started)
{
try
{
context = httpListener.GetContext();
if (context != null)
{
WebsiteVisitedEvent?.Invoke(context);
//if(typeEvent.ContainsKey(context.Request.HttpMethod)){
typeEvent[context.Request.HttpMethod]?.InvokeEvent(context);
//}
//if (typeEvent.TryGetValue(context.Request.HttpMethod, out var value)) value.InvokeEvent(context);
//context.Response.Close(); WSSB
}
}
catch (HttpListenerException ex)
{
//Console.WriteLine("EXCEPTION");
if (ex.HResult != -2147467259) HttpListenerExceptionEvent.Invoke(ex); // if not stop IO operation by the program.
//else Console.WriteLine("Stopped");
}
Thread.Sleep(_sleepTime);
}
Console.WriteLine("OUT");
}
}
public class TypeEvent
{
public delegate Task WebsiteVisitedEventHandler(HttpListenerContext context);
public event WebsiteVisitedEventHandler WebsiteVisitedEvent;
public void InvokeEvent(HttpListenerContext context)
{
WebsiteVisitedEvent?.Invoke(context);
}
}
}
}
And setup a server on ubuntu (publish-independent-linux_x64), then set up the tunnel and visit the site, it will show 404 Error.