Skip to content

Commit

Permalink
Merge pull request HelloZeroNet#376 from TheNain38/patch-1
Browse files Browse the repository at this point in the history
Enhance Zeroname-local so it works with non-standard rpcport
  • Loading branch information
HelloZeroNet committed Mar 19, 2016
2 parents ab19dba + c70d98d commit 3128b23
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 103 deletions.
86 changes: 43 additions & 43 deletions plugins/disabled-Zeroname-local/SiteManagerPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,57 @@

@PluginManager.registerTo("SiteManager")
class SiteManagerPlugin(object):
def load(self):
super(SiteManagerPlugin, self).load()
def load(self):
super(SiteManagerPlugin, self).load()

# Checks if its a valid address
def isAddress(self, address):
if self.isDomain(address):
return True
else:
return super(SiteManagerPlugin, self).isAddress(address)
# Checks if its a valid address
def isAddress(self, address):
if self.isDomain(address):
return True
else:
return super(SiteManagerPlugin, self).isAddress(address)


# Return: True if the address is domain
def isDomain(self, address):
return re.match("(.*?)([A-Za-z0-9_-]+\.[A-Za-z0-9]+)$", address)
# Return: True if the address is domain
def isDomain(self, address):
return re.match("(.*?)([A-Za-z0-9_-]+\.[A-Za-z0-9]+)$", address)


# Resolve domain
# Return: The address or None
def resolveDomain(self, domain):
return lookupDomain(domain)
# Resolve domain
# Return: The address or None
def resolveDomain(self, domain):
return lookupDomain(domain)


# Return or create site and start download site files
# Return: Site or None if dns resolve failed
def need(self, address, all_file=True):
if self.isDomain(address): # Its looks like a domain
address_resolved = self.resolveDomain(address)
if address_resolved:
address = address_resolved
else:
return None
return super(SiteManagerPlugin, self).need(address, all_file)
# Return or create site and start download site files
# Return: Site or None if dns resolve failed
def need(self, address, all_file=True):
if self.isDomain(address): # Its looks like a domain
address_resolved = self.resolveDomain(address)
if address_resolved:
address = address_resolved
else:
return None
return super(SiteManagerPlugin, self).need(address, all_file)


# Return: Site object or None if not found
def get(self, address):
if self.sites == None: # Not loaded yet
self.load()
if self.isDomain(address): # Its looks like a domain
address_resolved = self.resolveDomain(address)
if address_resolved: # Domain found
site = self.sites.get(address_resolved)
if site:
site_domain = site.settings.get("domain")
if site_domain != address:
site.settings["domain"] = address
else: # Domain not found
site = self.sites.get(address)
# Return: Site object or None if not found
def get(self, address):
if self.sites == None: # Not loaded yet
self.load()
if self.isDomain(address): # Its looks like a domain
address_resolved = self.resolveDomain(address)
if address_resolved: # Domain found
site = self.sites.get(address_resolved)
if site:
site_domain = site.settings.get("domain")
if site_domain != address:
site.settings["domain"] = address
else: # Domain not found
site = self.sites.get(address)

else: # Access by site address
site = self.sites.get(address)
return site
else: # Access by site address
site = self.sites.get(address)
return site

68 changes: 34 additions & 34 deletions plugins/disabled-Zeroname-local/UiRequestPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,38 @@

@PluginManager.registerTo("UiRequest")
class UiRequestPlugin(object):
def __init__(self, *args, **kwargs):
from Site import SiteManager
self.site_manager = SiteManager.site_manager
super(UiRequestPlugin, self).__init__(*args, **kwargs)


# Media request
def actionSiteMedia(self, path):
match = re.match("/media/(?P<address>[A-Za-z0-9]+\.[A-Za-z0-9\.]+)(?P<inner_path>/.*|$)", path)
if match: # Its a valid domain, resolve first
domain = match.group("address")
address = self.site_manager.resolveDomain(domain)
if address:
path = "/media/"+address+match.group("inner_path")
return super(UiRequestPlugin, self).actionSiteMedia(path) # Get the wrapper frame output


# Is mediarequest allowed from that referer
def isMediaRequestAllowed(self, site_address, referer):
referer_path = re.sub("http[s]{0,1}://.*?/", "/", referer).replace("/media", "") # Remove site address
referer_path = re.sub("\?.*", "", referer_path) # Remove http params

if self.isProxyRequest(): # Match to site domain
referer = re.sub("^http://zero[/]+", "http://", referer) # Allow /zero access
referer_site_address = re.match("http[s]{0,1}://(.*?)(/|$)", referer).group(1)
else: # Match to request path
referer_site_address = re.match("/(?P<address>[A-Za-z0-9\.]+)(?P<inner_path>/.*|$)", referer_path).group("address")

if referer_site_address == site_address: # Referer site address as simple address
return True
elif self.site_manager.resolveDomain(referer_site_address) == site_address: # Referer site address as dns
return True
else: # Invalid referer
return False
def __init__(self, *args, **kwargs):
from Site import SiteManager
self.site_manager = SiteManager.site_manager
super(UiRequestPlugin, self).__init__(*args, **kwargs)


# Media request
def actionSiteMedia(self, path):
match = re.match("/media/(?P<address>[A-Za-z0-9]+\.[A-Za-z0-9\.]+)(?P<inner_path>/.*|$)", path)
if match: # Its a valid domain, resolve first
domain = match.group("address")
address = self.site_manager.resolveDomain(domain)
if address:
path = "/media/"+address+match.group("inner_path")
return super(UiRequestPlugin, self).actionSiteMedia(path) # Get the wrapper frame output


# Is mediarequest allowed from that referer
def isMediaRequestAllowed(self, site_address, referer):
referer_path = re.sub("http[s]{0,1}://.*?/", "/", referer).replace("/media", "") # Remove site address
referer_path = re.sub("\?.*", "", referer_path) # Remove http params

if self.isProxyRequest(): # Match to site domain
referer = re.sub("^http://zero[/]+", "http://", referer) # Allow /zero access
referer_site_address = re.match("http[s]{0,1}://(.*?)(/|$)", referer).group(1)
else: # Match to request path
referer_site_address = re.match("/(?P<address>[A-Za-z0-9\.]+)(?P<inner_path>/.*|$)", referer_path).group("address")

if referer_site_address == site_address: # Referer site address as simple address
return True
elif self.site_manager.resolveDomain(referer_site_address) == site_address: # Referer site address as dns
return True
else: # Invalid referer
return False

55 changes: 29 additions & 26 deletions plugins/disabled-Zeroname-local/domainLookup.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import time, json, os, sys, re, socket, json
import time, json, os, sys, re, socket

# Either returns domain's address or none if it doesn't exist
# Supports subdomains and .bit on the end
def lookupDomain(domain):
domain = domain.lower()
domain = domain.lower()

#remove .bit on end
if domain[-4:] == ".bit":
domain = domain[0:-4]
#remove .bit on end
if domain[-4:] == ".bit":
domain = domain[0:-4]

#check for subdomain
if domain.find(".") != -1:
subdomain = domain[0:domain.find(".")]
domain = domain[domain.find(".")+1:]
else:
subdomain = ""
#check for subdomain
if domain.find(".") != -1:
subdomain = domain[0:domain.find(".")]
domain = domain[domain.find(".")+1:]
else:
subdomain = ""

try:
domain_object = rpc.name_show("d/"+domain)
except:
#domain doesn't exist
return None
try:
domain_object = rpc.name_show("d/"+domain)
except:
#domain doesn't exist
return None

domain_json = json.loads(domain_object['value'])
domain_json = json.loads(domain_object["value"])

try:
domain_address = domain_json["zeronet"][subdomain]
except:
#domain exists but doesn't have any zeronet value
return None
try:
domain_address = domain_json["zeronet"][subdomain]
except:
#domain exists but doesn't have any zeronet value
return None

return domain_address
return domain_address

# Loading config...

Expand All @@ -47,8 +47,11 @@ def lookupDomain(domain):
namecoin_conf = open(namecoin_location + "namecoin.conf").read()

# Connecting to RPC
rpc_user = re.search("rpcuser=(.*)$", namecoin_conf, re.M).group(1)
rpc_pass = re.search("rpcpassword=(.*)$", namecoin_conf, re.M).group(1)
rpc_url = "http://%s:%[email protected]:8336" % (rpc_user, rpc_pass)
rpc_user = re.search(r"^\s*rpcuser\s*=(\S+)\s*(?:#.*)?$", namecoin_conf, re.M).group(1)
rpc_pass = re.search(r"^\s*rpcpassword\s*=(\S+)\s*(?:#.*)?$", namecoin_conf, re.M).group(1)
rpc_port = re.search(r"^\s*rpcport\s*=(\d{1,5})\s*(?:#.*)?$", namecoin_conf, re.M)
rpc_port = rpc_port.group(1) if rpc_port is not None else "8336"

rpc_url = "http://%s:%[email protected]:%s" % (rpc_user, rpc_pass, rpc_port)

rpc = AuthServiceProxy(rpc_url, timeout=60*5)

0 comments on commit 3128b23

Please sign in to comment.