-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_server
120 lines (96 loc) · 4.54 KB
/
run_server
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
#!/usr/bin/env python3
#############################################################################
# Copyright [2023] [R0BM01@pm.me] #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#############################################################################
import time
import lib.server.server
import lib.server.machine
import lib.shared.storage
tempLogs = [] # we need to store logs before checking directory tree
def addTempLog(log):
tempLogs.append(log)
print(f"{str(time.ctime(time.time()))} - {log}")
def check_dirs_files(storage):
firstRun = False
certNotValid = False
success = False
def certificate_choice():
c = input("Type \n'I' to import \n'G' to generate \n'Q' to quit \n>> ").upper()
if c == 'I': storage.import_certificate(input("Certificate path >> "))
elif c == 'G': storage.generate_certificate()
return storage.check_certificate()
addTempLog("Checking base directory")
if not storage.check_base_dir():
firstRun = True
addTempLog("Server first run")
storage.init_dir(storage.saveDir)
#storage.init_all()
addTempLog("Checking directory tree integrity")
for d, p in storage.saveDirs.items():
check = storage.check_exists(p)
addTempLog(f"dir [{d}]: [{check}]")
if not check:
storage.init_dir(p)
addTempLog(f"{d} created")
addTempLog("Checking files")
for f, p in storage.saveFiles.items():
check = storage.check_exists(p)
addTempLog(f"file [{f}]: [{p}]")
if not check:
storage.init_file(p)
addTempLog(f"{f} created")
if f == 'cert' and not check: certNotValid = True
success = True
if firstRun or certNotValid:
addTempLog("Certificate must be generated or imported")
if not certificate_choice():
success = False
addTempLog("Certificate not imported/generated. Server stopping and exit.")
else:
addTempLog("Certificate valid!")
return success
def main():
storage = lib.shared.storage.Server()
if check_dirs_files(storage):
#adds the temporary log to the actual logger
logger = lib.shared.storage.Logger(storage.saveDirs['debug'], verbose = False)
[logger.add(log) for log in tempLogs]
logger.add("loading certificate")
storage.init_certificate()
logger.add("Certificate: " + str(storage.certificate))
logger.add("geolocation init")
storage.init_geolocation()
# server init with actual logger
logger.verbose = True
SERVER = lib.server.server.Server(logger, storage)
if SERVER.bitcoindRunning:
logger.add("updating base cache data")
SERVER.updateCacheData()
logger.add("BitcoinCore uptime", SERVER.BITCOIN_DATA.uptime['uptime'])
logger.add("BitcoinCore chain", SERVER.BITCOIN_DATA.blockchainInfo['chain'])
logger.add("BitcoinCore blocks", SERVER.BITCOIN_DATA.blockchainInfo['blocks'])
logger.add("BitcoinCore peers", SERVER.BITCOIN_DATA.networkInfo['connections'])
logger.add("updating geolocation data... wait until complete (up to 2 minutes)")
SERVER.updateGeolocationData(logger)
else:
logger.add("Bitcoin Daemon is not running")
logger.add("starting network")
SERVER.start_network()
if SERVER.isOnline: SERVER.start_all()
else: logger.add("Server socket not working")
else:
print("Directories and Files check has failed. Exiting server application")
if __name__ == '__main__':
main()