Skip to content

Commit

Permalink
refactor: fixed ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
leoslf committed Jun 6, 2024
1 parent 422da33 commit 47a92bb
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 29 deletions.
4 changes: 2 additions & 2 deletions socks_router/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import click

from typing import Mapping, Optional, IO
from typing import Optional

from click_option_group import optgroup, MutuallyExclusiveOptionGroup

Expand All @@ -16,7 +16,7 @@
from socketserver import ThreadingTCPServer

from socks_router.parsers import configuration
from socks_router.models import *
from socks_router.models import ApplicationContext, RoutingTable
from socks_router.router import SocksRouter

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion socks_router/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Mapping, Optional, Callable, Iterable
from typing import Mapping, Optional
from enum import IntEnum, StrEnum, auto
from dataclasses import dataclass
from threading import Lock
Expand Down
41 changes: 28 additions & 13 deletions socks_router/parsers.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
from __future__ import annotations

import operator
import re

import logging

# from collection.abc import Callable
from functools import reduce

from parsec import *
from socks_router.models import *
from socks_router.utils import *
from parsec import (
Parser,
string,
regex,
optional,
end_of_line,
times,
many,
one_of,
sepBy,
between,
joint,
separated,
try_choices_longest,
fail_with,
validate,
decimal_number,
hexadecimal_number,
)
from socks_router.models import Address, IPv4, IPv6, Host, Pattern, RoutingTable
from socks_router.utils import to_hex

logger = logging.getLogger(__name__)

def trace(value: T) -> T:
def trace[T](value: T) -> T:
logger.debug(f"value: {value}")
return value

ipv4_octet = (decimal >= validate(lambda value: 0 <= value < (1 << 8))) | fail_with(f"ipv4_octet can only carry a value ranged from 0 to {1 << 8} exclusive")
ipv4_octet = (decimal_number >= validate(lambda value: 0 <= value < (1 << 8))) | fail_with(f"ipv4_octet can only carry a value ranged from 0 to {1 << 8} exclusive")
ipv6_doublet = (hexadecimal_number >= validate(lambda value: 0 <= value < (1 << 16))) | fail_with("doublet can only carry a value from 0 to {1 << 16} exclusive")

ipv4 = separated(ipv4_octet.map(str), string("."), 4, end=False).map(".".join)
Expand All @@ -36,19 +51,19 @@ def trace(value: T) -> T:

port = (decimal_number >= validate(lambda value: 0 <= value < 65536)) | fail_with("port can only carry between 0 to 65536 exclusive")

ipv4_address: Parser[SocketAddress] = (ipv4 + optional(string(":") >> port)).map(IPv4, star=True)
ipv4_address: Parser[IPv4] = (ipv4 + optional(string(":") >> port)).map(IPv4, star=True)

ipv6_address: Parser[SocketAddress] = ((between(string("["), string("]"), ipv6) + (string(":") >> port)) ^ ipv6.map(lambda ipv6: (ipv6, None))).map(IPv6, star=True)
ipv6_address: Parser[IPv6] = ((between(string("["), string("]"), ipv6) + (string(":") >> port)) ^ ipv6.map(lambda ipv6: (ipv6, None))).map(IPv6, star=True)

hostname: Parser[str] = regex(r"(?P<hostname>\b(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*(?:[A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\b)")

host_address: Parser[SocketAddress] = (hostname + optional(string(":") >> port)).map(Host, star=True)
host_address: Parser[Host] = (hostname + optional(string(":") >> port)).map(Host, star=True)

upstream: Parser[SocketAddress] = ipv4_address ^ ipv6_address ^ host_address
upstream: Parser[Address] = ipv4_address ^ ipv6_address ^ host_address

wildcard_hostname = regex(r"(?:\S*[*]\S*)|(?:(?:[*]|(?:(?:[a-zA-Z0-9?*]|[a-zA-Z0-9?*][a-zA-Z0-9\-?*]*[a-zA-Z0-9?*]))\.)*(?:[*]|(?:[A-Za-z0-9?*]|[A-Za-z0-9?*][A-Za-z0-9\-?*]*[A-Za-z0-9?*]))\b)")

wildcard_host_address: Parser[SocketAddress] = (wildcard_hostname + optional(string(":") >> port)).map(Host, star=True)
wildcard_host_address: Parser[Host] = (wildcard_hostname + optional(string(":") >> port)).map(Host, star=True)

pattern: Parser[Pattern] = (optional(string("!").result(False), True) + wildcard_host_address).map(Pattern, star=True)

Expand Down
17 changes: 13 additions & 4 deletions socks_router/router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import logging
import traceback

Expand All @@ -9,14 +8,24 @@
import socket
import socks

from typing import Mapping, Optional
from typing import Optional
from more_itertools import partition
from select import select
from threading import Lock
from subprocess import Popen
from socketserver import StreamRequestHandler

from socks_router.models import *
from socks_router.models import (
Socks5Command,
Socks5Method,
Socks5AddressType,
Socks5Reply,
Socks5State,
Socks5Addresses,
Address,
IPv4,
ApplicationContext,
RoutingTable,
)

SOCKS_VERSION = 5
CHUNK_SIZE = 4096
Expand Down
15 changes: 15 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sonar.projectKey=${env.SONAR_PROJECT_KEY}
sonar.organization=${env.SONAR_ORGANIZATION}

sonar.language=py
sonar.python.version=3

sonar.sourceEncoding=UTF-8
sonar.dynamicAnalysis=reuseReports
sonar.core.codeCoveragePlugin=cobertura

sonar.sources=more_decorators
sonar.tests=tests

sonar.python.xunit.reportPath=.output/unittest/merged.xml
sonar.python.coverage.reportPaths=.output/coverage/**/*.xml
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

def test_cli():
runner = CliRunner()
# result = runner.invoke(cli)
# assert result.exit_code == 0
result = runner.invoke(cli, ["--version"])
assert result.exit_code == 0
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from socks_router.models import *
from socks_router.models import IPv4, IPv6

@pytest.mark.parametrize("address,port,ipv4", [
("10.0.0.1", None, IPv4("10.0.0.1")),
Expand Down
19 changes: 13 additions & 6 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

import pytest

import re

from typing import Optional

from parsec import *
from socks_router.parsers import *
from parsec import ParseError
from socks_router.models import Host, IPv4, IPv6, Address, Pattern, RoutingTable
from socks_router.parsers import (
trace,
ipv4_octet,
ipv4,
ipv4_address,
ipv6,
ipv6_address,
pattern,
configuration_entry,
configuration,
)

def test_trace():
assert trace(1) == 1
Expand Down

0 comments on commit 47a92bb

Please sign in to comment.