Skip to content

Commit

Permalink
feat: set route config
Browse files Browse the repository at this point in the history
  • Loading branch information
AkinoKaede committed May 28, 2023
1 parent 02df397 commit 43f3265
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 26 deletions.
61 changes: 37 additions & 24 deletions app/tun/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/tun/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ message Config {
uint32 mtu = 2;
uint32 user_level = 3;
v2ray.core.net.packetaddr.PacketAddrType packet_encoding = 4;
string tag = 6;
repeated v2ray.core.app.router.routercommon.CIDR ips = 7;
string tag = 5;
repeated v2ray.core.app.router.routercommon.CIDR ips = 6;
repeated v2ray.core.app.router.routercommon.CIDR routes = 7;
bool enable_promiscuous_mode = 8;
bool enable_spoofing = 9;
}
21 changes: 21 additions & 0 deletions app/tun/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,24 @@ func AddProtocolAddress(id tcpip.NICID, ips []*routercommon.CIDR) StackOption {
return nil
}
}

func SetRouteTable(id tcpip.NICID, routes []*routercommon.CIDR) StackOption {
return func(s *stack.Stack) error {
s.SetRouteTable(func() (table []tcpip.Route) {
for _, cidrs := range routes {
subnet := tcpip.AddressWithPrefix{
Address: tcpip.AddrFrom4Slice(cidrs.Ip),
PrefixLen: int(cidrs.Prefix),
}.Subnet()
route := tcpip.Route{
Destination: subnet,
NIC: id,
}
table = append(table, route)
}
return
}())

return nil
}
}
1 change: 1 addition & 0 deletions app/tun/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (t *TUN) CreateStack(linkedEndpoint stack.LinkEndpoint) (*stack.Stack, erro

CreateNIC(nicID, linkedEndpoint),
AddProtocolAddress(nicID, t.config.Ips),
SetRouteTable(nicID, t.config.Routes),
SetPromiscuousMode(nicID, t.config.EnablePromiscuousMode),
SetSpoofing(nicID, t.config.EnableSpoofing),
}
Expand Down

0 comments on commit 43f3265

Please sign in to comment.