Get I2P/clearnet url conversion working

For reference: a context processor was used to add the function in because this
needs to be evaluated on every request, but the output from a template filter
of a hard-coded string gets cached.
This commit is contained in:
str4d
2012-12-03 10:45:35 +00:00
parent 9d6258e5cc
commit ceb822e2f4
2 changed files with 29 additions and 23 deletions

View File

@@ -106,27 +106,33 @@ def restructuredtext(value):
parts = publish_parts(source=value, writer_name="html")
return parts['html_body']
# Convert an I2P url to an equivalent clearnet one
i2ptoclear = {
'www.i2p2.i2p': 'www.i2p2.de',
'forum.i2p': 'forum.i2p2.de',
'trac.i2p2.i2p': 'trac.i2p2.de',
}
@app.template_filter('i2pconv')
def convert_url_to_clearnet(value):
if not value.endswith('.i2p'):
# The url being passed in isn't an I2P url, so just return it
return value
if request.headers.get('X-I2P-Desthash') and not request.headers.get('X-Forwarded-Server'):
# The request is from within I2P, so use I2P url
return value
# The request is either directly from clearnet or through an inproxy
try:
# Return the known clearnet url corresponding to the I2P url
return i2ptoclear[value]
except KeyError:
# The I2P site has no known clearnet address, so use an inproxy
return value + '.to'
####################
# Context processors
@app.context_processor
def utility_processor():
# Convert an I2P url to an equivalent clearnet one
i2ptoclear = {
'www.i2p2.i2p': 'www.i2p2.de',
'forum.i2p': 'forum.i2p2.de',
'trac.i2p2.i2p': 'trac.i2p2.de',
}
def convert_url_to_clearnet(value):
if not value.endswith('.i2p'):
# The url being passed in isn't an I2P url, so just return it
return value
if request.headers.get('X-I2P-Desthash') and not request.headers.get('X-Forwarded-Server'):
# The request is from within I2P, so use I2P url
return value
# The request is either directly from clearnet or through an inproxy
try:
# Return the known clearnet url corresponding to the I2P url
return i2ptoclear[value]
except KeyError:
# The I2P site has no known clearnet address, so use an inproxy
return value + '.to'
return dict(i2pconv=convert_url_to_clearnet)
################

View File

@@ -60,8 +60,8 @@
<ul>
<li><a href="{{ site_url('support/faq') }}"><span>{{ _('FAQ') }}</span></a></li>
<li><a href="{{ site_url('support/contact') }}"><span>{{ _('Contact us') }}</span></a></li>
<li><a href="http://forum.i2p2.de/"><span>{{ _('Forums') }}</span></a></li>
<li><a href="http://trac.i2p2.i2p/"><span>Trac</span></a></li>
<li><a href="http://{{ i2pconv('forum.i2p') }}/"><span>{{ _('Forums') }}</span></a></li>
<li><a href="http://{{ i2pconv('trac.i2p2.i2p') }}/"><span>Trac</span></a></li>
</ul>
</li>
<li class="has-sub"><a href="#"><span>{{ _('Develop') }}</span></a>