<s> __author__ = '<STR_LIT>' <EOL> import argparse <EOL> import json <EOL> import os . path <EOL> import platform <EOL> import socket <EOL> import stun <EOL> import sys <EOL> import time <EOL> from api . ws import WSFactory , AuthenticatedWebSocketProtocol , AuthenticatedWebSocketFactory <EOL> from api . restapi import RestAPI <EOL> from config import DATA_FOLDER , KSIZE , ALPHA , LIBBITCOIN_SERVERS , LIBBITCOIN_SERVERS_TESTNET , SSL_KEY , SSL_CERT , SEEDS , SEEDS_TESTNET , SSL <EOL> from daemon import Daemon <EOL> from db . datastore import Database <EOL> from dht . network import Server <EOL> from dht . node import Node <EOL> from dht . storage import ForgetfulStorage <EOL> from keys . credentials import get_credentials <EOL> from keys . keychain import KeyChain <EOL> from log import Logger , FileLogObserver <EOL> from market import network <EOL> from market . listeners import MessageListenerImpl , BroadcastListenerImpl , NotificationListenerImpl <EOL> from market . contracts import check_unfunded_for_payment <EOL> from market . btcprice import BtcPrice <EOL> from market . profile import Profile <EOL> from market . transactions import rebroadcast_unconfirmed <EOL> from net . heartbeat import HeartbeatFactory <EOL> from net . sslcontext import ChainedOpenSSLContextFactory <EOL> from net . upnp import PortMapper <EOL> from net . utils import looping_retry <EOL> from net . wireprotocol import OpenBazaarProtocol <EOL> from obelisk . client import LibbitcoinClient <EOL> from protos . objects import FULL_CONE , RESTRICTED , SYMMETRIC <EOL> from twisted . internet import reactor , task <EOL> from twisted . python import log , logfile <EOL> from txws import WebSocketFactory <EOL> def run ( * args ) : <EOL> TESTNET = args [ <NUM_LIT:0> ] <EOL> LOGLEVEL = args [ <NUM_LIT:1> ] <EOL> PORT = args [ <NUM_LIT:2> ] <EOL> ALLOWIP = args [ <NUM_LIT:3> ] <EOL> RESTPORT = args [ <NUM_LIT:4> ] <EOL> WSPORT = args [ <NUM_LIT:5> ] <EOL> HEARTBEATPORT = args [ <NUM_LIT:6> ] <EOL> def start_server ( keys , first_startup = False ) : <EOL> logFile = logfile . LogFile . fromFullPath ( <EOL> os . path . join ( DATA_FOLDER , "<STR_LIT>" ) , <EOL> rotateLength = <NUM_LIT> , <EOL> maxRotatedFiles = <NUM_LIT:1> ) <EOL> log . addObserver ( FileLogObserver ( logFile , level = LOGLEVEL ) . emit ) <EOL> log . addObserver ( FileLogObserver ( level = LOGLEVEL ) . emit ) <EOL> logger = Logger ( system = "<STR_LIT>" ) <EOL> p = PortMapper ( ) <EOL> p . add_port_mapping ( PORT , PORT , "<STR_LIT>" ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> response = looping_retry ( stun . get_ip_info , "<STR_LIT>" , PORT ) <EOL> logger . info ( "<STR_LIT>" % ( response [ <NUM_LIT:0> ] , response [ <NUM_LIT:1> ] , response [ <NUM_LIT:2> ] ) ) <EOL> ip_address = response [ <NUM_LIT:1> ] <EOL> port = response [ <NUM_LIT:2> ] <EOL> if response [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> nat_type = FULL_CONE <EOL> elif response [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> nat_type = RESTRICTED <EOL> else : <EOL> nat_type = SYMMETRIC <EOL> def on_bootstrap_complete ( resp ) : <EOL> logger . info ( "<STR_LIT>" ) <EOL> task . LoopingCall ( mserver . get_messages , mlistener ) . start ( <NUM_LIT> ) <EOL> task . LoopingCall ( check_unfunded_for_payment , db , libbitcoin_client , nlistener , TESTNET ) . start ( <NUM_LIT> ) <EOL> task . LoopingCall ( rebroadcast_unconfirmed , db , libbitcoin_client , TESTNET ) . start ( <NUM_LIT> ) <EOL> protocol = OpenBazaarProtocol ( db , ( ip_address , port ) , nat_type , testnet = TESTNET , <EOL> relaying = True if nat_type == FULL_CONE else False ) <EOL> SEED_URLS = SEEDS_TESTNET if TESTNET else SEEDS <EOL> relay_node = None <EOL> if nat_type != FULL_CONE : <EOL> for seed in SEED_URLS : <EOL> try : <EOL> relay_node = ( socket . gethostbyname ( seed [ <NUM_LIT:0> ] . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> ] ) , <EOL> <NUM_LIT> if TESTNET else <NUM_LIT> ) <EOL> break <EOL> except socket . gaierror : <EOL> pass <EOL> try : <EOL> kserver = Server . loadState ( os . path . join ( DATA_FOLDER , '<STR_LIT>' ) , ip_address , port , protocol , db , <EOL> nat_type , relay_node , on_bootstrap_complete , storage ) <EOL> except Exception : <EOL> node = Node ( keys . guid , ip_address , port , keys . verify_key . encode ( ) , <EOL> relay_node , nat_type , Profile ( db ) . get ( ) . vendor ) <EOL> protocol . relay_node = node . relay_node <EOL> kserver = Server ( node , db , keys . signing_key , KSIZE , ALPHA , storage = storage ) <EOL> kserver . protocol . connect_multiplexer ( protocol ) <EOL> kserver . bootstrap ( kserver . querySeed ( SEED_URLS ) ) . addCallback ( on_bootstrap_complete ) <EOL> kserver . saveStateRegularly ( os . path . join ( DATA_FOLDER , '<STR_LIT>' ) , <NUM_LIT:10> ) <EOL> protocol . register_processor ( kserver . protocol ) <EOL> mserver = network . Server ( kserver , keys . signing_key , db ) <EOL> mserver . protocol . connect_multiplexer ( protocol ) <EOL> protocol . register_processor ( mserver . protocol ) <EOL> looping_retry ( reactor . listenUDP , port , protocol ) <EOL> interface = "<STR_LIT>" if ALLOWIP != [ "<STR_LIT:127.0.0.1>" ] else "<STR_LIT:127.0.0.1>" <EOL> authenticated_sessions = [ ] <EOL> ws_api = WSFactory ( mserver , kserver , only_ip = ALLOWIP ) <EOL> ws_factory = AuthenticatedWebSocketFactory ( ws_api ) <EOL> ws_factory . authenticated_sessions = authenticated_sessions <EOL> ws_factory . protocol = AuthenticatedWebSocketProtocol <EOL> if SSL : <EOL> reactor . listenSSL ( WSPORT , ws_factory , <EOL> ChainedOpenSSLContextFactory ( SSL_KEY , SSL_CERT ) , interface = interface ) <EOL> else : <EOL> reactor . listenTCP ( WSPORT , ws_factory , interface = interface ) <EOL> rest_api = RestAPI ( mserver , kserver , protocol , username , password , <EOL> authenticated_sessions , only_ip = ALLOWIP ) <EOL> if SSL : <EOL> reactor . listenSSL ( RESTPORT , rest_api , <EOL> ChainedOpenSSLContextFactory ( SSL_KEY , SSL_CERT ) , interface = interface ) <EOL> else : <EOL> reactor . listenTCP ( RESTPORT , rest_api , interface = interface ) <EOL> if TESTNET : <EOL> libbitcoin_client = LibbitcoinClient ( LIBBITCOIN_SERVERS_TESTNET , log = Logger ( service = "<STR_LIT>" ) ) <EOL> else : <EOL> libbitcoin_client = LibbitcoinClient ( LIBBITCOIN_SERVERS , log = Logger ( service = "<STR_LIT>" ) ) <EOL> heartbeat_server . libbitcoin = libbitcoin_client <EOL> nlistener = NotificationListenerImpl ( ws_api , db ) <EOL> mserver . protocol . add_listener ( nlistener ) <EOL> mlistener = MessageListenerImpl ( ws_api , db ) <EOL> mserver . protocol . add_listener ( mlistener ) <EOL> blistener = BroadcastListenerImpl ( ws_api , db ) <EOL> mserver . protocol . add_listener ( blistener ) <EOL> protocol . set_servers ( ws_api , libbitcoin_client ) <EOL> if first_startup : <EOL> heartbeat_server . push ( json . dumps ( { <EOL> "<STR_LIT:status>" : "<STR_LIT>" , <EOL> "<STR_LIT:username>" : username , <EOL> "<STR_LIT:password>" : password <EOL> } ) ) <EOL> heartbeat_server . set_status ( "<STR_LIT>" ) <EOL> logger . info ( "<STR_LIT>" % str ( round ( time . time ( ) - args [ <NUM_LIT:7> ] , <NUM_LIT:2> ) ) ) <EOL> def shutdown ( ) : <EOL> logger . info ( "<STR_LIT>" ) <EOL> for vendor in protocol . vendors . values ( ) : <EOL> db . vendors . save_vendor ( vendor . id . encode ( "<STR_LIT>" ) , vendor . getProto ( ) . SerializeToString ( ) ) <EOL> PortMapper ( ) . clean_my_mappings ( PORT ) <EOL> protocol . shutdown ( ) <EOL> reactor . addSystemEventTrigger ( '<STR_LIT>' , '<STR_LIT>' , shutdown ) <EOL> db = Database ( TESTNET ) <EOL> storage = ForgetfulStorage ( ) <EOL> username , password = get_credentials ( db ) <EOL> interface = "<STR_LIT>" if ALLOWIP != [ "<STR_LIT:127.0.0.1>" ] else "<STR_LIT:127.0.0.1>" <EOL> heartbeat_server = HeartbeatFactory ( only_ip = ALLOWIP ) <EOL> if SSL : <EOL> reactor . listenSSL ( HEARTBEATPORT , WebSocketFactory ( heartbeat_server ) , <EOL> ChainedOpenSSLContextFactory ( SSL_KEY , SSL_CERT ) , interface = interface ) <EOL> else : <EOL> reactor . listenTCP ( HEARTBEATPORT , WebSocketFactory ( heartbeat_server ) , interface = interface ) <EOL> btcPrice = BtcPrice ( ) <EOL> btcPrice . start ( ) <EOL> KeyChain ( db , start_server , heartbeat_server ) <EOL> reactor . run ( ) <EOL> btcPrice . closethread ( ) <EOL> btcPrice . join ( <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> class OpenBazaard ( Daemon ) : <EOL> def run ( self , * args ) : <EOL> run ( * args ) <EOL> class Parser ( object ) : <EOL> def __init__ ( self , daemon ) : <EOL> self . daemon = daemon <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' , <EOL> usage = '''<STR_LIT>''' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( sys . argv [ <NUM_LIT:1> : <NUM_LIT:2> ] ) <EOL> if not hasattr ( self , args . command ) : <EOL> parser . print_help ( ) <EOL> exit ( <NUM_LIT:1> ) <EOL> getattr ( self , args . command ) ( ) <EOL> def start ( self ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = "<STR_LIT>" , <EOL> usage = "<STR_LIT>" <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = "<STR_LIT:info>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , default = [ "<STR_LIT:127.0.0.1>" ] , action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = "<STR_LIT>" , default = <NUM_LIT> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = "<STR_LIT>" , default = <NUM_LIT> ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = "<STR_LIT>" , default = <NUM_LIT> ) <EOL> parser . add_argument ( '<STR_LIT>' , help = "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> args = parser . parse_args ( sys . argv [ <NUM_LIT:2> : ] ) <EOL> self . print_splash_screen ( ) <EOL> unix = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if args . port : <EOL> port = int ( args . port ) <EOL> else : <EOL> port = <NUM_LIT> if not args . testnet else <NUM_LIT> <EOL> if args . daemon and platform . system ( ) . lower ( ) in unix : <EOL> self . daemon . pidfile = "<STR_LIT>" + args . pidfile <EOL> self . daemon . start ( args . testnet , args . loglevel , port , args . allowip , <EOL> int ( args . restapiport ) , int ( args . websocketport ) , <EOL> int ( args . heartbeatport ) , time . time ( ) ) <EOL> else : <EOL> run ( args . testnet , args . loglevel , port , args . allowip , <EOL> int ( args . restapiport ) , int ( args . websocketport ) , <EOL> int ( args . heartbeatport ) , time . time ( ) ) <EOL> def stop ( self ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = "<STR_LIT>" , <EOL> usage = '''<STR_LIT>''' ) <EOL> args = parser . parse_args ( sys . argv [ <NUM_LIT:2> : ] ) <EOL> print "<STR_LIT>" <EOL> self . daemon . stop ( ) <EOL> def restart ( self ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = "<STR_LIT>" , <EOL> usage = '''<STR_LIT>''' ) <EOL> parser . parse_args ( sys . argv [ <NUM_LIT:2> : ] ) <EOL> print "<STR_LIT>" <EOL> self . daemon . restart ( ) <EOL> @ staticmethod <EOL> def print_splash_screen ( ) : <EOL> OKBLUE = '<STR_LIT>' <EOL> ENDC = '<STR_LIT>' <EOL> print "<STR_LIT>" + OKBLUE + "<STR_LIT>" + ENDC <EOL> print "<STR_LIT>" + OKBLUE + "<STR_LIT>" + ENDC <EOL> print "<STR_LIT>" + OKBLUE + "<STR_LIT>" + ENDC <EOL> print "<STR_LIT>" + OKBLUE + "<STR_LIT>" + ENDC <EOL> print "<STR_LIT>" + OKBLUE + "<STR_LIT>" + ENDC <EOL> print "<STR_LIT>" + OKBLUE + "<STR_LIT>" + ENDC <EOL> print <EOL> print "<STR_LIT>" <EOL> Parser ( OpenBazaard ( '<STR_LIT>' ) ) </s>
