add HTTP plugin

This commit is contained in:
ginuerzh
2023-09-20 22:56:43 +08:00
parent afddd2d29e
commit 1760151500
29 changed files with 1032 additions and 147 deletions

View File

@ -9,7 +9,7 @@ import (
"sync"
"time"
bypass_pkg "github.com/go-gost/core/bypass"
"github.com/go-gost/core/bypass"
"github.com/go-gost/core/logger"
"github.com/go-gost/x/internal/loader"
"github.com/go-gost/x/internal/matcher"
@ -77,7 +77,7 @@ func LoggerOption(logger logger.Logger) Option {
}
}
type bypass struct {
type localBypass struct {
ipMatcher matcher.Matcher
cidrMatcher matcher.Matcher
domainMatcher matcher.Matcher
@ -89,7 +89,7 @@ type bypass struct {
// NewBypass creates and initializes a new Bypass.
// The rules will be reversed if the reverse option is true.
func NewBypass(opts ...Option) bypass_pkg.Bypass {
func NewBypass(opts ...Option) bypass.Bypass {
var options options
for _, opt := range opts {
opt(&options)
@ -97,7 +97,7 @@ func NewBypass(opts ...Option) bypass_pkg.Bypass {
ctx, cancel := context.WithCancel(context.TODO())
bp := &bypass{
bp := &localBypass{
cancelFunc: cancel,
options: options,
}
@ -112,7 +112,7 @@ func NewBypass(opts ...Option) bypass_pkg.Bypass {
return bp
}
func (bp *bypass) periodReload(ctx context.Context) error {
func (bp *localBypass) periodReload(ctx context.Context) error {
period := bp.options.period
if period < time.Second {
period = time.Second
@ -133,7 +133,7 @@ func (bp *bypass) periodReload(ctx context.Context) error {
}
}
func (bp *bypass) reload(ctx context.Context) error {
func (bp *localBypass) reload(ctx context.Context) error {
v, err := bp.load(ctx)
if err != nil {
return err
@ -171,7 +171,7 @@ func (bp *bypass) reload(ctx context.Context) error {
return nil
}
func (bp *bypass) load(ctx context.Context) (patterns []string, err error) {
func (bp *localBypass) load(ctx context.Context) (patterns []string, err error) {
if bp.options.fileLoader != nil {
if lister, ok := bp.options.fileLoader.(loader.Lister); ok {
list, er := lister.List(ctx)
@ -224,7 +224,7 @@ func (bp *bypass) load(ctx context.Context) (patterns []string, err error) {
return
}
func (bp *bypass) parsePatterns(r io.Reader) (patterns []string, err error) {
func (bp *localBypass) parsePatterns(r io.Reader) (patterns []string, err error) {
if r == nil {
return
}
@ -240,7 +240,7 @@ func (bp *bypass) parsePatterns(r io.Reader) (patterns []string, err error) {
return
}
func (bp *bypass) Contains(ctx context.Context, addr string) bool {
func (bp *localBypass) Contains(ctx context.Context, addr string) bool {
if addr == "" || bp == nil {
return false
}
@ -260,14 +260,14 @@ func (bp *bypass) Contains(ctx context.Context, addr string) bool {
return b
}
func (bp *bypass) parseLine(s string) string {
func (bp *localBypass) parseLine(s string) string {
if n := strings.IndexByte(s, '#'); n >= 0 {
s = s[:n]
}
return strings.TrimSpace(s)
}
func (bp *bypass) matched(addr string) bool {
func (bp *localBypass) matched(addr string) bool {
bp.mu.RLock()
defer bp.mu.RUnlock()
@ -280,7 +280,7 @@ func (bp *bypass) matched(addr string) bool {
bp.wildcardMatcher.Match(addr)
}
func (bp *bypass) Close() error {
func (bp *localBypass) Close() error {
bp.cancelFunc()
if bp.options.fileLoader != nil {
bp.options.fileLoader.Close()

View File

@ -1,13 +1,17 @@
package bypass
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
bypass_pkg "github.com/go-gost/core/bypass"
"github.com/go-gost/core/bypass"
"github.com/go-gost/core/logger"
"github.com/go-gost/plugin/bypass/proto"
auth_util "github.com/go-gost/x/internal/util/auth"
"github.com/go-gost/x/internal/util/plugin"
"google.golang.org/grpc"
)
@ -18,13 +22,24 @@ type grpcPluginBypass struct {
}
// NewGRPCPluginBypass creates a Bypass plugin based on gRPC.
func NewGRPCPluginBypass(name string, conn grpc.ClientConnInterface) bypass_pkg.Bypass {
func NewGRPCPluginBypass(name string, addr string, opts ...plugin.Option) bypass.Bypass {
var options plugin.Options
for _, opt := range opts {
opt(&options)
}
log := logger.Default().WithFields(map[string]any{
"kind": "bypass",
"bypass": name,
})
conn, err := plugin.NewGRPCConn(addr, &options)
if err != nil {
log.Error(err)
}
p := &grpcPluginBypass{
conn: conn,
log: logger.Default().WithFields(map[string]any{
"kind": "bypass",
"bypass": name,
}),
log: log,
}
if conn != nil {
p.client = proto.NewBypassClient(conn)
@ -55,3 +70,77 @@ func (p *grpcPluginBypass) Close() error {
}
return nil
}
type httpBypassRequest struct {
Addr string `json:"addr"`
Client string `json:"client"`
}
type httpBypassResponse struct {
OK bool `json:"ok"`
}
type httpPluginBypass struct {
url string
client *http.Client
header http.Header
log logger.Logger
}
// NewHTTPPluginBypass creates an Bypass plugin based on HTTP.
func NewHTTPPluginBypass(name string, url string, opts ...plugin.Option) bypass.Bypass {
var options plugin.Options
for _, opt := range opts {
opt(&options)
}
return &httpPluginBypass{
url: url,
client: plugin.NewHTTPClient(&options),
header: options.Header,
log: logger.Default().WithFields(map[string]any{
"kind": "bypass",
"bypass": name,
}),
}
}
func (p *httpPluginBypass) Contains(ctx context.Context, addr string) (ok bool) {
if p.client == nil {
return
}
rb := httpBypassRequest{
Addr: addr,
Client: string(auth_util.IDFromContext(ctx)),
}
v, err := json.Marshal(&rb)
if err != nil {
return
}
req, err := http.NewRequest(http.MethodPost, p.url, bytes.NewReader(v))
if err != nil {
return
}
if p.header != nil {
req.Header = p.header.Clone()
}
req.Header.Set("Content-Type", "application/json")
resp, err := p.client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return
}
res := httpBypassResponse{}
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return
}
return res.OK
}