forked from OpenBazaar/OpenBazaar-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenbazaard.py
304 lines (259 loc) · 12.3 KB
/
openbazaard.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
__author__ = 'chris'
import argparse
import json
import platform
import socket
import stun
import sys
import time
from api.ws import WSFactory, AuthenticatedWebSocketProtocol, AuthenticatedWebSocketFactory
from api.restapi import RestAPI
from config import DATA_FOLDER, KSIZE, ALPHA, LIBBITCOIN_SERVERS,\
LIBBITCOIN_SERVERS_TESTNET, SSL_KEY, SSL_CERT, SEEDS, SEEDS_TESTNET, SSL
from daemon import Daemon
from db.datastore import Database
from dht.network import Server
from dht.node import Node
from dht.storage import ForgetfulStorage
from keys.credentials import get_credentials
from keys.keychain import KeyChain
from log import Logger, FileLogObserver
from market import network
from market.listeners import MessageListenerImpl, BroadcastListenerImpl, NotificationListenerImpl
from market.contracts import check_unfunded_for_payment
from market.btcprice import BtcPrice
from market.profile import Profile
from market.transactions import rebroadcast_unconfirmed
from net.heartbeat import HeartbeatFactory
from net.sslcontext import ChainedOpenSSLContextFactory
from net.upnp import PortMapper
from net.utils import looping_retry
from net.wireprotocol import OpenBazaarProtocol
from obelisk.client import LibbitcoinClient
from protos.objects import FULL_CONE, RESTRICTED, SYMMETRIC
from twisted.internet import reactor, task
from twisted.python import log, logfile
from txws import WebSocketFactory
def run(*args):
TESTNET = args[0]
LOGLEVEL = args[1]
PORT = args[2]
ALLOWIP = args[3]
RESTPORT = args[4]
WSPORT = args[5]
HEARTBEATPORT = args[6]
def start_server(keys, first_startup=False):
# logging
logFile = logfile.LogFile.fromFullPath(DATA_FOLDER + "debug.log", rotateLength=15000000, maxRotatedFiles=1)
log.addObserver(FileLogObserver(logFile, level=LOGLEVEL).emit)
log.addObserver(FileLogObserver(level=LOGLEVEL).emit)
logger = Logger(system="OpenBazaard")
# NAT traversal
p = PortMapper()
p.add_port_mapping(PORT, PORT, "UDP")
logger.info("Finding NAT Type...")
response = looping_retry(stun.get_ip_info, "0.0.0.0", PORT)
logger.info("%s on %s:%s" % (response[0], response[1], response[2]))
ip_address = response[1]
port = response[2]
if response[0] == "Full Cone":
nat_type = FULL_CONE
elif response[0] == "Restric NAT":
nat_type = RESTRICTED
else:
nat_type = SYMMETRIC
def on_bootstrap_complete(resp):
logger.info("bootstrap complete")
task.LoopingCall(mserver.get_messages, mlistener).start(3600)
task.LoopingCall(check_unfunded_for_payment, db, libbitcoin_client, nlistener, TESTNET).start(600)
task.LoopingCall(rebroadcast_unconfirmed, db, libbitcoin_client, TESTNET).start(600)
protocol = OpenBazaarProtocol(db, (ip_address, port), nat_type, testnet=TESTNET,
relaying=True if nat_type == FULL_CONE else False)
# kademlia
SEED_URLS = SEEDS_TESTNET if TESTNET else SEEDS
storage = ForgetfulStorage()
relay_node = None
if nat_type != FULL_CONE:
for seed in SEED_URLS:
try:
relay_node = (socket.gethostbyname(seed[0].split(":")[0]),
28469 if TESTNET else 18469)
break
except socket.gaierror:
pass
try:
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol, db,
nat_type, relay_node, on_bootstrap_complete, storage)
except Exception:
node = Node(keys.guid, ip_address, port, keys.verify_key.encode(),
relay_node, nat_type, Profile(db).get().vendor)
protocol.relay_node = node.relay_node
kserver = Server(node, db, keys.signing_key, KSIZE, ALPHA, storage=storage)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(kserver.querySeed(SEED_URLS)).addCallback(on_bootstrap_complete)
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver, keys.signing_key, db)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
looping_retry(reactor.listenUDP, port, protocol)
interface = "0.0.0.0" if ALLOWIP != ["127.0.0.1"] else "127.0.0.1"
# websockets api
authenticated_sessions = []
ws_api = WSFactory(mserver, kserver, only_ip=ALLOWIP)
ws_factory = AuthenticatedWebSocketFactory(ws_api)
ws_factory.authenticated_sessions = authenticated_sessions
ws_factory.protocol = AuthenticatedWebSocketProtocol
if SSL:
reactor.listenSSL(WSPORT, ws_factory,
ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT), interface=interface)
else:
reactor.listenTCP(WSPORT, ws_factory, interface=interface)
# rest api
rest_api = RestAPI(mserver, kserver, protocol, username, password,
authenticated_sessions, only_ip=ALLOWIP)
if SSL:
reactor.listenSSL(RESTPORT, rest_api,
ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT), interface=interface)
else:
reactor.listenTCP(RESTPORT, rest_api, interface=interface)
# blockchain
if TESTNET:
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVERS_TESTNET, log=Logger(service="LibbitcoinClient"))
else:
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVERS, log=Logger(service="LibbitcoinClient"))
heartbeat_server.libbitcoin = libbitcoin_client
# listeners
nlistener = NotificationListenerImpl(ws_api, db)
mserver.protocol.add_listener(nlistener)
mlistener = MessageListenerImpl(ws_api, db)
mserver.protocol.add_listener(mlistener)
blistener = BroadcastListenerImpl(ws_api, db)
mserver.protocol.add_listener(blistener)
protocol.set_servers(ws_api, libbitcoin_client)
if first_startup:
heartbeat_server.push(json.dumps({
"status": "GUID generation complete",
"username": username,
"password": password
}))
heartbeat_server.set_status("online")
logger.info("startup took %s seconds" % str(round(time.time() - args[7], 2)))
def shutdown():
logger.info("shutting down server")
for vendor in protocol.vendors.values():
db.vendors.save_vendor(vendor.id.encode("hex"), vendor.getProto().SerializeToString())
PortMapper().clean_my_mappings(PORT)
protocol.shutdown()
reactor.addSystemEventTrigger('before', 'shutdown', shutdown)
# database
db = Database(TESTNET)
# client authentication
username, password = get_credentials(db)
# heartbeat server
interface = "0.0.0.0" if ALLOWIP != ["127.0.0.1"] else "127.0.0.1"
heartbeat_server = HeartbeatFactory(only_ip=ALLOWIP)
if SSL:
reactor.listenSSL(HEARTBEATPORT, WebSocketFactory(heartbeat_server),
ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT), interface=interface)
else:
reactor.listenTCP(HEARTBEATPORT, WebSocketFactory(heartbeat_server), interface=interface)
btcPrice = BtcPrice()
btcPrice.start()
# key generation
KeyChain(db, start_server, heartbeat_server)
reactor.run()
btcPrice.closethread()
btcPrice.join(1)
if __name__ == "__main__":
# pylint: disable=anomalous-backslash-in-string
class OpenBazaard(Daemon):
def run(self, *args):
run(*args)
class Parser(object):
def __init__(self, daemon):
self.daemon = daemon
parser = argparse.ArgumentParser(
description='OpenBazaar-Server v0.1.6',
usage='''
python openbazaard.py <command> [<args>]
python openbazaard.py <command> --help
commands:
start start the OpenBazaar server
stop shutdown the server and disconnect
restart restart the server
''')
parser.add_argument('command', help='Execute the given command')
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
parser.print_help()
exit(1)
getattr(self, args.command)()
def start(self):
parser = argparse.ArgumentParser(
description="Start the OpenBazaar server",
usage="python openbazaard.py start [<args>]"
)
parser.add_argument('-d', '--daemon', action='store_true',
help="run the server in the background as a daemon")
parser.add_argument('-t', '--testnet', action='store_true', help="use the test network")
parser.add_argument('-l', '--loglevel', default="info",
help="set the logging level [debug, info, warning, error, critical]")
parser.add_argument('-p', '--port', help="set the network port")
parser.add_argument('-a', '--allowip', default=["127.0.0.1"], action="append",
help="only allow api connections from this ip")
parser.add_argument('-r', '--restapiport', help="set the rest api port", default=18469)
parser.add_argument('-w', '--websocketport', help="set the websocket api port", default=18466)
parser.add_argument('-b', '--heartbeatport', help="set the heartbeat port", default=18470)
parser.add_argument('--pidfile', help="name of the pid file", default="openbazaard.pid")
args = parser.parse_args(sys.argv[2:])
self.print_splash_screen()
unix = ("linux", "linux2", "darwin")
if args.port:
port = int(args.port)
else:
port = 18467 if not args.testnet else 28467
if args.daemon and platform.system().lower() in unix:
self.daemon.pidfile = "/tmp/" + args.pidfile
self.daemon.start(args.testnet, args.loglevel, port, args.allowip,
int(args.restapiport), int(args.websocketport),
int(args.heartbeatport), time.time())
else:
run(args.testnet, args.loglevel, port, args.allowip,
int(args.restapiport), int(args.websocketport),
int(args.heartbeatport), time.time())
def stop(self):
# pylint: disable=W0612
parser = argparse.ArgumentParser(
description="Shutdown the server and disconnect",
usage='''usage:
python openbazaard.py stop''')
args = parser.parse_args(sys.argv[2:])
print "OpenBazaar server stopping..."
self.daemon.stop()
def restart(self):
# pylint: disable=W0612
parser = argparse.ArgumentParser(
description="Restart the server",
usage='''usage:
python openbazaard.py restart''')
parser.parse_args(sys.argv[2:])
print "Restarting OpenBazaar server..."
self.daemon.restart()
@staticmethod
def print_splash_screen():
OKBLUE = '\033[94m'
ENDC = '\033[0m'
print "________ " + OKBLUE + " __________" + ENDC
print "\_____ \ ______ ____ ____" + OKBLUE + \
"\______ \_____ _____________ _____ _______" + ENDC
print " / | \\\____ \_/ __ \ / \\" + OKBLUE +\
"| | _/\__ \ \___ /\__ \ \__ \\\_ __ \ " + ENDC
print "/ | \ |_> > ___/| | \ " + OKBLUE \
+ "| \ / __ \_/ / / __ \_/ __ \| | \/" + ENDC
print "\_______ / __/ \___ >___| /" + OKBLUE + "______ /(____ /_____ \(____ (____ /__|" + ENDC
print " \/|__| \/ \/ " + OKBLUE + " \/ \/ \/ \/ \/" + ENDC
print
print "OpenBazaar Server v0.1 starting..."
Parser(OpenBazaard('/tmp/openbazaard.pid'))