add router component

This commit is contained in:
ginuerzh
2023-11-19 14:23:21 +08:00
parent 88cc6ff4d5
commit 74639e9c4e
25 changed files with 788 additions and 123 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/go-gost/core/listener"
"github.com/go-gost/core/logger"
mdata "github.com/go-gost/core/metadata"
"github.com/go-gost/core/router"
xnet "github.com/go-gost/x/internal/net"
limiter "github.com/go-gost/x/limiter/traffic/wrapper"
mdx "github.com/go-gost/x/metadata"
@ -26,6 +27,7 @@ type tunListener struct {
logger logger.Logger
md metadata
options listener.Options
routes []*router.Route
}
func NewListener(opts ...listener.Option) listener.Listener {

View File

@ -6,7 +6,10 @@ import (
mdata "github.com/go-gost/core/metadata"
mdutil "github.com/go-gost/core/metadata/util"
"github.com/go-gost/core/router"
tun_util "github.com/go-gost/x/internal/util/tun"
"github.com/go-gost/x/registry"
xrouter "github.com/go-gost/x/router"
)
const (
@ -36,9 +39,10 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
}
config := &tun_util.Config{
Name: mdutil.GetString(md, name),
Peer: mdutil.GetString(md, peer),
MTU: mdutil.GetInt(md, mtu),
Name: mdutil.GetString(md, name),
Peer: mdutil.GetString(md, peer),
MTU: mdutil.GetInt(md, mtu),
Router: registry.RouterRegistry().Get(mdutil.GetString(md, "router")),
}
if config.MTU <= 0 {
config.MTU = defaultMTU
@ -62,35 +66,42 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
}
for _, s := range strings.Split(mdutil.GetString(md, route), ",") {
var route tun_util.Route
_, ipNet, _ := net.ParseCIDR(strings.TrimSpace(s))
if ipNet == nil {
continue
}
route.Net = *ipNet
route.Gateway = config.Gateway
config.Routes = append(config.Routes, route)
l.routes = append(l.routes, &router.Route{
Net: ipNet,
Gateway: config.Gateway,
})
}
for _, s := range mdutil.GetStrings(md, routes) {
ss := strings.SplitN(s, " ", 2)
if len(ss) == 2 {
var route tun_util.Route
var route router.Route
_, ipNet, _ := net.ParseCIDR(strings.TrimSpace(ss[0]))
if ipNet == nil {
continue
}
route.Net = *ipNet
route.Gateway = net.ParseIP(ss[1])
if route.Gateway == nil {
route.Gateway = config.Gateway
route.Net = ipNet
gw := net.ParseIP(ss[1])
if gw == nil {
gw = config.Gateway
}
config.Routes = append(config.Routes, route)
l.routes = append(l.routes, &router.Route{
Net: ipNet,
Gateway: gw,
})
}
}
if config.Router == nil && len(l.routes) > 0 {
config.Router = xrouter.NewRouter(xrouter.RoutesOption(l.routes))
}
l.md.config = config
return

View File

@ -6,8 +6,6 @@ import (
"net"
"os/exec"
"strings"
tun_util "github.com/go-gost/x/internal/util/tun"
)
const (
@ -45,8 +43,8 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.
return
}
func (l *tunListener) addRoutes(ifName string, routes ...tun_util.Route) error {
for _, route := range routes {
func (l *tunListener) addRoutes(ifName string) error {
for _, route := range l.routes {
cmd := fmt.Sprintf("route add -net %s -interface %s", route.Net.String(), ifName)
l.logger.Debug(cmd)
args := strings.Split(cmd, " ")

View File

@ -6,8 +6,6 @@ import (
"net"
"github.com/vishvananda/netlink"
tun_util "github.com/go-gost/x/internal/util/tun"
)
func (l *tunListener) createTun() (dev io.ReadWriteCloser, name string, ip net.IP, err error) {
@ -42,17 +40,17 @@ func (l *tunListener) createTun() (dev io.ReadWriteCloser, name string, ip net.I
return
}
if err = l.addRoutes(ifce, l.md.config.Routes...); err != nil {
if err = l.addRoutes(ifce); err != nil {
return
}
return
}
func (l *tunListener) addRoutes(ifce *net.Interface, routes ...tun_util.Route) error {
for _, route := range routes {
func (l *tunListener) addRoutes(ifce *net.Interface) error {
for _, route := range l.routes {
r := netlink.Route{
Dst: &route.Net,
Dst: route.Net,
Gw: route.Gateway,
}
if r.Gw == nil {

View File

@ -8,8 +8,6 @@ import (
"net"
"os/exec"
"strings"
tun_util "github.com/go-gost/x/internal/util/tun"
)
const (
@ -45,8 +43,8 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.
return
}
func (l *tunListener) addRoutes(ifName string, routes ...tun_util.Route) error {
for _, route := range routes {
func (l *tunListener) addRoutes(ifName string) error {
for _, route := range l.routes {
cmd := fmt.Sprintf("route add -net %s -interface %s", route.Net.String(), ifName)
l.logger.Debug(cmd)
args := strings.Split(cmd, " ")

View File

@ -6,8 +6,6 @@ import (
"net"
"os/exec"
"strings"
tun_util "github.com/go-gost/x/internal/util/tun"
)
const (
@ -45,8 +43,8 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net.
return
}
func (l *tunListener) addRoutes(ifName string, gw net.IP, routes ...tun_util.Route) error {
for _, route := range routes {
func (l *tunListener) addRoutes(ifName string, gw net.IP) error {
for _, route := range l.routes {
l.deleteRoute(ifName, route.Net.String())
cmd := fmt.Sprintf("netsh interface ip add route prefix=%s interface=%s store=active",