Jump to content
gerwinb500

Script only works when using debugger

Recommended Posts

Currently I'm working on a script that supports communication trough Pusher (websockets). For this I use the Pusher-websocket-java library. I've been trying to get some code working that connects to Pusher and subscribes to a Presence channel. Connecting to Pusher is no problem at all, but when I try to subscribe to the prescence channel I receive an error. After some trial and error, I decided to try and debug the script with intellij as explained here. This is how I found out that the code works whenever I start the OsBot client from intellij. I believe it has something to do with the this:

VW3N1sk.jpg

Based on this I think the problem is that the Pusher library is not build the right way. But I don't get any missing library errors, and connecting to Pusher does work which uses the same library....

I added the Pusher library to the output root. I think this is the right way to use external libraries in OsBot?

In short: The Pusher library only works whenever I use the debugger. 

Any suggestions on how to fix this?

 

Link to post
Share on other sites

Interestingly you mention an error many times but you don't show us what the error is...

I think it's possible your problem is that your artifact does not have the dependencies in it. For example, if you're using maven for your pusher dependencies, you can't just build your artifact and expect the dependencies to come with it. You might need to configure maven to build an artifact with the dependencies. But then again, you need to show us your errors.

Link to post
Share on other sites

Thank you for your reaction.

You are right, I should've put the error in. Here it is:

Authentication failure due to [Unable to parse response from Authorizer: {"auth":"315578f9acfb71d039d6:f0487dc4d6e28bfed9a556366afb2ff8ef660dc36de5864f6a426f696a028406","channel_data":"{\"user_id\":5,\"user_info\":{\"id\":5,\"name\":\"name\",\"email\":\"name@name.com\",\"type\":\"bot\",\"botpass\":\"pass\",\"server\":1,\"is_mule\":0,\"stage\":\"readybot\",\"is_online\":0,\"email_verified_at\":null,\"api_token\":\"{the correct api token}\",\"created_at\":\"2021-01-28T17:14:00.000000Z\",\"updated_at\":\"2021-01-28T17:14:00.000000Z\"}}"}], exception was [com.pusher.client.AuthorizationFailureException: Unable to parse response from Authorizer: {"auth":"315578f9acfb71d039d6:f0487dc4d6e28bfed9a556366afb2ff8ef660dc36de5864f6a426f696a028406","channel_data":"{\"user_id\":5,\"user_info\":{\"id\":5,\"name\":\"name\",\"email\":\"name@name.com\",\"type\":\"bot\",\"botpass\":\"pass\",\"server\":1,\"is_mule\":0,\"stage\":\"readybot\",\"is_online\":0,\"email_verified_at\":null,\"api_token\":\"{the correct api token}\",\"created_at\":\"2021-01-28T17:14:00.000000Z\",\"updated_at\":\"2021-01-28T17:14:00.000000Z\"}}"}]

 As you can see my local http server, which handles the authorization, responses with correct message. Only the message cannot be parsed for some reason. 

The Pusher-websocket-java library I downloaded comes with it's dependencies. I got it from here. Also I checked if the dependencies for the library it self where there, and they where. It contains both the GSON and Websocket dependency. I also tried to download and use the Pusher-websocket-java library without the dependencies, and added the dependencies manually, but I still got the same error.  

My artifacts tab looks tike this:

PO11t0E.jpg

I extracted the Pusher-websocket-java, and some other libraries, into the output root. 

Am I missing something? 

Link to post
Share on other sites
7 hours ago, gerwinb500 said:

Am I missing something? 

So, I've never used pusher but I did some quick googling. This could have happened due to two reasons:

1. Your json is not parsed as string

2. Someone mentioned having problems with async calls (unlikely to be the problem)

Or maybe there's some other reason that I didn't find with my quick googling.

 

Anyway, try logging the response/type of the response. Maybe for some reason your response is a null. Or maybe for some reason the gson library did not work. Just some quick troubleshooting. If this doesn't help, you're gonna have to wait for someone from here who has experience or ask on github.

  • Like 1
Link to post
Share on other sites

Thank you both for your reaction.

@Nbacon Yes, the authentication works when I run it outside of OsBot. Also, the authentication works in OsBot when running OsBot in debug mode as I mentioned above. Maybe I could try your possible fix if you still think it can help?

@Jarl I probably should've mentioned this before, but the code has exited before it even makes a subscribe request to Pusher. If I check in the Pusher debug console, I can see the client has connected.

mTJsfa6.jpg

But I don't see any subscription request/failure in the debug console or the error logs.

CTkFfE3.jpg

So the script throws an exception before it even sends a subscribe request to Pusher. If the problems was one of those you mentioned, I probably should've seen it in the Pusher debug consolse/error logs. I think the problem has something to do with the GSON library. In the error below (also mentioned above) you can see that the server responded correctly. Otherwise the channel_data would not match the correct values.

Authentication failure due to [Unable to parse response from Authorizer: {"auth":"315578f9acfb71d039d6:f0487dc4d6e28bfed9a556366afb2ff8ef660dc36de5864f6a426f696a028406","channel_data":"{\"user_id\":5,\"user_info\":{\"id\":5,\"name\":\"name\",\"email\":\"name@name.com\",\"type\":\"bot\",\"botpass\":\"pass\",\"server\":1,\"is_mule\":0,\"stage\":\"readybot\",\"is_online\":0,\"email_verified_at\":null,\"api_token\":\"{the correct api token}\",\"created_at\":\"2021-01-28T17:14:00.000000Z\",\"updated_at\":\"2021-01-28T17:14:00.000000Z\"}}"}], exception was [com.pusher.client.AuthorizationFailureException: Unable to parse response from Authorizer: {"auth":"315578f9acfb71d039d6:f0487dc4d6e28bfed9a556366afb2ff8ef660dc36de5864f6a426f696a028406","channel_data":"{\"user_id\":5,\"user_info\":{\"id\":5,\"name\":\"name\",\"email\":\"name@name.com\",\"type\":\"bot\",\"botpass\":\"pass\",\"server\":1,\"is_mule\":0,\"stage\":\"readybot\",\"is_online\":0,\"email_verified_at\":null,\"api_token\":\"{the correct api token}\",\"created_at\":\"2021-01-28T17:14:00.000000Z\",\"updated_at\":\"2021-01-28T17:14:00.000000Z\"}}"}]

Logging the response from pusher is not possible because the code throws an exception before sending a subscribe request.

Maybe someone can explain what the difference is between starting OsBot from intellij like I mentioned above, and starting OsBot the normal way? 

Link to post
Share on other sites
46 minutes ago, Jarl said:

Maybe the permissions you get are different?

It seems that you are right. When I try to connect to a public channel where no authorization is required, I get the following exception:

java.security.AccessControlException: access denied ("java.lang.reflect.ReflectPermission" "suppressAccessChecks")
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at org.osbot.LpT7.checkPermission(ns:156)
	at java.lang.reflect.AccessibleObject.setAccessible(Unknown Source)
	at com.google.gson.internal.reflect.PreJava9ReflectionAccessor.makeAccessible(PreJava9ReflectionAccessor.java:31)
	at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:103)
	at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:85)
	at com.google.gson.internal.bind.MapTypeAdapterFactory.create(MapTypeAdapterFactory.java:127)
	at com.google.gson.Gson.getAdapter(Gson.java:458)
	at com.google.gson.Gson.toJson(Gson.java:696)
	at com.google.gson.Gson.toJson(Gson.java:683)
	at com.google.gson.Gson.toJson(Gson.java:638)
	at com.google.gson.Gson.toJson(Gson.java:618)
	at com.pusher.client.channel.impl.ChannelImpl.toSubscribeMessage(ChannelImpl.java:132)
	at com.pusher.client.channel.impl.ChannelManager$1.run(ChannelManager.java:149)
	at com.pusher.client.util.Factory$1.run(Factory.java:119)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

This exception is thrown when this line of code is executed in the pusher-websocket-java library:

GSON.toJson(jsonObject);

This is probably also the reason why subscibing to a presence channel doesn't work. It seems that OsBot blocks some functionallity of the GSON library due to secuirity reasons. I don't know why tho. 

Does anybody know a way to solve this problem? 

Link to post
Share on other sites

Finaly managed to solve the problem. I needed to allow reflection in the OsBot client. Even tough I now get the following uncaught exception:

java.lang.RuntimeException: Unable to invoke no-args constructor for class com.pusher.client.channel.impl.PresenceChannelImpl$Presence. Registering an InstanceCreator with Gson for this type may fix this problem.
	at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:228)
	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
	at com.google.gson.Gson.fromJson(Gson.java:927)
	at com.google.gson.Gson.fromJson(Gson.java:892)
	at com.google.gson.Gson.fromJson(Gson.java:841)
	at com.google.gson.Gson.fromJson(Gson.java:813)
	at com.pusher.client.channel.impl.PresenceChannelImpl.extractPresenceDataFrom(PresenceChannelImpl.java:162)
	at com.pusher.client.channel.impl.PresenceChannelImpl.handleSubscriptionSuccessfulMessage(PresenceChannelImpl.java:100)
	at com.pusher.client.channel.impl.PresenceChannelImpl.onMessage(PresenceChannelImpl.java:58)
	at com.pusher.client.channel.impl.ChannelManager.onMessage(ChannelManager.java:116)
	at com.pusher.client.connection.websocket.WebSocketConnection.handleEvent(WebSocketConnection.java:177)
	at com.pusher.client.connection.websocket.WebSocketConnection.access$600(WebSocketConnection.java:27)
	at com.pusher.client.connection.websocket.WebSocketConnection$6.run(WebSocketConnection.java:261)
	at com.pusher.client.util.Factory$1.run(Factory.java:119)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.UnsupportedOperationException: Cannot allocate class com.pusher.client.channel.impl.PresenceChannelImpl$Presence
	at com.google.gson.internal.UnsafeAllocator$4.newInstance(UnsafeAllocator.java:104)
	at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:225)
	... 16 more

Everything seems to work fine.

Would like to fix this tho. 

Link to post
Share on other sites
33 minutes ago, gerwinb500 said:

Finaly managed to solve the problem. I needed to allow reflection in the OsBot client.

I think you may be better off using another library which doesn't use reflection.


Also, I think you need to get used to reading the errors or trying to google the errors. Tbh, I had suspected the gson library might have been the problem because a while ago, someone else had problems with gson and osbot not allowing reflection. But anyway, read:

java.lang.RuntimeException: Unable to invoke no-args constructor for class com.pusher.client.channel.impl.PresenceChannelImpl$Presence. Registering an InstanceCreator with Gson for this type may fix this problem.

Maybe you need to make a no-args constructor.

Or maybe something else went wrong but there's no way to tell as I cannot see your code.

Link to post
Share on other sites
1 hour ago, Jarl said:

I think you may be better off using another library which doesn't use reflection.


Also, I think you need to get used to reading the errors or trying to google the errors. Tbh, I had suspected the gson library might have been the problem because a while ago, someone else had problems with gson and osbot not allowing reflection. But anyway, read:


java.lang.RuntimeException: Unable to invoke no-args constructor for class com.pusher.client.channel.impl.PresenceChannelImpl$Presence. Registering an InstanceCreator with Gson for this type may fix this problem.

Maybe you need to make a no-args constructor.

Or maybe something else went wrong but there's no way to tell as I cannot see your code.

As far as I can tell there are two other java-pusher libraries which both use the GSON library for JSON parsing. So these probably also use reflection. 

The source code is available on GitHub, so maybe I can do a few adjustments to get rid of the error. I will try your suggestion.   

Thank you for your help.

Link to post
Share on other sites
9 hours ago, gerwinb500 said:

As far as I can tell there are two other java-pusher libraries which both use the GSON library for JSON parsing. So these probably also use reflection.

There are other json libraries you can use, it doesn't have to be related to pusher.

9 hours ago, gerwinb500 said:

The source code is available on GitHub, so maybe I can do a few adjustments to get rid of the error. I will try your suggestion.   

No dude, I think you misunderstood. The problem is not with the pusher code. Google gson user guide and look at the instance creator section. Your problem is with how you use gson.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...