forked from vespassassina-zz/SimpleProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
173 lines (137 loc) · 5.68 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
namespace SimpleProxy{
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Configuration;
using System.Collections.Generic;
class Program{
static string port;
static string host;
static void Main(){
port = ConfigurationManager.AppSettings["proxy_port"];
host = ConfigurationManager.AppSettings["proxy_host"];
//http server listener
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://" + host + ":" + port + "/");
listener.Start();
Console.WriteLine("Listening on " + host + ":" + port);
//main server loop
while (true){
//as soon as there is a connection request
HttpListenerContext ctx = listener.GetContext();
new Thread(new Worker(ctx).ProcessRequest).Start();
}
}
}
class Worker{
HttpListenerContext context;
WebProxy parent;
//service port
string port;
string host;
//pass through headers
string[] headers = new string[] { "Cookie", "Accept", "Referrer", "Accept-Language" };
public Worker(HttpListenerContext context){
this.context = context;
port = ConfigurationManager.AppSettings["proxy_port"];
host = ConfigurationManager.AppSettings["proxy_host"];
//init proxy
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["parent_host"]) && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["parent_port"]))
{
parent = new WebProxy(ConfigurationManager.AppSettings["parent_host"], int.Parse(ConfigurationManager.AppSettings["parent_port"]));
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["parent_user"]))
{
parent.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["parent_user"], ConfigurationManager.AppSettings["parent_pass"], ConfigurationManager.AppSettings["parent_domain"]);
}
else
{
parent.UseDefaultCredentials = true;
}
}
else
{
parent = null;
}
}
private byte[] GetBytesFromStream(Stream stream)
{
byte[] result;
byte[] buffer = new byte[256];
BinaryReader reader = new BinaryReader(stream);
MemoryStream memoryStream = new MemoryStream();
int count = 0;
while (true)
{
count = reader.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
if (count == 0)
break;
}
result = memoryStream.ToArray();
memoryStream.Close();
reader.Close();
stream.Close();
return result;
}
public void ProcessRequest()
{
//request console log
string url = context.Request.Url.ToString().Replace(":" + port, "");
string msg = DateTime.Now.ToString("hh:mm:ss") + " " + context.Request.HttpMethod + " " + context.Request.Url.Host.ToString();
Console.WriteLine(msg);
byte[] result;
try
{
WebRequest request = WebRequest.Create(url);
//config proxy
if (parent!=null)
request.Proxy = parent;
//addo le chiavi nella testa
request.Method = context.Request.HttpMethod;
request.ContentType = context.Request.ContentType;
request.ContentLength = context.Request.ContentLength64;
if (context.Request.ContentLength64 > 0 && context.Request.HasEntityBody)
{
using (System.IO.Stream body = context.Request.InputStream)
{
byte[] requestdata = GetBytesFromStream(body);
request.ContentLength = requestdata.Length;
Stream s = request.GetRequestStream();
s.Write(requestdata, 0, requestdata.Length);
s.Close();
}
}
//request processing
WebResponse response = request.GetResponse();
result = GetBytesFromStream(response.GetResponseStream());
context.Response.ContentType = response.ContentType;
response.Close();
}
catch (WebException wex)
{
//exception handler (404,407...)
result = Encoding.UTF8.GetBytes(wex.Message);
HttpWebResponse resp = (HttpWebResponse)wex.Response;
context.Response.StatusCode = (int)resp.StatusCode;
context.Response.StatusDescription = resp.StatusDescription;
Console.WriteLine("ERROR:" + wex.Message);
}
catch (Exception ex)
{
result = Encoding.UTF8.GetBytes(ex.Message);
Console.WriteLine("ERROR:" + ex.Message);
}
//response
byte[] b = result;
context.Response.ContentLength64 = b.Length;
context.Response.OutputStream.Write(b, 0, b.Length);
context.Response.OutputStream.Close();
}
}
}