Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
T
tun
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Railgun
tun
Commits
64f983ee
Commit
64f983ee
authored
Feb 03, 2025
by
nanamicat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
split
parent
3e09c408
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
17 deletions
+30
-17
src/main.rs
src/main.rs
+24
-12
src/router.rs
src/router.rs
+6
-5
No files found.
src/main.rs
View file @
64f983ee
...
@@ -56,16 +56,28 @@ fn main() -> Result<(), Box<dyn Error>> {
...
@@ -56,16 +56,28 @@ fn main() -> Result<(), Box<dyn Error>> {
.iter
()
.iter
()
.map
(|
c
|
Router
::
new
(
c
,
&
mut
sockets
)
.map
(|
router
|
(
c
.remote_id
,
router
)))
.map
(|
c
|
Router
::
new
(
c
,
&
mut
sockets
)
.map
(|
router
|
(
c
.remote_id
,
router
)))
.collect
::
<
Result
<
_
,
_
>>
()
?
;
.collect
::
<
Result
<
_
,
_
>>
()
?
;
let
(
mut
router_readers
,
mut
router_writers
):
(
HashMap
<
u8
,
RouterReader
>
,
HashMap
<
u8
,
RouterWriter
>
)
=
let
(
mut
router_readers
,
router_writers
):
(
routers
HashMap
<
u8
,
RouterReader
>
,
.into_iter
()
HashMap
<
u8
,
RouterWriter
>
,
.map
(|(
id
,
router
)|
{
)
=
routers
let
(
reader
,
writer
)
=
router
.split
();
.into_iter
()
((
id
,
reader
),
(
id
,
writer
))
.map
(|(
id
,
router
)|
{
})
let
(
reader
,
writer
)
=
router
.split
();
.unzip
();
((
id
,
reader
),
(
id
,
writer
))
})
.unzip
();
let
router_writers2
=
router_writers
.into_values
()
.grouping_by
(|
k
|
k
.key
());
let
router_writers3
:
Vec
<
(
Arc
<
Socket
>
,
HashMap
<
u8
,
RouterWriter
>
)
>
=
router_writers
.into_iter
()
.grouping_by
(|(
k
,
v
)|
v
.key
())
.into_iter
()
.map
(|(
k
,
v
)|
{
(
Arc
::
clone
(
sockets
.get_mut
(
&
k
)
.unwrap
()),
v
.into_iter
()
.collect
(),
)
})
.collect
();
thread
::
scope
(|
s
|
{
thread
::
scope
(|
s
|
{
for
router
in
router_readers
.values_mut
()
{
for
router
in
router_readers
.values_mut
()
{
...
@@ -109,8 +121,8 @@ fn main() -> Result<(), Box<dyn Error>> {
...
@@ -109,8 +121,8 @@ fn main() -> Result<(), Box<dyn Error>> {
});
});
}
}
for
socket
in
sockets
.values
()
{
for
(
socket
,
mut
router_writers
)
in
router_writers3
{
s
.spawn
(|
_
|
{
s
.spawn
(
move
|
_
|
{
let
mut
recv_buf
=
[
MaybeUninit
::
uninit
();
1500
];
let
mut
recv_buf
=
[
MaybeUninit
::
uninit
();
1500
];
loop
{
loop
{
match
socket
.recv_from
(
&
mut
recv_buf
)
{
match
socket
.recv_from
(
&
mut
recv_buf
)
{
...
@@ -133,7 +145,7 @@ fn main() -> Result<(), Box<dyn Error>> {
...
@@ -133,7 +145,7 @@ fn main() -> Result<(), Box<dyn Error>> {
if
let
Some
(
router
)
=
router_writers
.get_mut
(
&
meta
.src_id
)
{
if
let
Some
(
router
)
=
router_writers
.get_mut
(
&
meta
.src_id
)
{
if
meta
.dst_id
==
config
.local_id
&&
meta
.reversed
==
0
{
if
meta
.dst_id
==
config
.local_id
&&
meta
.reversed
==
0
{
*
router
_readers
.get
(
&
meta
.src_id
)
.unwrap
()
.endpoint
.write
()
.unwrap
()
=
Some
(
addr
);
*
router
.endpoint
.write
()
.unwrap
()
=
Some
(
addr
);
router
.decrypt
(
payload
);
router
.decrypt
(
payload
);
router
.tun_writer
.write_all
(
payload
)
.unwrap
();
router
.tun_writer
.write_all
(
payload
)
.unwrap
();
}
}
...
...
src/router.rs
View file @
64f983ee
...
@@ -15,7 +15,7 @@ pub struct RouterReader {
...
@@ -15,7 +15,7 @@ pub struct RouterReader {
pub
secret
:
[
u8
;
SECRET_LENGTH
],
pub
secret
:
[
u8
;
SECRET_LENGTH
],
pub
tun_reader
:
Reader
,
pub
tun_reader
:
Reader
,
pub
socket
:
Arc
<
Socket
>
,
pub
socket
:
Arc
<
Socket
>
,
pub
endpoint
:
RwLock
<
Option
<
SockAddr
>>
,
pub
endpoint
:
Arc
<
RwLock
<
Option
<
SockAddr
>
>>
,
}
}
impl
RouterReader
{
impl
RouterReader
{
...
@@ -30,6 +30,7 @@ impl RouterReader {
...
@@ -30,6 +30,7 @@ impl RouterReader {
pub
struct
RouterWriter
{
pub
struct
RouterWriter
{
pub
config
:
&
'static
ConfigRouter
,
pub
config
:
&
'static
ConfigRouter
,
pub
tun_writer
:
Writer
,
pub
tun_writer
:
Writer
,
pub
endpoint
:
Arc
<
RwLock
<
Option
<
SockAddr
>>>
,
}
}
impl
RouterWriter
{
impl
RouterWriter
{
...
@@ -50,7 +51,7 @@ pub struct Router {
...
@@ -50,7 +51,7 @@ pub struct Router {
pub
tun_reader
:
Reader
,
pub
tun_reader
:
Reader
,
pub
tun_writer
:
Writer
,
pub
tun_writer
:
Writer
,
pub
socket
:
Arc
<
Socket
>
,
pub
socket
:
Arc
<
Socket
>
,
pub
endpoint
:
RwLock
<
Option
<
SockAddr
>>
,
pub
endpoint
:
Arc
<
RwLock
<
Option
<
SockAddr
>
>>
,
}
}
impl
Router
{
impl
Router
{
...
@@ -104,9 +105,9 @@ impl Router {
...
@@ -104,9 +105,9 @@ impl Router {
fn
create_endpoint
(
fn
create_endpoint
(
config
:
&
ConfigRouter
,
config
:
&
ConfigRouter
,
)
->
Result
<
RwLock
<
Option
<
SockAddr
>>
,
Box
<
dyn
std
::
error
::
Error
>>
{
)
->
Result
<
Arc
<
RwLock
<
Option
<
SockAddr
>
>>
,
Box
<
dyn
std
::
error
::
Error
>>
{
let
parsed
=
config
.endpoint
.to_socket_addrs
()
?
.next
()
.unwrap
();
let
parsed
=
config
.endpoint
.to_socket_addrs
()
?
.next
()
.unwrap
();
Ok
(
RwLock
::
new
(
Some
(
parsed
.into
(
))))
Ok
(
Arc
::
new
(
RwLock
::
new
(
Some
(
parsed
.into
()
))))
}
}
pub
fn
new
(
pub
fn
new
(
...
@@ -134,7 +135,7 @@ impl Router {
...
@@ -134,7 +135,7 @@ impl Router {
pub
fn
split
(
self
)
->
(
RouterReader
,
RouterWriter
)
{
pub
fn
split
(
self
)
->
(
RouterReader
,
RouterWriter
)
{
let
writer
=
RouterWriter
{
let
writer
=
RouterWriter
{
config
:
self
.config
,
config
:
self
.config
,
// endpoint: self.endpoint.read().unwrap().clone(
),
endpoint
:
Arc
::
clone
(
&
self
.endpoint
),
tun_writer
:
self
.tun_writer
,
tun_writer
:
self
.tun_writer
,
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment