def forward_hosts_tcp(self, addrs, req): self.remoteconn = create_connection_hosts(addrs, req.dstport, self.timeout) self.track_sock(self.remoteconn) addrtype, bndaddr, bndport = sock_addr_info(self.remoteconn) request_success(self.socksconn, addrtype, bndaddr, bndport) data = self.socksconn.recv(65536) if data[:3] == 'GET': request, rest = data.split('\r\n', 1) method, path, version = request.split() headers = mimetools.Message(StringIO(rest)) host = headers.getheader("host", "") if self.matcher.need_redirect(method, host): response = [ "%s 301 Moved Permanently" % version, "Location: https://%s" % "".join([host, path]), "Connection: close", "", "" ] self.socksconn.sendall("\r\n".join(response)) else: self.remoteconn.sendall(data) pipe_tcp(self.socksconn, self.remoteconn, self.timeout, self.timeout) else: self.remoteconn.sendall(data) pipe_tcp(self.socksconn, self.remoteconn, self.timeout, self.timeout)
def forward_hosts_tcp(self, addrs, req): self.remoteconn = create_connection_hosts(addrs, req.dstport, self.timeout) self.track_sock(self.remoteconn) addrtype, bndaddr, bndport = sock_addr_info(self.remoteconn) request_success(self.socksconn, addrtype, bndaddr, bndport) data = self.socksconn.recv(65536) if data[:3] == "GET": request, rest = data.split("\r\n", 1) method, path, version = request.split() headers = mimetools.Message(StringIO(rest)) host = headers.getheader("host", "") if self.matcher.need_redirect(method, host): response = [ "%s 301 Moved Permanently" % version, "Location: https://%s" % "".join([host, path]), "Connection: close", "", "", ] self.socksconn.sendall("\r\n".join(response)) else: self.remoteconn.sendall(data) pipe_tcp(self.socksconn, self.remoteconn, self.timeout, self.timeout) else: self.remoteconn.sendall(data) pipe_tcp(self.socksconn, self.remoteconn, self.timeout, self.timeout)
def cmd_udp_associate(self, req): self.client_associate = (req.dstaddr, req.dstport) self.last_clientaddr = self.client_associate for (resp, err) in self.meek_roundtrip([req.pack()]): if err: return if resp: Reply(resp) self.udpsock = bind_local_udp(self.socksconn) if not self.udpsock: request_fail(self.socksconn, req, GENERAL_SOCKS_SERVER_FAILURE) return self.track_sock(self.udpsock) read_thread = gevent.spawn(self.meek_read_from_client_thread) write_thread = gevent.spawn(self.meek_write_to_client_thread) relay_thread = gevent.spawn(self.meek_relay_thread) request_success(self.socksconn, *sock_addr_info(self.udpsock)) [t.join() for t in (read_thread, write_thread, relay_thread)] log.info("Session %s Ended" % self.sessionid)