Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL][CORE] moves Socks5Logic out of XMPPConnection #869

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

srossbach
Copy link
Contributor

The whole logic regarding the Socks5Proxy is now handled in its own
class.


socks5Proxy = null;

if (socks5ProxyPort < 0) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<=

core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
Comment on lines 87 to 88
final Socks5Proxy newSocks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy = newSocks5Proxy;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final Socks5Proxy newSocks5Proxy = Socks5Proxy.getSocks5Proxy();
socks5Proxy = newSocks5Proxy;
socks5Proxy = Socks5Proxy.getSocks5Proxy();

Do you need the "newSocks5Proxy" variable? It is used by l. 137, l. 159. I see no need for an additional variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not found any information if lambdas act as closures nor not. The lambda should definitely not access the socks5Proxy variable as it can already be null inside the threads.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean

ThreadUtils.runSafeAsync(
"saros-stun-discovery",
log,
() -> {
discoverAndPublishStunAddresses(socks5Proxy, stunServerAddress, stunServerPort);
});
?
In this case socks5Proxy is used from the heap, so formally this is a concurrency problem which is unlikely (disableProxy() would need to run before...).

core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
return;
}

Socks5Proxy.getSocks5Proxy().stop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would lead to a NPE if called before the "enableProxy" method. I don't know whether it is worth to check for null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a C&P, socks5Proxy.stop() is the correct call.

core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
private static void addSocks5ProxyAddresses(
final Socks5Proxy proxy, final Collection<String> addresses, boolean inFront) {

synchronized (socks5AddressReplacementLock) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my own understanding: I would assume that you could make the method synchronized without the extra object, because synchronized <methodname> locks this and synchronized static locks <ClassName>.class. Is this true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I could also synchronize on the class variable. I could also make this method non static however it should not use synchronized(this) then.

core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved

if (!upnpService.isMapped(device, socks5ProxyPort, IUPnPService.TCP)) return;

if (!upnpService.deletePortMapping(device, socks5ProxyPort, IUPnPService.TCP)) {
Copy link
Member

@tobous tobous Mar 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move call out of if condition to make it clearer that it actually has an effect?

Suggested change
if (!upnpService.deletePortMapping(device, socks5ProxyPort, IUPnPService.TCP)) {
boolean portMappingDeletionSuccessful =
upnpService.deletePortMapping(device, socks5ProxyPort, IUPnPService.TCP);
if (!portMappingDeletionSuccessful) {

core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
}
}

try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block only tears down the upnp service. But during the setup, you also initialized the stun service. Does it not require a "manual" teardown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Stun service does only lookup some stuff while the UPNP stuff may setup a permanent port mapping. I.e the retrieved addresses goes to no longer valid socks5 proxy instance and therefore it does not matter. I can also change the code to not even wait for the stun process to finish at all, it would not matter.


upnpService.deletePortMapping(device, socks5ProxyPort, IUPnPService.TCP);

if (!upnpService.createPortMapping(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move call that actually has an effect out of if condition?

* @return <code>true</code> if the proxy was successfully started, <code>false</code> if it could
* not be started or is already running
*/
public synchronized boolean enableProxy(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old logic called socks5Proxy.start() in the setup. Is such a call no longer necessary?

Sames goes for socks5Proxy.stop() in the teardown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socks 5 proxy will automatically start if you call getSocks5Proxy() after setting setLocalSocks5ProxyEnabled(true)

I still have I hard time as the Smack source code is no longer available and I cannot find the source anymore for the version we are currently using.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
+ socks5Proxy.getPort()
+ " [listening on all interfaces]");

final List<String> proxyAddressesToPublish = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not directly assign the collected / copied lists?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because proxyAddresses is a collection and not a list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?
you could do proxyAddressesToPublish = new ArrayList<>(proxyAddresses);

core/src/saros/net/xmpp/Socks5ProxySupport.java Outdated Show resolved Hide resolved
The whole logic regarding the Socks5Proxy is now handled in its own
class.

In addition only the STUN discovery is performed asynchronous.

Assuming the UPNP router works as expected mapping and unmapping ports
just takes a few milliseconds.
@saros-infrastructure
Copy link

Codacy Here is an overview of what got changed by this pull request:

Complexity increasing per file
==============================
- core/src/saros/net/xmpp/Socks5ProxySupport.java  17
         

Complexity decreasing per file
==============================
+ core/src/saros/net/xmpp/XMPPConnectionService.java  -3
         

See the complete overview on Codacy

@m273d15
Copy link
Contributor

m273d15 commented Jun 25, 2020

@srossbach Is this ready to review?

@srossbach
Copy link
Contributor Author

Yes.

return;
}

log.info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to else block and remove return from if to improve readability/make code paths easier to understand?

+ "]");
}

log.info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Move to else block?

Otherwise, you should add a return statement to the if block as currently the "successful" message is still also displayed on failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants