forked from cryptosharks131/lndg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize.py
357 lines (317 loc) · 12.7 KB
/
initialize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import secrets, argparse, django, os
from pathlib import Path
from django.core.management import call_command
from django.contrib.auth import get_user_model
from django.conf import settings
BASE_DIR = Path(__file__).resolve().parent
def write_settings(node_ip, lnd_tls_path, lnd_macaroon_path, lnd_database_path, lnd_network, lnd_rpc_server, lnd_max_message, whitenoise, debug, csrftrusted, nologinrequired):
#Generate a unique secret to be used for your django site
secret = secrets.token_urlsafe(64)
if whitenoise:
wnl = """
'whitenoise.middleware.WhiteNoiseMiddleware',"""
else:
wnl = ''
if csrftrusted:
csrf = """
CSRF_TRUSTED_ORIGINS = [%s]
""" % (csrftrusted)
else:
csrf = ''
if not nologinrequired:
api_login = """
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],"""
else:
print('WARNING: No password login option detected, LNDg will not require authentication...')
api_login = ''
settings_file = '''"""
Django settings for lndg project.
Generated by 'django-admin startproject' using Django {{ django_version }}.
For more information on this file, see
https://docs.djangoproject.com/en/{{ docs_version }}/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '%s'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = %s
ALLOWED_HOSTS = ['%s']
%s
LND_TLS_PATH = '%s'
LND_MACAROON_PATH = '%s'
LND_DATABASE_PATH = '%s'
LND_NETWORK = '%s'
LND_RPC_SERVER = '%s'
LND_MAX_MESSAGE = '%s'
LOGIN_REQUIRED = %s
# Application definition
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.humanize',
'gui',
'rest_framework',
'django_filters',
'qr_code',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',%s
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'lndg.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'lndg.wsgi.application'
# Database
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'data/db.sqlite3',
'OPTIONS': {
'timeout': 20,
},
}
}
# Password validation
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 100,%s
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
],
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
}
# Internationalization
# https://docs.djangoproject.com/en/{{ docs_version }}/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/{{ docs_version }}/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'gui/static/')
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
''' % (secret, debug, node_ip, csrf, lnd_tls_path, lnd_macaroon_path, lnd_database_path, lnd_network, lnd_rpc_server, lnd_max_message, not nologinrequired, wnl, api_login)
try:
f = open("lndg/settings.py", "x")
f.close()
except:
print('A settings file may already exist, skipping creation...')
return
try:
f = open("lndg/settings.py", "w")
f.write(settings_file)
f.close()
except Exception as e:
print('Error creating the settings file:', e)
def write_supervisord_settings(sduser):
supervisord_secret = secrets.token_urlsafe(16)
supervisord_settings_file = '''[supervisord]
user=%s
childlogdir = /var/log
logfile = /var/log/supervisord.log
logfile_maxbytes = 50MB
logfile_backups = 30
loglevel = info
pidfile = /var/supervisord.pid
umask = 022
nodaemon = false
nocleanup = false
[inet_http_server]
port = 9001
username = lndg-supervisord
password = %s
[supervisorctl]
serverurl = http://localhost:9001
username = lndg-supervisord
password = %s
[rpcinterface:supervisor]
supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface
[program:jobs]
command = sh -c "python jobs.py && sleep 15"
process_name = lndg-jobs
directory = %s
autorestart = true
redirect_stderr = true
stdout_logfile = /var/log/lndg-jobs.log
stdout_logfile_maxbytes = 150MB
stdout_logfile_backups = 15
[program:rebalancer]
command = sh -c "python rebalancer.py && sleep 15"
process_name = lndg-rebalancer
directory = %s
autorestart = true
redirect_stderr = true
stdout_logfile = /var/log/lndg-rebalancer.log
stdout_logfile_maxbytes = 150MB
stdout_logfile_backups = 15
[program:htlc-stream]
command = sh -c "python htlc_stream.py && sleep 15"
process_name = lndg-htlc-stream
directory = %s
autorestart = true
redirect_stderr = true
stdout_logfile = /var/log/lndg-htlc-stream.log
stdout_logfile_maxbytes = 150MB
stdout_logfile_backups = 15
''' % (sduser, supervisord_secret, supervisord_secret, BASE_DIR, BASE_DIR, BASE_DIR)
try:
f = open("/usr/local/etc/supervisord.conf", "x")
f.close()
except:
print('A supervisord settings file may already exist, skipping creation...')
return
try:
f = open("/usr/local/etc/supervisord.conf", "w")
f.write(supervisord_settings_file)
f.close()
except Exception as e:
print('Error creating the settings file:', str(e))
def main():
help_msg = "LNDg Initializer"
parser = argparse.ArgumentParser(description = help_msg)
parser.add_argument('-ip', '--nodeip',help = 'IP that will be used to access the LNDg page', default='*')
parser.add_argument('-dir', '--lnddir',help = 'LND Directory for tls cert and admin macaroon paths', default=None)
parser.add_argument('-net', '--network', help = 'Network LND will run over', default='mainnet')
parser.add_argument('-server', '--rpcserver', help = 'Server address to use for rpc communications with LND', default='localhost:10009')
parser.add_argument('-maxmsg', '--maxmessage', help = 'Maximum message size for grpc communications (MB)', default='35')
parser.add_argument('-sd', '--supervisord', help = 'Setup supervisord to run jobs/rebalancer background processes', action='store_true')
parser.add_argument('-sdu', '--sduser', help = 'Configure supervisord with a non-root user', default='root')
parser.add_argument('-wn', '--whitenoise', help = 'Add whitenoise middleware (docker requirement for static files)', action='store_true')
parser.add_argument('-d', '--docker', help = 'Single option for docker container setup (supervisord + whitenoise)', action='store_true')
parser.add_argument('-dx', '--debug', help = 'Setup the django site in debug mode', action='store_true')
parser.add_argument('-u', '--adminuser', help = 'Setup a custom admin username', default='lndg-admin')
parser.add_argument('-pw', '--adminpw', help = 'Setup a custom admin password', default=None)
parser.add_argument('-csrf', '--csrftrusted', help = 'Set trusted CSRF origins', default=None)
parser.add_argument('-tls', '--tlscert', help = 'Set the path to a custom tls cert', default=None)
parser.add_argument('-mcrn', '--macaroon', help = 'Set the path to a custom macroon file', default=None)
parser.add_argument('-lnddb', '--lnddatabase', help = 'Set the path to the channel.db for monitoring', default=None)
parser.add_argument('-nologin', '--nologinrequired', help = 'By default, force all connections to be authenticated', action='store_true')
args = parser.parse_args()
node_ip = args.nodeip
lnd_dir_path = args.lnddir if args.lnddir else '~/.lnd'
lnd_network = args.network
lnd_rpc_server = args.rpcserver
lnd_max_message = args.maxmessage
setup_supervisord = args.supervisord
sduser = args.sduser
whitenoise = args.whitenoise
docker = args.docker
debug = args.debug
adminuser = args.adminuser
adminpw = args.adminpw
csrftrusted = args.csrftrusted
nologinrequired = args.nologinrequired
lnd_tls_path = args.tlscert if args.tlscert else lnd_dir_path + '/tls.cert'
lnd_macaroon_path = args.macaroon if args.macaroon else lnd_dir_path + '/data/chain/bitcoin/' + lnd_network + '/admin.macaroon'
lnd_database_path = args.lnddatabase if args.lnddatabase else lnd_dir_path + '/data/graph/' + lnd_network + '/channel.db'
if docker:
setup_supervisord = True
whitenoise = True
write_settings(node_ip, lnd_tls_path, lnd_macaroon_path, lnd_database_path, lnd_network, lnd_rpc_server, lnd_max_message, whitenoise, debug, csrftrusted, nologinrequired)
if setup_supervisord:
print('Supervisord setup requested...')
write_supervisord_settings(sduser)
try:
DATA_DIR = os.path.join(BASE_DIR, 'data')
try:
os.mkdir(DATA_DIR)
except:
print('Data directory already found...')
Path(os.path.join(DATA_DIR, 'db.sqlite3')).touch()
settings.configure(
SECRET_KEY = secrets.token_urlsafe(64),
DATABASES = {
'default':{
'ENGINE':'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'data/db.sqlite3'
}
},
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'rest_framework',
'gui',
],
STATIC_URL = 'static/',
STATIC_ROOT = os.path.join(BASE_DIR, 'gui/static/')
)
django.setup()
call_command('migrate', verbosity=0)
call_command('collectstatic', verbosity=0, interactive=False)
if get_user_model().objects.count() == 0:
print('Setting up initial user...')
try:
call_command('createsuperuser', username=adminuser, email='[email protected]', interactive=False)
admin = get_user_model().objects.get(username=adminuser)
login_pw = secrets.token_urlsafe(16) if adminpw is None else adminpw
admin.set_password(login_pw)
admin.save()
if adminpw is None:
try:
Path(os.path.join(DATA_DIR, 'lndg-admin.txt')).touch()
f = open('data/lndg-admin.txt', 'w')
f.write(login_pw)
f.close()
except Exception as e:
print('Error writing password file:', str(e))
print('FIRST TIME LOGIN PASSWORD:' + login_pw)
except Exception as e:
print('Error setting up initial user:', str(e))
except Exception as e:
print('Error initializing django:', str(e))
if __name__ == '__main__':
main()