rawandahmad698 / noble-tls

TLS-Spoofing HTTP library, based on requests. Automatically updates JA3 fingerprints.
MIT License
161 stars 14 forks source link

[NEW FEATURE] how to close the session manually like session.close() #14

Open xishandong opened 1 week ago

xishandong commented 1 week ago

When I use noble_tls, I need to frequently create new sessions to request URLs, but I find that after a while, the system's memory usage becomes quite high. It turns out that the connections are occupying a large amount of memory. When I shut down and restart the application service (as opposed to ending the Python process), the memory is released. Here is my usage demo. Is there a method to support manually closing connections to release session objects?

xishandong commented 1 week ago

@app.post("/v4/universal/get") async def universal_get(body: UniversalModel): url = body.url universal_headers = json.loads(body.headers) universal_proxy = body.proxy global_session = noble_tls.Session(client=identifier(), random_tls_extension_order=True) response = await global_session.get(url, headers=universal_headers, proxy=universal_proxy) return Response(content=response.text, status_code=response.status_code)

xishandong commented 1 week ago
def close(self) -> str:
    destroy_session_payload = {
        "sessionId": self._session_id
    }

    destroy_session_response = destroySession(dumps(destroy_session_payload).encode('utf-8'))
    # we dereference the pointer to a byte array
    destroy_session_response_bytes = ctypes.string_at(destroy_session_response)
    # convert our byte array to a string (tls client returns json)
    destroy_session_response_string = destroy_session_response_bytes.decode('utf-8')
    # convert response string to json
    destroy_session_response_object = loads(destroy_session_response_string)

    free_memory(destroy_session_response_object['id'].encode('utf-8'))

    return destroy_session_response_string
xishandong commented 1 week ago

Here is the close method I mimicked from tls-client. Will this method also effectively release memory in an asynchronous context?

xishandong commented 1 week ago

Based on my testing, this approach doesn't seem to have a significant impact. Do you have any recommended methods or similar implementations that could support memory release needs?