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
4c68f687
Commit
4c68f687
authored
Oct 01, 2016
by
Hakan Arıcı
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Various code refactorings.
parent
c1d0e60e
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
159 additions
and
272 deletions
+159
-272
StreamExtensions.cs
Titanium.Web.Proxy/Extensions/StreamExtensions.cs
+45
-48
CustomBinaryReader.cs
Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
+24
-31
SystemProxy.cs
Titanium.Web.Proxy/Helpers/SystemProxy.cs
+59
-112
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+2
-16
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+13
-35
Request.cs
Titanium.Web.Proxy/Http/Request.cs
+5
-5
Response.cs
Titanium.Web.Proxy/Http/Response.cs
+2
-3
OkResponse.cs
Titanium.Web.Proxy/Http/Responses/OkResponse.cs
+1
-1
RedirectResponse.cs
Titanium.Web.Proxy/Http/Responses/RedirectResponse.cs
+2
-4
CertificateManager.cs
Titanium.Web.Proxy/Network/CertificateManager.cs
+0
-2
TcpConnectionFactory.cs
Titanium.Web.Proxy/Network/TcpConnectionFactory.cs
+3
-3
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+3
-12
No files found.
Titanium.Web.Proxy/Extensions/StreamExtensions.cs
View file @
4c68f687
...
@@ -29,10 +29,12 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -29,10 +29,12 @@ namespace Titanium.Web.Proxy.Extensions
await
input
.
CopyToAsync
(
output
);
await
input
.
CopyToAsync
(
output
);
}
}
/// <summary>
/// <summary>
/// copies the specified bytes to the stream from the input stream
/// copies the specified bytes to the stream from the input stream
/// </summary>
/// </summary>
/// <param name="streamReader"></param>
/// <param name="streamReader"></param>
/// <param name="bufferSize"></param>
/// <param name="stream"></param>
/// <param name="stream"></param>
/// <param name="totalBytesToRead"></param>
/// <param name="totalBytesToRead"></param>
/// <returns></returns>
/// <returns></returns>
...
@@ -40,16 +42,7 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -40,16 +42,7 @@ namespace Titanium.Web.Proxy.Extensions
{
{
var
totalbytesRead
=
0
;
var
totalbytesRead
=
0
;
long
bytesToRead
;
long
bytesToRead
=
totalBytesToRead
<
bufferSize
?
totalBytesToRead
:
bufferSize
;
if
(
totalBytesToRead
<
bufferSize
)
{
bytesToRead
=
totalBytesToRead
;
}
else
{
bytesToRead
=
bufferSize
;
}
while
(
totalbytesRead
<
totalBytesToRead
)
while
(
totalbytesRead
<
totalBytesToRead
)
{
{
...
@@ -76,6 +69,7 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -76,6 +69,7 @@ namespace Titanium.Web.Proxy.Extensions
/// Copies the stream chunked
/// Copies the stream chunked
/// </summary>
/// </summary>
/// <param name="clientStreamReader"></param>
/// <param name="clientStreamReader"></param>
/// <param name="bufferSize"></param>
/// <param name="stream"></param>
/// <param name="stream"></param>
/// <returns></returns>
/// <returns></returns>
internal
static
async
Task
CopyBytesToStreamChunked
(
this
CustomBinaryReader
clientStreamReader
,
int
bufferSize
,
Stream
stream
)
internal
static
async
Task
CopyBytesToStreamChunked
(
this
CustomBinaryReader
clientStreamReader
,
int
bufferSize
,
Stream
stream
)
...
@@ -117,30 +111,32 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -117,30 +111,32 @@ namespace Titanium.Web.Proxy.Extensions
await
WriteResponseBodyChunked
(
data
,
clientStream
);
await
WriteResponseBodyChunked
(
data
,
clientStream
);
}
}
}
}
/// <summary>
/// <summary>
/// Copies the specified content length number of bytes to the output stream from the given inputs stream
/// Copies the specified content length number of bytes to the output stream from the given inputs stream
/// optionally chunked
/// optionally chunked
/// </summary>
/// </summary>
/// <param name="inStreamReader"></param>
/// <param name="inStreamReader"></param>
/// <param name="bufferSize"></param>
/// <param name="outStream"></param>
/// <param name="outStream"></param>
/// <param name="isChunked"></param>
/// <param name="isChunked"></param>
/// <param name="C
ontentLength"></param>
/// <param name="c
ontentLength"></param>
/// <returns></returns>
/// <returns></returns>
internal
static
async
Task
WriteResponseBody
(
this
CustomBinaryReader
inStreamReader
,
int
bufferSize
,
Stream
outStream
,
bool
isChunked
,
long
C
ontentLength
)
internal
static
async
Task
WriteResponseBody
(
this
CustomBinaryReader
inStreamReader
,
int
bufferSize
,
Stream
outStream
,
bool
isChunked
,
long
c
ontentLength
)
{
{
if
(!
isChunked
)
if
(!
isChunked
)
{
{
//http 1.0
//http 1.0
if
(
C
ontentLength
==
-
1
)
if
(
c
ontentLength
==
-
1
)
{
{
C
ontentLength
=
long
.
MaxValue
;
c
ontentLength
=
long
.
MaxValue
;
}
}
int
bytesToRead
=
bufferSize
;
int
bytesToRead
=
bufferSize
;
if
(
C
ontentLength
<
bufferSize
)
if
(
c
ontentLength
<
bufferSize
)
{
{
bytesToRead
=
(
int
)
C
ontentLength
;
bytesToRead
=
(
int
)
c
ontentLength
;
}
}
var
buffer
=
new
byte
[
bufferSize
];
var
buffer
=
new
byte
[
bufferSize
];
...
@@ -153,11 +149,11 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -153,11 +149,11 @@ namespace Titanium.Web.Proxy.Extensions
await
outStream
.
WriteAsync
(
buffer
,
0
,
bytesRead
);
await
outStream
.
WriteAsync
(
buffer
,
0
,
bytesRead
);
totalBytesRead
+=
bytesRead
;
totalBytesRead
+=
bytesRead
;
if
(
totalBytesRead
==
C
ontentLength
)
if
(
totalBytesRead
==
c
ontentLength
)
break
;
break
;
bytesRead
=
0
;
bytesRead
=
0
;
var
remainingBytes
=
(
C
ontentLength
-
totalBytesRead
);
var
remainingBytes
=
(
c
ontentLength
-
totalBytesRead
);
bytesToRead
=
remainingBytes
>
(
long
)
bufferSize
?
bufferSize
:
(
int
)
remainingBytes
;
bytesToRead
=
remainingBytes
>
(
long
)
bufferSize
?
bufferSize
:
(
int
)
remainingBytes
;
}
}
}
}
...
@@ -171,6 +167,7 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -171,6 +167,7 @@ namespace Titanium.Web.Proxy.Extensions
/// Copies the streams chunked
/// Copies the streams chunked
/// </summary>
/// </summary>
/// <param name="inStreamReader"></param>
/// <param name="inStreamReader"></param>
/// <param name="bufferSize"></param>
/// <param name="outStream"></param>
/// <param name="outStream"></param>
/// <returns></returns>
/// <returns></returns>
internal
static
async
Task
WriteResponseBodyChunked
(
this
CustomBinaryReader
inStreamReader
,
int
bufferSize
,
Stream
outStream
)
internal
static
async
Task
WriteResponseBodyChunked
(
this
CustomBinaryReader
inStreamReader
,
int
bufferSize
,
Stream
outStream
)
...
...
Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
View file @
4c68f687
...
@@ -36,8 +36,6 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -36,8 +36,6 @@ namespace Titanium.Web.Proxy.Helpers
internal
async
Task
<
string
>
ReadLineAsync
()
internal
async
Task
<
string
>
ReadLineAsync
()
{
{
using
(
var
readBuffer
=
new
MemoryStream
())
using
(
var
readBuffer
=
new
MemoryStream
())
{
try
{
{
var
lastChar
=
default
(
char
);
var
lastChar
=
default
(
char
);
var
buffer
=
new
byte
[
1
];
var
buffer
=
new
byte
[
1
];
...
@@ -64,11 +62,6 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -64,11 +62,6 @@ namespace Titanium.Web.Proxy.Helpers
return
encoding
.
GetString
(
readBuffer
.
ToArray
());
return
encoding
.
GetString
(
readBuffer
.
ToArray
());
}
}
catch
(
IOException
)
{
throw
;
}
}
}
}
/// <summary>
/// <summary>
...
...
Titanium.Web.Proxy/Helpers/SystemProxy.cs
View file @
4c68f687
...
@@ -4,12 +4,18 @@ using Microsoft.Win32;
...
@@ -4,12 +4,18 @@ using Microsoft.Win32;
using
System.Text.RegularExpressions
;
using
System.Text.RegularExpressions
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Linq
;
using
System.Net.Sockets
;
/// <summary>
/// <summary>
/// Helper classes for setting system proxy settings
/// Helper classes for setting system proxy settings
/// </summary>
/// </summary>
namespace
Titanium.Web.Proxy.Helpers
namespace
Titanium.Web.Proxy.Helpers
{
{
internal
enum
ProxyProtocolType
{
Http
,
Https
,
}
internal
partial
class
NativeMethods
internal
partial
class
NativeMethods
{
{
...
@@ -26,16 +32,10 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -26,16 +32,10 @@ namespace Titanium.Web.Proxy.Helpers
public
override
string
ToString
()
public
override
string
ToString
()
{
{
if
(!
IsHttps
)
return
$"
{(
IsHttps
?
"https"
:
"http"
)}
=
{
HostName
}
:
{
Port
}
"
;
{
return
"http="
+
HostName
+
":"
+
Port
;
}
else
{
return
"https="
+
HostName
+
":"
+
Port
;
}
}
}
}
}
/// <summary>
/// <summary>
/// Manage system proxy settings
/// Manage system proxy settings
/// </summary>
/// </summary>
...
@@ -46,28 +46,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -46,28 +46,7 @@ namespace Titanium.Web.Proxy.Helpers
internal
void
SetHttpProxy
(
string
hostname
,
int
port
)
internal
void
SetHttpProxy
(
string
hostname
,
int
port
)
{
{
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
SetProxy
(
hostname
,
port
,
ProxyProtocolType
.
Http
);
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
if
(
reg
!=
null
)
{
prepareRegistry
(
reg
);
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
!
x
.
IsHttps
);
existingSystemProxyValues
.
Add
(
new
HttpSystemProxyValue
()
{
HostName
=
hostname
,
IsHttps
=
false
,
Port
=
port
});
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
String
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
}
Refresh
();
}
}
/// <summary>
/// <summary>
...
@@ -75,31 +54,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -75,31 +54,7 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary>
/// </summary>
internal
void
RemoveHttpProxy
()
internal
void
RemoveHttpProxy
()
{
{
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
RemoveProxy
(
ProxyProtocolType
.
Http
);
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
if
(
reg
!=
null
)
{
if
(
reg
.
GetValue
(
"ProxyServer"
)
!=
null
)
{
var
exisitingContent
=
reg
.
GetValue
(
"ProxyServer"
)
as
string
;
var
existingSystemProxyValues
=
GetSystemProxyValues
(
exisitingContent
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
!
x
.
IsHttps
);
if
(!(
existingSystemProxyValues
.
Count
()
==
0
))
{
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
String
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
}
else
{
reg
.
SetValue
(
"ProxyEnable"
,
0
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
}
}
}
Refresh
();
}
}
/// <summary>
/// <summary>
...
@@ -108,60 +63,65 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -108,60 +63,65 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="hostname"></param>
/// <param name="hostname"></param>
/// <param name="port"></param>
/// <param name="port"></param>
internal
void
SetHttpsProxy
(
string
hostname
,
int
port
)
internal
void
SetHttpsProxy
(
string
hostname
,
int
port
)
{
SetProxy
(
hostname
,
port
,
ProxyProtocolType
.
Https
);
}
/// <summary>
/// Removes the https proxy setting to nothing
/// </summary>
internal
void
RemoveHttpsProxy
()
{
RemoveProxy
(
ProxyProtocolType
.
Https
);
}
private
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
);
if
(
reg
!=
null
)
if
(
reg
!=
null
)
{
{
p
repareRegistry
(
reg
);
P
repareRegistry
(
reg
);
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
=>
x
.
IsHttps
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
protocolType
==
ProxyProtocolType
.
Https
?
x
.
IsHttps
:
!
x
.
IsHttps
);
existingSystemProxyValues
.
Add
(
new
HttpSystemProxyValue
()
existingSystemProxyValues
.
Add
(
new
HttpSystemProxyValue
()
{
{
HostName
=
hostname
,
HostName
=
hostname
,
IsHttps
=
true
,
IsHttps
=
protocolType
==
ProxyProtocolType
.
Https
,
Port
=
port
Port
=
port
});
});
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
S
tring
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
reg
.
SetValue
(
"ProxyServer"
,
s
tring
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
}
}
Refresh
();
Refresh
();
}
}
/// <summary>
private
void
RemoveProxy
(
ProxyProtocolType
protocolType
)
/// Removes the https proxy setting to nothing
/// </summary>
internal
void
RemoveHttpsProxy
()
{
{
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
var
reg
=
Registry
.
CurrentUser
.
OpenSubKey
(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
,
true
);
if
(
reg
!=
null
)
if
(
reg
?.
GetValue
(
"ProxyServer"
)
!=
null
)
{
if
(
reg
.
GetValue
(
"ProxyServer"
)
!=
null
)
{
{
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
=>
x
.
IsHttps
);
existingSystemProxyValues
.
RemoveAll
(
x
=>
protocolType
==
ProxyProtocolType
.
Https
?
x
.
IsHttps
:
!
x
.
IsHttps
);
if
(!(
existingSystemProxyValues
.
Count
()
==
0
)
)
if
(
existingSystemProxyValues
.
Count
!=
0
)
{
{
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyEnable"
,
1
);
reg
.
SetValue
(
"ProxyServer"
,
S
tring
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
reg
.
SetValue
(
"ProxyServer"
,
s
tring
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
}
}
else
else
{
{
reg
.
SetValue
(
"ProxyEnable"
,
0
);
reg
.
SetValue
(
"ProxyEnable"
,
0
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
reg
.
SetValue
(
"ProxyServer"
,
string
.
Empty
);
}
}
}
}
}
Refresh
();
Refresh
();
...
@@ -200,16 +160,11 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -200,16 +160,11 @@ namespace Titanium.Web.Proxy.Helpers
if
(
proxyValues
.
Length
>
0
)
if
(
proxyValues
.
Length
>
0
)
{
{
foreach
(
var
value
in
proxyValues
)
result
.
AddRange
(
proxyValues
.
Select
(
ParseProxyValue
).
Where
(
parsedValue
=>
parsedValue
!=
null
));
{
var
parsedValue
=
parseProxyValue
(
value
);
if
(
parsedValue
!=
null
)
result
.
Add
(
parsedValue
);
}
}
}
else
else
{
{
var
parsedValue
=
p
arseProxyValue
(
prevServerValue
);
var
parsedValue
=
P
arseProxyValue
(
prevServerValue
);
if
(
parsedValue
!=
null
)
if
(
parsedValue
!=
null
)
result
.
Add
(
parsedValue
);
result
.
Add
(
parsedValue
);
}
}
...
@@ -222,29 +177,21 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -222,29 +177,21 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary>
/// </summary>
/// <param name="value"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <returns></returns>
private
HttpSystemProxyValue
p
arseProxyValue
(
string
value
)
private
HttpSystemProxyValue
P
arseProxyValue
(
string
value
)
{
{
var
tmp
=
Regex
.
Replace
(
value
,
@"\s+"
,
" "
).
Trim
().
ToLower
();
var
tmp
=
Regex
.
Replace
(
value
,
@"\s+"
,
" "
).
Trim
().
ToLower
();
if
(
tmp
.
StartsWith
(
"http="
))
{
if
(
tmp
.
StartsWith
(
"http="
)
||
tmp
.
StartsWith
(
"https="
))
var
endPoint
=
tmp
.
Substring
(
5
);
return
new
HttpSystemProxyValue
()
{
HostName
=
endPoint
.
Split
(
':'
)[
0
],
Port
=
int
.
Parse
(
endPoint
.
Split
(
':'
)[
1
]),
IsHttps
=
false
};
}
else
if
(
tmp
.
StartsWith
(
"https="
))
{
{
var
endPoint
=
tmp
.
Substring
(
5
);
var
endPoint
=
tmp
.
Substring
(
5
);
return
new
HttpSystemProxyValue
()
return
new
HttpSystemProxyValue
()
{
{
HostName
=
endPoint
.
Split
(
':'
)[
0
],
HostName
=
endPoint
.
Split
(
':'
)[
0
],
Port
=
int
.
Parse
(
endPoint
.
Split
(
':'
)[
1
]),
Port
=
int
.
Parse
(
endPoint
.
Split
(
':'
)[
1
]),
IsHttps
=
t
rue
IsHttps
=
t
mp
.
StartsWith
(
"https="
)
};
};
}
}
return
null
;
return
null
;
}
}
...
@@ -252,7 +199,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -252,7 +199,7 @@ namespace Titanium.Web.Proxy.Helpers
/// Prepares the proxy server registry (create empty values if they don't exist)
/// Prepares the proxy server registry (create empty values if they don't exist)
/// </summary>
/// </summary>
/// <param name="reg"></param>
/// <param name="reg"></param>
private
void
p
repareRegistry
(
RegistryKey
reg
)
private
static
void
P
repareRegistry
(
RegistryKey
reg
)
{
{
if
(
reg
.
GetValue
(
"ProxyEnable"
)
==
null
)
if
(
reg
.
GetValue
(
"ProxyEnable"
)
==
null
)
{
{
...
...
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
4c68f687
...
@@ -4,7 +4,6 @@ using System.IO;
...
@@ -4,7 +4,6 @@ using System.IO;
using
System.Linq
;
using
System.Linq
;
using
System.Net.NetworkInformation
;
using
System.Net.NetworkInformation
;
using
System.Net.Security
;
using
System.Net.Security
;
using
System.Net.Sockets
;
using
System.Runtime.InteropServices
;
using
System.Runtime.InteropServices
;
using
System.Security.Authentication
;
using
System.Security.Authentication
;
using
System.Text
;
using
System.Text
;
...
@@ -169,31 +168,18 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -169,31 +168,18 @@ namespace Titanium.Web.Proxy.Helpers
try
try
{
{
TcpClient
tunnelClient
=
tcpConnection
.
TcpClient
;
Stream
tunnelStream
=
tcpConnection
.
Stream
;
Stream
tunnelStream
=
tcpConnection
.
Stream
;
Task
sendRelay
;
Task
sendRelay
;
//Now async relay all server=>client & client=>server data
//Now async relay all server=>client & client=>server data
if
(
sb
!=
null
)
sendRelay
=
clientStream
.
CopyToAsync
(
sb
?.
ToString
()
??
string
.
Empty
,
tunnelStream
);
{
sendRelay
=
clientStream
.
CopyToAsync
(
sb
.
ToString
(),
tunnelStream
);
}
else
{
sendRelay
=
clientStream
.
CopyToAsync
(
string
.
Empty
,
tunnelStream
);
}
var
receiveRelay
=
tunnelStream
.
CopyToAsync
(
string
.
Empty
,
clientStream
);
var
receiveRelay
=
tunnelStream
.
CopyToAsync
(
string
.
Empty
,
clientStream
);
await
Task
.
WhenAll
(
sendRelay
,
receiveRelay
);
await
Task
.
WhenAll
(
sendRelay
,
receiveRelay
);
}
}
catch
{
throw
;
}
finally
finally
{
{
tcpConnection
.
Dispose
();
tcpConnection
.
Dispose
();
...
...
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
4c68f687
...
@@ -50,22 +50,16 @@ namespace Titanium.Web.Proxy.Http
...
@@ -50,22 +50,16 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// <summary>
/// Is Https?
/// Is Https?
/// </summary>
/// </summary>
public
bool
IsHttps
public
bool
IsHttps
=>
this
.
Request
.
RequestUri
.
Scheme
==
Uri
.
UriSchemeHttps
;
{
get
{
return
this
.
Request
.
RequestUri
.
Scheme
==
Uri
.
UriSchemeHttps
;
}
}
/// <summary>
/// <summary>
/// Set the tcp connection to server used by this webclient
/// Set the tcp connection to server used by this webclient
/// </summary>
/// </summary>
/// <param name="
Connection"
></param>
/// <param name="
connection">Instance of <see cref="TcpConnection"/
></param>
internal
void
SetConnection
(
TcpConnection
C
onnection
)
internal
void
SetConnection
(
TcpConnection
c
onnection
)
{
{
C
onnection
.
LastAccess
=
DateTime
.
Now
;
c
onnection
.
LastAccess
=
DateTime
.
Now
;
ServerConnection
=
C
onnection
;
ServerConnection
=
c
onnection
;
}
}
internal
HttpWebClient
()
internal
HttpWebClient
()
...
@@ -87,28 +81,18 @@ namespace Titanium.Web.Proxy.Http
...
@@ -87,28 +81,18 @@ namespace Titanium.Web.Proxy.Http
//prepare the request & headers
//prepare the request & headers
if
((
ServerConnection
.
UpStreamHttpProxy
!=
null
&&
ServerConnection
.
IsHttps
==
false
)
||
(
ServerConnection
.
UpStreamHttpsProxy
!=
null
&&
ServerConnection
.
IsHttps
==
true
))
if
((
ServerConnection
.
UpStreamHttpProxy
!=
null
&&
ServerConnection
.
IsHttps
==
false
)
||
(
ServerConnection
.
UpStreamHttpsProxy
!=
null
&&
ServerConnection
.
IsHttps
==
true
))
{
{
requestLines
.
AppendLine
(
string
.
Join
(
" "
,
new
string
[
3
]
requestLines
.
AppendLine
(
string
.
Join
(
" "
,
this
.
Request
.
Method
,
this
.
Request
.
RequestUri
.
AbsoluteUri
,
$"HTTP/
{
this
.
Request
.
HttpVersion
.
Major
}
.
{
this
.
Request
.
HttpVersion
.
Minor
}
"
));
{
this
.
Request
.
Method
,
this
.
Request
.
RequestUri
.
AbsoluteUri
,
string
.
Format
(
"HTTP/{0}.{1}"
,
this
.
Request
.
HttpVersion
.
Major
,
this
.
Request
.
HttpVersion
.
Minor
)
}));
}
}
else
else
{
{
requestLines
.
AppendLine
(
string
.
Join
(
" "
,
new
string
[
3
]
requestLines
.
AppendLine
(
string
.
Join
(
" "
,
this
.
Request
.
Method
,
this
.
Request
.
RequestUri
.
PathAndQuery
,
$"HTTP/
{
this
.
Request
.
HttpVersion
.
Major
}
.
{
this
.
Request
.
HttpVersion
.
Minor
}
"
));
{
this
.
Request
.
Method
,
this
.
Request
.
RequestUri
.
PathAndQuery
,
string
.
Format
(
"HTTP/{0}.{1}"
,
this
.
Request
.
HttpVersion
.
Major
,
this
.
Request
.
HttpVersion
.
Minor
)
}));
}
}
//Send Authentication to Upstream proxy if needed
//Send Authentication to Upstream proxy if needed
if
(
ServerConnection
.
UpStreamHttpProxy
!=
null
&&
ServerConnection
.
IsHttps
==
false
&&
ServerConnection
.
UpStreamHttpProxy
.
UserName
!=
null
&&
ServerConnection
.
UpStreamHttpProxy
.
UserName
!=
""
&&
ServerConnection
.
UpStreamHttpProxy
.
Password
!=
null
)
if
(
ServerConnection
.
UpStreamHttpProxy
!=
null
&&
ServerConnection
.
IsHttps
==
false
&&
!
string
.
IsNullOrEmpty
(
ServerConnection
.
UpStreamHttpProxy
.
UserName
)
&&
ServerConnection
.
UpStreamHttpProxy
.
Password
!=
null
)
{
{
requestLines
.
AppendLine
(
"Proxy-Connection: keep-alive"
);
requestLines
.
AppendLine
(
"Proxy-Connection: keep-alive"
);
requestLines
.
AppendLine
(
"Proxy-Authorization"
+
": Basic "
+
Convert
.
ToBase64String
(
System
.
Text
.
Encoding
.
UTF8
.
GetBytes
(
ServerConnection
.
UpStreamHttpProxy
.
UserName
+
":"
+
ServerConnection
.
UpStreamHttpProxy
.
Password
)));
requestLines
.
AppendLine
(
"Proxy-Authorization"
+
": Basic "
+
Convert
.
ToBase64String
(
Encoding
.
UTF8
.
GetBytes
(
ServerConnection
.
UpStreamHttpProxy
.
UserName
+
":"
+
ServerConnection
.
UpStreamHttpProxy
.
Password
)));
}
}
//write request headers
//write request headers
foreach
(
var
headerItem
in
this
.
Request
.
RequestHeaders
)
foreach
(
var
headerItem
in
this
.
Request
.
RequestHeaders
)
...
@@ -138,8 +122,6 @@ namespace Titanium.Web.Proxy.Http
...
@@ -138,8 +122,6 @@ namespace Titanium.Web.Proxy.Http
string
request
=
requestLines
.
ToString
();
string
request
=
requestLines
.
ToString
();
byte
[]
requestBytes
=
Encoding
.
ASCII
.
GetBytes
(
request
);
byte
[]
requestBytes
=
Encoding
.
ASCII
.
GetBytes
(
request
);
var
test
=
Encoding
.
UTF8
.
GetString
(
requestBytes
);
await
stream
.
WriteAsync
(
requestBytes
,
0
,
requestBytes
.
Length
);
await
stream
.
WriteAsync
(
requestBytes
,
0
,
requestBytes
.
Length
);
await
stream
.
FlushAsync
();
await
stream
.
FlushAsync
();
...
@@ -240,10 +222,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -240,10 +222,7 @@ namespace Titanium.Web.Proxy.Http
{
{
var
existing
=
Response
.
ResponseHeaders
[
newHeader
.
Name
];
var
existing
=
Response
.
ResponseHeaders
[
newHeader
.
Name
];
var
nonUniqueHeaders
=
new
List
<
HttpHeader
>();
var
nonUniqueHeaders
=
new
List
<
HttpHeader
>
{
existing
,
newHeader
};
nonUniqueHeaders
.
Add
(
existing
);
nonUniqueHeaders
.
Add
(
newHeader
);
Response
.
NonUniqueResponseHeaders
.
Add
(
newHeader
.
Name
,
nonUniqueHeaders
);
Response
.
NonUniqueResponseHeaders
.
Add
(
newHeader
.
Name
,
nonUniqueHeaders
);
Response
.
ResponseHeaders
.
Remove
(
newHeader
.
Name
);
Response
.
ResponseHeaders
.
Remove
(
newHeader
.
Name
);
...
@@ -256,5 +235,4 @@ namespace Titanium.Web.Proxy.Http
...
@@ -256,5 +235,4 @@ namespace Titanium.Web.Proxy.Http
}
}
}
}
}
}
}
}
\ No newline at end of file
Titanium.Web.Proxy/Http/Request.cs
View file @
4c68f687
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Text
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
...
@@ -234,12 +233,13 @@ namespace Titanium.Web.Proxy.Http
...
@@ -234,12 +233,13 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// <summary>
/// Request Url
/// Request Url
/// </summary>
/// </summary>
public
string
Url
{
get
{
return
RequestUri
.
OriginalString
;
}
}
public
string
Url
=>
RequestUri
.
OriginalString
;
/// <summary>
/// <summary>
/// Encoding for this request
/// Encoding for this request
/// </summary>
/// </summary>
internal
Encoding
Encoding
{
get
{
return
this
.
GetEncoding
();
}
}
internal
Encoding
Encoding
=>
this
.
GetEncoding
();
/// <summary>
/// <summary>
/// Terminates the underlying Tcp Connection to client after current request
/// Terminates the underlying Tcp Connection to client after current request
/// </summary>
/// </summary>
...
...
Titanium.Web.Proxy/Http/Response.cs
View file @
4c68f687
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.IO
;
using
System.Linq
;
using
System.Text
;
using
System.Text
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
...
@@ -16,7 +15,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -16,7 +15,7 @@ namespace Titanium.Web.Proxy.Http
public
string
ResponseStatusCode
{
get
;
set
;
}
public
string
ResponseStatusCode
{
get
;
set
;
}
public
string
ResponseStatusDescription
{
get
;
set
;
}
public
string
ResponseStatusDescription
{
get
;
set
;
}
internal
Encoding
Encoding
{
get
{
return
this
.
GetResponseCharacterEncoding
();
}
}
internal
Encoding
Encoding
=>
this
.
GetResponseCharacterEncoding
();
/// <summary>
/// <summary>
/// Content encoding for this response
/// Content encoding for this response
...
...
Titanium.Web.Proxy/Http/Responses/OkResponse.cs
View file @
4c68f687
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
/// <summary>
/// <summary>
/// 200 Ok response
/// 200 Ok response
/// </summary>
/// </summary>
public
class
OkResponse
:
Response
public
sealed
class
OkResponse
:
Response
{
{
public
OkResponse
()
public
OkResponse
()
{
{
...
...
Titanium.Web.Proxy/Http/Responses/RedirectResponse.cs
View file @
4c68f687
using
Titanium.Web.Proxy.Network
;
namespace
Titanium.Web.Proxy.Http.Responses
namespace
Titanium.Web.Proxy.Http.Responses
{
{
/// <summary>
/// <summary>
/// Redirect response
/// Redirect response
/// </summary>
/// </summary>
public
class
RedirectResponse
:
Response
public
sealed
class
RedirectResponse
:
Response
{
{
public
RedirectResponse
()
public
RedirectResponse
()
{
{
...
...
Titanium.Web.Proxy/Network/CertificateManager.cs
View file @
4c68f687
...
@@ -2,11 +2,9 @@
...
@@ -2,11 +2,9 @@
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Security.Cryptography.X509Certificates
;
using
System.Security.Cryptography.X509Certificates
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
System.Threading
;
using
System.Linq
;
using
System.Linq
;
using
System.Collections.Concurrent
;
using
System.Collections.Concurrent
;
using
System.IO
;
using
System.IO
;
using
System.Diagnostics
;
namespace
Titanium.Web.Proxy.Network
namespace
Titanium.Web.Proxy.Network
{
{
...
...
Titanium.Web.Proxy/Network/TcpConnectionFactory.cs
View file @
4c68f687
...
@@ -56,11 +56,11 @@ namespace Titanium.Web.Proxy.Network
...
@@ -56,11 +56,11 @@ namespace Titanium.Web.Proxy.Network
using
(
var
writer
=
new
StreamWriter
(
stream
,
Encoding
.
ASCII
,
bufferSize
,
true
))
using
(
var
writer
=
new
StreamWriter
(
stream
,
Encoding
.
ASCII
,
bufferSize
,
true
))
{
{
await
writer
.
WriteLineAsync
(
string
.
Format
(
"CONNECT {0}:{1} HTTP/{2}"
,
remoteHostName
,
remotePort
,
httpVersion
)
);
await
writer
.
WriteLineAsync
(
$"CONNECT
{
remoteHostName
}
:
{
remotePort
}
HTTP/
{
httpVersion
}
"
);
await
writer
.
WriteLineAsync
(
string
.
Format
(
"Host: {0}:{1}"
,
remoteHostName
,
remotePort
)
);
await
writer
.
WriteLineAsync
(
$"Host:
{
remoteHostName
}
:
{
remotePort
}
"
);
await
writer
.
WriteLineAsync
(
"Connection: Keep-Alive"
);
await
writer
.
WriteLineAsync
(
"Connection: Keep-Alive"
);
if
(
externalHttpsProxy
.
UserName
!=
null
&&
externalHttpsProxy
.
UserName
!=
""
&&
externalHttpsProxy
.
Password
!=
null
)
if
(
!
string
.
IsNullOrEmpty
(
externalHttpsProxy
.
UserName
)
&&
externalHttpsProxy
.
Password
!=
null
)
{
{
await
writer
.
WriteLineAsync
(
"Proxy-Connection: keep-alive"
);
await
writer
.
WriteLineAsync
(
"Proxy-Connection: keep-alive"
);
await
writer
.
WriteLineAsync
(
"Proxy-Authorization"
+
": Basic "
+
Convert
.
ToBase64String
(
System
.
Text
.
Encoding
.
UTF8
.
GetBytes
(
externalHttpsProxy
.
UserName
+
":"
+
externalHttpsProxy
.
Password
)));
await
writer
.
WriteLineAsync
(
"Proxy-Authorization"
+
": Basic "
+
Convert
.
ToBase64String
(
System
.
Text
.
Encoding
.
UTF8
.
GetBytes
(
externalHttpsProxy
.
UserName
+
":"
+
externalHttpsProxy
.
Password
)));
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
4c68f687
...
@@ -17,22 +17,13 @@ namespace Titanium.Web.Proxy
...
@@ -17,22 +17,13 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
public
partial
class
ProxyServer
:
IDisposable
public
partial
class
ProxyServer
:
IDisposable
{
{
private
static
Action
<
Exception
>
_exceptionFunc
=
null
;
private
static
readonly
Lazy
<
Action
<
Exception
>>
_defaultExceptionFunc
=
new
Lazy
<
Action
<
Exception
>>(()
=>
(
e
=>
{
}));
private
static
Action
<
Exception
>
_exceptionFunc
;
public
static
Action
<
Exception
>
ExceptionFunc
public
static
Action
<
Exception
>
ExceptionFunc
{
{
get
get
{
{
if
(
_exceptionFunc
!=
null
)
return
_exceptionFunc
??
_defaultExceptionFunc
.
Value
;
{
return
_exceptionFunc
;
}
else
{
return
(
e
)=>
{
};
}
}
}
set
set
{
{
...
...
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