9
9
using System . Threading . Tasks ;
10
10
using Titanium . Web . Proxy . EventArguments ;
11
11
using Titanium . Web . Proxy . Helpers ;
12
+ using Titanium . Web . Proxy . Models ;
12
13
using Titanium . Web . Proxy . Network ;
14
+ using System . Linq ;
13
15
14
16
namespace Titanium . Web . Proxy
15
17
{
@@ -31,89 +33,115 @@ public partial class ProxyServer
31
33
private static readonly byte [ ] ChunkEnd =
32
34
Encoding . ASCII . GetBytes ( 0 . ToString ( "x2" ) + Environment . NewLine + Environment . NewLine ) ;
33
35
34
- private static TcpListener _listener ;
36
+ private static List < ProxyEndPoint > _proxyEndPoints { get ; set ; }
35
37
36
- public static List < string > ExcludedHttpsHostNameRegex = new List < string > ( ) ;
37
38
38
39
static ProxyServer ( )
39
40
{
40
41
CertManager = new CertificateManager ( "Titanium" ,
41
42
"Titanium Root Certificate Authority" ) ;
42
43
43
- ListeningIpAddress = IPAddress . Any ;
44
- ListeningPort = 0 ;
44
+ _proxyEndPoints = new List < ProxyEndPoint > ( ) ;
45
45
46
46
Initialize ( ) ;
47
47
}
48
48
49
49
private static CertificateManager CertManager { get ; set ; }
50
+ private static bool EnableSsl { get ; set ; }
51
+ private static bool certTrusted { get ; set ; }
52
+ private static bool proxyStarted { get ; set ; }
50
53
51
54
public static string RootCertificateName { get ; set ; }
52
- public static bool EnableSsl { get ; set ; }
53
- public static bool SetAsSystemProxy { get ; set ; }
54
-
55
- public static int ListeningPort { get ; set ; }
56
- public static IPAddress ListeningIpAddress { get ; set ; }
57
55
58
56
public static event EventHandler < SessionEventArgs > BeforeRequest ;
59
57
public static event EventHandler < SessionEventArgs > BeforeResponse ;
60
58
59
+
61
60
public static void Initialize ( )
62
- {
63
- Task . Factory . StartNew ( ( ) => TcpConnectionManager . ClearIdleConnections ( ) ) ;
61
+ {
62
+ Task . Factory . StartNew ( ( ) => TcpConnectionManager . ClearIdleConnections ( ) ) ;
64
63
}
65
64
65
+ public static void AddEndPoint ( ProxyEndPoint endPoint )
66
+ {
67
+ if ( proxyStarted )
68
+ throw new Exception ( "Cannot add end points after proxy started." ) ;
69
+
70
+ _proxyEndPoints . Add ( endPoint ) ;
71
+ }
72
+
66
73
67
- public static bool Start ( )
74
+ public static void SetAsSystemProxy ( ExplicitProxyEndPoint endPoint )
68
75
{
69
- _listener = new TcpListener ( ListeningIpAddress , ListeningPort ) ;
70
- _listener . Start ( ) ;
76
+ if ( _proxyEndPoints . Contains ( endPoint ) == false )
77
+ throw new Exception ( "Cannot set endPoints not added to proxy as system proxy" ) ;
71
78
72
- ListeningPort = ( ( IPEndPoint ) _listener . LocalEndpoint ) . Port ;
73
- // accept clients asynchronously
74
- _listener . BeginAcceptTcpClient ( OnAcceptConnection , _listener ) ;
79
+ if ( ! proxyStarted )
80
+ throw new Exception ( "Cannot set system proxy settings before proxy has been started." ) ;
75
81
76
- var certTrusted = false ;
82
+ //clear any settings previously added
83
+ _proxyEndPoints . OfType < ExplicitProxyEndPoint > ( ) . ToList ( ) . ForEach ( x => x . IsSystemProxy = false ) ;
77
84
78
- if ( EnableSsl )
79
- certTrusted = CertManager . CreateTrustedRootCertificate ( ) ;
85
+ endPoint . IsSystemProxy = true ;
80
86
81
- if ( SetAsSystemProxy )
82
- {
83
- SystemProxyHelper . EnableProxyHttp (
84
- Equals ( ListeningIpAddress , IPAddress . Any ) ? "127.0.0.1" : ListeningIpAddress . ToString ( ) , ListeningPort ) ;
87
+ SystemProxyHelper . EnableProxyHttp (
88
+ Equals ( endPoint . IpAddress , IPAddress . Any ) ? "127.0.0.1" : endPoint . IpAddress . ToString ( ) , endPoint . Port ) ;
85
89
86
90
#if ! DEBUG
87
- FireFoxHelper . AddFirefox ( ) ;
91
+ FireFoxHelper . AddFirefox ( ) ;
88
92
#endif
89
93
94
+ if ( endPoint . EnableSsl )
95
+ {
96
+ RootCertificateName = RootCertificateName ?? "Titanium_Proxy_Test_Root" ;
90
97
91
- if ( EnableSsl )
98
+ //If certificate was trusted by the machine
99
+ if ( certTrusted )
92
100
{
93
- RootCertificateName = RootCertificateName ?? "Titanium_Proxy_Test_Root" ;
94
-
95
- //If certificate was trusted by the machine
96
- if ( certTrusted )
97
- {
98
- SystemProxyHelper . EnableProxyHttps (
99
- Equals ( ListeningIpAddress , IPAddress . Any ) ? "127.0.0.1" : ListeningIpAddress . ToString ( ) ,
100
- ListeningPort ) ;
101
- }
101
+ SystemProxyHelper . EnableProxyHttps (
102
+ Equals ( endPoint . IpAddress , IPAddress . Any ) ? "127.0.0.1" : endPoint . IpAddress . ToString ( ) ,
103
+ endPoint . Port ) ;
102
104
}
103
105
}
104
106
105
- return true ;
107
+ }
108
+
109
+ public static void Start ( )
110
+ {
111
+ EnableSsl = _proxyEndPoints . Any ( x => x . EnableSsl ) ;
112
+
113
+ if ( EnableSsl )
114
+ certTrusted = CertManager . CreateTrustedRootCertificate ( ) ;
115
+
116
+ foreach ( var endPoint in _proxyEndPoints )
117
+ {
118
+
119
+ endPoint . listener = new TcpListener ( endPoint . IpAddress , endPoint . Port ) ;
120
+ endPoint . listener . Start ( ) ;
121
+
122
+ endPoint . Port = ( ( IPEndPoint ) endPoint . listener . LocalEndpoint ) . Port ;
123
+ // accept clients asynchronously
124
+ endPoint . listener . BeginAcceptTcpClient ( OnAcceptConnection , endPoint ) ;
125
+ }
126
+
127
+ proxyStarted = true ;
106
128
}
107
129
108
130
private static void OnAcceptConnection ( IAsyncResult asyn )
109
131
{
132
+ var endPoint = ( ProxyEndPoint ) asyn . AsyncState ;
133
+
134
+ // Get the listener that handles the client request.
135
+ endPoint . listener . BeginAcceptTcpClient ( OnAcceptConnection , endPoint ) ;
136
+
137
+ var client = endPoint . listener . EndAcceptTcpClient ( asyn ) ;
138
+
110
139
try
111
140
{
112
- // Get the listener that handles the client request.
113
- _listener . BeginAcceptTcpClient ( OnAcceptConnection , _listener ) ;
114
-
115
- var client = _listener . EndAcceptTcpClient ( asyn ) ;
116
- Task . Factory . StartNew ( ( ) => HandleClient ( client ) ) ;
141
+ if ( endPoint . GetType ( ) == typeof ( TransparentProxyEndPoint ) )
142
+ Task . Factory . StartNew ( ( ) => HandleClient ( endPoint as TransparentProxyEndPoint , client ) ) ;
143
+ else
144
+ Task . Factory . StartNew ( ( ) => HandleClient ( endPoint as ExplicitProxyEndPoint , client ) ) ;
117
145
}
118
146
catch
119
147
{
@@ -124,6 +152,8 @@ private static void OnAcceptConnection(IAsyncResult asyn)
124
152
125
153
public static void Stop ( )
126
154
{
155
+ var SetAsSystemProxy = _proxyEndPoints . OfType < ExplicitProxyEndPoint > ( ) . Any ( x => x . IsSystemProxy ) ;
156
+
127
157
if ( SetAsSystemProxy )
128
158
{
129
159
SystemProxyHelper . DisableAllProxy ( ) ;
@@ -132,7 +162,11 @@ public static void Stop()
132
162
#endif
133
163
}
134
164
135
- _listener . Stop ( ) ;
165
+ foreach ( var endPoint in _proxyEndPoints )
166
+ {
167
+ endPoint . listener . Stop ( ) ;
168
+ }
169
+
136
170
CertManager . Dispose ( ) ;
137
171
}
138
172
}
0 commit comments