In [1]:
#!/usr/bin/python3
import argparse
import json
import urllib.request
import urllib.parse
import http.client
import socket
import ssl
import sys
import yaml
from urllib.error import HTTPError, URLError
from string import whitespace
# Info about requestable data can be found at http://i2p2.de/i2pcontrol.html & http://www.i2p2.de/ratestats.html
address = "localhost" # Default I2PControl Address
port = 7650 # Default I2PControl Port
apiPassword = "itoopie" # Default I2PControl password
## Do not edit below
apiVersion = 2 # Default API Version
msgId = 1
token = None
def checkToken():
global token
if (token == None):
print(token)
token = getToken()
if (token == None):
print("Unable to login. Quitting..")
sys.exit()
def getToken():
loginStr = "{\"id\":" + str(msgId) + ", \"method\":\"Authenticate\",\"params\":{\"API\":" + str(apiVersion) + ", \"Password\":\"" + apiPassword + "\"}, \"jsonrpc\":\"2.0\"}"
try:
jsonResp = sendMsg(loginStr)
print(jsonResp)
return jsonResp.get("result").get("Token")
except HTTPError as e:
print("HTTPError: %s" % e.reason)
except URLError as e:
print("URLError: %s" % e.reason)
def sendMsg(jsonStr):
global msgId
https_handler = UnauthenticatedHTTPSHandler()
url_opener = urllib.request.build_opener(https_handler)
json_data = jsonStr.encode('utf-8')
handle = url_opener.open("https://"+address+":"+ str(port) + "/jsonrpc", data = json_data)
response = str(handle.read().decode('utf-8'))
handle.close()
msgId = msgId + 1;
jsonResp = json.loads(response)#
if ("error" in jsonResp):
print("Remote server: I2PControl Error: " + str(jsonResp.get("error").get("code")) + ", " + jsonResp.get("error").get("message"))
sys.exit()
return jsonResp
###
# Overrides the version in httplib so that we can ignore server certificate authenticity
# and use SSLv3
###
class UnauthenticatedHTTPSConnection(http.client.HTTPSConnection):
def connect(self):
#
sock = socket.create_connection((self.host, self.port), self.timeout)
if self._tunnel_host:
self.sock = sock
self._tunnel()
self.sock = ssl.wrap_socket(sock,
cert_reqs=ssl.CERT_NONE,
ssl_version=ssl.PROTOCOL_SSLv23)
###
# HTTPS handler which uses SSLv3 and ignores server cert authenticity
###
class UnauthenticatedHTTPSHandler(urllib.request.HTTPSHandler):
def __init__(self, connection_class = UnauthenticatedHTTPSConnection):
self.specialized_conn_class = connection_class
urllib.request.HTTPSHandler.__init__(self)
def https_open(self, req):
return self.do_open(self.specialized_conn_class, req)
In [2]:
token = getToken()
i = 2
In [3]:
print(token)
Request: { "id": "id", "method": "Method-name", "params": { "Param-key-1": "param-value-1", "Param-key-2": "param-value-2", "Token": "actual token" }, "jsonrpc": "2.0" }
Response: { "id": "id", "result": { "Result-key-1": "result-value-1", "Result-key-2": "result-value-2" }, "jsonrpc": "2.0" }
In [4]:
echo = { 'id' : i,
'method' : 'Echo',
'params' : {
'Echo' : 'echostring',
'Token' : token
},
'jsonrpc' : '2.0'
}
result = sendMsg(json.dumps(echo))
print(result['result']['Result'])
i = result['id']
In [5]:
router_info = { 'id' : i,
'method' : 'RouterInfo',
'params' : {
'i2p.router.uptime' : '',
'i2p.router.version' : '',
'i2p.router.status' : '',
'i2p.router.netdb.knownpeers' : '',
'i2p.router.netdb.activepeers' : '',
'i2p.router.net.bw.inbound.1s' : '',
'i2p.router.net.bw.outbound.1s' : '',
'i2p.router.net.status' : '',
'i2p.router.net.tunnels.participating' : '',
'i2p.router.net.tunnels.successrate' : '',
'i2p.router.net.total.received.bytes' : '',
'i2p.router.net.total.sent.bytes' : '',
'Token' : token
},
'jsonrpc' : '2.0'
}
result = sendMsg(json.dumps(router_info))
print(result)
i = result['id'] + 1
In [ ]:
In [ ]: