File tree Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Original file line number Diff line number Diff line change 6
6
using System . IO ;
7
7
using System . IO . Compression ;
8
8
using System . Text ;
9
+ using System . Net . NetworkInformation ;
10
+ using System . Net ;
9
11
10
12
namespace Shadowsocks . Controller
11
13
{
@@ -47,6 +49,7 @@ public void Start(Configuration configuration)
47
49
}
48
50
string polipoConfig = Resources . polipo_config ;
49
51
polipoConfig = polipoConfig . Replace ( "__SOCKS_PORT__" , configuration . localPort . ToString ( ) ) ;
52
+ polipoConfig = polipoConfig . Replace ( "__POLIPO_BIND_PORT__" , this . GetFreePort ( ) . ToString ( ) ) ;
50
53
polipoConfig = polipoConfig . Replace ( "__POLIPO_BIND_IP__" , configuration . shareOverLan ? "0.0.0.0" : "127.0.0.1" ) ;
51
54
FileManager . ByteArrayToFile ( temppath + "/polipo.conf" , System . Text . Encoding . UTF8 . GetBytes ( polipoConfig ) ) ;
52
55
@@ -79,5 +82,25 @@ public void Stop()
79
82
_process = null ;
80
83
}
81
84
}
85
+
86
+ private int GetFreePort ( )
87
+ {
88
+ IPGlobalProperties properties = IPGlobalProperties . GetIPGlobalProperties ( ) ;
89
+ IPEndPoint [ ] tcpEndPoints = properties . GetActiveTcpListeners ( ) ;
90
+
91
+ List < int > usedPorts = new List < int > ( ) ;
92
+ foreach ( IPEndPoint endPoint in IPGlobalProperties . GetIPGlobalProperties ( ) . GetActiveTcpListeners ( ) )
93
+ {
94
+ usedPorts . Add ( endPoint . Port ) ;
95
+ }
96
+ for ( int port = 8123 ; port < 65535 ; port ++ )
97
+ {
98
+ if ( ! usedPorts . Contains ( port ) )
99
+ {
100
+ return port ;
101
+ }
102
+ }
103
+ throw new Exception ( "No free port found." ) ;
104
+ }
82
105
}
83
106
}
Original file line number Diff line number Diff line change @@ -222,7 +222,7 @@ protected void Reload()
222
222
List < Listener . Service > services = new List < Listener . Service > ( ) ;
223
223
services . Add ( local ) ;
224
224
services . Add ( _pacServer ) ;
225
- services . Add ( new PortForwarder ( 8123 ) ) ;
225
+ services . Add ( new PortForwarder ( _config . localPort ) ) ;
226
226
_listener = new Listener ( services ) ;
227
227
_listener . Start ( _config ) ;
228
228
}
Original file line number Diff line number Diff line change 1
- proxyAddress = "__POLIPO_BIND_IP__"
1
+ proxyAddress = "__POLIPO_BIND_IP__"
2
+ proxyPort = 8123
2
3
3
4
socksParentProxy = "127.0.0.1:__SOCKS_PORT__"
4
5
socksProxyType = socks5
You can’t perform that action at this time.
0 commit comments