Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
WebServer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
codecraft
WebServer
Commits
9191168b
Commit
9191168b
authored
1 year ago
by
codecraft
Browse files
Options
Downloads
Patches
Plain Diff
Add HTTPS-REdirect from Port 8080 to 8443
parent
56a7d1fd
No related branches found
No related tags found
1 merge request
!1
Initial feature merge
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
core/http/src/handling/response/build_and_write.rs
+6
-0
6 additions, 0 deletions
core/http/src/handling/response/build_and_write.rs
core/http/src/setup.rs
+26
-10
26 additions, 10 deletions
core/http/src/setup.rs
site/src/main.rs
+1
-1
1 addition, 1 deletion
site/src/main.rs
with
33 additions
and
11 deletions
core/http/src/handling/response/build_and_write.rs
+
6
−
0
View file @
9191168b
...
@@ -38,4 +38,10 @@ impl Response {
...
@@ -38,4 +38,10 @@ impl Response {
stream
.write_all
(
&
resp
)
.await
?
;
stream
.write_all
(
&
resp
)
.await
?
;
Ok
(())
Ok
(())
}
}
pub
async
fn
write_unencrypted
(
self
,
mut
stream
:
TcpStream
)
->
Result
<
()
>
{
let
resp
=
self
.build
(
None
);
stream
.write_all
(
&
resp
)
.await
?
;
Ok
(())
}
}
}
This diff is collapsed.
Click to expand it.
core/http/src/setup.rs
+
26
−
10
View file @
9191168b
...
@@ -9,7 +9,7 @@ use tokio_native_tls::{native_tls::{Identity, self}, TlsAcceptor};
...
@@ -9,7 +9,7 @@ use tokio_native_tls::{native_tls::{Identity, self}, TlsAcceptor};
use
crate
::{
use
crate
::{
handlers
::
handler
::
handle_connection
,
handlers
::
handler
::
handle_connection
,
handling
::
routes
::{
Route
,
Uri
},
handling
::
{
routes
::{
Route
,
Uri
},
response
::{
Response
,
Status
}},
};
};
#[derive(Clone)]
#[derive(Clone)]
...
@@ -29,6 +29,7 @@ pub struct Config {
...
@@ -29,6 +29,7 @@ pub struct Config {
mountpoints
:
Option
<
Vec
<
MountPoint
<
'static
>>>
,
mountpoints
:
Option
<
Vec
<
MountPoint
<
'static
>>>
,
/// Contains a [tokio::net::TcpListener] that is bound for the server
/// Contains a [tokio::net::TcpListener] that is bound for the server
address
:
TcpListener
,
address
:
TcpListener
,
to_secure_redirect
:
TcpListener
,
tls_acceptor
:
TlsAcceptor
,
tls_acceptor
:
TlsAcceptor
,
}
}
...
@@ -93,10 +94,11 @@ impl<'a> Config {
...
@@ -93,10 +94,11 @@ impl<'a> Config {
/// is no interrupt signal
/// is no interrupt signal
pub
async
fn
launch
(
self
)
{
pub
async
fn
launch
(
self
)
{
println!
(
println!
(
"Server launched from http://{}"
,
"Server launched from
https://{} and
http://{}"
,
self
.address
.local_addr
()
.unwrap
()
self
.address
.local_addr
()
.unwrap
()
,
self
.to_secure_redirect
.local_addr
()
.unwrap
()
);
);
let
mut
sigint
=
signal
(
SignalKind
::
interrupt
())
.unwrap
();
let
mut
sigint
=
signal
(
SignalKind
::
interrupt
())
.unwrap
();
let
location_string
=
format!
(
"Location: https://{}"
,
self
.address
.local_addr
()
.unwrap
());
loop
{
loop
{
select!
{
select!
{
_
=
sigint
.recv
()
=>
{
_
=
sigint
.recv
()
=>
{
...
@@ -108,6 +110,15 @@ impl<'a> Config {
...
@@ -108,6 +110,15 @@ impl<'a> Config {
let
tls_acceptor
=
self
.tls_acceptor
.clone
();
let
tls_acceptor
=
self
.tls_acceptor
.clone
();
tokio
::
spawn
(
async
move
{
handle_connection
(
socket
,
mountpoints
,
tls_acceptor
)
.await
;
});
tokio
::
spawn
(
async
move
{
handle_connection
(
socket
,
mountpoints
,
tls_acceptor
)
.await
;
});
}
}
Ok
((
socket
,
_
))
=
self
.to_secure_redirect
.accept
()
=>
{
let
redirect_response
=
Response
{
headers
:
vec!
[
location_string
.clone
()],
cookies
:
None
,
status
:
Some
(
Status
::
MovedPermanently
),
body
:
Box
::
new
(
""
),
};
tokio
::
spawn
(
async
move
{
let
_
=
redirect_response
.write_unencrypted
(
socket
)
.await
;
});
}
}
}
}
}
}
}
...
@@ -122,22 +133,24 @@ impl<'a> Config {
...
@@ -122,22 +133,24 @@ impl<'a> Config {
/// # Example
/// # Example
/// ```
/// ```
/// async fn example() {
/// async fn example() {
/// let _ = http::build("127.0.0.1:80
00
");
/// let _ = http::build("127.0.0.1:80
80", "127.0.0.1:8443
");
/// }
/// }
/// ```
/// ```
/// # Panics
/// # Panics
/// Panics if the IP is not bindable, or other forms of system errors or it's not a valid
/// Panics if the IP is not bindable, or other forms of system errors or it's not a valid
/// IP-Address
/// IP-Address
pub
async
fn
build
(
ip
:
&
str
)
->
Config
{
pub
async
fn
build
(
ip_http
:
&
str
,
ip_secure
:
&
str
)
->
Config
{
let
listener
=
if
let
Ok
(
listener
)
=
TcpListener
::
bind
(
ip
)
.await
{
let
Ok
(
listener_secure
)
=
TcpListener
::
bind
(
ip_secure
)
.await
else
{
listener
}
else
{
panic!
(
"
\x1b
[31mCould't bind Listener to address
\x1b
[0m"
);
panic!
(
"
\x1b
[31mCould't bind Listener to address
\x1b
[0m"
);
};
};
let
ip
=
ip
.splitn
(
2
,
':'
)
.collect
::
<
Vec
<&
str
>>
();
let
ip
=
ip
_secure
.splitn
(
2
,
':'
)
.collect
::
<
Vec
<&
str
>>
();
if
ip
.len
()
!=
2
{
if
ip
.len
()
!=
2
{
panic!
(
"Invalid IP Address"
);
panic!
(
"Invalid IP Address"
);
}
}
let
Ok
(
listener_http
)
=
TcpListener
::
bind
(
ip_http
)
.await
else
{
panic!
(
"
\x1b
[31mCould't bind Listener to address
\x1b
[0m"
);
};
let
identity
=
Identity
::
from_pkcs12
(
include_bytes!
(
"certificates/identity.pfx"
),
"1234"
)
.unwrap
();
let
identity
=
Identity
::
from_pkcs12
(
include_bytes!
(
"certificates/identity.pfx"
),
"1234"
)
.unwrap
();
let
port
=
ip
[
1
];
let
port
=
ip
[
1
];
...
@@ -148,11 +161,14 @@ pub async fn build(ip: &str) -> Config {
...
@@ -148,11 +161,14 @@ pub async fn build(ip: &str) -> Config {
>>
\x1b
[34mIp
\x1b
[0m: {ip}
>>
\x1b
[34mIp
\x1b
[0m: {ip}
>>
\x1b
[34mPort
\x1b
[0m: {port}
>>
\x1b
[34mPort
\x1b
[0m: {port}
>>
\x1b
[34mWorkers
\x1b
[0m: {workers}
>>
\x1b
[34mWorkers
\x1b
[0m: {workers}
\x1b
[32m Security
\x1b
[0m
>>
\x1b
[32mHttp to Https Redirect: {ip_http} -> {ip_secure}
\x1b
[0m
\x1b
[35m🛪 Mountpoints
\x1b
[0m"
\x1b
[35m🛪 Mountpoints
\x1b
[0m"
);
);
Config
{
Config
{
mountpoints
:
None
,
mountpoints
:
None
,
address
:
listener
,
address
:
listener_secure
,
to_secure_redirect
:
listener_http
,
tls_acceptor
:
native_tls
::
TlsAcceptor
::
builder
(
identity
)
.build
()
.unwrap
()
.into
()
tls_acceptor
:
native_tls
::
TlsAcceptor
::
builder
(
identity
)
.build
()
.unwrap
()
.into
()
}
}
}
}
This diff is collapsed.
Click to expand it.
site/src/main.rs
+
1
−
1
View file @
9191168b
...
@@ -104,7 +104,7 @@ async fn main() {
...
@@ -104,7 +104,7 @@ async fn main() {
rank
:
0
,
rank
:
0
,
};
};
http
::
build
(
"127.0.0.1:8080"
)
http
::
build
(
"127.0.0.1:8080"
,
"127.0.0.1:8443"
)
.await
.await
.mount
(
"/"
,
vec!
[
fileserver
,
post_test
,
static_hi
])
.mount
(
"/"
,
vec!
[
fileserver
,
post_test
,
static_hi
])
.mount
(
"/post/"
,
vec!
[
post_test
])
.mount
(
"/post/"
,
vec!
[
post_test
])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment