@@ -9,12 +9,13 @@ namespace PortProxyGUI
99 public partial class NewProxy : Form
1010 {
1111 public readonly PortProxyGUI PortProxyGUI ;
12+ private string AutoString { get ; }
1213
1314 public NewProxy ( PortProxyGUI portProxyGUI )
1415 {
1516 PortProxyGUI = portProxyGUI ;
16-
1717 InitializeComponent ( ) ;
18+ AutoString = comboBox_type . Text = comboBox_type . Items . OfType < string > ( ) . First ( ) ;
1819 }
1920
2021 private void AddPortProxy ( string type , string listenOn , string listenPort , string connectTo , string connectPort )
@@ -25,13 +26,18 @@ private void AddPortProxy(string type, string listenOn, string listenPort, strin
2526
2627 private bool IsIPv4 ( string ip )
2728 {
28- if ( ip == "localhost" || ip == "*" ) return true ;
29- else return ip . IsMatch ( new Regex ( @"^(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])){3}$" ) ) ;
29+ return ip . IsMatch ( new Regex ( @"^(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])){3}$" ) ) ;
3030 }
3131 private bool IsIPv6 ( string ip )
3232 {
33- if ( ip == "localhost" || ip == "*" ) return true ;
34- else return ip . IsMatch ( new Regex ( @"^[\dABCDEF]{2}(?::(?:[\dABCDEF]{2})){5}$" ) ) ;
33+ return ip . IsMatch ( new Regex ( @"^[\dABCDEF]{2}(?::(?:[\dABCDEF]{2})){5}$" ) ) ;
34+ }
35+
36+ private string GetPassType ( string listenOn , string connectTo )
37+ {
38+ var from = IsIPv6 ( listenOn ) ? "v6" : "v4" ;
39+ var to = IsIPv6 ( connectTo ) ? "v6" : "v4" ;
40+ return $ "{ from } to{ to } ";
3541 }
3642
3743 private void button1_Click ( object sender , EventArgs e )
@@ -54,37 +60,11 @@ private void button1_Click(object sender, EventArgs e)
5460 return ;
5561 }
5662
57- if ( string . IsNullOrEmpty ( type ) )
58- {
59- if ( IsIPv4 ( listenOn ) && IsIPv4 ( connectTo ) ) type = comboBox_type . Text = "v4tov4" ;
60- else if ( IsIPv4 ( listenOn ) && IsIPv6 ( connectTo ) ) type = comboBox_type . Text = "v4tov6" ;
61- else if ( IsIPv6 ( listenOn ) && IsIPv4 ( connectTo ) ) type = comboBox_type . Text = "v6tov4" ;
62- else if ( IsIPv6 ( listenOn ) && IsIPv6 ( connectTo ) ) type = comboBox_type . Text = "v6tov6" ;
63- else
64- {
65- MessageBox . Show ( $ "The address which is connect to is neither IPv4 nor IPv6.", "Fail" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
66- return ;
67- }
68- }
69- else if ( new [ ] { "v4tov4" , "v4tov6" , "v6tov4" , "v6tov6" } . Contains ( type ) )
70- {
71- bool invalid = false ;
72- switch ( type )
73- {
74- case "v4tov4" : if ( ! IsIPv4 ( listenOn ) || ! IsIPv4 ( connectTo ) ) invalid = true ; break ;
75- case "v4tov6" : if ( ! IsIPv4 ( listenOn ) || ! IsIPv6 ( connectTo ) ) invalid = true ; break ;
76- case "v6tov4" : if ( ! IsIPv6 ( listenOn ) || ! IsIPv4 ( connectTo ) ) invalid = true ; break ;
77- case "v6tov6" : if ( ! IsIPv6 ( listenOn ) || ! IsIPv6 ( connectTo ) ) invalid = true ; break ;
78- }
79- if ( invalid )
80- {
81- MessageBox . Show ( $ "The type ({ type } ) is invalid for ({ listenOn } -> { connectTo } ).", "Fail" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
82- return ;
83- }
84- }
85- else
63+ if ( type == AutoString ) type = GetPassType ( listenOn , connectTo ) ;
64+
65+ if ( ! new [ ] { "v4tov4" , "v4tov6" , "v6tov4" , "v6tov6" } . Contains ( type ) )
8666 {
87- MessageBox . Show ( $ "Unknow type ( { type } ).", "Fail" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
67+ MessageBox . Show ( $ "Unknow type for ( { listenOn } -> { connectTo } ).", "Fail" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
8868 return ;
8969 }
9070
@@ -93,7 +73,6 @@ private void button1_Click(object sender, EventArgs e)
9373
9474 private void NewProxy_Load ( object sender , EventArgs e )
9575 {
96-
9776 }
9877
9978 private void NewProxy_FormClosing ( object sender , FormClosingEventArgs e )
0 commit comments