Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
opt aead cipher && update readme close #43 (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Sep 11, 2020
1 parent 5113b42 commit d035878
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 19 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@
* 流量统计
* 速率控制
* 开放了grpc接口(类似ss-manager)
* **单端口多用户(利用AEAD加密在不破坏协议的情况下实现)**

## 性能测试

> Shadowsocks本身是一个IO密集行的应用,但是由于加入了AEAD加密,使得SS本身变成了CPU密集行的应用
> 而Python本身是不太适合CPU密集的场景的,所以在AEAD模式中的表现不佳
> PS: 当然,其实是我代码写的烂,python不背锅
* Steam-Cipher-None(不加密 高IO)

![](images/stream-none.png)

* AEAD-Cipher-CHACHA-20(加密 高CPU)

![](images/aead-chacha-20-ietf-poly-1305.png)


## rpc proto

Expand Down
9 changes: 4 additions & 5 deletions grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

from grpclib.client import Channel


from shadowsocks.protos.aioshadowsocks_grpc import ssStub
from shadowsocks.protos.aioshadowsocks_pb2 import (
UserIdReq,
UserReq,
User,
HealthCheckReq,
HealthCheckRes,
User,
UserIdReq,
UserReq,
)
from shadowsocks.protos.aioshadowsocks_grpc import ssStub


class Client:
Expand Down
Binary file added images/aead-chacha-20-ietf-poly-1305.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/stream-none.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 14 additions & 8 deletions shadowsocks/cipherman.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import time
from typing import List

from shadowsocks import protocol_flag as flag
from shadowsocks.ciphers import (
AES128GCM,
Expand Down Expand Up @@ -95,12 +96,11 @@ def _find_access_user(self, first_data: bytes):
else:
cipher.unpack(d)
self.access_user = user
self.cipher = cipher
break
except ValueError as e:
if e.args[0] != "MAC check failed":
raise e
del cipher

logging.info(
f"用户:{self.access_user} 一共寻找了{ cnt }个user,共花费{(time.time()-t1)*1000}ms"
)
Expand Down Expand Up @@ -144,18 +144,24 @@ def encrypt(self, data: bytes):

@DECRYPT_DATA_TIME.time()
def decrypt(self, data: bytes):
if len(data) + len(self._buffer) < self._first_data_len:
if (
self.access_user is None
and len(data) + len(self._buffer) < self._first_data_len
):
self._buffer.extend(data)
return
else:
data = bytes(self._buffer) + data
del self._buffer[:]

if not self.access_user:
self.find_access_user_by_data(data)
self._buffer.extend(data)
first_data, self._buffer = (
self._buffer[: self._first_data_len],
self._buffer[self._first_data_len :],
)
self.find_access_user_by_data(first_data)
data = bytes(self._buffer)
del self._buffer

self._record_user_traffic(len(data), 0)

if self.ts_protocol == flag.TRANSPORT_TCP:
if not self.cipher:
self.cipher = self.cipher_cls(self.access_user.password)
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks/ciphers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
import hashlib
import os
import logging
import os

import hkdf
from Crypto.Cipher import AES, ChaCha20_Poly1305
Expand Down
1 change: 1 addition & 0 deletions shadowsocks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def handle_data_received(self, data):
return

if not data:
print("iniinini")
return

if self._stage == self.STAGE_INIT:
Expand Down
7 changes: 5 additions & 2 deletions shadowsocks/metrics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from prometheus_client import Counter, Gauge, Histogram, Info
import socket

from prometheus_client import Counter, Gauge, Histogram, Info

# METRICS
NODE_HOST_NAME = socket.gethostname()

Expand All @@ -24,7 +25,9 @@


NETWORK_TRANSMIT_BYTES = Counter(
"network_transmit_bytes", "shadowsocks network transmit bytes", labelnames=["ss_node",]
"network_transmit_bytes",
"shadowsocks network transmit bytes",
labelnames=["ss_node",],
)
NETWORK_TRANSMIT_BYTES = NETWORK_TRANSMIT_BYTES.labels(ss_node=NODE_HOST_NAME)

Expand Down
2 changes: 1 addition & 1 deletion shadowsocks/protos/aioshadowsocks_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import abc
import typing

import grpclib.const
import grpclib.client
import grpclib.const

if typing.TYPE_CHECKING:
import grpclib.server
Expand Down
1 change: 0 additions & 1 deletion shadowsocks/proxyman.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import logging

from collections import defaultdict

from shadowsocks.core import LocalTCP, LocalUDP
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
HealthCheckRes,
User,
UserIdReq,
UserReq,
UserList,
UserReq,
)


Expand Down

0 comments on commit d035878

Please sign in to comment.