<commit_before>from _socket import inet_pton, inet_ntoa

import struct
from gevent import socket
from packet import Packet

from crypto import crypto_pool

class VPNConnection(object):
    def __init__(self, crypto=None, auth=None):
        raise NotImplementedError()

    def read_packet(self):
        packet = Packet.read_from_socket(self.sock)
        print "read from net %s" % packet
        return self.decrypt_packet(packet)

    def write_packet(self, packet):
        encrypted_packet = self.encrypt_packet(packet)
        self.sock.send(encrypted_packet.data)
        print "write to net %s" % packet

    def encrypt_packet(self, packet):
        packet.data = self.crypto.encrypt(packet.data)
        return packet

    def decrypt_packet(self, packet):
        packet.data = self.crypto.decrypt(packet.data)
        return packet

# connection with server
class VPNServerConnection(VPNConnection):
    def __init__(self, host=None, port=None, app=None):
        self.host, self.port, self.app = host, port, app

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.host, self.port))

        if "ip" not in self.app.config:
            self.app.config["ip"] = "0.0.0.0" # server allocate address from pull (like simple dummy DHCP)

        self.make_handshake()

    def make_handshake(self):
        self.sock.send(socket.inet_pton(socket.AF_INET, self.app.config.ip))
        self.sock.send(struct.pack("HH", self.app.config.auth_no, self.app.config.crypto_no))

        self.crypto_no = self.app.config.crypto_no
        self.crypto = crypto_pool.get(self.app.config.crypto_no)

        self.app.config["ip"] = inet_ntoa(self.sock.recv(4)) # ip

# connection with client
class VPNClientConnection(VPNConnection):
    def __init__(self, sock):
        self.sock = sock

        self.make_handshake()

    def make_handshake(self):
        self.ip, self.auth_no, self.crypto_no = struct.unpack("iHH", self.sock.recv(8))

        if self.ip == 0:
            self.ip = inet_pton("10.0.0.17") # !!! allocate address

        self.sock.send(struct.pack("i", self.ip))

        self.crypto = crypto_pool.get(self.crypto_no)

<commit_msg>fix net<commit_after>from _socket import inet_pton, inet_ntoa

import struct
from gevent import socket
from packet import Packet

from crypto import crypto_pool

class VPNConnection(object):
    def __init__(self, crypto=None, auth=None):
        raise NotImplementedError()

    def read_packet(self):
        packet = Packet.read_from_socket(self.sock)
        print "read from net %s" % packet
        return self.decrypt_packet(packet)

    def write_packet(self, packet):
        encrypted_packet = self.encrypt_packet(packet)
        self.sock.send(encrypted_packet.data)
        print "write to net %s" % packet

    def encrypt_packet(self, packet):
        packet.data = self.crypto.encrypt(packet.data)
        return packet

    def decrypt_packet(self, packet):
        packet.data = self.crypto.decrypt(packet.data)
        return packet

# connection with server
class VPNServerConnection(VPNConnection):
    def __init__(self, host=None, port=None, app=None):
        self.host, self.port, self.app = host, port, app

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.host, self.port))

        if "ip" not in self.app.config:
            self.app.config["ip"] = "0.0.0.0" # server allocate address from pull (like simple dummy DHCP)

        self.make_handshake()

    def make_handshake(self):
        self.sock.send(socket.inet_pton(socket.AF_INET, self.app.config.ip))
        self.sock.send(struct.pack("HH", self.app.config.auth_no, self.app.config.crypto_no))

        self.crypto_no = self.app.config.crypto_no
        self.crypto = crypto_pool.get(self.app.config.crypto_no)

        self.app.config["ip"] = inet_ntoa(self.sock.recv(4)) # ip

# connection with client
class VPNClientConnection(VPNConnection):
    def __init__(self, sock):
        self.sock = sock

        self.make_handshake()

    def make_handshake(self):
        data = self.sock.recv(8)
        if len(data) < 8:
            data += self.sock.recv(8 - len(data))
        self.ip, self.auth_no, self.crypto_no = struct.unpack("iHH", data)

        if self.ip == 0:
            self.ip = inet_pton("10.0.0.17") # !!! allocate address

        self.sock.send(struct.pack("i", self.ip))

        self.crypto = crypto_pool.get(self.crypto_no)


{'fraction_non_alphanumeric': 0.11725367810967455, 'fraction_numerical': 0.006241640659830584, 'mean_word_length': 2.925634295713036, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3566999', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1420, 'n_words': 301}