add all the stuff!
This commit is contained in:
commit
541f6707b6
7 changed files with 319 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
Cargo.lock
|
17
Cargo.toml
Normal file
17
Cargo.toml
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
[package]
|
||||||
|
name = "packeteer"
|
||||||
|
description = "An attempt at a Rust library that can be used to assist in programmatically analysing, serving and handling received protocol packets."
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
authors = ["Celeste <colean@colean.cc>"]
|
||||||
|
license = "LGPL-3.0-or-later"
|
||||||
|
homepage = "https://git.colean.cc/packeteer/"
|
||||||
|
repository = "https://git.colean.cc/packeteer/"
|
||||||
|
readme = "README.md"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[features]
|
||||||
|
http1 = []
|
||||||
|
|
||||||
|
[dependencies]
|
66
LICENSE
Normal file
66
LICENSE
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
|
||||||
|
GNU LESSER GENERAL PUBLIC LICENSE
|
||||||
|
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
||||||
|
0. Additional Definitions.
|
||||||
|
|
||||||
|
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
||||||
|
|
||||||
|
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
||||||
|
|
||||||
|
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
||||||
|
|
||||||
|
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
||||||
|
|
||||||
|
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
||||||
|
1. Exception to Section 3 of the GNU GPL.
|
||||||
|
|
||||||
|
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
||||||
|
2. Conveying Modified Versions.
|
||||||
|
|
||||||
|
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
||||||
|
|
||||||
|
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
||||||
|
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
||||||
|
|
||||||
|
3. Object Code Incorporating Material from Library Header Files.
|
||||||
|
|
||||||
|
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
||||||
|
|
||||||
|
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
||||||
|
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
||||||
|
|
||||||
|
4. Combined Works.
|
||||||
|
|
||||||
|
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
||||||
|
|
||||||
|
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
||||||
|
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
||||||
|
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
||||||
|
d) Do one of the following:
|
||||||
|
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
||||||
|
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
||||||
|
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
||||||
|
|
||||||
|
5. Combined Libraries.
|
||||||
|
|
||||||
|
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
||||||
|
|
||||||
|
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
||||||
|
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
||||||
|
|
||||||
|
6. Revised Versions of the GNU Lesser General Public License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
14
README
Normal file
14
README
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
Packeteer
|
||||||
|
-------------
|
||||||
|
Packeteer is a library that can handle structurally organising protocol packets like HTTP 1.x requests and responses as well as generating, structuring, unpacking and potentially more features depending on the protocol.
|
||||||
|
|
||||||
|
Supported protocols
|
||||||
|
-----------------------
|
||||||
|
* HTTP/1.x
|
||||||
|
|
||||||
|
What Packeteer is not
|
||||||
|
-------------------------
|
||||||
|
* a TCP/UDP stream handling library
|
||||||
|
* a server
|
||||||
|
* a protocol client
|
||||||
|
* the only way to do this, probably
|
15
README.html
Normal file
15
README.html
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<h1>Packeteer</h1>
|
||||||
|
<p>Packeteer is a library that can handle structurally organising protocol packets like HTTP 1.x requests and responses as well as generating, structuring, unpacking and potentially more features depending on the protocol.</p>
|
||||||
|
|
||||||
|
<h2>Supported protocols</h2>
|
||||||
|
<ul>
|
||||||
|
<li>HTTP/1.x</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>What Packeteer is not</h2>
|
||||||
|
<ul>
|
||||||
|
<li>a TCP/UDP stream handling library</li>
|
||||||
|
<li>a server</li>
|
||||||
|
<li>a protocol client</li>
|
||||||
|
<li>the only way to do this, probably</li>
|
||||||
|
</ul>
|
11
README.md
Normal file
11
README.md
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# Packeteer
|
||||||
|
Packeteer is a library that can handle structurally organising protocol packets like HTTP 1.x requests and responses as well as generating, structuring, unpacking and potentially more features depending on the protocol.
|
||||||
|
|
||||||
|
## Supported protocols
|
||||||
|
* HTTP/1.x
|
||||||
|
|
||||||
|
## What Packeteer is _not_
|
||||||
|
* a TCP/UDP stream handling library
|
||||||
|
* a server
|
||||||
|
* a protocol client
|
||||||
|
* the only way to do this, probably
|
194
src/lib.rs
Normal file
194
src/lib.rs
Normal file
|
@ -0,0 +1,194 @@
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct KVHeader {
|
||||||
|
pub key: String,
|
||||||
|
pub value: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_kvheader(key: &str, value: &str) -> KVHeader {
|
||||||
|
let result = KVHeader { key: key.to_string(), value: value.to_string() };
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "http1")]
|
||||||
|
pub mod http1 {
|
||||||
|
use super::{KVHeader, generate_kvheader};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Http1Request {
|
||||||
|
pub method: String,
|
||||||
|
pub location: String,
|
||||||
|
pub version: String,
|
||||||
|
pub headers: Vec<KVHeader>,
|
||||||
|
pub body: String,
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Http1Response {
|
||||||
|
pub version: String,
|
||||||
|
pub status: String,
|
||||||
|
pub headers: Vec<KVHeader>,
|
||||||
|
pub body: String,
|
||||||
|
pub code: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_request(method: &str, host: &str, location: &str, body: &str) -> Http1Request {
|
||||||
|
let hostkv = generate_kvheader("Host", host);
|
||||||
|
let request = Http1Request { method: method.to_string(), location: location.to_string(), version: "1.1".to_string(), headers: vec![hostkv], body: body.to_string() };
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
pub fn generate_response(code: i32, body: &str) -> Http1Response {
|
||||||
|
let mut xcode = code;
|
||||||
|
if code_to_string(xcode) == "500 Internal Server Error" {
|
||||||
|
xcode = 500;
|
||||||
|
}
|
||||||
|
let response = Http1Response { version: "1.1".to_string(), status: code_to_string(xcode), headers: vec![], body: body.to_string(), code: xcode };
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
pub fn construct_request(request: &str) -> Http1Request {
|
||||||
|
let split = request.split("\r\n");
|
||||||
|
let mut request = Http1Request { method: "".to_string(), location: "".to_string(), version: "".to_string(), headers: vec![], body: "".to_string() };
|
||||||
|
let mut reachedbody = false;
|
||||||
|
for v in split {
|
||||||
|
if reachedbody != true {
|
||||||
|
if v.contains("HTTP/1.1") {
|
||||||
|
let split1: Vec<&str> = v.split(" ").collect();
|
||||||
|
request.method = split1[0].to_string();
|
||||||
|
request.location = split1[1].to_string();
|
||||||
|
request.version = "1.1".to_string();
|
||||||
|
} else if v.contains("HTTP/1.0") {
|
||||||
|
let split1: Vec<&str> = v.split(" ").collect();
|
||||||
|
request.method = split1[0].to_string();
|
||||||
|
request.location = split1[1].to_string();
|
||||||
|
request.version = "1.0".to_string();
|
||||||
|
} else if v == "" {
|
||||||
|
reachedbody = true;
|
||||||
|
} else {
|
||||||
|
let split1: Vec<&str> = v.split(": ").collect();
|
||||||
|
let header = generate_kvheader(split1[0], split1[1]);
|
||||||
|
request.headers.push(header)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
request.body = v.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
pub fn construct_response(response: &str) -> Http1Response {
|
||||||
|
let split = response.split("\r\n");
|
||||||
|
let mut response = Http1Response { version: "".to_string(), status: "".to_string(), headers: vec![], body: "".to_string(), code: 0 };
|
||||||
|
let mut reachedbody = false;
|
||||||
|
for v in split {
|
||||||
|
if reachedbody != true {
|
||||||
|
if v.contains("HTTP/1.1") {
|
||||||
|
let split1: Vec<&str> = v.split(" ").collect();
|
||||||
|
let split2: Vec<&str> = v.split("HTTP/1.1 ").collect();
|
||||||
|
response.version = "1.1".to_string();
|
||||||
|
let n: i32 = FromStr::from_str(split1[1]).unwrap();
|
||||||
|
response.status = split2[1].to_string();
|
||||||
|
response.code = n;
|
||||||
|
} else if v.contains("HTTP/1.0") {
|
||||||
|
let split1: Vec<&str> = v.split(" ").collect();
|
||||||
|
let split2: Vec<&str> = v.split("HTTP/1.0 ").collect();
|
||||||
|
response.version = "1.0".to_string();
|
||||||
|
let n: i32 = FromStr::from_str(split1[1]).unwrap();
|
||||||
|
response.status = split2[1].to_string();
|
||||||
|
response.code = n;
|
||||||
|
} else if v == "" {
|
||||||
|
reachedbody = true;
|
||||||
|
} else {
|
||||||
|
let split1: Vec<&str> = v.split(": ").collect();
|
||||||
|
let header = generate_kvheader(split1[0], split1[1]);
|
||||||
|
response.headers.push(header)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
response.body = v.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
pub fn unpack_request(request: Http1Request) -> String {
|
||||||
|
let mut unpacked = format!("{} {} HTTP/{}\r\n", request.method, request.location, request.version);
|
||||||
|
for header in request.headers {
|
||||||
|
unpacked = format!("{}{}: {}\r\n", unpacked, header.key, header.value);
|
||||||
|
}
|
||||||
|
unpacked = format!("{}\r\n{}",unpacked,request.body);
|
||||||
|
return unpacked;
|
||||||
|
}
|
||||||
|
pub fn unpack_response(response: Http1Response) -> String {
|
||||||
|
let mut unpacked = format!("HTTP/{} {}\r\n", response.version, response.status);
|
||||||
|
for header in response.headers {
|
||||||
|
unpacked = format!("{}{}: {}\r\n", unpacked, header.key, header.value);
|
||||||
|
}
|
||||||
|
unpacked = format!("{}\r\n{}",unpacked,response.body);
|
||||||
|
return unpacked;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn code_to_string(code: i32) -> String {
|
||||||
|
match code {
|
||||||
|
100 => "100 Continue".to_string(),
|
||||||
|
101 => "101 Switching Protocols".to_string(),
|
||||||
|
102 => "102 Processing".to_string(),
|
||||||
|
103 => "103 Early Hints".to_string(),
|
||||||
|
200 => "200 OK".to_string(),
|
||||||
|
201 => "201 Created".to_string(),
|
||||||
|
202 => "202 Accepted".to_string(),
|
||||||
|
203 => "203 Non-Authoritative Information".to_string(),
|
||||||
|
204 => "204 No Content".to_string(),
|
||||||
|
205 => "205 Reset Content".to_string(),
|
||||||
|
206 => "206 Partial Content".to_string(),
|
||||||
|
207 => "207 Multi-Status".to_string(),
|
||||||
|
208 => "208 Already Reported".to_string(),
|
||||||
|
226 => "226 IM Used".to_string(),
|
||||||
|
300 => "300 Multiple Choices".to_string(),
|
||||||
|
301 => "301 Moved Permanently".to_string(),
|
||||||
|
302 => "302 Found".to_string(),
|
||||||
|
303 => "303 See Other".to_string(),
|
||||||
|
304 => "304 Not Modified".to_string(),
|
||||||
|
305 => "305 Use Proxy".to_string(),
|
||||||
|
306 => "306 Switch Proxy".to_string(),
|
||||||
|
307 => "307 Temporary Redirect".to_string(),
|
||||||
|
308 => "308 Permanent Redirect".to_string(),
|
||||||
|
400 => "400 Bad Request".to_string(),
|
||||||
|
401 => "401 Unauthorized".to_string(),
|
||||||
|
402 => "402 Payment Required".to_string(),
|
||||||
|
403 => "403 Forbidden".to_string(),
|
||||||
|
404 => "404 Not Found".to_string(),
|
||||||
|
405 => "405 Method Not Allowed".to_string(),
|
||||||
|
406 => "406 Not Acceptable".to_string(),
|
||||||
|
407 => "407 Proxy Authentication Required".to_string(),
|
||||||
|
408 => "408 Request Timeout".to_string(),
|
||||||
|
409 => "409 Conflict".to_string(),
|
||||||
|
410 => "410 Gone".to_string(),
|
||||||
|
411 => "411 Length Required".to_string(),
|
||||||
|
412 => "412 Precondition Failed".to_string(),
|
||||||
|
413 => "413 Payload Too Large".to_string(),
|
||||||
|
414 => "414 URI Too Long".to_string(),
|
||||||
|
415 => "415 Unsupported Media Type".to_string(),
|
||||||
|
416 => "416 Range Not Satisfiable".to_string(),
|
||||||
|
417 => "417 Expectation Failed".to_string(),
|
||||||
|
418 => "418 I'm a teapot".to_string(),
|
||||||
|
421 => "421 Misdirected Request".to_string(),
|
||||||
|
422 => "422 Unprocessable Entity".to_string(),
|
||||||
|
423 => "423 Locked".to_string(),
|
||||||
|
424 => "424 Failed Dependency".to_string(),
|
||||||
|
425 => "425 Too Early".to_string(),
|
||||||
|
426 => "426 Upgrade Required".to_string(),
|
||||||
|
428 => "428 Precondition Required".to_string(),
|
||||||
|
429 => "429 Too Many Requests".to_string(),
|
||||||
|
431 => "431 Request Header Fields Too Large".to_string(),
|
||||||
|
451 => "451 Unavailable For Legal Reasons".to_string(),
|
||||||
|
500 => "500 Internal Server Error".to_string(),
|
||||||
|
501 => "501 Not Implemented".to_string(),
|
||||||
|
502 => "502 Bad Gateway".to_string(),
|
||||||
|
503 => "503 Service Unavailable".to_string(),
|
||||||
|
504 => "504 Gateway Timeout".to_string(),
|
||||||
|
505 => "505 HTTP Version Not Supported".to_string(),
|
||||||
|
506 => "506 Variant Also Negotiates".to_string(),
|
||||||
|
507 => "507 Insufficient Storage".to_string(),
|
||||||
|
508 => "508 Loop Detected".to_string(),
|
||||||
|
510 => "510 Not Extended".to_string(),
|
||||||
|
511 => "511 Network Authentication Required".to_string(),
|
||||||
|
_ => "500 Internal Server Error".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue