增加内网读物连接失败的提示页面

This commit is contained in:
SpringHgui 2020-04-11 14:27:28 +08:00
parent a16ebc586b
commit 2d51f449d0
11 changed files with 406 additions and 102 deletions

View File

@ -20,7 +20,7 @@
"LocalIp": "127.0.0.1",
//
"LocalPort": 80,
"LocalPort": 880,
// , 访url http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/
"SubDomain": "test" // test.test.cc

View File

@ -15,13 +15,21 @@ namespace FastTunnel.Core
public Socket Socket { get; set; }
public Connecter(string v1, int v2)
public Connecter(string v1, int v2, int? sendTimeout = null)
{
this._ip = v1;
this._port = v2;
Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket.SendTimeout = 2000;
if (sendTimeout.HasValue)
{
Socket.SendTimeout = sendTimeout.Value;
}
else
{
Socket.SendTimeout = 2000;
}
}
public void Connect()
@ -32,10 +40,20 @@ namespace FastTunnel.Core
Socket.Connect(point);
}
public void Send(byte[] data)
{
Socket.Send(data);
}
public void Send<T>(Message<T> msg)
where T : TunnelMassage
{
Socket.Send(msg);
}
public void Close()
{
Socket.Close();
}
}
}

View File

@ -67,14 +67,11 @@ namespace FastTunnel.Core.Core
try
{
if (lastHeart == null)
return;
var timer = sender as System.Timers.Timer;
var span = (DateTime.Now - lastHeart).TotalMilliseconds;
if (span > timer.Interval)
{
_logger.LogDebug($"上次心跳时间为{span}ms前");
_logger.LogDebug($"last heart recived {span / 1000}s ago");
// 重新登录
reConnect();
@ -175,6 +172,8 @@ namespace FastTunnel.Core.Core
{
_logger.LogDebug("通信已建立");
lastHeart = DateTime.Now;
// 心跳开始
timer_heart.Start();
timer_timeout.Start();
@ -189,7 +188,7 @@ namespace FastTunnel.Core.Core
byte[] buffer = new byte[1024];
string lastBuffer = string.Empty;
int n;
int n = 0;
while (true)
{
@ -202,8 +201,24 @@ namespace FastTunnel.Core.Core
break;
}
}
catch
/// <see cref="https://docs.microsoft.com/zh-cn/windows/win32/winsock/windows-sockets-error-codes-2"/>
catch (SocketException socketEx)
{
// Connection timed out.
if (socketEx.ErrorCode == 10060)
{
_logger.LogInformation("Connection timed out");
}
else
{
_logger.LogError(socketEx);
}
break;
}
catch (Exception ex)
{
_logger.LogError(ex);
break;
}
@ -239,6 +254,8 @@ namespace FastTunnel.Core.Core
continue;
}
}
_logger.LogInformation("stop receive from server");
}
private IClientHandler HandleServerRequest(string words)

View File

@ -114,10 +114,9 @@ namespace FastTunnel.Core.Core
Host = collection[0].Value;
}
_logger.LogDebug(Host);
var domain = Host.Split(":")[1].Trim();
_logger.LogDebug($"Host: {domain}");
WebInfo web;
if (!WebList.TryGetValue(domain, out web))
{
@ -148,7 +147,7 @@ namespace FastTunnel.Core.Core
}
catch (Exception ex)
{
_logger.LogError(ex);
_logger.LogError("处理Http失败" + ex);
client.Close();
}
}
@ -158,13 +157,12 @@ namespace FastTunnel.Core.Core
_logger.LogDebug($"TunnelNotFound:'{domain}'");
string statusLine = "HTTP/1.1 200 OK\r\n";
string responseHeader = "Content-Type: text/html\r\n";
byte[] responseBody;
var file = Path.Combine(AppContext.BaseDirectory, "Htmls", "TunnelNotFound.html");
if (File.Exists(file))
responseBody = FileHelper.GetBytesFromFile(file);
else
responseBody = Encoding.UTF8.GetBytes(TunnelResource.NoTunnelPage);
//var file = Path.Combine(AppContext.BaseDirectory, "Htmls", "TunnelNotFound.html");
//if (File.Exists(file))
// responseBody = FileHelper.GetBytesFromFile(file);
byte[] responseBody = Encoding.UTF8.GetBytes(TunnelResource.NoTunnelPage);
clientsocket.Send(Encoding.UTF8.GetBytes(statusLine));
clientsocket.Send(Encoding.UTF8.GetBytes(responseHeader));
@ -223,7 +221,7 @@ namespace FastTunnel.Core.Core
catch (Exception ex)
{
_logger.LogError(ex);
_logger.LogError($"错误的消息内容{words}");
_logger.LogError($"handle fail msg{words}");
// throw;
client.Send(new Message<LogMassage>() { MessageType = MessageType.Log, Content = new LogMassage(LogMsgType.Error, ex.Message) });
@ -260,6 +258,7 @@ namespace FastTunnel.Core.Core
handler = _loginHandler;
break;
case MessageType.Heart: // 心跳
Console.WriteLine("收到心跳");
handler = _heartHandler;
break;
case MessageType.C_SwapMsg: // 交换数据

View File

@ -4,7 +4,11 @@ using FastTunnel.Core.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FastTunnel.Core.Handlers.Client
{
@ -18,7 +22,41 @@ namespace FastTunnel.Core.Handlers.Client
connecter.Send(new Message<SwapMassage> { MessageType = MessageType.C_SwapMsg, Content = new SwapMassage(request.MsgId) });
var localConnecter = new Connecter(request.WebConfig.LocalIp, request.WebConfig.LocalPort);
localConnecter.Connect();
try
{
localConnecter.Connect();
}
catch (SocketException sex)
{
localConnecter.Close();
if (sex.ErrorCode == 10061)
{
// 内网的站点不存在或无法访问
string statusLine = "HTTP/1.1 200 OK\r\n";
string responseHeader = "Content-Type: text/html\r\n";
byte[] responseBody;
responseBody = Encoding.UTF8.GetBytes(TunnelResource.NoSite);
connecter.Send(Encoding.UTF8.GetBytes(statusLine));
connecter.Send(Encoding.UTF8.GetBytes(responseHeader));
connecter.Send(Encoding.UTF8.GetBytes("\r\n"));
connecter.Send(responseBody);
connecter.Socket.Disconnect(false);
connecter.Socket.Close();
return;
}
else
{
throw;
}
}
catch (Exception ex)
{
localConnecter.Close();
throw;
}
new SocketSwap(connecter.Socket, localConnecter.Socket).StartSwap();
}

View File

@ -0,0 +1,110 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FastTunnel</title>
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<style>
.err-info {
margin-top: 120px;
}
/* Provide sufficient contrast against white background */
a {
color: #0366d6;
}
.btn-primary {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}
.nav-pills .nav-link.active,
.nav-pills .show>.nav-link {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}
/* Sticky footer styles
-------------------------------------------------- */
html {
font-size: 14px;
}
@media (min-width: 768px) {
html {
font-size: 16px;
}
}
.border-top {
border-top: 1px solid #e5e5e5;
}
.border-bottom {
border-bottom: 1px solid #e5e5e5;
}
.box-shadow {
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
}
button.accept-policy {
font-size: 1rem;
line-height: inherit;
}
/* Sticky footer styles
-------------------------------------------------- */
html {
position: relative;
min-height: 100%;
}
body {
/* Margin bottom by footer height */
margin-bottom: 60px;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
white-space: nowrap;
line-height: 60px;
/* Vertically center the text there */
}
</style>
</head>
<body>
<div class="container err-info">
<main role="main" class="pb-3">
<div class="text-center">
<h1 class="display-4">连接内网的服务失败</h1>
<p>了解有关 <a target="_blank" href="https://github.com/SpringHgui/FastTunnel">FastTunel</a>的信息.</p>
</div>
<blockquote style="padding-top: 4em;">
<p class="lead">您看到本页面表示连接您的内网服务失败,请检查:</p>
</blockquote>
<ol>
<li class="lead">本地是否部署了您的服务</li>
<li class="lead">配置的本地ip以及端口号是否与您搭建的服务对应</li>
<li class="lead">如果内网服务不在客户端运行的计算机,请检查您的计算是否与目标计算机位于内网的同一网段</li>
</ol>
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
<a href="https://github.com/SpringHgui/FastTunnel" target="_blank">© Power By FastTunnel</a>
</div>
</footer>
</body>
</html>

View File

@ -7,13 +7,8 @@
<title>FastTunnel</title>
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<style>
/* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
for details on configuring this project to bundle and minify static web assets. */
a.navbar-brand {
white-space: normal;
text-align: center;
word-break: break-all;
.err-info {
margin-top: 120px;
}
/* Provide sufficient contrast against white background */
@ -87,26 +82,15 @@ for details on configuring this project to bundle and minify static web assets.
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" src="https://suidao.io">FastTunnel</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
</header>
<div class="container">
<div class="container err-info">
<main role="main" class="pb-3">
<div class="text-center">
<h1 class="display-4">找不到隧道</h1>
<h1 class="display-4">隧道未找到</h1>
<p>了解有关 <a target="_blank" href="https://github.com/SpringHgui/FastTunnel">FastTunel</a>的信息.</p>
</div>
<blockquote style="padding-top: 4em;">
<p class="lead">看到本页面表示当前隧道未登录,如果您是当前隧道地址的拥有者,请检查以下原因:</p>
<p class="lead">您看到本页面表示当前隧道尚未登录,如果您是当前隧道地址的拥有者,请检查以下原因:</p>
</blockquote>
<ol>
<li class="lead">是否创建了当前子域名的隧道</li>

View File

@ -86,6 +86,9 @@ namespace FastTunnel.Core
{
ls.Shutdown(SocketShutdown.Both);
}
catch (Exception)
{
}
finally
{
ls.Close();

View File

@ -56,16 +56,17 @@ namespace FastTunnel.Core
if (num == 0)
{
if (chanel.Receive.Connected)
chanel.Receive.Close();
try
{
try
{
chanel.Receive.Shutdown(SocketShutdown.Both);
}
finally
{
chanel.Receive.Close();
}
// Release the socket.//
chanel.Send.Shutdown(SocketShutdown.Both);
}
catch { }
finally
{
chanel.Send.Close();
}
break;
}
@ -73,38 +74,39 @@ namespace FastTunnel.Core
if (!chanel.Send.Connected)
break;
var str = Encoding.UTF8.GetString(result, 0, num);
// var str = Encoding.UTF8.GetString(result, 0, num);
chanel.Send.Send(result, num, SocketFlags.None);
}
catch (SocketException sex)
{
// Interrupted function call. 10004
// An existing connection was forcibly closed by the remote host. 10054
try
{
chanel.Send.Shutdown(SocketShutdown.Both);
}
catch { }
finally
{
chanel.Send.Close();
}
try
{
chanel.Receive.Shutdown(SocketShutdown.Both);
}
catch { }
finally
{
chanel.Receive.Close();
}
break;
}
catch (Exception ex)
{
if (chanel.Receive.Connected)
{
try
{
chanel.Receive.Shutdown(SocketShutdown.Both);
}
finally
{
chanel.Receive.Close();
}
}
if (chanel.Send.Connected)
{
try
{
chanel.Send.Shutdown(SocketShutdown.Both);
}
finally
{
chanel.Send.Close();
}
}
break;
Console.Write(ex.ToString());
throw;
}
}
}

View File

@ -70,10 +70,47 @@ namespace FastTunnel.Core {
/// &lt;title&gt;FastTunnel&lt;/title&gt;
/// &lt;link href=&quot;https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;
/// &lt;style&gt;
/// /* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
///for details on configuring this project to bundle and minify static web assets. */
/// .err-info {
/// margin-top: 120px;
/// }
///
/// a.navbar-brand { [字符串的其余部分被截断]&quot;; 的本地化字符串。
/// /* Provide sufficient contrast against white background */
/// a {
/// color: #0366d6;
/// }
///
/// .btn-primary {
/// color: #fff;
/// background-color [字符串的其余部分被截断]&quot;; 的本地化字符串。
/// </summary>
internal static string NoSite {
get {
return ResourceManager.GetString("NoSite", resourceCulture);
}
}
/// <summary>
/// 查找类似 &lt;!DOCTYPE html&gt;
///&lt;html lang=&quot;en&quot;&gt;
///
///&lt;head&gt;
/// &lt;meta charset=&quot;utf-8&quot; /&gt;
/// &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
/// &lt;title&gt;FastTunnel&lt;/title&gt;
/// &lt;link href=&quot;https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;
/// &lt;style&gt;
/// .err-info {
/// margin-top: 120px;
/// }
///
/// /* Provide sufficient contrast against white background */
/// a {
/// color: #0366d6;
/// }
///
/// .btn-primary {
/// color: #fff;
/// background-color [字符串的其余部分被截断]&quot;; 的本地化字符串。
/// </summary>
internal static string NoTunnelPage {
get {

View File

@ -117,7 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="NoTunnelPage" xml:space="preserve">
<data name="NoSite" xml:space="preserve">
<value>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
@ -127,13 +127,8 @@
&lt;title&gt;FastTunnel&lt;/title&gt;
&lt;link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"&gt;
&lt;style&gt;
/* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
for details on configuring this project to bundle and minify static web assets. */
a.navbar-brand {
white-space: normal;
text-align: center;
word-break: break-all;
.err-info {
margin-top: 120px;
}
/* Provide sufficient contrast against white background */
@ -207,26 +202,127 @@ for details on configuring this project to bundle and minify static web assets.
&lt;/head&gt;
&lt;body&gt;
&lt;header&gt;
&lt;nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"&gt;
&lt;div class="container"&gt;
&lt;a class="navbar-brand" src="https://suidao.io"&gt;FastTunnel&lt;/a&gt;
&lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"&gt;
&lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;/header&gt;
&lt;div class="container"&gt;
&lt;div class="container err-info"&gt;
&lt;main role="main" class="pb-3"&gt;
&lt;div class="text-center"&gt;
&lt;h1 class="display-4"&gt;找不到隧道&lt;/h1&gt;
&lt;h1 class="display-4"&gt;连接内网的服务失败&lt;/h1&gt;
&lt;p&gt;了解有关 &lt;a target="_blank" href="https://github.com/SpringHgui/FastTunnel"&gt;FastTunel&lt;/a&gt;的信息.&lt;/p&gt;
&lt;/div&gt;
&lt;blockquote style="padding-top: 4em;"&gt;
&lt;p class="lead"&gt;看到本页面表示当前隧道未登录,如果您是当前隧道地址的拥有者,请检查以下原因:&lt;/p&gt;
&lt;p class="lead"&gt;您看到本页面表示连接您的内网服务失败,请检查:&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ol&gt;
&lt;li class="lead"&gt;本地是否部署了您的服务&lt;/li&gt;
&lt;li class="lead"&gt;配置的本地ip以及端口号是否与您搭建的服务对应&lt;/li&gt;
&lt;li class="lead"&gt;如果内网服务不在客户端运行的计算机,请检查您的计算是否与目标计算机位于内网的同一网段&lt;/li&gt;
&lt;/ol&gt;
&lt;/main&gt;
&lt;/div&gt;
&lt;footer class="border-top footer text-muted"&gt;
&lt;div class="container"&gt;
&lt;a href="https://github.com/SpringHgui/FastTunnel" target="_blank"&gt;© Power By FastTunnel&lt;/a&gt;
&lt;/div&gt;
&lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;</value>
</data>
<data name="NoTunnelPage" xml:space="preserve">
<value>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8" /&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;
&lt;title&gt;FastTunnel&lt;/title&gt;
&lt;link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"&gt;
&lt;style&gt;
.err-info {
margin-top: 120px;
}
/* Provide sufficient contrast against white background */
a {
color: #0366d6;
}
.btn-primary {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}
.nav-pills .nav-link.active,
.nav-pills .show&gt;.nav-link {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}
/* Sticky footer styles
-------------------------------------------------- */
html {
font-size: 14px;
}
@media (min-width: 768px) {
html {
font-size: 16px;
}
}
.border-top {
border-top: 1px solid #e5e5e5;
}
.border-bottom {
border-bottom: 1px solid #e5e5e5;
}
.box-shadow {
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
}
button.accept-policy {
font-size: 1rem;
line-height: inherit;
}
/* Sticky footer styles
-------------------------------------------------- */
html {
position: relative;
min-height: 100%;
}
body {
/* Margin bottom by footer height */
margin-bottom: 60px;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
white-space: nowrap;
line-height: 60px;
/* Vertically center the text there */
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="container err-info"&gt;
&lt;main role="main" class="pb-3"&gt;
&lt;div class="text-center"&gt;
&lt;h1 class="display-4"&gt;隧道未找到&lt;/h1&gt;
&lt;p&gt;了解有关 &lt;a target="_blank" href="https://github.com/SpringHgui/FastTunnel"&gt;FastTunel&lt;/a&gt;的信息.&lt;/p&gt;
&lt;/div&gt;
&lt;blockquote style="padding-top: 4em;"&gt;
&lt;p class="lead"&gt;您看到本页面表示当前隧道尚未登录,如果您是当前隧道地址的拥有者,请检查以下原因:&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ol&gt;
&lt;li class="lead"&gt;是否创建了当前子域名的隧道&lt;/li&gt;