Reputation: 1
Introduce the Problem: I'm developing an IRC bot in Python that needs to connect to an IRC server through a proxy server. However, the bot fails to establish a connection.
Expand on the Problem: I am using the following modules: irc
, socks
, socket
.
Terminal Output:
INFO:root:Bot created. Starting...
DEBUG:irc.client:connect(server='irc.libera.chat', port=6667, nickname='uwu', ...)
INFO:root:Successfully set proxy: 178.128.113.118:8000
DEBUG:irc.client:_dispatcher: disconnect
DEBUG:irc.client:process_forever(timeout=0.2)
Proxy: I am using a free proxy obtained from a website.
Expand on the Problem: Here is the relevant part of my code:
import irc.bot
import irc.strings
import irc.connection
import socks
import socket
import logging
import sys
logging.basicConfig(level=logging.DEBUG)
class IRCBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667, proxy_type=None, proxy_addr=None, proxy_port=None):
self.proxy_type = proxy_type
self.proxy_addr = proxy_addr
self.proxy_port = proxy_port
if proxy_type:
connect_factory = irc.connection.Factory(wrapper=self._get_wrapper)
else:
connect_factory = irc.connection.Factory()
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname, connect_factory=connect_factory)
self.channel = channel
def _get_wrapper(self, *args, **kwargs):
sock = socks.socksocket()
if self.proxy_type:
try:
sock.set_proxy(self.proxy_type, self.proxy_addr, self.proxy_port)
logging.info(f"Successfully set proxy: {self.proxy_addr}:{self.proxy_port}")
except Exception as e:
logging.error(f"Failed to set proxy: {e}")
logging.info("Falling back to direct connection")
sock = socket.socket()
return sock
def on_welcome(self, connection, event):
logging.info(f"Connected to server. Joining channel {self.channel}")
connection.join(self.channel)
def on_join(self, connection, event):
if event.source.nick == connection.get_nickname():
logging.info(f"Joined channel {event.target}")
connection.privmsg(self.channel, "hi guys")
def main():
channel = "#Anime-18"
nickname = "uwu"
server = "irc.libera.chat" # Using Libera Chat as it's a popular network
port = 6667
proxy_type = socks.PROXY_TYPE_SOCKS4
proxy_addr = "178.128.113.118"
proxy_port = 8000
try:
bot = IRCBot(channel, nickname, server, port, proxy_type, proxy_addr, proxy_port)
logging.info("Bot created. Starting...")
bot.start()
except irc.client.ServerConnectionError as e:
logging.error(f"Error connecting to server: {e}")
except Exception as e:
logging.error(f"An error occurred: {e}")
if __name__ == "__main__":
main()
Upvotes: 0
Views: 30