diff --git a/libretroshare/src/friend_server/fsclient.cc b/libretroshare/src/friend_server/fsclient.cc index 1da758250..4104b0e6a 100644 --- a/libretroshare/src/friend_server/fsclient.cc +++ b/libretroshare/src/friend_server/fsclient.cc @@ -25,8 +25,11 @@ #include "fsclient.h" #include "pqi/pqifdbin.h" +#include "pqi/pqiproxy.h" -bool FsClient::requestFriends(const std::string& address,uint16_t port,uint32_t reqs,std::map& 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& friend_certificates) { // 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; std::list response; - sendItem(address,port,pitem,response); + sendItem(address,port,proxy_address,proxy_port,pitem,response); // now decode the response @@ -84,11 +87,13 @@ void FsClient::handleServerResponse(RsFriendServerServerResponseItem *item) // friend_certificates.insert(it); } -bool FsClient::sendItem(const std::string& address,uint16_t port,RsItem *item,std::list& 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& response) { // 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; 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_port = htons(port); - ipOfServer.sin_addr.s_addr = inet_addr(address.c_str()); + ipOfServer.sin_port = htons(proxy_port); + ipOfServer.sin_addr.s_addr = inet_addr(proxy_address.c_str()); 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; } + // 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. - 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; RsSerialiser *rss = new RsSerialiser(); // deleted by ~pqistreamer() rss->addSerialType(fss); diff --git a/libretroshare/src/friend_server/fsclient.h b/libretroshare/src/friend_server/fsclient.h index 4c17c142d..b682a8bc4 100644 --- a/libretroshare/src/friend_server/fsclient.h +++ b/libretroshare/src/friend_server/fsclient.h @@ -31,7 +31,9 @@ class FsClient: public PQInterface public: FsClient() :PQInterface(RsPeerId()) {} - bool requestFriends(const std::string& address,uint16_t port,uint32_t reqs,std::map& 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& friend_certificates); protected: // Implements PQInterface @@ -41,7 +43,10 @@ protected: RsItem *GetItem() override; private: - bool sendItem(const std::string &address, uint16_t port, RsItem *item, std::list &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 &response); + void handleServerResponse(RsFriendServerServerResponseItem *item); std::list mIncomingItems; diff --git a/libretroshare/src/friend_server/fsmanager.cc b/libretroshare/src/friend_server/fsmanager.cc index 199bb99d7..ff6571ef7 100644 --- a/libretroshare/src/friend_server/fsmanager.cc +++ b/libretroshare/src/friend_server/fsmanager.cc @@ -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 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_PROXY_PORT = 9050; FriendServerManager::FriendServerManager() { mLastFriendReqestCampain = 0; mFriendsToRequest = DEFAULT_FRIENDS_TO_REQUEST; - mServerAddress = DEFAULT_FRIEND_SERVER_ADDRESS; + mProxyAddress = DEFAULT_PROXY_ADDRESS; + mProxyPort = DEFAULT_PROXY_PORT; mServerPort = DEFAULT_FRIEND_SERVER_PORT; } void FriendServerManager::startServer() @@ -48,7 +50,11 @@ void FriendServerManager::setServerAddress(const std::string& addr,uint16_t port mServerAddress = addr; mServerPort = port; } - +void FriendServerManager::setProxyAddress(const std::string& addr,uint16_t port) +{ + mProxyAddress = addr; + mProxyPort = port; +} void FriendServerManager::setFriendsToRequest(uint32_t n) { mFriendsToRequest = n; @@ -59,6 +65,11 @@ void FriendServerManager::threadTick() std::cerr << "Ticking FriendServerManager..." << std::endl; 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 std::vector friends; @@ -101,7 +112,7 @@ void FriendServerManager::threadTick() std::cerr << "Requesting new friends to friend server..." << std::endl; std::map 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; diff --git a/libretroshare/src/friend_server/fsmanager.h b/libretroshare/src/friend_server/fsmanager.h index ef5887afa..583a5febc 100644 --- a/libretroshare/src/friend_server/fsmanager.h +++ b/libretroshare/src/friend_server/fsmanager.h @@ -28,6 +28,7 @@ public: virtual void checkServerAddress_async(const std::string& addr,uint16_t, const std::function& callback) 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 uint32_t friendsToRequest() override { return mFriendsToRequest ; } @@ -45,4 +46,6 @@ private: std::map mPeers; std::string mServerAddress ; uint16_t mServerPort; + std::string mProxyAddress ; + uint16_t mProxyPort; }; diff --git a/libretroshare/src/pqi/pqiproxy.cc b/libretroshare/src/pqi/pqiproxy.cc index 1fd8fccec..008466847 100644 --- a/libretroshare/src/pqi/pqiproxy.cc +++ b/libretroshare/src/pqi/pqiproxy.cc @@ -5,7 +5,7 @@ //#define PROXY_DEBUG 1 -int pqiproxyconnection::proxy_negotiate_connection(int sockfd) +int pqiproxyconnection::proxy_negociate_connection(int sockfd) { int ret = 0; switch(mProxyState) diff --git a/libretroshare/src/pqi/pqiproxy.h b/libretroshare/src/pqi/pqiproxy.h index 121139817..6862342bb 100644 --- a/libretroshare/src/pqi/pqiproxy.h +++ b/libretroshare/src/pqi/pqiproxy.h @@ -36,6 +36,8 @@ public: PROXY_STATE_CONNECTION_COMPLETE = 0x04 }; + pqiproxyconnection() : mProxyState(PROXY_STATE_INIT) {} + /*! * \brief proxy_negotiate_connection * 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. * 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 setRemoteAddress(const std::string& s) { mDomainAddress = s; } diff --git a/libretroshare/src/pqi/pqisslproxy.cc b/libretroshare/src/pqi/pqisslproxy.cc index 3c22b04c5..8ae2b162d 100644 --- a/libretroshare/src/pqi/pqisslproxy.cc +++ b/libretroshare/src/pqi/pqisslproxy.cc @@ -97,7 +97,7 @@ int pqisslproxy::Basic_Connection_Complete() if(proxyConnectionState() == PROXY_STATE_INIT && 1!=(ret=pqissl::Basic_Connection_Complete())) return ret; // basic connection not complete. - ret = proxy_negotiate_connection(sockfd); + ret = proxy_negociate_connection(sockfd); if(ret < 0) reset_locked(); diff --git a/libretroshare/src/retroshare/rsfriendserver.h b/libretroshare/src/retroshare/rsfriendserver.h index df9951cee..80bf2a029 100644 --- a/libretroshare/src/retroshare/rsfriendserver.h +++ b/libretroshare/src/retroshare/rsfriendserver.h @@ -28,6 +28,7 @@ public: virtual void checkServerAddress_async(const std::string& addr,uint16_t, const std::function& callback) =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 uint32_t friendsToRequest() =0;