Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
s084088 committed Jan 22, 2020
1 parent 9a642b5 commit 7e69486
Show file tree
Hide file tree
Showing 47 changed files with 2,465 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Client/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Client"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions Client/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Client
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
10 changes: 10 additions & 0 deletions Client/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
13 changes: 13 additions & 0 deletions Client/Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Util\Util.csproj" />
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions Client/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Window x:Class="Client.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client" Loaded="Window_Loaded"
mc:Ignorable="d" Closing="Window_Closing"
Title="端口映射器---ByNemo" Height="300" Width="406">
<Grid>
<StackPanel Margin="10" x:Name="G1" IsEnabled="False">
<WrapPanel Margin="10 0" HorizontalAlignment="Center">
<TextBlock Text="内网IP:" VerticalAlignment="Center" Margin="10"></TextBlock>
<TextBox x:Name="T1" Text="127.0.0.1" IsEnabled="False" Width="100" VerticalAlignment="Center" Margin="10"></TextBox>
<TextBlock Text="内网端口:" VerticalAlignment="Center" Margin="10"></TextBlock>
<TextBox x:Name="T2" Text="3306" Width="50" VerticalAlignment="Center" Margin="10"></TextBox>
</WrapPanel>
<WrapPanel Margin="10 0" HorizontalAlignment="Center">
<TextBlock Text="外网IP:" VerticalAlignment="Center" Margin="10"></TextBlock>
<TextBox Text="jiyiwm.cn" Width="100" IsEnabled="False" VerticalAlignment="Center" Margin="10"></TextBox>
<TextBlock Text="外网端口:" VerticalAlignment="Center" Margin="10"></TextBlock>
<TextBox x:Name="T3" Text="4408" Width="50" VerticalAlignment="Center" Margin="10"></TextBox>
</WrapPanel>
<WrapPanel Margin="10" HorizontalAlignment="Center">
<Button x:Name="button1" Click="button1_Click" Content="应用" HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Width="60"/>
</WrapPanel>
</StackPanel>
<StackPanel Margin="10" VerticalAlignment="Bottom">
<WrapPanel Margin="10" HorizontalAlignment="Left">
<TextBlock>
<Run Text="当前连接数:"></Run>
<Run x:Name="ConnCount" Foreground="Green" Text="0"></Run>
</TextBlock>
</WrapPanel>
</StackPanel>


</Grid>
</Window>
74 changes: 74 additions & 0 deletions Client/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Client.Socket.Map;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Client
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
Hosts.Open(T1.Text, T2.Text, T3.Text);
G1.IsEnabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
Hosts.Start();
G1.IsEnabled = true;
new Thread(() =>
{
Thread.Sleep(1000);
ConnCount.Text = Hosts.connectMaps.Count.ToString();
})
{ IsBackground = true }.Start();
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
Close();
}
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
G1.IsEnabled = false;
Visibility = Visibility.Hidden;
try
{
Hosts.Close();
}
catch { }
}
}
}
144 changes: 144 additions & 0 deletions Client/Socket/Map/Hosts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using Client.Socket.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Util;

namespace Client.Socket.Map
{
public static class Hosts
{
public static List<ConnectMap> connectMaps = new List<ConnectMap>();
public static NetworkStream kongzhins = null;

public static int serverport = 4999; //外网主服务端口
public static string outip = "jiyiwm.cn"; //外网IP

public static int outport; //外网端口
public static int inport; //内网端口
public static string inip; //内网IP

public static TcpClient tc1;
public static TcpClient tc2;

public static TcpClient tc;
public static void Start()
{
tc = new TcpClient(outip, serverport);
kongzhins = tc.GetStream();
}

/// <summary>
/// 打开端口
/// </summary>
/// <param name="ip">内网IP</param>
/// <param name="port1">内网端口</param>
/// <param name="port2">外网对外端口</param>
/// <returns></returns>
public static void Open(string ip, string port1, string port2)
{
try
{
inport = Convert.ToInt32(port1);
inip = ip;

kongzhins.H_Send($"1,{port2}");
string s = kongzhins.H_Recv();
string[] r = s.Split(',');
if (r[0] == "0")
{
throw new Exception("此服务器端口已经被占用");
}
else if (r[0] == "1")
{
outport = Convert.ToInt32(r[1]);
tc1 = new TcpClient(inip, inport);
tc2 = new TcpClient(outip, outport);

new Thread(StartLisen) { IsBackground = true }.Start();
}
else
{
throw new Exception("返回数据未知");
}
}
catch (Exception e)
{
throw new Exception("连接发生错误" + e.Message);
}
}

private static void CheckConnect()
{
connectMaps.ForEach(l =>
{
if (l.inClient.Connected && l.outClient.Connected) { }
else
{
l.inClient.Close();
l.inClient.Dispose();
l.outClient.Close();
l.outClient.Dispose();
l.state = 0;
}
});
List<ConnectMap> ss = connectMaps.Where(l => l.state == 0).ToList();
ss.ForEach(l =>
{
connectMaps.Remove(l);
});
}

/// <summary>
/// 关闭服务
/// </summary>
public static void Close()
{
kongzhins.H_Send($"0");
kongzhins.Dispose();
tc.Close();
}

/// <summary>
/// 开启监听
/// </summary>
private static void StartLisen()
{
try
{
connectMaps.Add(new ConnectMap
{
inClient = tc1,
outClient = tc2,
});
PortHelper.Lianjie(tc1, tc2);
while (true)
{
string s = kongzhins.H_Recv();
string[] r = s.Split(',');
if (r[0] == "1")
{
outport = Convert.ToInt32(r[1]);
TcpClient tc1 = new TcpClient(inip, inport);
TcpClient tc2 = new TcpClient(outip, outport);
connectMaps.Add(new ConnectMap
{
inClient = tc1,
outClient = tc2,
});
PortHelper.Lianjie(tc1, tc2);
}
}
}
catch (Exception ex)
{
LogHelper.Logger("客户端开启监听出错 " + ex.Message);
kongzhins.Dispose();
tc.Close();
}
}
}
}
21 changes: 21 additions & 0 deletions Client/Socket/Models/ClientModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace Client.Socket.Models
{
public class ClientModel
{
public List<ConnectMap> connectMaps;
}

public class ConnectMap
{
public TcpClient inClient;

public TcpClient outClient;

internal int state = 1;
}
}
Loading

0 comments on commit 7e69486

Please sign in to comment.