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
1f97c198
Commit
1f97c198
authored
Aug 27, 2025
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make local id inside routers
parent
b63f8153
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
15 deletions
+20
-15
src/main.rs
src/main.rs
+9
-9
src/router.rs
src/router.rs
+11
-6
No files found.
src/main.rs
View file @
1f97c198
...
...
@@ -20,11 +20,13 @@ use serde::Deserialize;
#[derive(Deserialize)]
pub
struct
ConfigRouter
{
pub
local_id
:
u16
,
pub
remote_id
:
u16
,
pub
proto
:
i32
,
pub
family
:
u8
,
pub
mark
:
u32
,
pub
endpoint
:
String
,
pub
local_secret
:
String
,
pub
remote_secret
:
String
,
pub
dev
:
String
,
pub
up
:
String
,
...
...
@@ -32,8 +34,6 @@ pub struct ConfigRouter {
#[derive(Deserialize)]
pub
struct
Config
{
pub
local_id
:
u16
,
pub
local_secret
:
String
,
pub
routers
:
Vec
<
ConfigRouter
>
,
}
use
crossbeam_utils
::
thread
;
...
...
@@ -45,8 +45,6 @@ fn main() -> Result<(), Box<dyn Error>> {
println!
(
"Init"
);
let
config
:
Config
=
serde_json
::
from_str
(
env
::
args
()
.nth
(
1
)
.ok_or
(
"need param"
)
?
.as_str
())
?
;
println!
(
"Read config"
);
let
local_secret
:
[
u8
;
SECRET_LENGTH
]
=
Router
::
create_secret
(
config
.local_secret
.as_str
())
?
;
println!
(
"Created local secret"
);
let
mut
sockets
:
HashMap
<
u16
,
Arc
<
Socket
>>
=
HashMap
::
new
();
println!
(
"Ready"
);
let
routers
:
HashMap
<
u16
,
Router
>
=
config
...
...
@@ -87,7 +85,7 @@ fn main() -> Result<(), Box<dyn Error>> {
// Pre-initialize with our Meta header (local -> remote)
let
meta
=
Meta
{
src_id
:
config
.local_id
,
src_id
:
router
.
config.local_id
,
dst_id
:
router
.config.remote_id
,
reversed
:
0
,
};
...
...
@@ -124,13 +122,15 @@ fn main() -> Result<(), Box<dyn Error>> {
.split_at_mut_checked
(
size_of
::
<
Meta
>
())
.ok_or
(
"malformed packet"
)
?
;
let
meta
:
&
Meta
=
unsafe
{
transmute
(
meta_bytes
.as_ptr
())
};
if
meta
.
dst_id
==
config
.local_id
&&
meta
.
reversed
==
0
{
if
meta
.reversed
==
0
{
let
router
=
router_writers
.get_mut
(
&
meta
.src_id
)
.ok_or
(
"missing router"
)
?
;
*
router
.endpoint
.write
()
.unwrap
()
=
Some
(
addr
);
router
.decrypt
(
payload
,
&
local_secret
);
router
.tun_writer
.write_all
(
payload
)
?
;
if
meta
.dst_id
==
router
.config.local_id
{
*
router
.endpoint
.write
()
.unwrap
()
=
Some
(
addr
);
router
.decrypt
(
payload
);
router
.tun_writer
.write_all
(
payload
)
?
;
}
}
Ok
::
<
(),
Box
<
dyn
Error
>>
(())
...
...
src/router.rs
View file @
1f97c198
...
...
@@ -48,14 +48,15 @@ impl<'a> RouterReader<'a> {
// raw -> tun
pub
struct
RouterWriter
<
'a
>
{
pub
config
:
&
'a
ConfigRouter
,
pub
secret
:
[
u8
;
SECRET_LENGTH
],
pub
tun_writer
:
Writer
,
pub
endpoint
:
Arc
<
RwLock
<
Option
<
SockAddr
>>>
,
}
impl
<
'a
>
RouterWriter
<
'a
>
{
#[inline]
pub
(
crate
)
fn
decrypt
(
&
self
,
data
:
&
mut
[
u8
]
,
secret
:
&
[
u8
;
SECRET_LENGTH
]
)
{
xor_with_secret
(
data
,
secret
);
pub
(
crate
)
fn
decrypt
(
&
self
,
data
:
&
mut
[
u8
])
{
xor_with_secret
(
data
,
&
self
.
secret
);
}
pub
(
crate
)
fn
key
(
&
self
)
->
u16
{
...
...
@@ -65,7 +66,8 @@ impl<'a> RouterWriter<'a> {
pub
struct
Router
<
'a
>
{
pub
config
:
&
'a
ConfigRouter
,
pub
secret
:
[
u8
;
SECRET_LENGTH
],
pub
local_secret
:
[
u8
;
SECRET_LENGTH
],
pub
remote_secret
:
[
u8
;
SECRET_LENGTH
],
pub
tun_reader
:
Reader
,
pub
tun_writer
:
Writer
,
pub
socket
:
Arc
<
Socket
>
,
...
...
@@ -134,7 +136,8 @@ impl<'a> Router<'a> {
config
:
&
'a
ConfigRouter
,
sockets
:
&
mut
HashMap
<
u16
,
Arc
<
Socket
>>
,
)
->
Result
<
Router
<
'a
>
,
Box
<
dyn
std
::
error
::
Error
>>
{
let
secret
=
Self
::
create_secret
(
config
.remote_secret
.as_str
())
?
;
let
local_secret
=
Self
::
create_secret
(
config
.local_secret
.as_str
())
?
;
let
remote_secret
=
Self
::
create_secret
(
config
.remote_secret
.as_str
())
?
;
let
endpoint
=
Self
::
create_endpoint
(
&
config
)
?
;
let
socket
=
Self
::
create_raw_socket
(
&
config
,
sockets
)
?
;
if
(
config
.mark
>
0
)
{
...
...
@@ -146,7 +149,8 @@ impl<'a> Router<'a> {
let
router
=
Router
{
config
,
secret
,
local_secret
,
remote_secret
,
endpoint
,
tun_reader
,
tun_writer
,
...
...
@@ -159,13 +163,14 @@ impl<'a> Router<'a> {
pub
fn
split
(
self
)
->
(
RouterReader
<
'a
>
,
RouterWriter
<
'a
>
)
{
let
writer
=
RouterWriter
{
config
:
self
.config
,
secret
:
self
.local_secret
,
endpoint
:
Arc
::
clone
(
&
self
.endpoint
),
tun_writer
:
self
.tun_writer
,
};
let
reader
=
RouterReader
{
config
:
self
.config
,
secret
:
self
.secret
,
secret
:
self
.
remote_
secret
,
endpoint
:
self
.endpoint
,
tun_reader
:
self
.tun_reader
,
socket
:
self
.socket
,
...
...
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