2017-12-02 17:33:55 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 19:37:45 +01:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018 OzzieIsaacs
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-06-06 17:10:22 +02:00
|
|
|
from __future__ import division, print_function, unicode_literals
|
2018-03-30 21:20:47 +02:00
|
|
|
import sys
|
2019-06-06 17:10:22 +02:00
|
|
|
import os
|
|
|
|
import argparse
|
2019-07-14 21:06:40 +02:00
|
|
|
import socket
|
2019-06-06 17:10:22 +02:00
|
|
|
|
|
|
|
from .constants import CONFIG_DIR as _CONFIG_DIR
|
2019-06-10 12:18:11 +02:00
|
|
|
from .constants import STABLE_VERSION as _STABLE_VERSION
|
|
|
|
from .constants import NIGHTLY_VERSION as _NIGHTLY_VERSION
|
|
|
|
|
|
|
|
|
|
|
|
def version_info():
|
|
|
|
if _NIGHTLY_VERSION[1].startswith('$Format'):
|
|
|
|
return "Calibre-Web version: %s - unkown git-clone" % _STABLE_VERSION['version']
|
2019-07-14 21:06:40 +02:00
|
|
|
return "Calibre-Web version: %s -%s" % (_STABLE_VERSION['version'], _NIGHTLY_VERSION[1])
|
2019-06-06 17:10:22 +02:00
|
|
|
|
2017-12-02 11:15:51 +01:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Calibre Web is a web app'
|
2020-04-19 19:08:58 +02:00
|
|
|
' providing a interface for browsing, reading and downloading eBooks\n', prog='cps.py')
|
2017-12-02 11:15:51 +01:00
|
|
|
parser.add_argument('-p', metavar='path', help='path and name to settings db, e.g. /opt/cw.db')
|
|
|
|
parser.add_argument('-g', metavar='path', help='path and name to gdrive db, e.g. /opt/gd.db')
|
2019-06-02 09:33:19 +02:00
|
|
|
parser.add_argument('-c', metavar='path',
|
|
|
|
help='path and name to SSL certfile, e.g. /opt/test.cert, works only in combination with keyfile')
|
|
|
|
parser.add_argument('-k', metavar='path',
|
|
|
|
help='path and name to SSL keyfile, e.g. /opt/test.key, works only in combination with certfile')
|
2019-06-10 12:18:11 +02:00
|
|
|
parser.add_argument('-v', '--version', action='version', help='Shows version number and exits Calibre-web',
|
|
|
|
version=version_info())
|
2020-01-11 11:42:30 +01:00
|
|
|
parser.add_argument('-i', metavar='ip-address', help='Server IP-Address to listen')
|
2019-06-10 12:18:11 +02:00
|
|
|
parser.add_argument('-s', metavar='user:pass', help='Sets specific username to new password')
|
2021-08-18 20:32:13 +02:00
|
|
|
parser.add_argument('-f', action='store_true', help='Flag is depreciated and will be removed in next version')
|
2017-12-02 11:15:51 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-06-23 09:22:26 +02:00
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
if args.p:
|
|
|
|
args.p = args.p.decode('utf-8')
|
|
|
|
if args.g:
|
|
|
|
args.g = args.g.decode('utf-8')
|
|
|
|
if args.k:
|
|
|
|
args.k = args.k.decode('utf-8')
|
|
|
|
if args.c:
|
|
|
|
args.c = args.c.decode('utf-8')
|
|
|
|
if args.s:
|
|
|
|
args.s = args.s.decode('utf-8')
|
|
|
|
|
2019-06-10 12:18:11 +02:00
|
|
|
|
2019-07-14 21:06:40 +02:00
|
|
|
settingspath = args.p or os.path.join(_CONFIG_DIR, "app.db")
|
|
|
|
gdpath = args.g or os.path.join(_CONFIG_DIR, "gdrive.db")
|
2017-12-02 11:15:51 +01:00
|
|
|
|
2019-06-10 12:18:11 +02:00
|
|
|
# handle and check parameter for ssl encryption
|
2018-03-30 21:20:47 +02:00
|
|
|
certfilepath = None
|
|
|
|
keyfilepath = None
|
|
|
|
if args.c:
|
|
|
|
if os.path.isfile(args.c):
|
|
|
|
certfilepath = args.c
|
|
|
|
else:
|
2021-04-26 19:03:01 +02:00
|
|
|
print("Certfile path is invalid. Exiting...")
|
2018-03-30 21:20:47 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2019-07-14 21:06:40 +02:00
|
|
|
if args.c == "":
|
2018-03-30 21:20:47 +02:00
|
|
|
certfilepath = ""
|
|
|
|
|
|
|
|
if args.k:
|
|
|
|
if os.path.isfile(args.k):
|
|
|
|
keyfilepath = args.k
|
|
|
|
else:
|
2021-04-26 19:03:01 +02:00
|
|
|
print("Keyfile path is invalid. Exiting...")
|
2018-03-30 21:20:47 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2018-09-23 19:15:50 +02:00
|
|
|
if (args.k and not args.c) or (not args.k and args.c):
|
|
|
|
print("Certfile and Keyfile have to be used together. Exiting...")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-07-14 21:06:40 +02:00
|
|
|
if args.k == "":
|
2018-03-30 21:20:47 +02:00
|
|
|
keyfilepath = ""
|
2019-05-31 15:48:19 +02:00
|
|
|
|
2021-04-26 19:03:01 +02:00
|
|
|
# handle and check ip address argument
|
|
|
|
ip_address = args.i or None
|
|
|
|
if ip_address:
|
2019-07-14 21:06:40 +02:00
|
|
|
try:
|
|
|
|
# try to parse the given ip address with socket
|
|
|
|
if hasattr(socket, 'inet_pton'):
|
2021-04-26 19:03:01 +02:00
|
|
|
if ':' in ip_address:
|
|
|
|
socket.inet_pton(socket.AF_INET6, ip_address)
|
2019-07-14 21:06:40 +02:00
|
|
|
else:
|
2021-04-26 19:03:01 +02:00
|
|
|
socket.inet_pton(socket.AF_INET, ip_address)
|
2019-07-14 21:06:40 +02:00
|
|
|
else:
|
|
|
|
# on windows python < 3.4, inet_pton is not available
|
|
|
|
# inet_atom only handles IPv4 addresses
|
2021-04-26 19:03:01 +02:00
|
|
|
socket.inet_aton(ip_address)
|
2019-07-14 21:06:40 +02:00
|
|
|
except socket.error as err:
|
2021-04-26 19:03:01 +02:00
|
|
|
print(ip_address, ':', err)
|
2019-07-14 21:06:40 +02:00
|
|
|
sys.exit(1)
|
2019-06-10 12:18:11 +02:00
|
|
|
|
|
|
|
# handle and check user password argument
|
2021-01-17 09:17:46 +01:00
|
|
|
user_credentials = args.s or None
|
|
|
|
if user_credentials and ":" not in user_credentials:
|
2021-04-26 19:03:01 +02:00
|
|
|
print("No valid 'username:password' format")
|
2021-01-17 09:17:46 +01:00
|
|
|
sys.exit(3)
|
2021-08-18 20:32:13 +02:00
|
|
|
|
|
|
|
if args.f:
|
|
|
|
print("Warning: -f flag is depreciated and will be removed in next version")
|