Remove useless sync api
parent
84a8edfc34
commit
63ce54f133
|
@ -20,6 +20,7 @@ def serve_directory(server_address, path):
|
|||
def main(args):
|
||||
sam_address = i2plib.get_sam_address()
|
||||
server_address = ('127.0.0.1', i2plib.utils.get_free_port())
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
if not os.path.isdir(args.web_directory):
|
||||
raise OSError("No such directory {}".format(args.web_directory))
|
||||
|
@ -27,7 +28,7 @@ def main(args):
|
|||
if args.key:
|
||||
dest = i2plib.Destination(path=args.key, has_private_key=True)
|
||||
else:
|
||||
dest = i2plib.utils.get_new_destination(sam_address=sam_address)
|
||||
dest = loop.run_until_complete(i2plib.new_destination(sam_address=sam_address, loop=loop))
|
||||
|
||||
logging.info("Listening: {}.b32.i2p".format(dest.base32))
|
||||
logging.info("Server: {}:{}".format(server_address[0], server_address[1]))
|
||||
|
@ -38,7 +39,6 @@ def main(args):
|
|||
http_server_thread.daemon = True
|
||||
http_server_thread.start()
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
tunnel = i2plib.ServerTunnel(server_address,
|
||||
loop=loop, destination=dest, sam_address=sam_address)
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
import sys
|
||||
|
||||
import i2plib
|
||||
from i2plib.sam import lookup, get_socket
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) == 2:
|
||||
domain = sys.argv[1]
|
||||
try:
|
||||
r = lookup(get_socket(), domain)
|
||||
print("Domain: \n{}\n".format(domain))
|
||||
print("Full destination: \n{}\n".format(r.base64))
|
||||
print("B32 address: \n{}.b32.i2p\n".format(r.base32))
|
||||
except i2plib.InvalidKey:
|
||||
print("Domain not found")
|
||||
else:
|
||||
print("Usage: resolve.py [.i2p domain]")
|
||||
exit()
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
import sys
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import i2plib
|
||||
from i2plib.sam import generate_session_id, lookup, get_socket, StreamSession
|
||||
|
||||
def http_get(url, sam_address):
|
||||
url = urlparse(url)
|
||||
s_id = generate_session_id()
|
||||
ss = StreamSession(session_id=s_id, sam_address=sam_address)
|
||||
|
||||
dest = lookup(get_socket(sam_address=sam_address), url.netloc)
|
||||
|
||||
# connect to remote server, returns socket to use
|
||||
client_sock = ss.connect(dest.base64)
|
||||
client_sock.send("GET {} HTTP/1.0\nHost: {}\r\n\r\n".format(
|
||||
url.path, url.netloc).encode())
|
||||
|
||||
buflen, resp = 4096, b""
|
||||
while 1:
|
||||
data = client_sock.recv(buflen)
|
||||
if len(data) > 0:
|
||||
resp += data
|
||||
else:
|
||||
break
|
||||
|
||||
resp = resp.decode()
|
||||
try:
|
||||
return resp.split("\r\n\r\n", 1)[1]
|
||||
except IndexError:
|
||||
return resp
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) == 2:
|
||||
url = sys.argv[1] if sys.argv[1].startswith("http://") else "//"+sys.argv[1]
|
||||
else:
|
||||
url = "http://irkvgdnlc6tidoqomre4qr7q4w4qcjfyvbovatgyolk6d4uvcyha.b32.i2p/uploads/BSD"
|
||||
|
||||
r = http_get(url, i2plib.get_sam_address())
|
||||
|
||||
print(r)
|
|
@ -1,4 +1,3 @@
|
|||
import socket
|
||||
from base64 import b64decode, b64encode, b32encode
|
||||
from hashlib import sha256
|
||||
import struct
|
||||
|
@ -6,7 +5,6 @@ import random
|
|||
import string
|
||||
import re
|
||||
|
||||
from .exceptions import SAM_EXCEPTIONS
|
||||
|
||||
I2P_B64_CHARS = "-~"
|
||||
|
||||
|
@ -51,26 +49,6 @@ class Message(object):
|
|||
def __repr__(self):
|
||||
return self._reply_string
|
||||
|
||||
def get_socket(sam_address=None):
|
||||
"""Return new SAM socket"""
|
||||
sam_address = sam_address or DEFAULT_ADDRESS
|
||||
sam_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sam_socket.connect(sam_address)
|
||||
sam_socket.send(hello(DEFAULT_MIN_VER, DEFAULT_MAX_VER))
|
||||
a = get_response(sam_socket)
|
||||
if a.ok:
|
||||
return sam_socket
|
||||
else:
|
||||
raise SAM_EXCEPTIONS[a["RESULT"]]()
|
||||
|
||||
def lookup(sam_socket, name):
|
||||
"""Lookup destination by name"""
|
||||
sam_socket.send(naming_lookup(name))
|
||||
a = get_response(sam_socket)
|
||||
if a.ok:
|
||||
return Destination(a["VALUE"])
|
||||
else:
|
||||
raise SAM_EXCEPTIONS[a["RESULT"]]()
|
||||
|
||||
def generate_session_id(length=6):
|
||||
"""Generate random session id"""
|
||||
|
@ -78,10 +56,6 @@ def generate_session_id(length=6):
|
|||
sid = [rand.choice(string.ascii_letters) for _ in range(length)]
|
||||
return "i2plib-" + "".join(sid)
|
||||
|
||||
def get_response(sam_socket):
|
||||
"""Read message from SAM API"""
|
||||
return Message(sam_socket.recv(SAM_BUFSIZE))
|
||||
|
||||
|
||||
# SAM request messages
|
||||
|
||||
|
@ -180,47 +154,3 @@ class PrivateKey(object):
|
|||
#: Base64 encoded private key
|
||||
self.base64 = data if type(data) == str else i2p_b64encode(data)
|
||||
|
||||
class StreamSession(object):
|
||||
|
||||
def __init__(self, sam_address=DEFAULT_ADDRESS, \
|
||||
session_id=None, destination=TRANSIENT_DESTINATION, options="", \
|
||||
min_ver=DEFAULT_MIN_VER, max_ver=DEFAULT_MAX_VER):
|
||||
self.sam_address = sam_address
|
||||
self.min_ver = min_ver
|
||||
self.max_ver = max_ver
|
||||
self.session_id = session_id or generate_session_id()
|
||||
self._session_socket = get_socket(self.sam_address)
|
||||
self._session_socket.send(
|
||||
session_create("STREAM", self.session_id, destination, options))
|
||||
a = get_response(self._session_socket)
|
||||
|
||||
if a.ok:
|
||||
self.destination = a["DESTINATION"]
|
||||
else:
|
||||
raise SAM_EXCEPTIONS[a["RESULT"]]()
|
||||
|
||||
def _get_socket(self):
|
||||
"""New socket for this session"""
|
||||
return get_socket(self.sam_address)
|
||||
|
||||
def connect(self, dest):
|
||||
"""Return new I2P stream to destination"""
|
||||
sam_socket = self._get_socket()
|
||||
sam_socket.send(stream_connect(self.session_id, dest, silent="false"))
|
||||
a = get_response(sam_socket)
|
||||
if a.ok:
|
||||
return sam_socket
|
||||
else:
|
||||
raise SAM_EXCEPTIONS[a["RESULT"]]()
|
||||
|
||||
def accept(self):
|
||||
"""Return new accepting socket"""
|
||||
sam_socket = self._get_socket()
|
||||
sam_socket.send(stream_accept(self.session_id, silent="false"))
|
||||
return sam_socket
|
||||
|
||||
def forward(self, session_id, port, options=""):
|
||||
sam_socket = self._get_socket()
|
||||
sam_socket.send(stream_forward(self.session_id, port, options))
|
||||
return sam_socket
|
||||
|
||||
|
|
|
@ -31,12 +31,3 @@ def get_sam_address():
|
|||
value = os.getenv("I2P_SAM_ADDRESS")
|
||||
return address_from_string(value) if value else i2plib.sam.DEFAULT_ADDRESS
|
||||
|
||||
def get_new_destination(sam_address=i2plib.sam.DEFAULT_ADDRESS,
|
||||
sig_type=i2plib.sam.Destination.default_sig_type):
|
||||
"""Generates new I2P destination of a chosen signature type"""
|
||||
sam_socket = i2plib.sam.get_socket(sam_address)
|
||||
sam_socket.send(i2plib.sam.dest_generate(sig_type))
|
||||
a = i2plib.sam.get_response(sam_socket)
|
||||
sam_socket.close()
|
||||
return i2plib.sam.Destination(a['PRIV'], has_private_key=True)
|
||||
|
||||
|
|
Loading…
Reference in New Issue