byteplussdk-arkruntime 3.0.64__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- byteplussdk_arkruntime-3.0.64.dist-info/METADATA +36 -0
- byteplussdk_arkruntime-3.0.64.dist-info/RECORD +240 -0
- byteplussdk_arkruntime-3.0.64.dist-info/WHEEL +5 -0
- byteplussdk_arkruntime-3.0.64.dist-info/licenses/LICENSE.txt +203 -0
- byteplussdk_arkruntime-3.0.64.dist-info/top_level.txt +1 -0
- byteplussdkarkruntime/__init__.py +7 -0
- byteplussdkarkruntime/_base_client.py +1439 -0
- byteplussdkarkruntime/_client.py +570 -0
- byteplussdkarkruntime/_compat.py +245 -0
- byteplussdkarkruntime/_constants.py +43 -0
- byteplussdkarkruntime/_exceptions.py +202 -0
- byteplussdkarkruntime/_files.py +149 -0
- byteplussdkarkruntime/_legacy_response.py +451 -0
- byteplussdkarkruntime/_models.py +734 -0
- byteplussdkarkruntime/_qs.py +168 -0
- byteplussdkarkruntime/_request_options.py +117 -0
- byteplussdkarkruntime/_resource.py +47 -0
- byteplussdkarkruntime/_response.py +615 -0
- byteplussdkarkruntime/_streaming.py +470 -0
- byteplussdkarkruntime/_types.py +210 -0
- byteplussdkarkruntime/_utils/__init__.py +58 -0
- byteplussdkarkruntime/_utils/_key_agreement.py +207 -0
- byteplussdkarkruntime/_utils/_logs.py +24 -0
- byteplussdkarkruntime/_utils/_model_breaker.py +83 -0
- byteplussdkarkruntime/_utils/_streams.py +12 -0
- byteplussdkarkruntime/_utils/_transform.py +489 -0
- byteplussdkarkruntime/_utils/_typing.py +171 -0
- byteplussdkarkruntime/_utils/_utils.py +497 -0
- byteplussdkarkruntime/common/__init__.py +14 -0
- byteplussdkarkruntime/common/_parsing/__init__.py +22 -0
- byteplussdkarkruntime/common/_parsing/_completions.py +290 -0
- byteplussdkarkruntime/common/_parsing/_responses.py +189 -0
- byteplussdkarkruntime/common/_pydantic.py +192 -0
- byteplussdkarkruntime/common/_tools.py +67 -0
- byteplussdkarkruntime/common/streaming/__init__.py +12 -0
- byteplussdkarkruntime/common/streaming/_deltas.py +86 -0
- byteplussdkarkruntime/common/streaming/chat/__init__.py +33 -0
- byteplussdkarkruntime/common/streaming/chat/_completions.py +772 -0
- byteplussdkarkruntime/common/streaming/chat/_events.py +103 -0
- byteplussdkarkruntime/common/streaming/chat/_types.py +36 -0
- byteplussdkarkruntime/pagination.py +134 -0
- byteplussdkarkruntime/resources/__init__.py +48 -0
- byteplussdkarkruntime/resources/batch/__init__.py +3 -0
- byteplussdkarkruntime/resources/batch/_utils.py +155 -0
- byteplussdkarkruntime/resources/batch/batch.py +37 -0
- byteplussdkarkruntime/resources/batch/chat/__init__.py +3 -0
- byteplussdkarkruntime/resources/batch/chat/chat.py +19 -0
- byteplussdkarkruntime/resources/batch/chat/completions.py +329 -0
- byteplussdkarkruntime/resources/batch/embeddings.py +105 -0
- byteplussdkarkruntime/resources/batch/multimodal_embeddings.py +111 -0
- byteplussdkarkruntime/resources/batch_chat/__init__.py +3 -0
- byteplussdkarkruntime/resources/batch_chat/chat.py +21 -0
- byteplussdkarkruntime/resources/batch_chat/completions.py +328 -0
- byteplussdkarkruntime/resources/beta/__init__.py +12 -0
- byteplussdkarkruntime/resources/beta/beta.py +81 -0
- byteplussdkarkruntime/resources/beta/chat/__init__.py +21 -0
- byteplussdkarkruntime/resources/beta/chat/chat.py +31 -0
- byteplussdkarkruntime/resources/beta/chat/completions.py +454 -0
- byteplussdkarkruntime/resources/chat/__init__.py +3 -0
- byteplussdkarkruntime/resources/chat/chat.py +21 -0
- byteplussdkarkruntime/resources/chat/completions.py +258 -0
- byteplussdkarkruntime/resources/content_generation/__init__.py +6 -0
- byteplussdkarkruntime/resources/content_generation/content_generation.py +17 -0
- byteplussdkarkruntime/resources/content_generation/tasks.py +348 -0
- byteplussdkarkruntime/resources/context/__init__.py +3 -0
- byteplussdkarkruntime/resources/context/completions.py +221 -0
- byteplussdkarkruntime/resources/context/context.py +98 -0
- byteplussdkarkruntime/resources/embeddings.py +110 -0
- byteplussdkarkruntime/resources/encryption.py +215 -0
- byteplussdkarkruntime/resources/files.py +592 -0
- byteplussdkarkruntime/resources/images/__init__.py +6 -0
- byteplussdkarkruntime/resources/images/images.py +335 -0
- byteplussdkarkruntime/resources/multimodal_embeddings.py +113 -0
- byteplussdkarkruntime/resources/responses/__init__.py +16 -0
- byteplussdkarkruntime/resources/responses/input_items.py +248 -0
- byteplussdkarkruntime/resources/responses/responses.py +417 -0
- byteplussdkarkruntime/types/__init__.py +28 -0
- byteplussdkarkruntime/types/chat/__init__.py +78 -0
- byteplussdkarkruntime/types/chat/chat_completion.py +93 -0
- byteplussdkarkruntime/types/chat/chat_completion_assistant_message_param.py +61 -0
- byteplussdkarkruntime/types/chat/chat_completion_audio.py +22 -0
- byteplussdkarkruntime/types/chat/chat_completion_chunk.py +165 -0
- byteplussdkarkruntime/types/chat/chat_completion_content_part_image_param.py +20 -0
- byteplussdkarkruntime/types/chat/chat_completion_content_part_param.py +30 -0
- byteplussdkarkruntime/types/chat/chat_completion_content_part_text_param.py +13 -0
- byteplussdkarkruntime/types/chat/chat_completion_content_part_video_param.py +32 -0
- byteplussdkarkruntime/types/chat/chat_completion_function_call_option_param.py +10 -0
- byteplussdkarkruntime/types/chat/chat_completion_function_message_param.py +17 -0
- byteplussdkarkruntime/types/chat/chat_completion_message.py +56 -0
- byteplussdkarkruntime/types/chat/chat_completion_message_param.py +19 -0
- byteplussdkarkruntime/types/chat/chat_completion_message_tool_call.py +29 -0
- byteplussdkarkruntime/types/chat/chat_completion_message_tool_call_param.py +29 -0
- byteplussdkarkruntime/types/chat/chat_completion_named_tool_choice_param.py +17 -0
- byteplussdkarkruntime/types/chat/chat_completion_role.py +5 -0
- byteplussdkarkruntime/types/chat/chat_completion_stream_options_param.py +19 -0
- byteplussdkarkruntime/types/chat/chat_completion_system_message_param.py +34 -0
- byteplussdkarkruntime/types/chat/chat_completion_token_logprob.py +55 -0
- byteplussdkarkruntime/types/chat/chat_completion_tool_choice_option_param.py +12 -0
- byteplussdkarkruntime/types/chat/chat_completion_tool_message_param.py +37 -0
- byteplussdkarkruntime/types/chat/chat_completion_tool_param.py +14 -0
- byteplussdkarkruntime/types/chat/chat_completion_user_message_param.py +34 -0
- byteplussdkarkruntime/types/chat/completion_create_params.py +258 -0
- byteplussdkarkruntime/types/chat/parsed_chat_completion.py +52 -0
- byteplussdkarkruntime/types/chat/parsed_function_tool_call.py +34 -0
- byteplussdkarkruntime/types/completion_usage.py +33 -0
- byteplussdkarkruntime/types/content_generation/__init__.py +0 -0
- byteplussdkarkruntime/types/content_generation/content_generation_task.py +106 -0
- byteplussdkarkruntime/types/content_generation/content_generation_task_id.py +11 -0
- byteplussdkarkruntime/types/content_generation/create_task_content_param.py +72 -0
- byteplussdkarkruntime/types/content_generation/list_content_generation_tasks_response.py +24 -0
- byteplussdkarkruntime/types/context/__init__.py +8 -0
- byteplussdkarkruntime/types/context/context_chat_completion.py +33 -0
- byteplussdkarkruntime/types/context/context_chat_completion_chunk.py +41 -0
- byteplussdkarkruntime/types/context/context_create_params.py +37 -0
- byteplussdkarkruntime/types/context/create_context_response.py +25 -0
- byteplussdkarkruntime/types/context/truncation_strategy.py +13 -0
- byteplussdkarkruntime/types/create_embedding_response.py +36 -0
- byteplussdkarkruntime/types/embedding.py +21 -0
- byteplussdkarkruntime/types/file_create_params.py +49 -0
- byteplussdkarkruntime/types/file_deleted.py +24 -0
- byteplussdkarkruntime/types/file_list_params.py +43 -0
- byteplussdkarkruntime/types/file_object.py +69 -0
- byteplussdkarkruntime/types/file_purpose.py +16 -0
- byteplussdkarkruntime/types/images/__init__.py +3 -0
- byteplussdkarkruntime/types/images/image_gen_completed_event.py +59 -0
- byteplussdkarkruntime/types/images/image_gen_generating_event.py +53 -0
- byteplussdkarkruntime/types/images/image_gen_stream_event.py +24 -0
- byteplussdkarkruntime/types/images/images.py +99 -0
- byteplussdkarkruntime/types/multimodal_embedding/__init__.py +15 -0
- byteplussdkarkruntime/types/multimodal_embedding/embedding_content_part_image_param.py +17 -0
- byteplussdkarkruntime/types/multimodal_embedding/embedding_content_part_text_param.py +13 -0
- byteplussdkarkruntime/types/multimodal_embedding/embedding_data.py +15 -0
- byteplussdkarkruntime/types/multimodal_embedding/embedding_input.py +12 -0
- byteplussdkarkruntime/types/multimodal_embedding/embedding_response.py +29 -0
- byteplussdkarkruntime/types/multimodal_embedding/embedding_usage.py +23 -0
- byteplussdkarkruntime/types/responses/__init__.py +133 -0
- byteplussdkarkruntime/types/responses/easy_input_message.py +36 -0
- byteplussdkarkruntime/types/responses/easy_input_message_param.py +37 -0
- byteplussdkarkruntime/types/responses/function_tool.py +35 -0
- byteplussdkarkruntime/types/responses/function_tool_param.py +35 -0
- byteplussdkarkruntime/types/responses/input_item_list_params.py +47 -0
- byteplussdkarkruntime/types/responses/mcp_input_item_param.py +109 -0
- byteplussdkarkruntime/types/responses/mcp_tool.py +80 -0
- byteplussdkarkruntime/types/responses/mcp_tool_param.py +78 -0
- byteplussdkarkruntime/types/responses/response.py +152 -0
- byteplussdkarkruntime/types/responses/response_annotation_added_event.py +36 -0
- byteplussdkarkruntime/types/responses/response_caching.py +25 -0
- byteplussdkarkruntime/types/responses/response_caching_param.py +22 -0
- byteplussdkarkruntime/types/responses/response_completed_event.py +26 -0
- byteplussdkarkruntime/types/responses/response_content_part_added_event.py +39 -0
- byteplussdkarkruntime/types/responses/response_content_part_done_event.py +39 -0
- byteplussdkarkruntime/types/responses/response_create_params.py +26 -0
- byteplussdkarkruntime/types/responses/response_created_event.py +26 -0
- byteplussdkarkruntime/types/responses/response_error.py +25 -0
- byteplussdkarkruntime/types/responses/response_error_event.py +32 -0
- byteplussdkarkruntime/types/responses/response_failed_event.py +26 -0
- byteplussdkarkruntime/types/responses/response_format_text_config.py +25 -0
- byteplussdkarkruntime/types/responses/response_format_text_config_param.py +25 -0
- byteplussdkarkruntime/types/responses/response_function_call_arguments_delta_event.py +33 -0
- byteplussdkarkruntime/types/responses/response_function_call_arguments_done_event.py +30 -0
- byteplussdkarkruntime/types/responses/response_function_tool_call.py +42 -0
- byteplussdkarkruntime/types/responses/response_function_tool_call_item.py +20 -0
- byteplussdkarkruntime/types/responses/response_function_tool_call_output_item.py +39 -0
- byteplussdkarkruntime/types/responses/response_function_tool_call_param.py +31 -0
- byteplussdkarkruntime/types/responses/response_in_progress_event.py +26 -0
- byteplussdkarkruntime/types/responses/response_includable.py +19 -0
- byteplussdkarkruntime/types/responses/response_incomplete_details.py +30 -0
- byteplussdkarkruntime/types/responses/response_incomplete_event.py +26 -0
- byteplussdkarkruntime/types/responses/response_input_content.py +24 -0
- byteplussdkarkruntime/types/responses/response_input_content_param.py +23 -0
- byteplussdkarkruntime/types/responses/response_input_image.py +38 -0
- byteplussdkarkruntime/types/responses/response_input_image_param.py +42 -0
- byteplussdkarkruntime/types/responses/response_input_image_pixel_limit_param.py +29 -0
- byteplussdkarkruntime/types/responses/response_input_message_content_list.py +20 -0
- byteplussdkarkruntime/types/responses/response_input_message_content_list_param.py +26 -0
- byteplussdkarkruntime/types/responses/response_input_message_item.py +43 -0
- byteplussdkarkruntime/types/responses/response_input_param.py +51 -0
- byteplussdkarkruntime/types/responses/response_input_text.py +25 -0
- byteplussdkarkruntime/types/responses/response_input_text_param.py +25 -0
- byteplussdkarkruntime/types/responses/response_input_video_param.py +34 -0
- byteplussdkarkruntime/types/responses/response_item.py +40 -0
- byteplussdkarkruntime/types/responses/response_item_list.py +36 -0
- byteplussdkarkruntime/types/responses/response_mcp_call_arguments_delta_event.py +37 -0
- byteplussdkarkruntime/types/responses/response_mcp_call_arguments_done_event.py +34 -0
- byteplussdkarkruntime/types/responses/response_mcp_call_completed_event.py +31 -0
- byteplussdkarkruntime/types/responses/response_mcp_call_failed_event.py +31 -0
- byteplussdkarkruntime/types/responses/response_mcp_call_in_progress_event.py +31 -0
- byteplussdkarkruntime/types/responses/response_mcp_item.py +110 -0
- byteplussdkarkruntime/types/responses/response_mcp_list_tools_completed_event.py +31 -0
- byteplussdkarkruntime/types/responses/response_mcp_list_tools_failed_event.py +31 -0
- byteplussdkarkruntime/types/responses/response_mcp_list_tools_in_progress_event.py +31 -0
- byteplussdkarkruntime/types/responses/response_output_item.py +36 -0
- byteplussdkarkruntime/types/responses/response_output_item_added_event.py +29 -0
- byteplussdkarkruntime/types/responses/response_output_item_done_event.py +29 -0
- byteplussdkarkruntime/types/responses/response_output_message.py +43 -0
- byteplussdkarkruntime/types/responses/response_output_text.py +61 -0
- byteplussdkarkruntime/types/responses/response_output_text_param.py +77 -0
- byteplussdkarkruntime/types/responses/response_reasoning_item.py +46 -0
- byteplussdkarkruntime/types/responses/response_reasoning_item_param.py +37 -0
- byteplussdkarkruntime/types/responses/response_reasoning_summary_part_added_event.py +42 -0
- byteplussdkarkruntime/types/responses/response_reasoning_summary_part_done_event.py +42 -0
- byteplussdkarkruntime/types/responses/response_reasoning_summary_text_delta_event.py +34 -0
- byteplussdkarkruntime/types/responses/response_reasoning_summary_text_done_event.py +34 -0
- byteplussdkarkruntime/types/responses/response_status.py +17 -0
- byteplussdkarkruntime/types/responses/response_stream_event.py +93 -0
- byteplussdkarkruntime/types/responses/response_text_config.py +28 -0
- byteplussdkarkruntime/types/responses/response_text_config_param.py +31 -0
- byteplussdkarkruntime/types/responses/response_text_delta_event.py +34 -0
- byteplussdkarkruntime/types/responses/response_text_done_event.py +34 -0
- byteplussdkarkruntime/types/responses/response_tool_usage.py +32 -0
- byteplussdkarkruntime/types/responses/response_usage.py +54 -0
- byteplussdkarkruntime/types/responses/response_web_search_call_completed_event.py +29 -0
- byteplussdkarkruntime/types/responses/response_web_search_call_in_progress_event.py +29 -0
- byteplussdkarkruntime/types/responses/response_web_search_call_searching_event.py +29 -0
- byteplussdkarkruntime/types/responses/response_web_search_item.py +43 -0
- byteplussdkarkruntime/types/responses/tool.py +25 -0
- byteplussdkarkruntime/types/responses/tool_choice_function.py +25 -0
- byteplussdkarkruntime/types/responses/tool_choice_function_param.py +25 -0
- byteplussdkarkruntime/types/responses/tool_choice_mcp_param.py +29 -0
- byteplussdkarkruntime/types/responses/tool_choice_options.py +17 -0
- byteplussdkarkruntime/types/responses/tool_param.py +24 -0
- byteplussdkarkruntime/types/responses/web_search_tool.py +49 -0
- byteplussdkarkruntime/types/responses/web_search_tool_param.py +46 -0
- byteplussdkarkruntime/types/shared/__init__.py +21 -0
- byteplussdkarkruntime/types/shared/reasoning.py +10 -0
- byteplussdkarkruntime/types/shared/reasoning_effort.py +16 -0
- byteplussdkarkruntime/types/shared/response_format_json_object.py +22 -0
- byteplussdkarkruntime/types/shared/response_format_json_schema.py +56 -0
- byteplussdkarkruntime/types/shared/response_format_text.py +22 -0
- byteplussdkarkruntime/types/shared/thinking.py +25 -0
- byteplussdkarkruntime/types/shared_params/__init__.py +13 -0
- byteplussdkarkruntime/types/shared_params/function_definition.py +34 -0
- byteplussdkarkruntime/types/shared_params/function_parameters.py +9 -0
- byteplussdkarkruntime/types/shared_params/metadata.py +20 -0
- byteplussdkarkruntime/types/shared_params/reasoning.py +12 -0
- byteplussdkarkruntime/types/shared_params/reasoning_effort.py +8 -0
- byteplussdkarkruntime/types/shared_params/response_format_json_object.py +22 -0
- byteplussdkarkruntime/types/shared_params/response_format_json_schema.py +54 -0
- byteplussdkarkruntime/types/shared_params/response_format_text.py +22 -0
- byteplussdkarkruntime/types/shared_params/thinking.py +22 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from ._utils import (
|
|
2
|
+
with_sts_token,
|
|
3
|
+
async_with_sts_token,
|
|
4
|
+
flatten as flatten,
|
|
5
|
+
is_dict as is_dict,
|
|
6
|
+
is_list as is_list,
|
|
7
|
+
is_given as is_given,
|
|
8
|
+
is_tuple as is_tuple,
|
|
9
|
+
json_safe as json_safe,
|
|
10
|
+
lru_cache as lru_cache,
|
|
11
|
+
is_mapping as is_mapping,
|
|
12
|
+
is_tuple_t as is_tuple_t,
|
|
13
|
+
is_iterable as is_iterable,
|
|
14
|
+
is_sequence as is_sequence,
|
|
15
|
+
coerce_float as coerce_float,
|
|
16
|
+
is_mapping_t as is_mapping_t,
|
|
17
|
+
removeprefix as removeprefix,
|
|
18
|
+
removesuffix as removesuffix,
|
|
19
|
+
extract_files as extract_files,
|
|
20
|
+
is_sequence_t as is_sequence_t,
|
|
21
|
+
required_args as required_args,
|
|
22
|
+
coerce_boolean as coerce_boolean,
|
|
23
|
+
coerce_integer as coerce_integer,
|
|
24
|
+
file_from_path as file_from_path,
|
|
25
|
+
strip_not_given as strip_not_given,
|
|
26
|
+
deepcopy_minimal as deepcopy_minimal,
|
|
27
|
+
get_async_library as get_async_library,
|
|
28
|
+
maybe_coerce_float as maybe_coerce_float,
|
|
29
|
+
maybe_coerce_boolean as maybe_coerce_boolean,
|
|
30
|
+
maybe_coerce_integer as maybe_coerce_integer,
|
|
31
|
+
)
|
|
32
|
+
from .._compat import (
|
|
33
|
+
parse_date as parse_date,
|
|
34
|
+
parse_datetime as parse_datetime,
|
|
35
|
+
)
|
|
36
|
+
from ._typing import (
|
|
37
|
+
is_list_type as is_list_type,
|
|
38
|
+
is_union_type as is_union_type,
|
|
39
|
+
extract_type_arg as extract_type_arg,
|
|
40
|
+
is_iterable_type as is_iterable_type,
|
|
41
|
+
is_required_type as is_required_type,
|
|
42
|
+
is_annotated_type as is_annotated_type,
|
|
43
|
+
is_type_alias_type as is_type_alias_type,
|
|
44
|
+
strip_annotated_type as strip_annotated_type,
|
|
45
|
+
extract_type_var_from_base as extract_type_var_from_base,
|
|
46
|
+
)
|
|
47
|
+
from ._streams import (
|
|
48
|
+
consume_sync_iterator as consume_sync_iterator,
|
|
49
|
+
consume_async_iterator as consume_async_iterator,
|
|
50
|
+
)
|
|
51
|
+
from ._transform import (
|
|
52
|
+
PropertyInfo as PropertyInfo,
|
|
53
|
+
transform as transform,
|
|
54
|
+
async_transform as async_transform,
|
|
55
|
+
maybe_transform as maybe_transform,
|
|
56
|
+
async_maybe_transform as async_maybe_transform,
|
|
57
|
+
)
|
|
58
|
+
from ._logs import setup_logging as setup_logging
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Copyright (c) [2025] [OpenAI]
|
|
2
|
+
# Copyright (c) [2025] [ByteDance Ltd. and/or its affiliates.]
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
#
|
|
5
|
+
# This file has been modified by [ByteDance Ltd. and/or its affiliates.] on 2025.7
|
|
6
|
+
#
|
|
7
|
+
# Original file was released under Apache License Version 2.0, with the full license text
|
|
8
|
+
# available at https://github.com/openai/openai-python/blob/main/LICENSE.
|
|
9
|
+
#
|
|
10
|
+
# This modified file is released under the same license.
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import re
|
|
15
|
+
import base64
|
|
16
|
+
from typing import Tuple
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def get_cert_info(cert_pem: str) -> Tuple[str, str, float]:
|
|
20
|
+
import re
|
|
21
|
+
from cryptography import x509
|
|
22
|
+
from cryptography.hazmat.backends import default_backend
|
|
23
|
+
|
|
24
|
+
cert = x509.load_pem_x509_certificate(cert_pem.encode(), default_backend())
|
|
25
|
+
try:
|
|
26
|
+
dns = cert.extensions.get_extension_for_class(
|
|
27
|
+
x509.SubjectAlternativeName
|
|
28
|
+
).value.get_values_for_type(x509.DNSName)
|
|
29
|
+
if (
|
|
30
|
+
dns
|
|
31
|
+
and len(dns) > 1
|
|
32
|
+
and re.match(r"^ring\..*$", dns[0])
|
|
33
|
+
and re.match(r"^key\..*$", dns[1])
|
|
34
|
+
):
|
|
35
|
+
return dns[0][5:], dns[1][4:], cert.not_valid_after_utc.timestamp()
|
|
36
|
+
except Exception:
|
|
37
|
+
pass
|
|
38
|
+
return "", "", cert.not_valid_after_utc.timestamp()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def aes_gcm_encrypt_bytes(
|
|
42
|
+
key: bytes, iv: bytes, plain_bytes: bytes, associated_data: bytes = b""
|
|
43
|
+
) -> bytes:
|
|
44
|
+
# aes_gcm_encrypt_bytes encrypt message using AES-GCM
|
|
45
|
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
46
|
+
|
|
47
|
+
encryptor = Cipher(
|
|
48
|
+
algorithms.AES(key),
|
|
49
|
+
modes.GCM(iv),
|
|
50
|
+
).encryptor()
|
|
51
|
+
# associated_data will be authenticated but not encrypted,
|
|
52
|
+
# it must also be passed in on decryption.
|
|
53
|
+
encryptor.authenticate_additional_data(associated_data)
|
|
54
|
+
# Encrypt the plaintext and get the associated ciphertext.
|
|
55
|
+
# GCM does not require padding.
|
|
56
|
+
ciphertext = encryptor.update(plain_bytes) + encryptor.finalize()
|
|
57
|
+
return ciphertext + encryptor.tag
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def aes_gcm_encrypt_base64_string(key: bytes, nonce: bytes, plaintext: str) -> str:
|
|
61
|
+
"""aes_gcm_encrypt_base64_string Encrypt message from base64 string to string using AES-GCM"""
|
|
62
|
+
plain_bytes = plaintext.encode()
|
|
63
|
+
# Encrypt message to string using AES-GCM
|
|
64
|
+
c = aes_gcm_encrypt_bytes(key, nonce, plain_bytes)
|
|
65
|
+
return base64.b64encode(c).decode()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def aes_gcm_decrypt_bytes(
|
|
69
|
+
key: bytes, iv: bytes, cipher_bytes: bytes, associated_data: bytes = b""
|
|
70
|
+
) -> bytes:
|
|
71
|
+
"""aes_gcm_decrypt_bytes Decrypt message from bytes to bytes using AES-GCM"""
|
|
72
|
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
73
|
+
|
|
74
|
+
tag_length = 16 # default aes gcm tag length
|
|
75
|
+
cipher = cipher_bytes[:-tag_length]
|
|
76
|
+
tag = cipher_bytes[-tag_length:]
|
|
77
|
+
# Construct a Cipher object, with the key, iv, and additionally the
|
|
78
|
+
# GCM tag used for authenticating the message.
|
|
79
|
+
decryptor = Cipher(
|
|
80
|
+
algorithms.AES(key),
|
|
81
|
+
modes.GCM(iv, tag),
|
|
82
|
+
).decryptor()
|
|
83
|
+
# We put associated_data back in or the tag will fail to verify
|
|
84
|
+
# when we finalize the decryptor.
|
|
85
|
+
decryptor.authenticate_additional_data(associated_data)
|
|
86
|
+
# Decryption gets us the authenticated plaintext.
|
|
87
|
+
# If the tag does not match an InvalidTag exception will be raised.
|
|
88
|
+
return decryptor.update(cipher) + decryptor.finalize()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def aes_gcm_decrypt_base64_string(key: bytes, nonce: bytes, ciphertext: str) -> str:
|
|
92
|
+
# Decrypt message(base64.std.string) using AES-GCM
|
|
93
|
+
cipher_bytes = base64.decodebytes(ciphertext.encode())
|
|
94
|
+
return aes_gcm_decrypt_bytes(key, nonce, cipher_bytes).decode()
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
base64_pattern = (
|
|
98
|
+
r"(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})"
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def decrypt_corner_case(key: bytes, nonce: bytes, data: str) -> str:
|
|
103
|
+
"""decrypt_corner_case Decrypt corner case data"""
|
|
104
|
+
if len(data) < 24:
|
|
105
|
+
return ""
|
|
106
|
+
for i in range(20, len(data), 4):
|
|
107
|
+
try:
|
|
108
|
+
decrypted = aes_gcm_decrypt_base64_string(key, nonce, data[: i + 4])
|
|
109
|
+
if i + 4 == len(data):
|
|
110
|
+
return decrypted
|
|
111
|
+
return decrypted + decrypt_corner_case(key, nonce, data[i + 4:])
|
|
112
|
+
except Exception:
|
|
113
|
+
pass
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def aes_gcm_decrypt_base64_list(key: bytes, nonce: bytes, ciphertext: str) -> str:
|
|
117
|
+
# Decrypt
|
|
118
|
+
base64_array = re.findall(base64_pattern, ciphertext)
|
|
119
|
+
result = []
|
|
120
|
+
for b64 in base64_array:
|
|
121
|
+
try:
|
|
122
|
+
result.append(aes_gcm_decrypt_base64_string(key, nonce, b64))
|
|
123
|
+
except Exception:
|
|
124
|
+
result.append(decrypt_corner_case(key, nonce, b64))
|
|
125
|
+
return "".join(result)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def decrypt_validate(ciphertext: str) -> bool:
|
|
129
|
+
cipher_bytes = ciphertext.encode()
|
|
130
|
+
cipher_b64_bytes = base64.decodebytes(cipher_bytes)
|
|
131
|
+
return (
|
|
132
|
+
len(cipher_bytes) / 4 >= len(cipher_b64_bytes) / 3 >= len(cipher_bytes) / 4 - 1
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def marshal_cryptography_pub_key(key) -> bytes:
|
|
137
|
+
# python version of crypto/elliptic/elliptic.go Marshal
|
|
138
|
+
# without point on curve check
|
|
139
|
+
return bytes([4]) + key.x.to_bytes(32, "big") + key.y.to_bytes(32, "big")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class key_agreement_client:
|
|
143
|
+
def __init__(self, certificate_pem_string: str) -> None:
|
|
144
|
+
"""Load cert and extract public key"""
|
|
145
|
+
__fixed_version__ = "42.0.0" # version check
|
|
146
|
+
from cryptography import __version__
|
|
147
|
+
|
|
148
|
+
if __version__ < __fixed_version__:
|
|
149
|
+
raise Exception(
|
|
150
|
+
"The cryptography package of Ark SDK only supports versions after {}, "
|
|
151
|
+
'please install the cryptography package by using pip install "cryptography>={}"'.format(
|
|
152
|
+
__fixed_version__, __fixed_version__
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
from cryptography import x509
|
|
156
|
+
from cryptography.hazmat.primitives.asymmetric import ec
|
|
157
|
+
|
|
158
|
+
pem_data = certificate_pem_string.encode()
|
|
159
|
+
self._cert = x509.load_pem_x509_certificate(pem_data)
|
|
160
|
+
cert_pub = self._cert.public_key().public_numbers()
|
|
161
|
+
self._curve = ec._CURVE_TYPES[self._cert.public_key().curve.name]
|
|
162
|
+
self._public_key = ec.EllipticCurvePublicNumbers(
|
|
163
|
+
cert_pub.x, cert_pub.y, self._curve
|
|
164
|
+
).public_key()
|
|
165
|
+
|
|
166
|
+
def encrypt_string(self, plaintext: str) -> Tuple[bytes, bytes, str, str]:
|
|
167
|
+
"""encrypt_string encrypt plaintext with ECIES DH protocol"""
|
|
168
|
+
key, nonce, token = self.generate_ecies_key_pair()
|
|
169
|
+
# Encrypt message using AES-GCM
|
|
170
|
+
ciphertext = aes_gcm_encrypt_base64_string(key, nonce, plaintext)
|
|
171
|
+
return key, nonce, token, ciphertext
|
|
172
|
+
|
|
173
|
+
def encrypt_string_with_key(self, key: bytes, nonce: bytes, plaintext: str) -> str:
|
|
174
|
+
"""encrypt_string_with_key encrypt plaintext with ECIES DH protocol"""
|
|
175
|
+
# Encrypt message using AES-GCM
|
|
176
|
+
ciphertext = aes_gcm_encrypt_base64_string(key, nonce, plaintext)
|
|
177
|
+
return ciphertext
|
|
178
|
+
|
|
179
|
+
def decrypt_string_with_key(self, key: bytes, nonce: bytes, ciphertext: str) -> str:
|
|
180
|
+
"""decrypt_string_with_key decrypt ciphertext with ECIES DH protocol"""
|
|
181
|
+
# Decrypt message using AES-GCM
|
|
182
|
+
return aes_gcm_decrypt_base64_string(key, nonce, ciphertext)
|
|
183
|
+
|
|
184
|
+
def generate_ecies_key_pair(self) -> Tuple[bytes, bytes, str]:
|
|
185
|
+
"""generate_ecies_key_pair generate ECIES key pair"""
|
|
186
|
+
from cryptography.hazmat.primitives import hashes
|
|
187
|
+
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
|
188
|
+
from cryptography.hazmat.primitives.asymmetric import ec
|
|
189
|
+
|
|
190
|
+
# Generate an ephemeral elliptic curve scalar and point
|
|
191
|
+
peer_private_key = ec.generate_private_key(self._curve)
|
|
192
|
+
dh = peer_private_key.exchange(ec.ECDH(), self._public_key)
|
|
193
|
+
R = peer_private_key.public_key().public_numbers()
|
|
194
|
+
|
|
195
|
+
# Derive symmetric key and nonce via HKDF
|
|
196
|
+
length = 32 + 12
|
|
197
|
+
buf = HKDF(
|
|
198
|
+
algorithm=hashes.SHA256(),
|
|
199
|
+
length=length,
|
|
200
|
+
salt=None,
|
|
201
|
+
info=None,
|
|
202
|
+
).derive(dh)
|
|
203
|
+
key = buf[:32]
|
|
204
|
+
nonce = buf[32:length]
|
|
205
|
+
|
|
206
|
+
token = marshal_cryptography_pub_key(R)
|
|
207
|
+
return key, nonce, base64.b64encode(token).decode()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import logging
|
|
3
|
+
|
|
4
|
+
logger: logging.Logger = logging.getLogger("ark")
|
|
5
|
+
httpx_logger: logging.Logger = logging.getLogger("httpx")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _basic_config() -> None:
|
|
9
|
+
logging.basicConfig(
|
|
10
|
+
format="%(asctime)s %(levelname)s %(message)s",
|
|
11
|
+
datefmt="%Y-%m-%d %H:%M:%S",
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def setup_logging() -> None:
|
|
16
|
+
env = os.environ.get("ARK_LOG")
|
|
17
|
+
if env == "debug":
|
|
18
|
+
_basic_config()
|
|
19
|
+
logger.setLevel(logging.DEBUG)
|
|
20
|
+
httpx_logger.setLevel(logging.DEBUG)
|
|
21
|
+
elif env == "info":
|
|
22
|
+
_basic_config()
|
|
23
|
+
logger.setLevel(logging.INFO)
|
|
24
|
+
httpx_logger.setLevel(logging.INFO)
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import time
|
|
3
|
+
import uuid
|
|
4
|
+
import threading
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class _QuerySet(object):
|
|
8
|
+
def __init__(self):
|
|
9
|
+
self._items = list()
|
|
10
|
+
self._index = dict()
|
|
11
|
+
self._lock = threading.Lock()
|
|
12
|
+
|
|
13
|
+
def add(self, item: int) -> None:
|
|
14
|
+
with self._lock:
|
|
15
|
+
if item in self._index:
|
|
16
|
+
return
|
|
17
|
+
|
|
18
|
+
self._items.append(item)
|
|
19
|
+
self._index[item] = len(self._items) - 1
|
|
20
|
+
|
|
21
|
+
def remove(self, item: int) -> None:
|
|
22
|
+
with self._lock:
|
|
23
|
+
if item not in self._index:
|
|
24
|
+
return
|
|
25
|
+
|
|
26
|
+
index = self._index[item]
|
|
27
|
+
self._items[index] = self._items[-1]
|
|
28
|
+
self._index[self._items[-1]] = index
|
|
29
|
+
self._items.pop()
|
|
30
|
+
del self._index[item]
|
|
31
|
+
|
|
32
|
+
def query(self, item: int) -> int:
|
|
33
|
+
with self._lock:
|
|
34
|
+
return self._index[item]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ModelBreaker(object):
|
|
38
|
+
def __init__(self):
|
|
39
|
+
# 初始化 allow_time 为当前时间
|
|
40
|
+
self._allow_time = time.perf_counter()
|
|
41
|
+
self._waiters = _QuerySet()
|
|
42
|
+
|
|
43
|
+
def _allow(self, id: int) -> bool:
|
|
44
|
+
cur = time.perf_counter()
|
|
45
|
+
# 如果当前时间小于等于 allow_time,不允许通过
|
|
46
|
+
if cur <= self._allow_time:
|
|
47
|
+
return 0
|
|
48
|
+
# 如果当前时间与 allow_time 的差值大于 10,允许通过
|
|
49
|
+
if cur - self._allow_time > 10:
|
|
50
|
+
return True
|
|
51
|
+
# 如果当前时间与 allow_time 的差值小于等于 10,慢启动通过
|
|
52
|
+
return self._waiters.query(id) < 2 ** (cur - self._allow_time)
|
|
53
|
+
|
|
54
|
+
def _get_allowed_duration(self) -> float:
|
|
55
|
+
# 计算当前时间与 allow_time 之间的持续时间
|
|
56
|
+
allow_duration = self._allow_time - time.perf_counter()
|
|
57
|
+
|
|
58
|
+
# 至少有 1 秒的等待时间
|
|
59
|
+
return max(allow_duration, 1)
|
|
60
|
+
|
|
61
|
+
def _acquire(self) -> int:
|
|
62
|
+
id = uuid.uuid4().int
|
|
63
|
+
self._waiters.add(id)
|
|
64
|
+
return id
|
|
65
|
+
|
|
66
|
+
def _release(self, id: int) -> None:
|
|
67
|
+
self._waiters.remove(id)
|
|
68
|
+
|
|
69
|
+
def reset(self, duration: float) -> None:
|
|
70
|
+
# 将 allow_time 重置为当前时间加上指定的持续时间
|
|
71
|
+
self._allow_time = time.perf_counter() + duration
|
|
72
|
+
|
|
73
|
+
def wait(self) -> None:
|
|
74
|
+
id = self._acquire()
|
|
75
|
+
while not self._allow(id):
|
|
76
|
+
time.sleep(self._get_allowed_duration())
|
|
77
|
+
self._release(id)
|
|
78
|
+
|
|
79
|
+
async def asyncwait(self) -> None:
|
|
80
|
+
id = self._acquire()
|
|
81
|
+
while not self._allow(id):
|
|
82
|
+
await asyncio.sleep(self._get_allowed_duration())
|
|
83
|
+
self._release(id)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
from typing_extensions import Iterator, AsyncIterator
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def consume_sync_iterator(iterator: Iterator[Any]) -> None:
|
|
6
|
+
for _ in iterator:
|
|
7
|
+
...
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def consume_async_iterator(iterator: AsyncIterator[Any]) -> None:
|
|
11
|
+
async for _ in iterator:
|
|
12
|
+
...
|