fixed FriendServer client to use Tor proxy

This commit is contained in:
csoler 2022-01-09 17:46:51 +01:00
parent e7eb606a4a
commit 3bd9188a33
8 changed files with 59 additions and 25 deletions

View File

@ -25,8 +25,11 @@
#include "fsclient.h" #include "fsclient.h"
#include "pqi/pqifdbin.h" #include "pqi/pqifdbin.h"
#include "pqi/pqiproxy.h"
bool FsClient::requestFriends(const std::string& address,uint16_t port,uint32_t reqs,std::map<std::string,bool>& friend_certificates) bool FsClient::requestFriends(const std::string& address,uint16_t port,
const std::string& proxy_address,uint16_t proxy_port,
uint32_t reqs,std::map<std::string,bool>& friend_certificates)
{ {
// send our own certificate to publish and expects response frmo the server , decrypts it and reutnrs friend list // send our own certificate to publish and expects response frmo the server , decrypts it and reutnrs friend list
@ -47,7 +50,7 @@ bool FsClient::requestFriends(const std::string& address,uint16_t port,uint32_t
pitem->short_invite = short_invite; pitem->short_invite = short_invite;
std::list<RsItem*> response; std::list<RsItem*> response;
sendItem(address,port,pitem,response); sendItem(address,port,proxy_address,proxy_port,pitem,response);
// now decode the response // now decode the response
@ -84,11 +87,13 @@ void FsClient::handleServerResponse(RsFriendServerServerResponseItem *item)
// friend_certificates.insert(it); // friend_certificates.insert(it);
} }
bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,std::list<RsItem*>& response) bool FsClient::sendItem(const std::string& server_address,uint16_t server_port,
const std::string& proxy_address,uint16_t proxy_port,
RsItem *item,std::list<RsItem*>& response)
{ {
// open a connection // open a connection
RsDbg() << "Sending item to friend server at \"" << address << ":" << port ; RsDbg() << "Sending item to friend server at \"" << server_address << ":" << server_port << " through proxy " << proxy_address << ":" << proxy_port;
int CreateSocket = 0; int CreateSocket = 0;
char dataReceived[1024]; char dataReceived[1024];
@ -103,26 +108,33 @@ bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,st
} }
ipOfServer.sin_family = AF_INET; ipOfServer.sin_family = AF_INET;
ipOfServer.sin_port = htons(port); ipOfServer.sin_port = htons(proxy_port);
ipOfServer.sin_addr.s_addr = inet_addr(address.c_str()); ipOfServer.sin_addr.s_addr = inet_addr(proxy_address.c_str());
if(connect(CreateSocket, (struct sockaddr *)&ipOfServer, sizeof(ipOfServer))<0) if(connect(CreateSocket, (struct sockaddr *)&ipOfServer, sizeof(ipOfServer))<0)
{ {
printf("Connection failed due to port and ip problems, or server is not available\n"); printf("Connection to proxy failed due to port and ip problems, or proxy is not available\n");
return false; return false;
} }
// Now connect to the proxy
int ret=0;
pqiproxyconnection proxy;
proxy.setRemoteAddress(server_address);
proxy.setRemotePort(server_port);
while(1 != (ret = proxy.proxy_negociate_connection(CreateSocket)))
if(ret < 0)
{
RsErr() << "FriendServer client: Connection problem to the proxy!" ;
return false;
}
else
std::this_thread::sleep_for(std::chrono::milliseconds(200));
// Serialise the item and send it. // Serialise the item and send it.
uint32_t size = RsSerialiser::MAX_SERIAL_SIZE;
RsTemporaryMemory data(size);
if(!data)
{
RsErr() << "Cannot allocate memory to send item!" << std::endl;
return false;
}
FsSerializer *fss = new FsSerializer; FsSerializer *fss = new FsSerializer;
RsSerialiser *rss = new RsSerialiser(); // deleted by ~pqistreamer() RsSerialiser *rss = new RsSerialiser(); // deleted by ~pqistreamer()
rss->addSerialType(fss); rss->addSerialType(fss);

View File

@ -31,7 +31,9 @@ class FsClient: public PQInterface
public: public:
FsClient() :PQInterface(RsPeerId()) {} FsClient() :PQInterface(RsPeerId()) {}
bool requestFriends(const std::string& address,uint16_t port,uint32_t reqs,std::map<std::string,bool>& friend_certificates); bool requestFriends(const std::string& address, uint16_t port,
const std::string &proxy_address, uint16_t proxy_port,
uint32_t reqs, std::map<std::string,bool>& friend_certificates);
protected: protected:
// Implements PQInterface // Implements PQInterface
@ -41,7 +43,10 @@ protected:
RsItem *GetItem() override; RsItem *GetItem() override;
private: private:
bool sendItem(const std::string &address, uint16_t port, RsItem *item, std::list<RsItem *> &response); bool sendItem(const std::string &server_address, uint16_t server_port,
const std::string &proxy_address, uint16_t proxy_port,
RsItem *item, std::list<RsItem *> &response);
void handleServerResponse(RsFriendServerServerResponseItem *item); void handleServerResponse(RsFriendServerServerResponseItem *item);
std::list<RsItem*> mIncomingItems; std::list<RsItem*> mIncomingItems;

View File

@ -8,15 +8,17 @@ static const rstime_t MIN_DELAY_BETWEEN_FS_REQUESTS = 30;
static const rstime_t MAX_DELAY_BETWEEN_FS_REQUESTS = 3600; static const rstime_t MAX_DELAY_BETWEEN_FS_REQUESTS = 3600;
static const uint32_t DEFAULT_FRIENDS_TO_REQUEST = 10; static const uint32_t DEFAULT_FRIENDS_TO_REQUEST = 10;
static const std::string DEFAULT_FRIEND_SERVER_ADDRESS = "127.0.0.1"; static const std::string DEFAULT_PROXY_ADDRESS = "127.0.0.1";
static const uint16_t DEFAULT_FRIEND_SERVER_PORT = 2017; static const uint16_t DEFAULT_FRIEND_SERVER_PORT = 2017;
static const uint16_t DEFAULT_PROXY_PORT = 9050;
FriendServerManager::FriendServerManager() FriendServerManager::FriendServerManager()
{ {
mLastFriendReqestCampain = 0; mLastFriendReqestCampain = 0;
mFriendsToRequest = DEFAULT_FRIENDS_TO_REQUEST; mFriendsToRequest = DEFAULT_FRIENDS_TO_REQUEST;
mServerAddress = DEFAULT_FRIEND_SERVER_ADDRESS; mProxyAddress = DEFAULT_PROXY_ADDRESS;
mProxyPort = DEFAULT_PROXY_PORT;
mServerPort = DEFAULT_FRIEND_SERVER_PORT; mServerPort = DEFAULT_FRIEND_SERVER_PORT;
} }
void FriendServerManager::startServer() void FriendServerManager::startServer()
@ -48,7 +50,11 @@ void FriendServerManager::setServerAddress(const std::string& addr,uint16_t port
mServerAddress = addr; mServerAddress = addr;
mServerPort = port; mServerPort = port;
} }
void FriendServerManager::setProxyAddress(const std::string& addr,uint16_t port)
{
mProxyAddress = addr;
mProxyPort = port;
}
void FriendServerManager::setFriendsToRequest(uint32_t n) void FriendServerManager::setFriendsToRequest(uint32_t n)
{ {
mFriendsToRequest = n; mFriendsToRequest = n;
@ -59,6 +65,11 @@ void FriendServerManager::threadTick()
std::cerr << "Ticking FriendServerManager..." << std::endl; std::cerr << "Ticking FriendServerManager..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); std::this_thread::sleep_for(std::chrono::seconds(2));
if(mServerAddress.empty())
{
RsErr() << "No friend server address has been setup. This is probably a bug.";
return;
}
// Check for requests. Compute how much to wait based on how many friends we have already // Check for requests. Compute how much to wait based on how many friends we have already
std::vector<RsPgpId> friends; std::vector<RsPgpId> friends;
@ -101,7 +112,7 @@ void FriendServerManager::threadTick()
std::cerr << "Requesting new friends to friend server..." << std::endl; std::cerr << "Requesting new friends to friend server..." << std::endl;
std::map<std::string,bool> friend_certificates; std::map<std::string,bool> friend_certificates;
FsClient().requestFriends(mServerAddress,mServerPort,mFriendsToRequest,friend_certificates); // blocking call FsClient().requestFriends(mServerAddress,mServerPort,mProxyAddress,mProxyPort,mFriendsToRequest,friend_certificates); // blocking call
std::cerr << "Got the following list of friend certificates:" << std::endl; std::cerr << "Got the following list of friend certificates:" << std::endl;

View File

@ -28,6 +28,7 @@ public:
virtual void checkServerAddress_async(const std::string& addr,uint16_t, const std::function<void (const std::string& address,bool result_status)>& callback) override ; virtual void checkServerAddress_async(const std::string& addr,uint16_t, const std::function<void (const std::string& address,bool result_status)>& callback) override ;
virtual void setServerAddress(const std::string&,uint16_t) override ; virtual void setServerAddress(const std::string&,uint16_t) override ;
virtual void setProxyAddress(const std::string&,uint16_t) override ;
virtual void setFriendsToRequest(uint32_t) override ; virtual void setFriendsToRequest(uint32_t) override ;
virtual uint32_t friendsToRequest() override { return mFriendsToRequest ; } virtual uint32_t friendsToRequest() override { return mFriendsToRequest ; }
@ -45,4 +46,6 @@ private:
std::map<RsPeerId, FriendServerPeerInfo> mPeers; std::map<RsPeerId, FriendServerPeerInfo> mPeers;
std::string mServerAddress ; std::string mServerAddress ;
uint16_t mServerPort; uint16_t mServerPort;
std::string mProxyAddress ;
uint16_t mProxyPort;
}; };

View File

@ -5,7 +5,7 @@
//#define PROXY_DEBUG 1 //#define PROXY_DEBUG 1
int pqiproxyconnection::proxy_negotiate_connection(int sockfd) int pqiproxyconnection::proxy_negociate_connection(int sockfd)
{ {
int ret = 0; int ret = 0;
switch(mProxyState) switch(mProxyState)

View File

@ -36,6 +36,8 @@ public:
PROXY_STATE_CONNECTION_COMPLETE = 0x04 PROXY_STATE_CONNECTION_COMPLETE = 0x04
}; };
pqiproxyconnection() : mProxyState(PROXY_STATE_INIT) {}
/*! /*!
* \brief proxy_negotiate_connection * \brief proxy_negotiate_connection
* Negotiate the connection with the proxy that is connected with openned socket sockfd. The caller needs to * Negotiate the connection with the proxy that is connected with openned socket sockfd. The caller needs to
@ -46,7 +48,7 @@ public:
* 0 : in progress. The function needs to be called again asap. * 0 : in progress. The function needs to be called again asap.
* 1 : proxy connection is fully negociated. Client can send data to the socket. * 1 : proxy connection is fully negociated. Client can send data to the socket.
*/ */
int proxy_negotiate_connection(int sockfd); int proxy_negociate_connection(int sockfd);
void setRemotePort(uint16_t v) { mRemotePort = v; } void setRemotePort(uint16_t v) { mRemotePort = v; }
void setRemoteAddress(const std::string& s) { mDomainAddress = s; } void setRemoteAddress(const std::string& s) { mDomainAddress = s; }

View File

@ -97,7 +97,7 @@ int pqisslproxy::Basic_Connection_Complete()
if(proxyConnectionState() == PROXY_STATE_INIT && 1!=(ret=pqissl::Basic_Connection_Complete())) if(proxyConnectionState() == PROXY_STATE_INIT && 1!=(ret=pqissl::Basic_Connection_Complete()))
return ret; // basic connection not complete. return ret; // basic connection not complete.
ret = proxy_negotiate_connection(sockfd); ret = proxy_negociate_connection(sockfd);
if(ret < 0) if(ret < 0)
reset_locked(); reset_locked();

View File

@ -28,6 +28,7 @@ public:
virtual void checkServerAddress_async(const std::string& addr,uint16_t, const std::function<void (const std::string& address,bool result_status)>& callback) =0; virtual void checkServerAddress_async(const std::string& addr,uint16_t, const std::function<void (const std::string& address,bool result_status)>& callback) =0;
virtual void setServerAddress(const std::string&,uint16_t) =0; virtual void setServerAddress(const std::string&,uint16_t) =0;
virtual void setProxyAddress(const std::string&,uint16_t) =0;
virtual void setFriendsToRequest(uint32_t) =0; virtual void setFriendsToRequest(uint32_t) =0;
virtual uint32_t friendsToRequest() =0; virtual uint32_t friendsToRequest() =0;