Connection is created in proxies and its purpose is to handle connection between client ⇄ proxy ⇄ server. It reads data from client/server and sends it to PETEP using process method and writes data to client/server from queues.
In order to manage connections, you have to implement ConnectionManager or use the default connection manager class.
/*
* PEnetration TEsting Proxy (PETEP)
*
* Copyright (C) 2020 Michal Válka
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If
* not, see <https://www.gnu.org/licenses/>.
*/
/**
* Connection in PETEP proxy has to handle both connection between client and proxy and between
* proxy and server. Contains two outgoing queues that contain PDUs that should be sent in a given
* direction (C2S / S2C).
*/
@PetepAPI
public abstract class Connection {
/** Unique nummeric identifier of the connection. */
protected final int id;
/** Parent proxy. */
protected final Proxy proxy;
/** Outgoing queue in direction C2S (client -> server). */
protected final PduQueue queueC2S;
/** Outgoing queue in direction S2C (client <- server). */
protected final PduQueue queueS2C;
/** Connection constructor. */
public Connection(int id, Proxy proxy) {
this.id = id;
this.proxy = proxy;
this.queueC2S = new PduQueue();
this.queueS2C = new PduQueue();
}
public int getId() {
return id;
}
/** Sends PDU outside the PETEP. */
public final void send(PDU pdu) {
if (pdu.getDestination() == PduDestination.SERVER) {
queueC2S.add(pdu);
} else {
queueS2C.add(pdu);
}
}
/** Sends PDU outside the PETEP in direction C2S (client -> server). */
public final void sendC2S(PDU pdu) {
queueC2S.add(pdu);
}
/** Sends PDU outside the PETEP in direction S2C (client <- server). */
public final void sendS2C(PDU pdu) {
queueS2C.add(pdu);
}
/** Processes PDU in PETEP core. */
protected final void process(PDU pdu) {
proxy.getHelper().processPdu(pdu);
}
/** About connection. */
@Override
public String toString() {
return "Connection " + id;
}
/**
* Starts connection.
*
* <p>
* @return Returns true if the start was successful.
*
* <p>
* <b>Attention:</b> this method should return ASAP - it should be used to create threads and then
* return immediately.
*/
public abstract boolean start();
/** Stops connection. */
public abstract void stop();
}
You do not have to create your own PDU class and you can use or extend DefaultPdu class that builds PDU on top of byte arrays.
/*
* PEnetration TEsting Proxy (PETEP)
*
* Copyright (C) 2020 Michal Válka
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If
* not, see <https://www.gnu.org/licenses/>.
*/
/**
* Abstract class of connection manager that could be used by internal or external modules to work
* with connections of a given proxy.
*/
@PetepAPI
public abstract class ConnectionManager {
/** Returns connection by ID. */
public abstract Connection get(int id);
/** Adds connection to the connection manager. */
public abstract boolean add(Connection connection);
/** Removes connection from the connection manager. */
public abstract boolean remove(Connection connection);
/** Removes connection from the connection manager. */
public abstract Connection remove(int id);
/** Returns list of connections. */
public abstract List<Connection> getList();
/** Stops all connections. */
public abstract void stop();
}
Default Connection Manager class is build on top of concurrent hashmap that uses integer identifier as the key.
/*
* PEnetration TEsting Proxy (PETEP)
*
* Copyright (C) 2020 Michal Válka
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If
* not, see <https://www.gnu.org/licenses/>.
*/
/** Default connection manager that uses ConcurrentHashMap for connection storage. */
@PetepAPI
public class DefaultConnectionManager extends ConnectionManager {
/** Map of connections. */
protected final ConcurrentHashMap<Integer, Connection> connections;
/** ID of last connection. */
private int lastId;
public DefaultConnectionManager() {
connections = new ConcurrentHashMap<>();
lastId = 0;
}
@Override
public Connection get(int id) {
return connections.get(id);
}
@Override
public boolean add(Connection connection) {
return connections.putIfAbsent(connection.getId(), connection) == null;
}
@Override
public boolean remove(Connection connection) {
return connections.remove(connection.getId(), connection);
}
@Override
public Connection remove(int id) {
return connections.remove(id);
}
@Override
public List<Connection> getList() {
return new ArrayList<>(connections.values());
}
@Override
public void stop() {
connections.values().parallelStream().forEach(Connection::stop);
}
/** Generates new id for connection. */
public synchronized int nextId() {
return lastId++;
}
}