add entrypoint for router handler

This commit is contained in:
ginuerzh
2025-02-08 23:07:54 +08:00
parent 9cd03b6b59
commit 08ad260606
14 changed files with 259 additions and 122 deletions
+9 -4
View File
@@ -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
View File
@@ -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)
}