-
Notifications
You must be signed in to change notification settings - Fork 6
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
code update #36
base: dev
Are you sure you want to change the base?
code update #36
Conversation
Thank you for your contribution. |
I had to use a rust library to access python since the jython library is old and support python 2.7 public class TokenGrabber{
public static void main(String[] args) {
System.out.println(System.getenv("DISCORD_BOT_TOKEN"));
}
} to avoid this sort of trouble I recommand putting directly the token in the Main.java The compiled files from the library have been deleted, to recreate them just use cargo build after navigating to |
Hello,
Maybe I didn't understand how cargo works or I'm wrong. I apologize if this is the case. About the quick code changes :
Also, if you have a way around the environment variables issue, I'm open to it. I won't put the bot token directly into Main.java because this code ends up on the public Github. I'm thinking of running user code in a Docker container itself. |
Update : time limit and fixesTimelimitI also added a time limit to run process actually of 60 secs, done to avoid running infinite loop for nothing. It can be changed in the strings.json file. FixesFixing error on python by replacing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, sorry for the late, not as much time as I hoped.
As said in the comments, I'm not a huge fan of reflection because of all the trouble it brings.
Aside from that, I added a couple of comments about translating messages in French and putting them in strings.json file.
There are also some quick variable names and code fixes.
However, I like this feature so keep going, it looks great!
Ps: When I started the review, you hadn't implemented a time limit yet and I was ready to suggest it but you did it so ... nicely done!
src/main/resources/strings.json
Outdated
"COMMAND_CODE_LANG_OPTION_DESCRIPTION":"The chosen language", | ||
"COMMAND_CODE_FILE_OPTION_DESCRIPTION":"The file to be written and compiled", | ||
"COMMAND_CODE_HELP":"Allow you to run and compile code in the following languages : python,java, rust", | ||
"COMMAND_CODE_TIMELIMIT":"60" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to use the configuration system for this value. It allows moderators to update it on the fly without recompiling the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented it but did n t dive deep into the module configuration code so don t know how to test it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine I'll do it when I find time
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/globalcommand/GlobalCommandModule.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
Code Update
Non added-changes / waiting to be tested } catch (IOException | InterruptedException e) {
->
} catch (IOException e) { in PythonRunner.java due to if (process.waitFor() == 0) throwing #36 (comment) #36 (comment) |
Okay sounds good |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a couple of things I want changes in.
Moreover, in the Java runner, I would like you to implement the possibility of sending Java code without the wrapping by passing an option (maybe "--wrapped" or what...). It would then put the code in a template class with a dummy public static void main and a couple of handy imports. It would allow the execution of such code :
int test = 4;
double sqrt = Math.sqrt(test);
System.out.println(test);
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/PythonRunner.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/globalcommand/GlobalCommandModule.java
Outdated
Show resolved
Hide resolved
So working on implementing the --wrapped, I found it easier to just detect if the code needs to be wrapped public static boolean requiresWrapper(String javaCode) {
boolean hasClass = Pattern.compile("\\bpublic\\s+class\\s+[A-Z][a-zA-Z0-9]*").matcher(javaCode).find();
boolean hasMainMethod = Pattern.compile("\\bpublic\\s+static\\s+void\\s+main\\s*\\(\\s*String\\[\\]\\s+[a-zA-Z0-9]*\\s*\\)").matcher(javaCode).find();
boolean hasImports = Pattern.compile("\\bimport\\s+[a-zA-Z0-9.]+").matcher(javaCode).find();
if (hasClass && hasMainMethod) {
return false;
} else if (hasImports) {
return false;
} else {
return true;
}
}
public static String addWrapper(String input){
return """
import java.util.*;
import java.lang.Math;
public class Wrapper{
public static void main(String[] args){""" +
input +
"""
}
}""";
} using this. |
Yep it looks even better |
Applying the requested changes
Try-CatchRegarding the try catch blocks for running processes, I tried to minimize the number functions they encapsulate however for rust, since the process of compiling and running may create allot of IOException to handle this the process may return 5 error named like this : catch (IOException e) {
e.printStackTrace();
return "Server error code R01" + e.toString();
} wrapper public static boolean requiresWrapper(String javaCode) {
boolean hasClass = Pattern.compile("\\bpublic\\s+class\\s+[A-Z][a-zA-Z0-9]*").matcher(javaCode).find();
boolean hasMainMethod = Pattern.compile("\\bpublic\\s+static\\s+void\\s+main\\s*\\(\\s*String\\[\\]\\s+[a-zA-Z0-9]*\\s*\\)").matcher(javaCode).find();
boolean hasImports = Pattern.compile("\\bimport\\s+[a-zA-Z0-9.]+").matcher(javaCode).find();
if (hasClass && hasMainMethod) {
return false;
} else if (hasImports) {
return false;
} else {
return true;
}
}
public static String addWrapper(String input){
return """
import java.util.*;
import java.lang.Math;
public class Wrapper {
public static void main(String[] args){""" +
input +
"""
}
}""";
} The code used to detect the need for a wrapper doesn t support custom imports, its not planned to be added as its utility seems pointless knowing that every java.util are imported and writing/editing files isn t allowed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left another couple of changes and some batches.
Additionally, if you have time (if not I'll do it), it would be nice to implement a C runner
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
I can add a C compiler but not for now as C is not memory safe and I fear troubles since it has a large amount of exploits, if it is added it will probably be when the dockers get added wich will probably be made after the exam session. For now I have implemented the requested changes |
You're right. I'll implement it later with containers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last changes!
Once done, it should be immediately mergeable (if you rebase onto dev before of course)
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/CodeCommand.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/JavaRunner.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
src/main/java/com/github/hokkaydo/eplbot/module/code/RustCompiler.java
Outdated
Show resolved
Hide resolved
Should be done |
After some testing, here's what I found
For installing rust in the image, adding |
Hello, |
Not rn,
|
Lowercased in appcommand and make it required. If so, you doesn't need to ask for language name in appmodal because the user already inputted it
In that case, I think going for the modal only version is fine.
We'll need both venv and python installation in the docker script |
Applied requested changes
Problem regarding wslWhy do i need to have java installed to firstly build the project and then build the docker image ? To run it on WSL i have to install and setup java and its kinda fastidious, why don't you build the project inside of the docker container during the image build? TestingAs of now it seems to work just fine for file and modal interactions. I plan to keep both options as copy pasting large file is not possible due to the 4000 characters limit on modal text inputs. |
On the paper, I love how you implemented all of that. I'll check on my side to see if everything works, but you did an insane job. Concerning your previous comment, you asked
Indeed I could directly build the jar in the Dockerfile. But anyway it would require you to have Java installed to build the image |
Concerning this, it just came across my mind that you can send "sender-only" replies meaning that he will be the only one able to see (and discard) the message, hence not polluting the channel |
Regarding your suggestion of sender-only, this only works for InteractionEvent replies, while currently the answer gets separated in 3, the interaction response to the slash command with a timestamp to evaluate the running time of the command, then, when the dockers gets destroyed it sends the original code (to know what the command has been called with, I personnaly find it usefull while explaining code to have the answer and the code). And finally the result of the code directly from the docker ( with errors and exit code ). So right now I can only get one response to be sent as ephemeral (user-only). So how I see it is either I completely change how the replies are handled to only send one message that gets updated or to send in dm, but to be honest I'm not found of any of these alternatives as it's against the purpose of the feature : explaining and debugging code wich usually is done with 2 people. I dont think the feature will be a huge succes and be that much used, I see it more as an occasional usefull feature (the idea appeared when I wanted to help for a simple type error). I just suggested that there should be a place to send it so if the sent code is wrong, it doesn't pollute the channel with long error messages. Another reason that I dont like To conclude, I can implement it (more likely the first option) but I personally dont think it would be that much of an use for the contraints it presents (1 file instead of 2 messages) and the overrall utility and goal of the command. Anyway it's up to you to decide but I would rather keep it as is. |
Allow to compile and execute code in rust,java,python
Friendly interface
Using modals to submit code
![image](https://private-user-images.githubusercontent.com/119571167/267095832-eb6d59dd-1397-45fc-8efe-0079ca090309.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAxOTY5MzUsIm5iZiI6MTcyMDE5NjYzNSwicGF0aCI6Ii8xMTk1NzExNjcvMjY3MDk1ODMyLWViNmQ1OWRkLTEzOTctNDVmYy04ZWZlLTAwNzljYTA5MDMwOS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzA1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwNVQxNjIzNTVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0wYzU5NzIyYzY3YTJmYmNiMjdiNjcwYjIzNWMzZmQ2YjIyOTQxMWUwNjY4YTJkMTQzZTE4M2U4ZDM5MWJhZGQyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.JZV-bOxGkYtDnjpvpFGo-Ad_0lLp-W73lMDlEkLtWdo)
Alternatively use file to submit code
![image](https://private-user-images.githubusercontent.com/119571167/267096426-3faf4f59-96b2-479c-899e-aa4c49bc96d2.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAxOTY5MzUsIm5iZiI6MTcyMDE5NjYzNSwicGF0aCI6Ii8xMTk1NzExNjcvMjY3MDk2NDI2LTNmYWY0ZjU5LTk2YjItNDc5Yy04OTllLWFhNGM0OWJjOTZkMi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzA1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwNVQxNjIzNTVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05NzhkOTYyMWQyYjIyMDc0Mjg1ZjhmNWY2ZmNmNjI1YTBmNTFkOTRlYmY2ZTg4MjA4NjViM2UzNGY0NGMwYzVmJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.VaimcXSVVO3PIS23qtOx7-0oKaFVJXrpYc317IuxeyE)
Returns file less than 8mb for long outputs
![image](https://private-user-images.githubusercontent.com/119571167/267096063-cb174209-7e7c-473f-a5fa-b304e47cd5bd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAxOTY5MzUsIm5iZiI6MTcyMDE5NjYzNSwicGF0aCI6Ii8xMTk1NzExNjcvMjY3MDk2MDYzLWNiMTc0MjA5LTdlN2MtNDczZi1hNWZhLWIzMDRlNDdjZDViZC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzA1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwNVQxNjIzNTVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jMjkyZTk2NDE1YmMxMDljMDdiNDQ1MzJmZGIzMTZlNGYwYjVkMzQ2ZDhkM2IxNzVhZmQwYTVhM2E2N2I3ZWJjJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.V9zB_RRqqFapJHHfYOt8g5Q2vwlaQzk5iA5CHVHnPkw)
Setup
Languages
To allow the extension to work properly, you need to have rust setup on the machine, as well as python. Once python installed you will need to use
Environment variables
you will need JAVA_HOME to be a path environment variable that redirect to jdk, something like
C:\Program Files\Java\jdk-20
as well as python and rustc
Security
Since we are running code without dockers (choice made to avoid installing the lib since its not my repo/project)
There are security concerns.
Rust
regarding rust, the
use
andstd
are banned keywords this remove the possibility to import amythingJava
for java "java.lang.reflect","java.io","java.util.zip","java.net","java.nio.file","java.security","java.awt","javax.swing","javax.script","java.util.logging","java.lang.ProcessBuilder","java.lang.Runtime","com","net","org"
are banned however I doubt it patches everything ideally it should run in a docker
Python
for python, it removes
import
,open
,exec
andeval
Overall
usefull to debug and explain code as it renders the errors
![image](https://private-user-images.githubusercontent.com/119571167/267101197-e7a6b7d9-94dc-4e56-99d0-11abc133e286.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAxOTY5MzUsIm5iZiI6MTcyMDE5NjYzNSwicGF0aCI6Ii8xMTk1NzExNjcvMjY3MTAxMTk3LWU3YTZiN2Q5LTk0ZGMtNGU1Ni05OWQwLTExYWJjMTMzZTI4Ni5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzA1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwNVQxNjIzNTVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lYWZmZWYwY2RiYmMzYzE1MDI5N2NmNDdlMzNmYzBlOGNkYWI4NDBhYTEwMzEyOGQwMTdkOGIwM2RhOWQxNjQ0JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.vDP3pph0frwL7QWJwzMpSqHm7QB2VkV0G5XQIhjttcI)
but you might need to consider docker for perfect security