Ejemplo n.º 1
0
 public HttpRequest(IHttpTransaction transaction, Socket stream)
 {
     Transaction = transaction;
     Socket = stream;
     RemoteAddress = stream.Address;
     RemotePort = stream.Port;
 }
Ejemplo n.º 2
0
		private void OnData(Socket sourceSocket, Stream sourceStream, ByteBuffer buffer, Func<Socket, Socks4Packet, bool> checkUser)
		{
			var sourcePort = sourceSocket.Port;
			
			if (info[sourcePort].FirstMessage) {
				sourceStream.PauseReading();

				var data = Socks4Packet.GetPacketInfo(buffer.Bytes, buffer.Position, buffer.Length);
				
				if (!VersionCheck || !data.ValidVersion) {
					// invalid packet header
					sourceStream.Write(Socks4Packet.CreatePacket(Socks4Packet.CMD_REPLY_REQUEST_REJECTED_OR_FAILED), delegate {
						sourceSocket.Close();
					});
					return;
				}
				switch (data.Command) {
				case Socks4Packet.CMD_CONNECT:
					
					if (!checkUser(sourceSocket, data)) {
						
						sourceStream.Write(Socks4Packet.CreatePacket(Socks4Packet.CMD_REPLY_REQUEST_REJECTED_OR_FAILED), delegate {
							sourceSocket.Close();
						});
						
						return;
					}
					
					var destinationSocket = Context.CreateSocket();
					info[sourcePort].DestinationSocket = destinationSocket;
					destinationSocket.Connect(data.IPAddress.ToString(), data.Port, delegate {
						sourceStream.ResumeReading();
						var destinationStream = destinationSocket.GetSocketStream();
						
						destinationStream.Read(delegate (ByteBuffer destinationReadBuffer) {
							sourceStream.Write(destinationReadBuffer);
						}, delegate (Exception exception) { }, delegate {
							sourceSocket.Close();
						});
					});
					
					sourceStream.Write(Socks4Packet.CreatePacket(Socks4Packet.CMD_REPLY_REQUEST_GRANTED));
					
					info[sourcePort].FirstMessage = false;
					break;
				default:
					sourceStream.Close();
					info.Remove(sourcePort);
					break;
				}
				
			} else {
				if (info.ContainsKey(sourcePort)) {
					var sockInfo = info[sourcePort];
					sockInfo.DestinationSocket.GetSocketStream().Write(buffer);
				}
			}
		}
Ejemplo n.º 3
0
        public HttpResponse(IHttpRequest request, Socket socket)
        {
            Request = request;
            Socket = socket;

            StatusCode = 200;

            WriteHeaders = true;

            Stream = new HttpStream (this, socket.GetSocketStream ());
            Stream.Chunked = (request.MajorVersion > 0 && request.MinorVersion > 0);
        }
Ejemplo n.º 4
0
 private void WriteResponse()
 {
     var httpr = this.context.Request as HttpRequest;
     this.context.Response.Stream.Chunked = false;
     this.context.Response.StatusCode = 101;
     this.context.Response.Headers.SetHeader("Upgrade", "WebSocket");
     this.context.Response.Headers.SetHeader("Connection", "Upgrade");
     this.context.Response.Headers.SetHeader("Sec-WebSocket-Origin", this.context.Request.Headers["Origin"]);
     bool secure = this.context.Request.Headers["Origin"].Contains("https");
     string toreplace = secure ? "https" : "http";
     string replacewith = secure ? "wss" : "ws";
     this.context.Response.Headers.SetHeader("Sec-WebSocket-Location", this.context.Request.Headers["Origin"].Replace(toreplace, replacewith) + httpr.Path);
     string key1 = this.context.Request.Headers["Sec-WebSocket-Key1"];
     string key2 = this.context.Request.Headers["Sec-WebSocket-Key2"];
     byte[] end = this.context.Request.GetProperty<byte[]>("UPGRADE_HEAD");
     byte[] ret = ConnectionHandshake.GenerateHandshake(key1, key2, end);
     this.context.Response.Write(ret);
     this.context.Response.OnEnd += () => {
         connected = true;
         this.sock = (this.context.Response as HttpResponse).Socket;
         if (OnConnect != null) {
             OnConnect();
         }
     };
     this.context.Response.End();
 }
Ejemplo n.º 5
0
 public void Connect(Action connected)
 {
     this.sock = AppHost.Context.CreateSocket();
     sock.Connect(ConnectionPath.Host, (ConnectionPath.Port == -1) ? ConnectionPath.Scheme == "ws" ? 80 : 443 : ConnectionPath.Port, () => {
         var hand = ConnectionHandshake.Generate();
         this.WriteHeaders(hand);
         sock.GetSocketStream().Read((data) => {
             if (this.connected) {
                 if (data.Bytes[0] != 0x00 || data.Bytes[data.Length - 1] != 0xFF) return;
                 if (OnData != null)
                 {
                     OnData(new ByteBuffer(data.Bytes, data.Position + 1, data.Length - 2));
                 }
             } else {
                 if (this.EnsureResponse(data, hand))
                 {
                     this.connected = true;
                     connected();
                 }
                 else
                 {
                     this.sock.Close();
                 }
             }
         }, (e) => {}, () => {});
     });
 }
Ejemplo n.º 6
0
		public Socks4Server(Context context, Socket socket)
		{
			Context = context;
			Socket = socket;
			VersionCheck = true;
		}