Skip to content

Instantly share code, notes, and snippets.

@jfloff
Last active September 9, 2021 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfloff/2f19b9860e643ae45993eb2e1a9785ff to your computer and use it in GitHub Desktop.
Save jfloff/2f19b9860e643ae45993eb2e1a9785ff to your computer and use it in GitHub Desktop.
Intercepting Socket.getInputStream using AspectJ LTW
<!-- aop.xml with the options for launching LTW -->
<aspectj>
<aspects>
<aspect name="SocketOut"/>
</aspects>
<weaver options="-showWeaveInfo -Xset:weaveJavaPackages=true">
<include within="java.net.Socket"/>
</weaver>
</aspectj>
// Client: reads a string from stdin and sends it to the Server. Waits for the same message from the server.
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String[] args) throws IOException
{
Socket socket = new Socket("127.0.0.1", 1234);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromClient, fromServer;
do
{
System.out.print("? ");
fromClient = stdIn.readLine();
out.println(fromClient);
fromServer = in.readLine();
System.out.println("Server: " + fromServer);
} while (!fromServer.equals("quit"));
in.close();
out.close();
socket.close();
}
}
**Client:**
WE'VE FOUND java.net.Socket.getInputStream!!!
>> Client.java:11
? hello
Server: hello
? bye
Server: bye
**Server:**
WE'VE FOUND java.net.Socket.getInputStream!!!
>> Server.java:20
Echoing #0 : hello
Echoing #1 : bye
// Server: receives a string from the client, stores it in a local Redis server and echos it back to the client
import redis.clients.jedis.Jedis;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args) throws IOException
{
// connect to redis
Jedis jedis = new Jedis("localhost");
// connect to streams
ServerSocket serverSocket = new ServerSocket(1234);
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// send/rcv messages
String fromClient;
int keyCounter = 0;
do
{
fromClient = in.readLine();
String key = String.valueOf(keyCounter++);
// set and get client message from Redis
jedis.set(key, fromClient);
System.out.println("Echoing #" + key + " : " + jedis.get(key));
out.println(fromClient);
}
while (!fromClient.equals("quit"));
// close all connections
in.close();
out.close();
socket.close();
serverSocket.close();
}
}
// Aspect: intercept all call join points where Socket.getInputStream is called and prints the source location
// It's called in both Server.java and Client.java, but it's also called upon Redis.set (Server.java:31).
// Only the join points at the Server.java and Client.java are woven.
public aspect SocketOut
{
after(): !within(SocketOut) && call(* java.net.Socket.getInputStream(..))
{
System.out.println("WE'VE FOUND java.net.Socket.getInputStream!!!");
System.out.println(">> \t" + thisJoinPoint.getSourceLocation());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment