add entrypoint for router handler
This commit is contained in:
@@ -3,7 +3,6 @@ package router
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/core/router"
|
||||
@@ -45,21 +44,27 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) router.Route
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) GetRoute(ctx context.Context, dst net.IP, opts ...router.Option) *router.Route {
|
||||
func (p *grpcPlugin) GetRoute(ctx context.Context, dst string, opts ...router.Option) *router.Route {
|
||||
if p.client == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var options router.Options
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
r, err := p.client.GetRoute(ctx,
|
||||
&proto.GetRouteRequest{
|
||||
Dst: dst.String(),
|
||||
Dst: dst,
|
||||
Id: options.ID,
|
||||
})
|
||||
if err != nil {
|
||||
p.log.Error(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return xrouter.ParseRoute(r.Net, r.Gateway)
|
||||
return xrouter.ParseRoute(r.Dst, r.Gateway)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) Close() error {
|
||||
|
||||
+17
-4
@@ -3,7 +3,6 @@ package router
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-gost/core/logger"
|
||||
@@ -14,10 +13,12 @@ import (
|
||||
|
||||
type httpPluginGetRouteRequest struct {
|
||||
Dst string `json:"dst"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type httpPluginGetRouteResponse struct {
|
||||
Net string `json:"net"`
|
||||
Dst string `json:"dst"`
|
||||
Gateway string `json:"gateway"`
|
||||
}
|
||||
|
||||
@@ -46,11 +47,16 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) router.Router
|
||||
}
|
||||
}
|
||||
|
||||
func (p *httpPlugin) GetRoute(ctx context.Context, dst net.IP, opts ...router.Option) *router.Route {
|
||||
func (p *httpPlugin) GetRoute(ctx context.Context, dst string, opts ...router.Option) *router.Route {
|
||||
if p.client == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var options router.Options
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.url, nil)
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -61,7 +67,10 @@ func (p *httpPlugin) GetRoute(ctx context.Context, dst net.IP, opts ...router.Op
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Set("dst", dst.String())
|
||||
q.Set("dst", dst)
|
||||
if options.ID != "" {
|
||||
q.Set("id", options.ID)
|
||||
}
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
resp, err := p.client.Do(req)
|
||||
@@ -79,5 +88,9 @@ func (p *httpPlugin) GetRoute(ctx context.Context, dst net.IP, opts ...router.Op
|
||||
return nil
|
||||
}
|
||||
|
||||
return xrouter.ParseRoute(res.Net, res.Gateway)
|
||||
dstNet := res.Dst
|
||||
if dstNet == "" {
|
||||
dstNet = res.Net
|
||||
}
|
||||
return xrouter.ParseRoute(dstNet, res.Gateway)
|
||||
}
|
||||
|
||||
+5
-8
@@ -202,17 +202,19 @@ func (p *localRouter) parseRoutes(r io.Reader) (routes []*router.Route, err erro
|
||||
return
|
||||
}
|
||||
|
||||
func (p *localRouter) GetRoute(ctx context.Context, dst net.IP, opts ...router.Option) *router.Route {
|
||||
if dst == nil || p == nil {
|
||||
func (p *localRouter) GetRoute(ctx context.Context, dst string, opts ...router.Option) *router.Route {
|
||||
if dst == "" || p == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
dstIP := net.ParseIP(dst)
|
||||
|
||||
p.mu.RLock()
|
||||
routes := p.routes
|
||||
p.mu.RUnlock()
|
||||
|
||||
for _, route := range routes {
|
||||
if route.Net != nil && route.Net.Contains(dst) {
|
||||
if route.Dst == dst || route.Net != nil && route.Net.Contains(dstIP) {
|
||||
return route
|
||||
}
|
||||
}
|
||||
@@ -255,11 +257,6 @@ func ParseRoute(dst string, gateway string) *router.Route {
|
||||
}
|
||||
_, ipNet, _ := net.ParseCIDR(dst)
|
||||
|
||||
gw := net.ParseIP(gateway)
|
||||
if gw == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &router.Route{
|
||||
Net: ipNet,
|
||||
Dst: dst,
|
||||
|
||||
Reference in New Issue
Block a user