示例#1
0
 def __init__(self, dest):
     try:
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     except:
         raise Exception("Cannot initiate socket correctly")
     # self.socket.connect(dest)
     self.history = set()
     self.cookieJar = CookieJar()
     self.dest = dest
示例#2
0
    def getCookieJar(self, pluginName, account=None):
        if (pluginName, account) in self.cookiejars:
            return self.cookiejars[(pluginName, account)]

        cj = CookieJar(pluginName, account)
        self.cookiejars[(pluginName, account)] = cj
        return cj
示例#3
0
    def getRequest(self, pluginName, cj=None):
        req = Browser(self.bucket, self.getOptions())

        if cj:
            req.setCookieJar(cj)
        else:
            req.setCookieJar(CookieJar(pluginName))

        return req
示例#4
0
    def getRequest(self, pluginName, cj=None):
        # TODO: mostly obsolete, browser could be removed completely
        req = Browser(self.bucket, self.getOptions())

        if cj:
            req.setCookieJar(cj)
        else:
            req.setCookieJar(CookieJar(pluginName))

        return req
示例#5
0
    def __init__(self, mySocket):
        """initialize the variables"""
        self.version = ""
        self.status_code = None
        self.status = ""
        self.cookieJar = CookieJar()
        self.headers = {}
        self.body = ""

        # Start reading received message and update variables
        socketFile = SocketReader(mySocket)
        self.getStatus(socketFile)
        self.readHeader(socketFile)
        if "transfer-encoding" in self.headers and self.getHeader(
                "transfer-encoding") == "chunked":
            self.readChunkedBody(socketFile)
        else:
            bodyLength = int(self.getHeader("content-length"))
            self.readBody(socketFile, bodyLength)
        try:
            socketFile.close()
        except:
            raise Exception("Cannot close file stream from socket correctly")
示例#6
0
    def getRequest(self, pluginName, account=None, type="HTTP"):
        self.lock.acquire()

        if type == "XDCC":
            return XDCCRequest(proxies=self.getProxies())

        req = Browser(self.bucket, self.getOptions())

        if account:
            cj = self.getCookieJar(pluginName, account)
            req.setCookieJar(cj)
        else:
            req.setCookieJar(CookieJar(pluginName))

        self.lock.release()
        return req
示例#7
0
    def getRequest(self, pluginName, account=None, type="HTTP", **kwargs):
        self.lock.acquire()

        options = self.getOptions()
        options.update(kwargs)  # submit kwargs as additional options

        if type == "XDCC":
            req = XDCCRequest(self.bucket, options)

        else:
            req = Browser(self.bucket, options)

            if account:
                cj = self.getCookieJar(pluginName, account)
                req.setCookieJar(cj)
            else:
                req.setCookieJar(CookieJar(pluginName))

        self.lock.release()
        return req
示例#8
0
 def openCookieJar(self, pluginname):
     """Create new CookieJar"""
     return CookieJar(pluginname)
示例#9
0
 def getHTTPRequest(self, **kwargs):
     """ returns a http request, don't forget to close it ! """
     options = self.getOptions()
     options.update(kwargs)  # submit kwargs as additional options
     return HTTPRequest(CookieJar(None), options)
示例#10
0
def main():
    """ Catch the url (if any); then choose adequate defaults and build
    a browser window. Save cookies, if appropiate, at close.

    """

    # Considerations:
    # Will use proxy? (if not google, fb, twitter... then yes)
    use_proxy = True
    # Which whitelist will use instead?
    host_whitelist = None
    # Will allow cookies? Which? Where are they saved?
    cookie_allow = ["github.com", "linkedin.com", "freerepublic.com"]
    cookie_file = None
    prefix = ""

    if len(argv) == 2:
        sitio = argv[1]
        if (sitio.split('.')[-2:] == ["facebook", "com"]):
            use_proxy = False
            host_whitelist = ["facebook.com", "akamaihd.net", "fbcdn.net"]
            cookie_allow = ["facebook.com"]
            cookie_file = "fbcookies.cj"
            prefix = "FB"
        elif (sitio.split('.')[-2:] == ["twitter", "com"]):
            use_proxy = False
            host_whitelist = ["twitter.com", "twimg.com"]
            cookie_allow = ["twitter.com"]
            cookie_file = "twcookies.cj"
            prefix = "TW"
        elif (sitio.split('.')[-2:] == ["google", "com"]):
            print "GOOGLE"
            use_proxy = False
            host_whitelist = [
                "google.com", "google.com.mx", "googleusercontent.com",
                "gstatic.com", "googleapis.com"
            ]
            cookie_allow = ["google.com"]
            cookie_file = "gcookies.cj"
            prefix = "G"
        else:
            print "GENERAL"
    else:
        sitio = None
        print "EMPTY"

    # Proxy
    if use_proxy:
        proxy = QNetworkProxy()
        proxy.setType(QNetworkProxy.HttpProxy)
        proxy.setHostName('localhost')
        proxy.setPort(3128)
        QNetworkProxy.setApplicationProxy(proxy)

    app = QtGui.QApplication([])

    clipboard = app.clipboard()
    db_log = DatabaseLog()
    netmanager = InterceptNAM(parent=app,
                              name=prefix,
                              log=db_log,
                              whitelist=host_whitelist)
    cookiejar = CookieJar(parent=app,
                          allowed=cookie_allow,
                          storage=cookie_file)
    netmanager.setCookieJar(cookiejar)

    app.setApplicationName("Eilat")
    app.setApplicationVersion("1.2.002")
    mainwin = MainWin(netmanager, clipboard)

    if sitio:
        mainwin.add_tab(sitio)
    else:
        mainwin.add_tab()

    mainwin.show()

    def end_call():
        """ The browser is closing - save cookies, if required.

        """
        print "END"
        if cookie_file:
            print "SAVING COOKIES"
            with open(cookie_file, "w") as savefile:
                for cookie in cookiejar.allCookies():
                    savefile.write(cookie.toRawForm() + "\n")

    app.lastWindowClosed.connect(end_call)
    app.exec_()
示例#11
0
class MyCurl:
    """Curl to connect server and client"""
    def __init__(self, dest):
        try:
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except:
            raise Exception("Cannot initiate socket correctly")
        # self.socket.connect(dest)
        self.history = set()
        self.cookieJar = CookieJar()
        self.dest = dest

    def request(self, method, URL, headers=None, body=""):
        """sending request to server"""
        message = ClientMessage(method, URL, headers, body)
        message.headers['Cookie'] = str(self.cookieJar)
        self.history.add(URL)
        try:
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except:
            raise Exception("Cannot initiate socket correctly")
        try:
            self.socket.connect(self.dest)
            self.socket.sendall(str(message).encode())
        except:
            raise Exception("connection failed")
        try:
            response = ServerMessage(self.socket)
        except:
            raise Exception("empty socket")
        self.add_new_cookies(response)
        try:
            self.socket.close()
        except:
            raise Exception("Socket cannot close correctly")
        return response

    def get(self, URL, headers={}):
        """sending get request"""
        return self.request("GET", URL, headers)

    def post(self, URL, headers={}, body=""):
        """sending post request"""
        return self.request("POST", URL, headers, body)

    def add_new_cookies(self, message):
        """add new coockies to the cookie jar"""
        jar = message.cookieJar.getAll()
        for key in jar:
            self.cookieJar.add_cookie(key, jar[key])

    def is_visited_or_Not(self, link):
        """check if the link has been visited"""
        return link in self.history

    def get_cookie(self, str):
        """get the cookie"""
        return self.cookieJar.get_cookie(str)


# Used to test Curl and lower level HTTP Protocol
# if __name__=="__main__":
#     #test1 works so far
#     '''
#     Destination1=("cs5700sp17.ccs.neu.edu",80)
#     test1=MyCurl(Destination1)
#     test1.get("http://cs5700sp17.ccs.neu.edu/")
#     '''
#     #test2
#     Destination2=("cs5700sp17.ccs.neu.edu",80)
#     test2=MyCurl(Destination2)
#     test2.get("/accounts/login/?next=/fakebook/")
#
#
#     csrf_token=test2.cookieJar.get_cookie('csrftoken')
#     form="username=001156814&password=DVO8KW2F&csrfmiddlewaretoken="+csrf_token
#     headers= {
#             "content-type": "application/x-www-form-urlencoded"
#     }
#     loginResponse=test2.post("/accounts/login/?next=/fakebook/",headers,str(form))
#     print(loginResponse.headers)
#
#     print(test2.get("http://cs5700sp17.ccs.neu.edu/fakebook/").headers)
#
#     print(test2.get("/fakebook/294230082/").status_code)
示例#12
0
class ServerMessage:
    """this is a model that parsing received message from HTTP server"""
    def __init__(self, mySocket):
        """initialize the variables"""
        self.version = ""
        self.status_code = None
        self.status = ""
        self.cookieJar = CookieJar()
        self.headers = {}
        self.body = ""

        # Start reading received message and update variables
        socketFile = SocketReader(mySocket)
        self.getStatus(socketFile)
        self.readHeader(socketFile)
        if "transfer-encoding" in self.headers and self.getHeader(
                "transfer-encoding") == "chunked":
            self.readChunkedBody(socketFile)
        else:
            bodyLength = int(self.getHeader("content-length"))
            self.readBody(socketFile, bodyLength)
        try:
            socketFile.close()
        except:
            raise Exception("Cannot close file stream from socket correctly")

    def getStatus(self, file):
        """read the first line to get status info"""
        try:
            statusLine = file.readline().decode("utf-8").strip()
        except:
            raise Exception("Error in reading line")
        # check if the file is empty, raise exception
        if statusLine == "":
            raise Exception("socket is empty")

        try:
            version, status_code, status = statusLine.split(None, 2)
            self.version = str(version)
            self.status_code = str(status_code)
            self.status = str(status)
        except:
            raise Exception("Status line of response message is problematic")

    def readHeader(self, file):
        """start reading the 2nd line of header"""

        key = ""
        while 1:
            try:
                line = file.readline().decode("utf-8")
            except:
                raise Exception("Error in reading line")

            if line is None:
                raise Exception("Error read line")
            elif ":" not in line:
                # valid header line is supposed to always have ":" symbol
                break

            #remove leading space
            sLine = str(line.strip())

            if line[0] is " ":
                if str(key.lower()) == "set-cookie":
                    self.cookieJar.add_cookie_from_string(str(sLine))
                self.headers[str(key.lower())] = str(sLine)
                continue
            try:
                key, value = sLine.split(":", 1)
            except:
                raise Exception("invalid format of header")

            if str(key.lower()) == "set-cookie":
                self.cookieJar.add_cookie_from_string(str(value.strip()))
            self.headers[str(key.lower())] = str(value.strip())

    def readBody(self, file, fileLength):
        """read the body part of the message"""
        self.body = b""
        read = 0
        progress_bar(read, fileLength)
        while 1:
            try:
                data = file.read(fileLength)
            except:
                raise Exception("Error reading a line in the body")
            # check if the socket is empty
            if data is None:
                raise Exception("socket is empty")
            self.body += data
            fileLength -= len(data)
            progress_bar(read, fileLength)
            sys.stdout.write("\n")
            if num_read >= size:
                break

    def readChunkedBody(self, file):
        """read the special chunked body of the message"""
        print("Reading Chunked message, No file length")
        self.body = b""
        while 1:
            try:
                hexsize = file.readline().decode("utf-8")
            except:
                raise Exception("Error reading a line in chunked body")
            # check if the socket is empty
            if hexsize is None:
                raise Exception("empty socket")
            size = int(hexsize, 16)
            if size == 0:
                break
            data = ""
            while 1:
                try:
                    line = file.read(size).decode("utf-8")
                except:
                    raise Exception("Error reading a line in chunked body")
                data += line
                size -= len(line)
                if size <= 0:
                    break
            self.body += data
            file.read(2)
        self.readHeader(file)

    def getHeader(self, key):
        """get the header by given key"""
        if key not in self.headers:
            raise Exception("No such key in the header")
        else:
            return self.headers[key]