Skip to content

Instantly share code, notes, and snippets.

@gpolitis
Last active October 26, 2024 09:59
Show Gist options
  • Save gpolitis/368da88d013f82a3fe6c7447c6cf1125 to your computer and use it in GitHub Desktop.
Save gpolitis/368da88d013f82a3fe6c7447c6cf1125 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2024 Georgios Politis
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
/**
* <p>
* SS (SocketSend) is a super tiny Java class implementing a TCP server
* that can be used to send/receive a file to/from a TCP client. It can
* be useful in super restricted environments lacking netcat or even a
* text editor but having a JDK installed. On the server:
* </p>
*
* <pre>
* {@code
* $ cat <<EOF > SS.java
* ... (copy/paste the code) ...
* EOF
* javac SS.java
* java -cp . SS FILENAME
* }
* </pre>
*
* <p>
* If FILENAME exists, then SS will go into TX mode, otherwise it
* will go into RX mode. Then, on the client, you can send/receive the
* file using more traditional tools like netcat.
* </p>
*
* <p>
* If you run SS in a k8s container, you can use kubectl to forward port
* 1234 to connect to SS and transfer files between your computer and
* the container.
* </p>
*/
public class SS {
public static void main(String ... argv) throws IOException {
try (var ss = new ServerSocket()) {
ss.bind(new InetSocketAddress("localhost", 1234));
final var sock = ss.accept();
System.out.println("Connected to " + sock.getInetAddress());
final var f = new File(argv[0]);
final var tx = f.isFile();
final var buf = new byte[2048];
try (
final var in = tx
? new BufferedInputStream(
new FileInputStream(argv[0]))
: sock.getInputStream();
final var out = tx
? sock.getOutputStream()
: new BufferedOutputStream(
new FileOutputStream(argv[0]));
) {
var bytesRead = 0;
var totalBytes = 0;
while ((bytesRead = in.read(buf)) != -1) {
totalBytes += bytesRead;
out.write(buf, 0, bytesRead);
}
System.out.println(
(tx ? "TX" : "RX") + " bytes:"+ totalBytes);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment