Implement client-side bindhost

This commit is contained in:
jesopo 2018-10-01 14:06:50 +01:00
parent 59243f492f
commit 6af738107b
3 changed files with 14 additions and 11 deletions

View file

@ -5,22 +5,23 @@ class Table(object):
self.database = database self.database = database
class Servers(Table): class Servers(Table):
def add(self, alias, hostname, port, password, ipv4, tls, nickname, def add(self, alias, hostname, port, password, ipv4, tls, bindhost,
username=None, realname=None): nickname, username=None, realname=None):
username = username or nickname username = username or nickname
realname = realname or nickname realname = realname or nickname
self.database.execute( self.database.execute(
"""INSERT INTO servers (alias, hostname, port, password, ipv4, """INSERT INTO servers (alias, hostname, port, password, ipv4,
tls, nickname, username, realname) VALUES ( tls, bindhost, nickname, username, realname) VALUES (
?, ?, ?, ?, ?, ?, ?, ?)""", ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
[hostname, port, password, ipv4, tls, nickname, username, realname]) [hostname, port, password, ipv4, tls, bindhost, nickname, username,
realname])
def get_all(self): def get_all(self):
return self.database.execute_fetchall( return self.database.execute_fetchall(
"SELECT server_id, alias FROM servers") "SELECT server_id, alias FROM servers")
def get(self, id): def get(self, id):
return self.database.execute_fetchone( return self.database.execute_fetchone(
"""SELECT server_id, alias, hostname, port, password, ipv4, """SELECT server_id, alias, hostname, port, password, ipv4,
tls, nickname, username, realname FROM servers WHERE tls, bindhost, nickname, username, realname FROM servers WHERE
server_id=?""", server_id=?""",
[id]) [id])
@ -305,7 +306,7 @@ class Database(object):
self.execute("""CREATE TABLE servers self.execute("""CREATE TABLE servers
(server_id INTEGER PRIMARY KEY, alias TEXT, hostname TEXT, (server_id INTEGER PRIMARY KEY, alias TEXT, hostname TEXT,
port INTEGER, password TEXT, ipv4 BOOLEAN, tls BOOLEAN, port INTEGER, password TEXT, ipv4 BOOLEAN, tls BOOLEAN,
nickname TEXT, username TEXT, realname TEXT)""") bindhost TEXT, nickname TEXT, username TEXT, realname TEXT)""")
def make_channels_table(self): def make_channels_table(self):
if not self.has_table("channels"): if not self.has_table("channels"):
self.execute("""CREATE TABLE channels self.execute("""CREATE TABLE channels

View file

@ -23,11 +23,11 @@ class Bot(object):
self.poll = select.epoll() self.poll = select.epoll()
def add_server(self, server_id, connect=True): def add_server(self, server_id, connect=True):
(_, alias, hostname, port, password, ipv4, tls, nickname, (_, alias, hostname, port, password, ipv4, tls, bindhost, nickname,
username, realname) = self.database.servers.get(server_id) username, realname) = self.database.servers.get(server_id)
new_server = IRCServer.Server(self, self._events, server_id, alias, new_server = IRCServer.Server(self, self._events, server_id, alias,
hostname, port, password, ipv4, tls, nickname, username, hostname, port, password, ipv4, tls, bindhost, nickname, username,
realname) realname)
if not new_server.get_setting("connect", True): if not new_server.get_setting("connect", True):
return return

View file

@ -8,7 +8,7 @@ PING_INTERVAL_SECONDS = 30
class Server(IRCObject.Object): class Server(IRCObject.Object):
def __init__(self, bot, events, id, alias, hostname, port, password, def __init__(self, bot, events, id, alias, hostname, port, password,
ipv4, tls, nickname, username, realname): ipv4, tls, bindhost, nickname, username, realname):
self.connected = False self.connected = False
self.bot = bot self.bot = bot
self.events = events self.events = events
@ -59,6 +59,8 @@ class Server(IRCObject.Object):
self.socket = socket.socket(socket.AF_INET6, self.socket = socket.socket(socket.AF_INET6,
socket.SOCK_STREAM) socket.SOCK_STREAM)
if bindhost:
self.socket.bind((bindhost, 0))
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
self.socket.settimeout(5.0) self.socket.settimeout(5.0)