Exemplo n.º 1
0
def aaa():
    # import socket
    import sockschain as socks
    # socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, "2.93.134.125", 1080)
    s = socks.socksocket()
    # socket.socket = socks.socksocket
    # s.setproxy(socks.PROXY_TYPE_SOCKS5, "178.49.237.130", 1080)
    # --> s.setproxy(socks.PROXY_TYPE_SOCKS5, "173.192.21.89", 1080)
    # --> s.setproxy(socks.PROXY_TYPE_SOCKS5, "202.69.71.210", 1080)
    s.setproxy(socks.PROXY_TYPE_SOCKS4, "114.35.74.8", 1080)

    mailserver = ("alt1.gmail-smtp-in.l.google.com", 25)

    # client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # client_socket = socket.socket
    # client_socket.connect(mailserver)
    s.connect(mailserver)

    recv = s.recv(1024)
    print("Message after connection request:" + recv.decode())

    if recv[:3] != '220':
        print('220 reply not received from server.')

    heloCommand = 'HELO proxy.keksik.com.ua\r\n'
    s.send(heloCommand.encode())
    recv1 = s.recv(1024)
    print("Message after HeLO command:" + recv1.decode())

    if recv1[:3] != '250':
        print('250 reply not received from server.')

    mailFrom = "MAIL FROM:<[email protected]>\r\n"
    s.send(mailFrom.encode())
    recv2 = s.recv(1024)
    print("After MAIL FROM command: " + recv2.decode())

    rcptTo = "RCPT TO:<[email protected]>\r\n"

    s.send(rcptTo.encode())
    recv3 = s.recv(1024)
    print("After RCPT TO command: " + recv3.decode())

    data = "DATA\r\n"
    s.send(data.encode())
    recv4 = s.recv(1024)
    print("After DATA command: " + recv4.decode())

    msg = 'test - TEST - test\r\n.\r\n'
    s.send(msg.encode())

    recv_msg = s.recv(1024)
    print("Response after sending message body:" + recv_msg.decode())

    quit = "QUIT\r\n"
    s.send(quit.encode())
    recv5 = s.recv(1024)
    print(recv5.decode())
    s.close()
Exemplo n.º 2
0
 def setup_conn(self):
     # Clear the default chain
     socks.setdefaultproxy()
     # add all proxies except last one.
     # we will connect on 'start' to the last proxy
     for hop in proxychain[:-1]:
         socks.adddefaultproxy(*hop)
     self.forward = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
Exemplo n.º 3
0
 def _get_socket(self, host, port, timeout):
     if self.debuglevel > 0:
         print>>stderr, 'connect:', (host, port)
     # -------------------------------------------
     socket_proxy = socks.socksocket()
     socket_proxy.setproxy(self.proxy[0], self.proxy[1], self.proxy[2])
     socket_proxy.connect((host, port))
     if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
         socket_proxy.settimeout(timeout)
     return socket_proxy
Exemplo n.º 4
0
 def run(self):
   sock = sockschain.socksocket()
   if self.proto in ('ircs', 'ssl'):
     if sockschain.HAVE_SSL:
       chain = ['default']
       chain.append('ssl!%s!%s' % (self.hostname, self.port))
       for hop in chain:
         sock.addproxy(*sockschain.parseproxy(hop))
   try:
     sock.connect((self.hostname, self.port))
     sock.setblocking(0)
     self.callback_ok(sock)
   except:
     if self.callback_err:
       self.callback_err(sock)
Exemplo n.º 5
0
 def _conn(self, address, timeout=None, source_address=None):
     sock = socks.socksocket()
     sock.setproxy(proxytype=self.typemap[self.proxy_config.protocol],
                   addr=self.proxy_config.host,
                   port=self.proxy_config.port,
                   rdns=True,
                   **self._auth_args())
     if timeout and timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
         sock.settimeout(float(timeout))
     if source_address:
         raise IOError('Cannot bind source address')
     try:
         address = self._fix_address_tuple(address)
         sock.connect(address)
     except socks.ProxyError:
         if self._debug is not None:
             self._debug(traceback.format_exc())
         raise IOError('Proxy failed for %s:%s' % address)
     return sock
Exemplo n.º 6
0
 def _conn(self, address, timeout=None, source_address=None):
     sock = socks.socksocket()
     sock.setproxy(
         proxytype=self.typemap[self.proxy_config.protocol],
         addr=self.proxy_config.host,
         port=self.proxy_config.port,
         rdns=True,
         username=self.proxy_config.username or None,
         password=self.proxy_config.password or None,
     )
     if timeout and timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
         sock.settimeout(float(timeout))
     if source_address:
         raise IOError("Cannot bind source address")
     try:
         address = (str(address[0]), address[1])
         sock.connect(address)
     except socks.ProxyError:
         if self._debug is not None:
             self._debug(traceback.format_exc())
         raise IOError("Proxy failed for %s:%s" % address)
     return sock
Exemplo n.º 7
0
 def _conn(self, address, timeout=None, source_address=None, **kwargs):
     sock = socks.socksocket()
     proxytype = self.typemap.get(self.proxy_config.protocol,
                                  self.typemap[self.DEFAULT_PROTO])
     sock.setproxy(proxytype=proxytype,
                   addr=self.proxy_config.host,
                   port=self.proxy_config.port,
                   rdns=True,
                   **self._auth_args())
     if timeout and timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
         sock.settimeout(float(timeout))
     if source_address:
         raise CapabilityFailure('Cannot bind source address')
     try:
         address = self._fix_address_tuple(address)
         sock.connect(address)
     except socks.ProxyError as e:
         if self._debug is not None:
             self._debug(traceback.format_exc())
         code, msg = e.message
         raise IOError(_(self.IOERROR_FMT
                         ) % (_(self.IOERROR_MSG.get(msg, msg)), ))
     return sock
Exemplo n.º 8
0
def create_connection(address, timeout=None, source_address=None):
	sock = socks.socksocket()
	sock.connect(address)
	return sock
Exemplo n.º 9
0
def socks_server_test(*args, **kwargs):

    from_whence = int(kwargs.get('from_whence'))
    host = kwargs.get('host')
    port = int(kwargs.get('port'))
    socks4 = kwargs.get('socks4', False)
    socks5 = kwargs.get('socks5', False)

    types_socks = set(); types_socks.add(socks.PROXY_TYPE_SOCKS4, socks.PROXY_TYPE_SOCKS5, )
    if socks4 and not socks5:
        types_socks = set(socks.PROXY_TYPE_SOCKS4, )
    elif not socks4 and socks5:
        types_socks = set(socks.PROXY_TYPE_SOCKS5, )

    socket.setdefaulttimeout(10)
    s = socks.socksocket()
    connect = False
    first_type_socks, second_type_socks = None, None

    for type_socks in types_socks:
        s.setproxy(type_socks, host, port)

        try:
            s.connect(('smtp.yandex.ru', 25))
            recv = s.recv(1024)
            # print("Message after connection request:" + recv.decode())

            if recv[:3] != '220':
                # print('220 reply not received from server.')
                continue

            s.send('HELO proxy.keksik.com.ua\r\n'.encode())
            recv = s.recv(1024); print("Message after HeLO command:" + recv.decode())

            if recv[:3] != '250':
                print('250 reply not received from server.')

            connect = True

            if not first_type_socks and not second_type_socks:
                first_type_socks = type_socks
            elif first_type_socks and not second_type_socks:
                second_type_socks = type_socks

            # print('first_type_socks: ', first_type_socks, ' second_type_socks: ', second_type_socks, )

            quit = "QUIT\r\n"
            s.send(quit.encode())
            # print(s.recv(1024).decode())
            s.close()

        except socket.error as e:
            # print('Exception(socket.error): ', e)
            continue

        except socks.GeneralProxyError as e:
            # print('Exception(socks.GeneralProxyError): ', e)
            continue

        except (socks.Socks4Error, socks.Socks5Error) as e:
            # print('Exception(socks.Socks4Error or socks.Socks5Error): ', e)
            continue

    socks4 = False
    if first_type_socks == socks.PROXY_TYPE_SOCKS4\
            or second_type_socks == socks.PROXY_TYPE_SOCKS4:
        socks4 = True

    socks5 = False
    if first_type_socks == socks.PROXY_TYPE_SOCKS5\
            or second_type_socks == socks.PROXY_TYPE_SOCKS5:
        socks5 = True

    try:
        pr_serv = models.ProxyServer.objects.get(host=host, port=port, )

    except models.ProxyServer.DoesNotExist:
        pr_serv = models.ProxyServer(from_whence=from_whence, host=host, port=port, socks4=socks4, socks5=socks5, )

    except models.ProxyServer.MultipleObjectsReturned:
        pr_serv = models.ProxyServer.objects.filter(host=host, port=port, )
        pr_serv[1].delete()
        pr_serv = pr_serv[0]

    if connect:
        if socks4:
            pr_serv.socks4_success += 1
        if socks5:
            pr_serv.socks5_success += 1

        pr_serv.save()

        print('pr_serv: ', pr_serv, ' host: ', host, ' port: ', port, ' OK')

    else:
        if pr_serv.failed > 10:
            pr_serv.delete()
        else:
            pr_serv.failed += 1
            pr_serv.save()

    return connect
Exemplo n.º 10
0
def processing_delivery_through_socks(*args, **kwargs):

    # message_pk = kwargs.get('message_pk')
    # logger.info(u'delivery_pk: {0}'.format(delivery_pk))
    message = model_Message.objects.get(pk=kwargs.get('message_pk'), )

    proxy_servers = models_socks.ProxyServer.objects.filter(Q(socks4=True) | Q(socks5=True), ).order_by('-socks4_pos', '-socks5_pos', )
    socket.setdefaulttimeout(10)
    s = socks.socksocket()
    type_socks = 4

    for n, serv in enumerate(proxy_servers):
        print('n: ', n, ' from: ', len(proxy_servers))
        if serv.socks4 and serv.socks5:
            type_socks = randrange(start=4, stop=5)
            print('randrange: type_sock: ', type_socks)
        if serv.socks4 or type_socks == 4:
            type_socks = socks.PROXY_TYPE_SOCKS4
        elif serv.socks5 or type_socks == 5:
            type_socks = socks.PROXY_TYPE_SOCKS5
        s.setproxy(type_socks, serv.host, serv.port)
        print('type: ', type_socks, 'serv.host: ', serv.host, 'serv.port: ', serv.port, ' : ', serv.socks4_pos, ' : ', serv.socks5_pos)

        for preference, smtp_serv in get_MXes(message.email.domain).iteritems():
            try:
                s.connect((smtp_serv, 25))
            except socket.error as e:
                print('Exception(socket.error): ', e)
                serv.dec_pos(type_socks)
                if socket.error.errno == 110:
                    serv.dec_pos(type_socks, int_dec=10)
                break
            except socks.GeneralProxyError as e:
                print('Exception(socks.GeneralProxyError): ', e)
                serv.dec_pos(type_socks)
                break
            except socks.Socks4Error as e:
                print('Exception(socks.Socks4Error): ', e)
                serv.dec_pos(type_socks)
                break
            except socks.Socks5Error as e:
                print('Exception(socks.Socks5Error): ', e)
                serv.dec_pos(type_socks)
                break


            print('smtp_serv: ', smtp_serv, 'port: ', 25)

            recv = s.recv(1024)
            print("Message after connection request:" + recv.decode())

            if recv[:3] != '220':
                print('220 reply not received from server.')
                break
            else:
                serv.inc_pos(type_socks, )

            heloCommand = 'HELO proxy.keksik.com.ua\r\n'
            s.send(heloCommand.encode())
            recv1 = s.recv(1024)
            print("Message after HeLO command:" + recv1.decode())

            if recv1[:3] != '250':
                print('250 reply not received from server.')
                break
            else:
                serv.inc_pos(type_socks, )

            mailFrom = "MAIL FROM:<[email protected]>\r\n"
            s.send(mailFrom.encode())
            recv2 = s.recv(1024)
            print("After MAIL FROM command: " + recv2.decode())

            if recv2[:3] != '250':
                print('250 reply not received from server.')
                break
            else:
                serv.inc_pos(type_socks, )

            rcptTo = "RCPT TO:<[email protected]>\r\n"

            s.send(rcptTo.encode())
            recv3 = s.recv(1024)
            print("After RCPT TO command: " + recv3.decode())

            if recv3[:3] != '250':
                print('250 reply not received from server.')
                break
            else:
                serv.inc_pos(type_socks, )

            data = "DATA\r\n"
            s.send(data.encode())
            recv4 = s.recv(1024)
            print("After DATA command: " + recv4.decode())

            msg = 'test - TEST - test\r\n.\r\n'
            s.send(msg.encode())

            recv_msg = s.recv(1024)
            print("Response after sending message body:" + recv_msg.decode())

            if recv_msg[:3] != '250':
                print('250 reply not received from server.')
                break
            else:
                serv.inc_pos(type_socks, 100)

            quit = "QUIT\r\n"
            s.send(quit.encode())
            recv5 = s.recv(1024)
            print(recv5.decode())
            s.close()