Commit feb48777 authored by Yury's avatar Yury

none auth

parent ed702fe5
Pipeline #86 passed with stages
in 14 minutes and 38 seconds
stages:
- unittests
- build
- test_install
tests:
stage: unittests
......@@ -17,6 +18,6 @@ pypi_build:
- apk update
- apk add --update --no-cache --virtual .build-deps alpine-sdk python3-dev musl-dev postgresql-dev libffi-dev
- pip3 install -U setuptools pip twine
- python3 generate_setup.py
- python3 setup.py sdist
- twine upload dist/*
\ No newline at end of file
- twine upload dist/*
- pip3 install neptune_resolver_redis
from .core import Authenticator
\ No newline at end of file
from .core import Authenticator
from .core import Authenticator
\ No newline at end of file
class Authenticator:
method = 0
def __new__(cls, *args, **kwargs):
if not hasattr(cls, 'instance'):
cls.instance = super().__new__(cls)
return cls.instance
def __init__(self, **data):
pass
async def authenticate(self, data):
return True
\ No newline at end of file
AUTHENTICATION_METHODS = ['saturn.auth.dict']
AUTHENTICATION_METHODS = ['saturn.auth.none']
SATURN_AUTH_DICT = {
'test_user': 'Test_password'
......
......@@ -11,17 +11,21 @@ class Dispatcher:
self.state = state.NotAuthenticated()
async def handle(self, data):
if (not isinstance(self.state, state.Connected) or not isinstance(self.state, state.Authenticated)) and \
data[0] == 5 and len(data) == data[1] + 2:
self.state = state.NotAuthenticated()
# if (not isinstance(self.state, state.Connected) or not isinstance(self.state, state.Authenticated)) and \
# data[0] == 5 and len(data) == data[1] + 2:
# self.state = state.NotAuthenticated()
if isinstance(self.state, state.Connected):
self.client_transport.write(data)
elif isinstance(self.state, state.NotAuthenticated):
return SocksHello(self, data).reply(self.server)
elif isinstance(self.state, state.WaitingAuthenticationData):
elif isinstance(self.state, state.WaitingAuthenticationData) and data[0] == 1:
return await SocksAuthenticate(self, data).authenticate()
elif isinstance(self.state, state.Authenticated):
elif isinstance(self.state, state.Authenticated) and data[0] == 5:
request = SocksTcpRequest(self, data)
if request.cmd == 1:
result = bytes(await request.connect())
return result
def reply(self, data):
self.server_protocol.transport.write(data)
\ No newline at end of file
......@@ -25,6 +25,8 @@ class Server:
for m in self.auth_methods:
if m.method == method:
return await m.authenticate(*args, **kwargs)
else:
return False
def start(self):
loop = asyncio.new_event_loop()
......
......@@ -8,12 +8,15 @@ class TcpClient(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
print('connected')
self.on_connect.set_result(True)
def data_received(self, data):
self.dispatcher.server_protocol.transport.write(data)
print('got data')
self.dispatcher.reply(data)
def connection_lost(self, exc):
print('con lost')
self.dispatcher.server_protocol.transport.close()
def send(self, data):
......
import asyncio
from saturn.dispatcher import Dispatcher
from saturn import state
class Socks5TcpServer(asyncio.Protocol):
......@@ -14,7 +14,6 @@ class Socks5TcpServer(asyncio.Protocol):
self.dispatcher = Dispatcher(self.server, self.loop, self)
def data_received(self, data):
print(data)
asyncio.Task(self.async_data_handler(data))
def connection_lost(self, exc) -> None:
......@@ -24,7 +23,12 @@ class Socks5TcpServer(asyncio.Protocol):
def async_data_handler(self, data: bytes) -> None:
reply = yield from self.dispatcher.handle(data)
if reply:
print(self.dispatcher, self.dispatcher.state, data, reply)
self.transport.write(reply)
else:
if not isinstance(self.dispatcher.state, state.Connected):
print('fail', self.dispatcher, self.dispatcher.state, data)
return
async def start_server(self, host='0.0.0.0', port=8080):
server = await self.loop.create_server(
......
from ipaddress import IPv6Address, IPv4Address
from ipaddress import IPv6Address, IPv4Address, AddressValueError
from saturn import state
from saturn.protocol.client_tcp import TcpClient
class SocksPacket:
def __init__(self, data):
assert data[0] == 5
# assert data[0] == 5
self.ver = 5
......@@ -19,9 +19,9 @@ class SocksHello(SocksPacket):
def reply(self, server):
for m in self.dispatcher.server.server_auth_methods:
if m in self.methods:
self.dispatcher.state = state.WaitingAuthenticationData(method=m)
self.dispatcher.state = state.WaitingAuthenticationData(method=m) if not m == 0 else state.Authenticated()
return self.ver.to_bytes(1, byteorder='big') + int.to_bytes(m, 1, byteorder='big')
return self.ver.to_bytes(1, byteorder='big') + int.to_bytes(5, 1, byteorder='big')
return self.ver.to_bytes(1, byteorder='big') + int.to_bytes(255, 1, byteorder='big')
class SocksAuthenticate:
......@@ -34,7 +34,7 @@ class SocksAuthenticate:
if await self.server.auth(self.dispatcher.state.method, self.data):
self.dispatcher.state = state.Authenticated()
return int(1).to_bytes(1, byteorder='big') + int(0).to_bytes(1, byteorder='big')
return int(1).to_bytes(1, byteorder='big') + int(1).to_bytes(1, byteorder='big')
return int(1).to_bytes(1, byteorder='big') + int(10).to_bytes(1, byteorder='big')
class SocksTcpRequest:
......@@ -54,14 +54,21 @@ class SocksTcpRequest:
self.dst_port = int.from_bytes(data[-2:], byteorder='big')
async def connect(self):
assert not isinstance(self.dispatcher.state, state.Connected)
on_connect = self.dispatcher.loop.create_future()
self.dispatcher.client_transport, self.client_protocol = await self.dispatcher.loop.create_connection(
lambda: TcpClient(self.dispatcher, on_connect),
str(self.dst_addr), self.dst_port)
try:
self.dispatcher.client_transport, self.client_protocol = await self.dispatcher.loop.create_connection(
lambda: TcpClient(self.dispatcher, on_connect),
str(self.dst_addr), self.dst_port)
except OSError as e :
print(e.errno)
return SocksTcpReply(self.dispatcher, 5, 1, 0, 1, int(IPv4Address('0.0.0.0')), 1)
self.dispatcher.connected = True
await on_connect
self.dispatcher.state = state.Connected()
return SocksTcpReply(self.dispatcher, 5, 0, 0, 1, int(IPv4Address('192.168.1.45')), 8080)
sock_info = self.dispatcher.client_transport.get_extra_info('socket').getsockname()
return SocksTcpReply(self.dispatcher, 5, 0, 0, 1, int(IPv4Address('80.211.196.34')), 8081)
async def bind(self):
pass
......
import socks
for _ in range(500):
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, "localhost", 8081, username='test_user', password='Test_password')
s.connect(("127.0.0.1", 8080))
s.connect(("google.com", 80))
s.sendall(b"GET / HTTP/1.1 ...")
s.sendall(b"GET / HTTP/1.1 ...")
s.close()
print(s.recv(4096))
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment