diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fcb19bf --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..fdc392f --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c18ac9f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..54d3c88 --- /dev/null +++ b/build.gradle @@ -0,0 +1,23 @@ +plugins { + id 'java' + id 'com.github.johnrengelman.shadow' version '8.1.1' +} + +group = 'tech.nevets' +version = '1.0' + +repositories { + mavenCentral() +} + +dependencies { + implementation "com.miglayout:miglayout:3.7.4" +} + +jar { + manifest { + attributes( + 'Main-Class': 'tech.nevets.packetmirror.Main' + ) + } +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..e8d9d3b --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Thu Jul 25 14:02:09 EDT 2024 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..6890276 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'PacketMirror' + diff --git a/src/main/java/tech/nevets/packetmirror/HomePanel.java b/src/main/java/tech/nevets/packetmirror/HomePanel.java new file mode 100644 index 0000000..4c6a2eb --- /dev/null +++ b/src/main/java/tech/nevets/packetmirror/HomePanel.java @@ -0,0 +1,268 @@ +/* + * Created by JFormDesigner on Thu Jul 25 14:05:38 EDT 2024 + */ + +package tech.nevets.packetmirror; + +import java.awt.event.*; +import java.io.IOException; +import javax.swing.*; +import net.miginfocom.swing.*; + +/** + * @author steven + */ +public class HomePanel extends JPanel { + private boolean send = true; + private PServer server; + + public HomePanel() { + initComponents(); + } + + private void sendBtn(ActionEvent e) { + startBtn.setText("Send"); + send = true; + hostLabel.setVisible(true); + hostField.setVisible(true); + dataLabel.setVisible(true); + dataArea.setVisible(true); + dataScroll.setVisible(true); + responseLabel.setText("Response"); + stopBtn.setVisible(false); + } + + private void receiveBtn(ActionEvent e) { + startBtn.setText("Start"); + send = false; + hostLabel.setVisible(false); + hostField.setVisible(false); + dataLabel.setVisible(false); + dataArea.setVisible(false); + dataScroll.setVisible(false); + responseLabel.setText("Data"); + stopBtn.setVisible(true); + } + + private void startBtn(ActionEvent e) { + if (send) { + switch (((String) protocol.getSelectedItem()).toLowerCase()) { + case "tcp" -> { + try { + statusLabel.setText("Sending..."); + responseArea.setText(TCPClient.sendPacket(hostField.getText(), Integer.parseInt(portField.getText()), dataArea.getText())); + statusLabel.setText("Sent"); + } catch (IOException ex) { + statusLabel.setText("Error 1"); + errorLabel.setText(ex.getMessage()); + ex.printStackTrace(); + } + } + case "udp" -> { + try { + statusLabel.setText("Sending..."); + responseArea.setText(UDPClient.sendDatagram(hostField.getText(), Integer.parseInt(portField.getText()), dataArea.getText())); + statusLabel.setText("Sent"); + } catch (IOException ex) { + statusLabel.setText("Error 2"); + errorLabel.setText(ex.getMessage()); + ex.printStackTrace(); + } + } + } + } else { + startBtn.setEnabled(false); + stopBtn.setEnabled(true); + switch (((String) protocol.getSelectedItem()).toLowerCase()) { + case "tcp" -> { + try { + statusLabel.setText("Running..."); + server = new TCPServer(Integer.parseInt(portField.getText())); + server.start(responseArea, statusLabel, errorLabel); + } catch (IOException ex) { + statusLabel.setText("Error 3"); + errorLabel.setText(ex.getMessage()); + ex.printStackTrace(); + } + } + case "udp" -> { + try { + statusLabel.setText("Running..."); + server = new UDPServer(Integer.parseInt(portField.getText())); + server.start(responseArea, statusLabel, errorLabel); + } catch (IOException ex) { + statusLabel.setText("Error 4"); + errorLabel.setText(ex.getMessage()); + ex.printStackTrace(); + } + } + } + } + } + + private void resetBtn(ActionEvent e) { + hostField.setText(""); + portField.setText(""); + dataArea.setText(""); + statusLabel.setText(""); + responseArea.setText(""); + errorLabel.setText(""); + } + + private void stopBtn(ActionEvent e) { + server.stop(); + statusLabel.setText("Server Stopped"); + startBtn.setEnabled(true); + stopBtn.setEnabled(false); + } + + private void initComponents() { + // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off + protoLabel = new JLabel(); + protocol = new JComboBox(); + typeLabel = new JLabel(); + sendBtn = new JRadioButton(); + receiveBtn = new JRadioButton(); + ButtonGroup sndRecBtnGroup = new ButtonGroup(); + dataLabel = new JLabel(); + hostLabel = new JLabel(); + hostField = new JTextField(); + dataScroll = new JScrollPane(); + dataArea = new JTextArea(); + portLabel = new JLabel(); + portField = new JTextField(); + responseLabel = new JLabel(); + responseScroll = new JScrollPane(); + responseArea = new JTextArea(); + statusLabel = new JLabel(); + startBtn = new JButton(); + stopBtn = new JButton(); + resetBtn = new JButton(); + errorLabel = new JLabel(); + + //======== this ======== + setLayout(new MigLayout( + "hidemode 3", + // columns + "[shrink 0,fill]" + + "[fill]" + + "[100,shrink 0,fill]" + + "[grow,shrink 0,fill]" + + "[fill]" + + "[fill]" + + "[fill]" + + "[shrink 0,fill]", + // rows + "[]" + + "[]" + + "[]" + + "[shrink 0]" + + "[shrink 0]" + + "[bottom]" + + "[grow]" + + "[]" + + "[]" + + "[]")); + + //---- protoLabel ---- + protoLabel.setText("Protocol"); + add(protoLabel, "cell 1 1"); + + //---- protocol ---- + protocol.setModel(new DefaultComboBoxModel<>(new String[]{"TCP", "UDP"})); + add(protocol, "cell 2 1,alignx leading,growx 0"); + + //---- typeLabel ---- + typeLabel.setText("Client Type"); + add(typeLabel, "cell 4 1"); + + //---- sendBtn ---- + sendBtn.setText("Send"); + sendBtn.setSelected(true); + sendBtn.addActionListener(e -> sendBtn(e)); + add(sendBtn, "cell 5 1"); + + //---- receiveBtn ---- + receiveBtn.setText("Receive"); + receiveBtn.addActionListener(e -> receiveBtn(e)); + sndRecBtnGroup.add(sendBtn); + sndRecBtnGroup.add(receiveBtn); + add(receiveBtn, "cell 6 1"); + + //---- dataLabel ---- + dataLabel.setText("Data"); + add(dataLabel, "cell 5 2"); + + //---- hostLabel ---- + hostLabel.setText("Host"); + add(hostLabel, "cell 1 3"); + add(hostField, "cell 2 3"); + + //======== dataScroll ======== + { + dataScroll.setViewportView(dataArea); + } + add(dataScroll, "cell 4 3 3 2,grow"); + + //---- portLabel ---- + portLabel.setText("Port"); + add(portLabel, "cell 1 4"); + add(portField, "cell 2 4"); + + //---- responseLabel ---- + responseLabel.setText("Response"); + add(responseLabel, "cell 3 5"); + + //======== responseScroll ======== + { + + //---- responseArea ---- + responseArea.setEditable(false); + responseScroll.setViewportView(responseArea); + } + add(responseScroll, "cell 2 6 3 2,grow"); + add(statusLabel, "cell 2 8"); + + //---- startBtn ---- + startBtn.setText("Send"); + startBtn.addActionListener(e -> startBtn(e)); + add(startBtn, "cell 3 8,alignx center,growx 0"); + + //---- stopBtn ---- + stopBtn.setText("Stop"); + stopBtn.setEnabled(false); + stopBtn.setVisible(false); + stopBtn.addActionListener(e -> stopBtn(e)); + add(stopBtn, "cell 4 8"); + + //---- resetBtn ---- + resetBtn.setText("Clear"); + resetBtn.addActionListener(e -> resetBtn(e)); + add(resetBtn, "cell 6 8"); + add(errorLabel, "cell 1 9 6 1"); + // JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on + } + + // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off + private JLabel protoLabel; + private JComboBox protocol; + private JLabel typeLabel; + private JRadioButton sendBtn; + private JRadioButton receiveBtn; + private JLabel dataLabel; + private JLabel hostLabel; + private JTextField hostField; + private JScrollPane dataScroll; + private JTextArea dataArea; + private JLabel portLabel; + private JTextField portField; + private JLabel responseLabel; + private JScrollPane responseScroll; + private JTextArea responseArea; + private JLabel statusLabel; + private JButton startBtn; + private JButton stopBtn; + private JButton resetBtn; + private JLabel errorLabel; + // JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on +} diff --git a/src/main/java/tech/nevets/packetmirror/HomePanel.jfd b/src/main/java/tech/nevets/packetmirror/HomePanel.jfd new file mode 100644 index 0000000..e467665 --- /dev/null +++ b/src/main/java/tech/nevets/packetmirror/HomePanel.jfd @@ -0,0 +1,142 @@ +JFDML JFormDesigner: "8.2.3.0.386" Java: "17.0.9" encoding: "UTF-8" + +new FormModel { + contentType: "form/swing" + root: new FormRoot { + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { + "$layoutConstraints": "hidemode 3" + "$columnConstraints": "[shrink 0,fill][fill][100,shrink 0,fill][grow,shrink 0,fill][fill][fill][fill][shrink 0,fill]" + "$rowConstraints": "[][][][shrink 0][shrink 0][bottom][grow][][][]" + } ) { + name: "this" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "protoLabel" + "text": "Protocol" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 1" + } ) + add( new FormComponent( "javax.swing.JComboBox" ) { + name: "protocol" + auxiliary() { + "JavaCodeGenerator.preInitCode": "protocol.setModel(new DefaultComboBoxModel<>(new String[]{\"TCP\", \"UDP\"}));" + } + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 1,alignx leading,growx 0" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "typeLabel" + "text": "Client Type" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 4 1" + } ) + add( new FormComponent( "javax.swing.JRadioButton" ) { + name: "sendBtn" + "text": "Send" + "selected": true + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "sendBtn", true ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 1" + } ) + add( new FormComponent( "javax.swing.JRadioButton" ) { + name: "receiveBtn" + "text": "Receive" + auxiliary() { + "JavaCodeGenerator.postInitCode": "sndRecBtnGroup.add(sendBtn);\nsndRecBtnGroup.add(receiveBtn);" + "JavaCodeGenerator.postCreateCode": "ButtonGroup sndRecBtnGroup = new ButtonGroup();" + } + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "receiveBtn", true ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 6 1" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "dataLabel" + "text": "Data" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 5 2" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "hostLabel" + "text": "Host" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 3" + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "hostField" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 3" + } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "dataScroll" + add( new FormComponent( "javax.swing.JTextArea" ) { + name: "dataArea" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 4 3 3 2,grow" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "portLabel" + "text": "Port" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 4" + } ) + add( new FormComponent( "javax.swing.JTextField" ) { + name: "portField" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 4" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "responseLabel" + "text": "Response" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 3 5" + } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "responseScroll" + add( new FormComponent( "javax.swing.JTextArea" ) { + name: "responseArea" + auxiliary() { + "JavaCodeGenerator.postInitCode": "responseArea.setEditable(false);" + } + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 6 3 2,grow" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "statusLabel" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 8" + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "startBtn" + "text": "Send" + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "startBtn", true ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 3 8,alignx center,growx 0" + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "stopBtn" + "text": "Stop" + "enabled": false + "visible": false + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "stopBtn", true ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 4 8" + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "resetBtn" + "text": "Clear" + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "resetBtn", true ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 6 8" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "errorLabel" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 9 6 1" + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 0, 0 ) + "size": new java.awt.Dimension( 515, 300 ) + } ) + } +} diff --git a/src/main/java/tech/nevets/packetmirror/Main.java b/src/main/java/tech/nevets/packetmirror/Main.java new file mode 100644 index 0000000..386a229 --- /dev/null +++ b/src/main/java/tech/nevets/packetmirror/Main.java @@ -0,0 +1,26 @@ +package tech.nevets.packetmirror; + +import javax.swing.*; + +public class Main { + static JFrame frame = new JFrame(); + + public static void main(String[] args) { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception e) { + //e.printStackTrace(); + } + + frame.add(new HomePanel()); + frame.pack(); + frame.setSize(420, 300); + frame.setResizable(false); + frame.setTitle("Packet Mirror"); + frame.setLocationRelativeTo(null); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + SwingUtilities.updateComponentTreeUI(frame); + frame.setVisible(true); + + } +} diff --git a/src/main/java/tech/nevets/packetmirror/PServer.java b/src/main/java/tech/nevets/packetmirror/PServer.java new file mode 100644 index 0000000..6081163 --- /dev/null +++ b/src/main/java/tech/nevets/packetmirror/PServer.java @@ -0,0 +1,10 @@ +package tech.nevets.packetmirror; + +import javax.swing.*; +import java.io.IOException; + +public interface PServer { + void start(JTextArea textArea, JLabel statusLabel, JLabel errorLabel); + + void stop(); +} diff --git a/src/main/java/tech/nevets/packetmirror/TCPClient.java b/src/main/java/tech/nevets/packetmirror/TCPClient.java new file mode 100644 index 0000000..814c799 --- /dev/null +++ b/src/main/java/tech/nevets/packetmirror/TCPClient.java @@ -0,0 +1,30 @@ +package tech.nevets.packetmirror; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; + +public class TCPClient { + public static String sendPacket(String host, int port, String data) throws IOException { + Socket s = new Socket(host, port); + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + + os.write(data.getBytes(StandardCharsets.UTF_8)); + os.write(0xf); + os.flush(); + + byte[] buf = new byte[1024]; + int i = 0; + for (byte b = (byte) is.read(); b != 0xf; b = (byte) is.read()) { + buf[i] = b; + i++; + } + + s.close(); + + return new String(buf); + } +} diff --git a/src/main/java/tech/nevets/packetmirror/TCPServer.java b/src/main/java/tech/nevets/packetmirror/TCPServer.java new file mode 100644 index 0000000..0c8c47d --- /dev/null +++ b/src/main/java/tech/nevets/packetmirror/TCPServer.java @@ -0,0 +1,69 @@ +package tech.nevets.packetmirror; + +import javax.swing.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; + +public class TCPServer implements PServer { + private ServerSocket ss; + private boolean running = true; + + public TCPServer(int port) throws IOException { + ss = new ServerSocket(port); + } + + @Override + public void start(JTextArea textArea, JLabel statusLabel, JLabel errorLabel) { + new Thread(() -> { + while (running) { + try { + Socket s = ss.accept(); + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + + byte[] buf = new byte[1024]; + int i = 0; + for (byte b = (byte) is.read(); b != 0xf; b = (byte) is.read()) { + buf[i] = b; + i++; + } + + textArea.setText(new String(buf)); + os.write(buf, 0, i); + os.write(0xf); + os.flush(); + } catch (IOException e) { + switch (e.getMessage().toLowerCase()) { + case "socket closed" -> { + statusLabel.setText("Server stopped"); + return; + } + case "connection reset" -> { + statusLabel.setText("Client disconnected"); + return; + } + } + statusLabel.setText("Error tser"); + errorLabel.setText(e.getMessage()); + e.printStackTrace(); + } + } + + }).start(); + } + + @Override + public void stop() { + running = false; + try { + ss.close(); + } catch (IOException e) { + System.out.println("error stop tcpserver"); + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/tech/nevets/packetmirror/UDPClient.java b/src/main/java/tech/nevets/packetmirror/UDPClient.java new file mode 100644 index 0000000..014f627 --- /dev/null +++ b/src/main/java/tech/nevets/packetmirror/UDPClient.java @@ -0,0 +1,26 @@ +package tech.nevets.packetmirror; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; + +public class UDPClient { + + public static String sendDatagram(String host, int port, String data) throws IOException { + DatagramSocket socket = new DatagramSocket(); + InetAddress address = InetAddress.getByName(host); + byte[] buf = data.getBytes(); + + DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port); + socket.send(packet); + packet = new DatagramPacket(buf, buf.length); + socket.receive(packet); + + String response = new String(packet.getData(), 0, packet.getLength()); + socket.close(); + + return response; + } + +} diff --git a/src/main/java/tech/nevets/packetmirror/UDPServer.java b/src/main/java/tech/nevets/packetmirror/UDPServer.java new file mode 100644 index 0000000..b7975fe --- /dev/null +++ b/src/main/java/tech/nevets/packetmirror/UDPServer.java @@ -0,0 +1,49 @@ +package tech.nevets.packetmirror; + +import javax.swing.*; +import java.io.IOException; +import java.net.*; + +public class UDPServer implements PServer { + private DatagramSocket s; + private boolean running = true; + + public UDPServer(int port) throws SocketException { + s = new DatagramSocket(port); + } + + @Override + public void start(JTextArea textArea, JLabel statusLabel, JLabel errorLabel) { + new Thread(() -> { + while (running) { + try { + byte[] buf = new byte[1024]; + DatagramPacket packet = new DatagramPacket(buf, buf.length); + s.receive(packet); + + InetAddress address = packet.getAddress(); + int port = packet.getPort(); + + packet = new DatagramPacket(buf, packet.getLength(), address, port); + textArea.setText(new String(packet.getData(), 0, packet.getLength())); + + s.send(packet); + } catch (IOException e) { + if (e.getMessage().toLowerCase().equals("socket closed")) { + statusLabel.setText("Client disconnected"); + return; + } + statusLabel.setText("Error udpserver"); + errorLabel.setText(e.getMessage()); + e.printStackTrace(); + } + } + }).start(); + } + + @Override + public void stop() { + running = false; + s.close(); + } +}