Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
fc8576bf
Commit
fc8576bf
authored
Jun 06, 2017
by
Honfika
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Set HTTP and HTTPS proxy in one step.
parent
d42864ec
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
163 additions
and
57 deletions
+163
-57
ProxyTestController.cs
.../Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
+4
-2
SystemProxy.cs
Titanium.Web.Proxy/Helpers/SystemProxy.cs
+80
-22
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+79
-33
No files found.
Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
View file @
fc8576bf
...
@@ -4,6 +4,7 @@ using System.Net;
...
@@ -4,6 +4,7 @@ using System.Net;
using
System.Net.Security
;
using
System.Net.Security
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
namespace
Titanium.Web.Proxy.Examples.Basic
namespace
Titanium.Web.Proxy.Examples.Basic
...
@@ -92,8 +93,9 @@ namespace Titanium.Web.Proxy.Examples.Basic
...
@@ -92,8 +93,9 @@ namespace Titanium.Web.Proxy.Examples.Basic
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
endPoint
.
GetType
().
Name
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
//Only explicit proxies can be set as system proxy!
//Only explicit proxies can be set as system proxy!
proxyServer
.
SetAsSystemHttpProxy
(
explicitEndPoint
);
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
proxyServer
.
SetAsSystemHttpsProxy
(
explicitEndPoint
);
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
proxyServer
.
SetAsSystemProxy
(
explicitEndPoint
,
ProxyProtocolType
.
AllHttp
);
}
}
public
void
Stop
()
public
void
Stop
()
...
...
Titanium.Web.Proxy/Helpers/SystemProxy.cs
View file @
fc8576bf
...
@@ -8,10 +8,28 @@ using Microsoft.Win32;
...
@@ -8,10 +8,28 @@ using Microsoft.Win32;
// Helper classes for setting system proxy settings
// Helper classes for setting system proxy settings
namespace
Titanium.Web.Proxy.Helpers
namespace
Titanium.Web.Proxy.Helpers
{
{
internal
enum
ProxyProtocolType
[
Flags
]
public
enum
ProxyProtocolType
{
{
Http
,
/// <summary>
Https
,
/// The none
/// </summary>
None
=
0
,
/// <summary>
/// HTTP
/// </summary>
Http
=
1
,
/// <summary>
/// HTTPS
/// </summary>
Https
=
2
,
/// <summary>
/// Both HTTP and HTTPS
/// </summary>
AllHttp
=
Http
|
Https
,
}
}
internal
partial
class
NativeMethods
internal
partial
class
NativeMethods
...
@@ -25,11 +43,23 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -25,11 +43,23 @@ namespace Titanium.Web.Proxy.Helpers
{
{
internal
string
HostName
{
get
;
set
;
}
internal
string
HostName
{
get
;
set
;
}
internal
int
Port
{
get
;
set
;
}
internal
int
Port
{
get
;
set
;
}
internal
bool
IsHttps
{
get
;
set
;
}
internal
ProxyProtocolType
ProtocolType
{
get
;
set
;
}
public
override
string
ToString
()
public
override
string
ToString
()
{
{
return
$"
{(
IsHttps
?
"https"
:
"http"
)}
=
{
HostName
}
:
{
Port
}
"
;
string
protocol
=
null
;
switch
(
ProtocolType
)
{
case
ProxyProtocolType
.
Http
:
protocol
=
"http"
;
break
;
case
ProxyProtocolType
.
Https
:
protocol
=
"https"
;
break
;
default
:
throw
new
Exception
(
"Unsupported protocol type"
);
}
return
$"
{
protocol
}
=
{
HostName
}
:
{
Port
}
"
;
}
}
}
}
...
@@ -72,7 +102,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -72,7 +102,7 @@ namespace Titanium.Web.Proxy.Helpers
RemoveProxy
(
ProxyProtocolType
.
Https
);
RemoveProxy
(
ProxyProtocolType
.
Https
);
}
}
private
void
SetProxy
(
string
hostname
,
int
port
,
ProxyProtocolType
protocolType
)
internal
void
SetProxy
(
string
hostname
,
int
port
,
ProxyProtocolType
protocolType
)
{
{
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
...
@@ -83,13 +113,26 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -83,13 +113,26 @@ namespace Titanium.Web.Proxy.Helpers
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
protocolType
==
ProxyProtocolType
.
Https
?
x
.
IsHttps
:
!
x
.
IsHttps
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
(
protocolType
&
x
.
ProtocolType
)
!=
0
);
existingSystemProxyValues
.
Add
(
new
HttpSystemProxyValue
if
((
protocolType
&
ProxyProtocolType
.
Http
)
!=
0
)
{
existingSystemProxyValues
.
Add
(
new
HttpSystemProxyValue
{
HostName
=
hostname
,
ProtocolType
=
ProxyProtocolType
.
Http
,
Port
=
port
});
}
if
((
protocolType
&
ProxyProtocolType
.
Https
)
!=
0
)
{
{
HostName
=
hostname
,
existingSystemProxyValues
.
Add
(
new
HttpSystemProxyValue
IsHttps
=
protocolType
==
ProxyProtocolType
.
Https
,
{
Port
=
port
HostName
=
hostname
,
});
ProtocolType
=
ProxyProtocolType
.
Https
,
Port
=
port
});
}
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
reg
.
SetValue
(
"ProxyServer"
,
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
...
@@ -98,7 +141,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -98,7 +141,7 @@ namespace Titanium.Web.Proxy.Helpers
Refresh
();
Refresh
();
}
}
private
void
RemoveProxy
(
ProxyProtocolType
protocolType
)
internal
void
RemoveProxy
(
ProxyProtocolType
protocolType
)
{
{
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
...
@@ -107,7 +150,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -107,7 +150,7 @@ namespace Titanium.Web.Proxy.Helpers
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
protocolType
==
ProxyProtocolType
.
Https
?
x
.
IsHttps
:
!
x
.
IsHttps
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
(
protocolType
|
x
.
ProtocolType
)
!=
0
);
if
(
existingSystemProxyValues
.
Count
!=
0
)
if
(
existingSystemProxyValues
.
Count
!=
0
)
{
{
...
@@ -176,17 +219,32 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -176,17 +219,32 @@ namespace Titanium.Web.Proxy.Helpers
/// <returns></returns>
/// <returns></returns>
private
HttpSystemProxyValue
ParseProxyValue
(
string
value
)
private
HttpSystemProxyValue
ParseProxyValue
(
string
value
)
{
{
var
tmp
=
Regex
.
Replace
(
value
,
@"\s+"
,
" "
).
Trim
()
.
ToLower
()
;
var
tmp
=
Regex
.
Replace
(
value
,
@"\s+"
,
" "
).
Trim
();
if
(
tmp
.
StartsWith
(
"http="
)
||
tmp
.
StartsWith
(
"https="
))
int
equalsIndex
=
tmp
.
IndexOf
(
"="
,
StringComparison
.
InvariantCulture
);
if
(
equalsIndex
>=
0
)
{
{
var
endPoint
=
tmp
.
Substring
(
5
);
string
protocolTypeStr
=
tmp
.
Substring
(
0
,
equalsIndex
);
return
new
HttpSystemProxyValue
ProxyProtocolType
?
protocolType
=
null
;
if
(
protocolTypeStr
.
Equals
(
"http"
,
StringComparison
.
InvariantCultureIgnoreCase
))
{
{
HostName
=
endPoint
.
Split
(
':'
)[
0
],
protocolType
=
ProxyProtocolType
.
Http
;
Port
=
int
.
Parse
(
endPoint
.
Split
(
':'
)[
1
]),
}
IsHttps
=
tmp
.
StartsWith
(
"https="
)
else
if
(
protocolTypeStr
.
Equals
(
"https"
,
StringComparison
.
InvariantCultureIgnoreCase
))
};
{
protocolType
=
ProxyProtocolType
.
Https
;
}
if
(
protocolType
.
HasValue
)
{
var
endPointParts
=
tmp
.
Substring
(
equalsIndex
+
1
).
Split
(
':'
);
return
new
HttpSystemProxyValue
{
HostName
=
endPointParts
[
0
],
Port
=
int
.
Parse
(
endPointParts
[
1
]),
ProtocolType
=
protocolType
.
Value
,
};
}
}
}
return
null
;
return
null
;
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
fc8576bf
...
@@ -348,24 +348,7 @@ namespace Titanium.Web.Proxy
...
@@ -348,24 +348,7 @@ namespace Titanium.Web.Proxy
/// <param name="endPoint"></param>
/// <param name="endPoint"></param>
public
void
SetAsSystemHttpProxy
(
ExplicitProxyEndPoint
endPoint
)
public
void
SetAsSystemHttpProxy
(
ExplicitProxyEndPoint
endPoint
)
{
{
if
(
RunTime
.
IsRunningOnMono
)
SetAsSystemProxy
(
endPoint
,
ProxyProtocolType
.
Http
);
{
throw
new
Exception
(
"Mono Runtime do not support system proxy settings."
);
}
ValidateEndPointAsSystemProxy
(
endPoint
);
//clear any settings previously added
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemHttpProxy
=
false
);
systemProxySettingsManager
.
SetHttpProxy
(
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
endPoint
.
Port
);
endPoint
.
IsSystemHttpProxy
=
true
;
firefoxProxySettingsManager
.
UseSystemProxy
();
Console
.
WriteLine
(
"Set endpoint at Ip {0} and port: {1} as System HTTP Proxy"
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
}
}
...
@@ -374,6 +357,16 @@ namespace Titanium.Web.Proxy
...
@@ -374,6 +357,16 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="endPoint"></param>
/// <param name="endPoint"></param>
public
void
SetAsSystemHttpsProxy
(
ExplicitProxyEndPoint
endPoint
)
public
void
SetAsSystemHttpsProxy
(
ExplicitProxyEndPoint
endPoint
)
{
SetAsSystemProxy
(
endPoint
,
ProxyProtocolType
.
Https
);
}
/// <summary>
/// Set the given explicit end point as the default proxy server for current machine
/// </summary>
/// <param name="endPoint"></param>
/// <param name="protocolType"></param>
public
void
SetAsSystemProxy
(
ExplicitProxyEndPoint
endPoint
,
ProxyProtocolType
protocolType
)
{
{
if
(
RunTime
.
IsRunningOnMono
)
if
(
RunTime
.
IsRunningOnMono
)
{
{
...
@@ -382,33 +375,73 @@ namespace Titanium.Web.Proxy
...
@@ -382,33 +375,73 @@ namespace Titanium.Web.Proxy
ValidateEndPointAsSystemProxy
(
endPoint
);
ValidateEndPointAsSystemProxy
(
endPoint
);
if
(!
endPoint
.
EnableSsl
)
bool
isHttp
=
(
protocolType
&
ProxyProtocolType
.
Http
)
>
0
;
bool
isHttps
=
(
protocolType
&
ProxyProtocolType
.
Https
)
>
0
;
if
(
isHttps
)
{
{
throw
new
Exception
(
"Endpoint do not support Https connections"
);
if
(!
endPoint
.
EnableSsl
)
{
throw
new
Exception
(
"Endpoint do not support Https connections"
);
}
EnsureRootCertificate
();
//If certificate was trusted by the machine
if
(!
CertificateManager
.
CertValidated
)
{
protocolType
=
protocolType
&
~
ProxyProtocolType
.
Https
;
isHttps
=
false
;
}
}
}
//clear any settings previously added
//clear any settings previously added
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>()
if
(
isHttp
)
.
ToList
()
{
.
ForEach
(
x
=>
x
.
IsSystemHttpsProxy
=
false
);
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemHttpProxy
=
false
);
}
EnsureRootCertificate
();
//If certificate was trusted by the machine
if
(
isHttps
)
if
(
CertificateManager
.
CertValidated
)
{
{
systemProxySettingsManager
.
SetHttpsProxy
(
ProxyEndPoints
.
OfType
<
ExplicitProxyEndPoint
>().
ToList
().
ForEach
(
x
=>
x
.
IsSystemHttpsProxy
=
false
);
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
endPoint
.
Port
);
}
}
systemProxySettingsManager
.
SetProxy
(
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Any
)
|
Equals
(
endPoint
.
IpAddress
,
IPAddress
.
Loopback
)
?
"127.0.0.1"
:
endPoint
.
IpAddress
.
ToString
(),
endPoint
.
Port
,
protocolType
);
endPoint
.
IsSystemHttpsProxy
=
true
;
if
(
isHttp
)
{
endPoint
.
IsSystemHttpsProxy
=
true
;
}
if
(
isHttps
)
{
endPoint
.
IsSystemHttpsProxy
=
true
;
}
firefoxProxySettingsManager
.
UseSystemProxy
();
firefoxProxySettingsManager
.
UseSystemProxy
();
Console
.
WriteLine
(
"Set endpoint at Ip {0} and port: {1} as System HTTPS Proxy"
,
endPoint
.
IpAddress
,
endPoint
.
Port
);
string
proxyType
=
null
;
switch
(
protocolType
)
{
case
ProxyProtocolType
.
Http
:
proxyType
=
"HTTP"
;
break
;
case
ProxyProtocolType
.
Https
:
proxyType
=
"HTTPS"
;
break
;
case
ProxyProtocolType
.
AllHttp
:
proxyType
=
"HTTP and HTTPS"
;
break
;
}
if
(
protocolType
!=
ProxyProtocolType
.
None
)
{
Console
.
WriteLine
(
"Set endpoint at Ip {0} and port: {1} as System {2} Proxy"
,
endPoint
.
IpAddress
,
endPoint
.
Port
,
proxyType
);
}
}
}
/// <summary>
/// <summary>
...
@@ -437,6 +470,19 @@ namespace Titanium.Web.Proxy
...
@@ -437,6 +470,19 @@ namespace Titanium.Web.Proxy
systemProxySettingsManager
.
RemoveHttpsProxy
();
systemProxySettingsManager
.
RemoveHttpsProxy
();
}
}
/// <summary>
/// Remove the specified proxy settings for current machine
/// </summary>
public
void
DisableSystemProxy
(
ProxyProtocolType
protocolType
)
{
if
(
RunTime
.
IsRunningOnMono
)
{
throw
new
Exception
(
"Mono Runtime do not support system proxy settings."
);
}
systemProxySettingsManager
.
RemoveProxy
(
protocolType
);
}
/// <summary>
/// <summary>
/// Clear all proxy settings for current machine
/// Clear all proxy settings for current machine
/// </summary>
/// </summary>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment