|
| 1 | +import { ProgramInfo } from "../graphics/renderables/device_info"; |
| 2 | +import { EthernetFrame } from "../packets/ethernet"; |
| 3 | +import { IPv4Packet } from "../packets/ip"; |
| 4 | +import { Flags, TcpSegment } from "../packets/tcp"; |
| 5 | +import { NetworkDevice } from "../types/devices"; |
| 6 | +import { DeviceId } from "../types/graphs/datagraph"; |
| 7 | +import { ViewGraph } from "../types/graphs/viewgraph"; |
| 8 | +import { sendRawPacket } from "../types/packet"; |
| 9 | +import { ProgramBase } from "./program_base"; |
| 10 | + |
| 11 | +export class HttpClient extends ProgramBase { |
| 12 | + static readonly PROGRAM_NAME = "Send HTTP request"; |
| 13 | + |
| 14 | + private dstId: DeviceId; |
| 15 | + |
| 16 | + protected _parseInputs(inputs: string[]): void { |
| 17 | + if (inputs.length !== 1) { |
| 18 | + console.error( |
| 19 | + "HttpClient requires 1 input. " + inputs.length + " were given.", |
| 20 | + ); |
| 21 | + return; |
| 22 | + } |
| 23 | + this.dstId = parseInt(inputs[0]); |
| 24 | + } |
| 25 | + |
| 26 | + protected _run() { |
| 27 | + this.sendHttpRequest(); |
| 28 | + this.signalStop(); |
| 29 | + } |
| 30 | + |
| 31 | + protected _stop() { |
| 32 | + // Nothing to do |
| 33 | + } |
| 34 | + |
| 35 | + private sendHttpRequest() { |
| 36 | + const dstDevice = this.viewgraph.getDevice(this.dstId); |
| 37 | + const srcDevice = this.viewgraph.getDevice(this.srcId); |
| 38 | + if (!dstDevice) { |
| 39 | + console.error("Destination device not found"); |
| 40 | + return; |
| 41 | + } |
| 42 | + if ( |
| 43 | + !(srcDevice instanceof NetworkDevice) || |
| 44 | + !(dstDevice instanceof NetworkDevice) |
| 45 | + ) { |
| 46 | + console.log( |
| 47 | + "At least one device between source and destination is not a network device", |
| 48 | + ); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // Encode dummy HTTP request |
| 53 | + const httpRequest = "GET / HTTP/1.1\r\nHost: " + dstDevice.ip + "\r\n\r\n"; |
| 54 | + const content = new TextEncoder().encode(httpRequest); |
| 55 | + |
| 56 | + // Random number between 1024 and 65535 |
| 57 | + const srcPort = Math.floor(Math.random() * (65535 - 1024) + 1024); |
| 58 | + const flags = new Flags(); |
| 59 | + |
| 60 | + // Wrap in TCP segment |
| 61 | + const payload = new TcpSegment(srcPort, 80, 0, 0, flags, content); |
| 62 | + |
| 63 | + // Wrap in IP packet |
| 64 | + const ipPacket = new IPv4Packet(srcDevice.ip, dstDevice.ip, payload); |
| 65 | + const path = this.viewgraph.getPathBetween(this.srcId, this.dstId); |
| 66 | + let dstMac = dstDevice.mac; |
| 67 | + if (!path) return; |
| 68 | + for (const id of path.slice(1)) { |
| 69 | + const device = this.viewgraph.getDevice(id); |
| 70 | + // if there’s a router in the middle, first send frame to router mac |
| 71 | + if (device instanceof NetworkDevice) { |
| 72 | + dstMac = device.mac; |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + const ethernetFrame = new EthernetFrame(srcDevice.mac, dstMac, ipPacket); |
| 77 | + sendRawPacket(this.viewgraph, this.srcId, ethernetFrame); |
| 78 | + } |
| 79 | + |
| 80 | + static getProgramInfo(viewgraph: ViewGraph, srcId: DeviceId): ProgramInfo { |
| 81 | + const programInfo = new ProgramInfo(this.PROGRAM_NAME); |
| 82 | + programInfo.withDestinationDropdown(viewgraph, srcId); |
| 83 | + return programInfo; |
| 84 | + } |
| 85 | +} |
0 commit comments