From a8a902a414576fd417f94eeea2361ec4b823ea35 Mon Sep 17 00:00:00 2001 From: Tommaso Sartor Date: Fri, 2 Dec 2016 03:27:26 +0100 Subject: [PATCH] Add option for unix-socket in config.yml Add configuration option to allow using unix-socket to connect with the daemonized telegram-cli (i.e. using the command `telegram-cli -d -S /var/run/telegram.sock`). If null value is set for the socket pack the TCP connection is used. If a path is set for the socket TCP connection options are ignored. WHY: With unix-socket running telegram-cli as a daemon is more secure. With tpc connection the interface to user personal data served by telegram-cli is accessible system wide. With unix-socket user can set specific privileges on the socket and thus restrict the access to that interface. --- config.yaml | 9 +++++++++ telegram-history-dump.rb | 12 +++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/config.yaml b/config.yaml index 2aae57f..7b6bc0b 100644 --- a/config.yaml +++ b/config.yaml @@ -94,6 +94,15 @@ ## Technical options ## + # Use a unix socket to communicate with telegram-cli + # Should match the path passed to telegram-cli with the -S option + # + # NB: Setting tg_sock to some not false value will cause the script + # to ignore the next options about TCP connection + + # tg_sock: "/var/run/telegram.sock" + tg_sock: null + # Address of the machine you are running telegram-cli on # If this is not localhost telegram-cli must be invoked with --accept-any-tcp tg_host: 'localhost' diff --git a/telegram-history-dump.rb b/telegram-history-dump.rb index ec5ac14..73c0c42 100755 --- a/telegram-history-dump.rb +++ b/telegram-history-dump.rb @@ -22,9 +22,15 @@ def connect_socket return if defined?($sock) && $sock - $log.info('Attaching to telegram-cli control socket at %s:%d' % - [$config['tg_host'], $config['tg_port']]) - $sock = TCPSocket.open($config['tg_host'], $config['tg_port']) + + if $config['tg_sock'] + $log.info('Attaching to telegram-cli control socket at /var/run/telegram.sock') + $sock = UNIXSocket.new($config['tg_sock']) + else + $log.info('Attaching to telegram-cli control socket at %s:%d' % + [$config['tg_host'], $config['tg_port']]) + $sock = TCPSocket.open($config['tg_host'], $config['tg_port']) + end end def disconnect_socket