Add an option to not error out when only a local IP address can be found.
[quix0rs-apt-p2p.git] / apt_dht / apt_dht_conf.py
1
2 import os, sys
3 from ConfigParser import SafeConfigParser
4
5 from twisted.python import log, versions
6
7 class ConfigError(Exception):
8     def __init__(self, message):
9         self.message = message
10     def __str__(self):
11         return repr(self.message)
12
13 version = versions.Version('apt-dht', 0, 0, 0)
14 home = os.path.expandvars('${HOME}')
15 if home == '${HOME}' or not os.path.isdir(home):
16     home = os.path.expanduser('~')
17     if not os.path.isdir(home):
18         home = os.path.abspath(os.path.dirname(sys.argv[0]))
19
20 DEFAULTS = {
21
22     # Port to listen on for all requests (TCP and UDP)
23     'PORT': '9977',
24     
25     # Directory to store the downloaded files in
26     'CACHE_DIR': home + '/.apt-dht/cache',
27     
28     # Other directories containing packages to share with others
29     # WARNING: all files in these directories will be hashed and available
30     #          for everybody to download
31     'OTHER_DIRS': """""",
32     
33     # User name to try and run as
34     'USERNAME': '',
35     
36     # Whether it's OK to use an IP addres from a known local/private range
37     'LOCAL_OK': 'no',
38
39     # Which DHT implementation to use.
40     # It must be possile to do "from <DHT>.DHT import DHT" to get a class that
41     # implements the IDHT interface.
42     'DHT': 'apt_dht_Khashmir',
43
44     # Whether to only run the DHT (for providing only a bootstrap node)
45     'DHT-ONLY': 'no',
46 }
47
48 DHT_DEFAULTS = {
49     # bootstrap nodes to contact to join the DHT
50     'BOOTSTRAP': """www.camrdale.org:9977
51         steveholt.hopto.org:9976""",
52     
53     # whether this node is a bootstrap node
54     'BOOTSTRAP_NODE': "no",
55     
56     # Kademlia "K" constant, this should be an even number
57     'K': '8',
58     
59     # SHA1 is 160 bits long
60     'HASH_LENGTH': '160',
61     
62     # checkpoint every this many seconds
63     'CHECKPOINT_INTERVAL': '15m', # fifteen minutes
64     
65     ### SEARCHING/STORING
66     # concurrent xmlrpc calls per find node/value request!
67     'CONCURRENT_REQS': '4',
68     
69     # how many hosts to post to
70     'STORE_REDUNDANCY': '3',
71     
72     ###  ROUTING TABLE STUFF
73     # how many times in a row a node can fail to respond before it's booted from the routing table
74     'MAX_FAILURES': '3',
75     
76     # never ping a node more often than this
77     'MIN_PING_INTERVAL': '15m', # fifteen minutes
78     
79     # refresh buckets that haven't been touched in this long
80     'BUCKET_STALENESS': '1h', # one hour
81     
82     ###  KEY EXPIRER
83     # time before expirer starts running
84     'KEINITIAL_DELAY': '15s', # 15 seconds - to clean out old stuff in persistent db
85     
86     # time between expirer runs
87     'KE_DELAY': '20m', # 20 minutes
88     
89     # expire entries older than this
90     'KE_AGE': '1h', # 60 minutes
91     
92     # whether to spew info about the requests/responses in the protocol
93     'SPEW': 'yes',
94 }
95
96 class AptDHTConfigParser(SafeConfigParser):
97     """
98     Adds 'gettime' to ConfigParser to interpret the suffixes.
99     """
100     time_multipliers={
101         's': 1,    #seconds
102         'm': 60,   #minutes
103         'h': 3600, #hours
104         'd': 86400,#days
105         }
106
107     def gettime(self, section, option):
108         mult = 1
109         value = self.get(section, option)
110         if len(value) == 0:
111             raise ConfigError("Configuration parse error: [%s] %s" % (section, option))
112         suffix = value[-1].lower()
113         if suffix in self.time_multipliers.keys():
114             mult = self.time_multipliers[suffix]
115             value = value[:-1]
116         return int(value)*mult
117     def getstring(self, section, option):
118         return self.get(section,option)
119     def getstringlist(self, section, option):
120         return self.get(section,option).split()
121     def optionxform(self, option):
122         return option.upper()
123
124 config = AptDHTConfigParser(DEFAULTS)
125 config.add_section(config.get('DEFAULT', 'DHT'))
126 for k in DHT_DEFAULTS:
127     config.set(config.get('DEFAULT', 'DHT'), k, DHT_DEFAULTS[k])