mirror of
https://github.com/transmission/transmission.git
synced 2025-12-24 20:35:36 +00:00
Factor out session ID into a standalone entity
This commit is contained in:
@@ -46,6 +46,7 @@ set(${PROJECT_NAME}_SOURCES
|
||||
rpcimpl.c
|
||||
rpc-server.c
|
||||
session.c
|
||||
session-id.c
|
||||
stats.c
|
||||
torrent.c
|
||||
torrent-ctor.c
|
||||
@@ -105,6 +106,7 @@ set(${PROJECT_NAME}_PUBLIC_HEADERS
|
||||
makemeta.h
|
||||
quark.h
|
||||
rpcimpl.h
|
||||
session-id.h
|
||||
tr-getopt.h
|
||||
transmission.h
|
||||
utils.h
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "rpcimpl.h"
|
||||
#include "rpc-server.h"
|
||||
#include "session.h"
|
||||
#include "session-id.h"
|
||||
#include "trevent.h"
|
||||
#include "utils.h"
|
||||
#include "variant.h"
|
||||
@@ -63,9 +64,6 @@ struct tr_rpc_server
|
||||
char * whitelistStr;
|
||||
tr_list * whitelist;
|
||||
|
||||
char * sessionId;
|
||||
time_t sessionIdExpiresAt;
|
||||
|
||||
bool isStreamInitialized;
|
||||
z_stream stream;
|
||||
};
|
||||
@@ -81,33 +79,12 @@ struct tr_rpc_server
|
||||
****
|
||||
***/
|
||||
|
||||
static char*
|
||||
static const char *
|
||||
get_current_session_id (struct tr_rpc_server * server)
|
||||
{
|
||||
const time_t now = tr_time ();
|
||||
|
||||
if (!server->sessionId || (now >= server->sessionIdExpiresAt))
|
||||
{
|
||||
int i;
|
||||
const int n = 48;
|
||||
const char * pool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const size_t pool_size = strlen (pool);
|
||||
unsigned char * buf = tr_new (unsigned char, n+1);
|
||||
|
||||
tr_rand_buffer (buf, n);
|
||||
for (i=0; i<n; ++i)
|
||||
buf[i] = pool[ buf[i] % pool_size ];
|
||||
buf[n] = '\0';
|
||||
|
||||
tr_free (server->sessionId);
|
||||
server->sessionId = (char*) buf;
|
||||
server->sessionIdExpiresAt = now + (60*60); /* expire in an hour */
|
||||
}
|
||||
|
||||
return server->sessionId;
|
||||
return tr_session_id_get_current (server->session->session_id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
***
|
||||
**/
|
||||
@@ -1004,7 +981,6 @@ closeServer (void * vserver)
|
||||
if (s->isStreamInitialized)
|
||||
deflateEnd (&s->stream);
|
||||
tr_free (s->url);
|
||||
tr_free (s->sessionId);
|
||||
tr_free (s->whitelistStr);
|
||||
tr_free (s->username);
|
||||
tr_free (s->password);
|
||||
|
||||
74
libtransmission/session-id.c
Normal file
74
libtransmission/session-id.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* This file Copyright (C) 2016 Mnemosyne LLC
|
||||
*
|
||||
* It may be used under the GNU GPL versions 2 or 3
|
||||
* or any future license endorsed by Mnemosyne LLC.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "transmission.h"
|
||||
#include "crypto-utils.h"
|
||||
#include "session-id.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define SESSION_ID_SIZE 48
|
||||
#define SESSION_ID_DURATION_SEC (60 * 60) /* expire in an hour */
|
||||
|
||||
struct tr_session_id
|
||||
{
|
||||
char * current_value;
|
||||
char * previous_value;
|
||||
time_t expires_at;
|
||||
};
|
||||
|
||||
static char *
|
||||
generate_new_session_id_value (void)
|
||||
{
|
||||
const char pool[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const size_t pool_size = sizeof (pool) - 1;
|
||||
|
||||
char * buf = tr_new (char, SESSION_ID_SIZE + 1);
|
||||
|
||||
tr_rand_buffer (buf, SESSION_ID_SIZE);
|
||||
for (size_t i = 0; i < SESSION_ID_SIZE; ++i)
|
||||
buf[i] = pool[(unsigned char) buf[i] % pool_size];
|
||||
buf[SESSION_ID_SIZE] = '\0';
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
tr_session_id_t
|
||||
tr_session_id_new (void)
|
||||
{
|
||||
return tr_new0 (struct tr_session_id, 1);
|
||||
}
|
||||
|
||||
void
|
||||
tr_session_id_free (tr_session_id_t session_id)
|
||||
{
|
||||
if (session_id == NULL)
|
||||
return;
|
||||
|
||||
tr_free (session_id->previous_value);
|
||||
tr_free (session_id->current_value);
|
||||
tr_free (session_id);
|
||||
}
|
||||
|
||||
const char *
|
||||
tr_session_id_get_current (tr_session_id_t session_id)
|
||||
{
|
||||
const time_t now = tr_time ();
|
||||
|
||||
if (session_id->current_value == NULL || now >= session_id->expires_at)
|
||||
{
|
||||
tr_free (session_id->previous_value);
|
||||
session_id->previous_value = session_id->current_value;
|
||||
session_id->current_value = generate_new_session_id_value ();
|
||||
session_id->expires_at = now + SESSION_ID_DURATION_SEC;
|
||||
}
|
||||
|
||||
return session_id->current_value;
|
||||
}
|
||||
43
libtransmission/session-id.h
Normal file
43
libtransmission/session-id.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* This file Copyright (C) 2016 Mnemosyne LLC
|
||||
*
|
||||
* It may be used under the GNU GPL versions 2 or 3
|
||||
* or any future license endorsed by Mnemosyne LLC.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct tr_session_id * tr_session_id_t;
|
||||
|
||||
/**
|
||||
* Create new session identifier object.
|
||||
*
|
||||
* @return New session identifier object.
|
||||
*/
|
||||
tr_session_id_t tr_session_id_new (void);
|
||||
|
||||
/**
|
||||
* Free session identifier object.
|
||||
*
|
||||
* @param[in] session_id Session identifier object.
|
||||
*/
|
||||
void tr_session_id_free (tr_session_id_t session_id);
|
||||
|
||||
/**
|
||||
* Get current session identifier as string.
|
||||
*
|
||||
* @param[in] session_id Session identifier object.
|
||||
*
|
||||
* @return String representation of current session identifier.
|
||||
*/
|
||||
const char * tr_session_id_get_current (tr_session_id_t session_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
#include "port-forwarding.h"
|
||||
#include "rpc-server.h"
|
||||
#include "session.h"
|
||||
#include "session-id.h"
|
||||
#include "stats.h"
|
||||
#include "torrent.h"
|
||||
#include "tr-dht.h" /* tr_dhtUpkeep () */
|
||||
@@ -599,6 +600,7 @@ tr_sessionInit (const char * configDir,
|
||||
session->lock = tr_lockNew ();
|
||||
session->cache = tr_cacheNew (1024*1024*2);
|
||||
session->magicNumber = SESSION_MAGIC_NUMBER;
|
||||
session->session_id = tr_session_id_new ();
|
||||
tr_bandwidthConstruct (&session->bandwidth, session, NULL);
|
||||
tr_variantInitList (&session->removedTorrents, 0);
|
||||
|
||||
@@ -1955,6 +1957,7 @@ tr_sessionClose (tr_session * session)
|
||||
tr_variantFree (&session->removedTorrents);
|
||||
tr_bandwidthDestruct (&session->bandwidth);
|
||||
tr_bitfieldDestruct (&session->turtle.minutes);
|
||||
tr_session_id_free (session->session_id);
|
||||
tr_lockFree (session->lock);
|
||||
if (session->metainfoLookup)
|
||||
{
|
||||
|
||||
@@ -196,6 +196,7 @@ struct tr_session
|
||||
|
||||
struct tr_web * web;
|
||||
|
||||
struct tr_session_id * session_id;
|
||||
struct tr_rpc_server * rpcServer;
|
||||
tr_rpc_func rpc_func;
|
||||
void * rpc_func_user_data;
|
||||
|
||||
Reference in New Issue
Block a user