mirror of
https://github.com/Sagit-chu/flvx.git
synced 2026-10-13 04:36:37 +08:00
init
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
package ingress
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/ingress"
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/x/internal/loader"
|
||||
)
|
||||
|
||||
type options struct {
|
||||
rules []*ingress.Rule
|
||||
fileLoader loader.Loader
|
||||
redisLoader loader.Loader
|
||||
httpLoader loader.Loader
|
||||
period time.Duration
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
type Option func(opts *options)
|
||||
|
||||
func RulesOption(rules []*ingress.Rule) Option {
|
||||
return func(opts *options) {
|
||||
opts.rules = rules
|
||||
}
|
||||
}
|
||||
|
||||
func ReloadPeriodOption(period time.Duration) Option {
|
||||
return func(opts *options) {
|
||||
opts.period = period
|
||||
}
|
||||
}
|
||||
|
||||
func FileLoaderOption(fileLoader loader.Loader) Option {
|
||||
return func(opts *options) {
|
||||
opts.fileLoader = fileLoader
|
||||
}
|
||||
}
|
||||
|
||||
func RedisLoaderOption(redisLoader loader.Loader) Option {
|
||||
return func(opts *options) {
|
||||
opts.redisLoader = redisLoader
|
||||
}
|
||||
}
|
||||
|
||||
func HTTPLoaderOption(httpLoader loader.Loader) Option {
|
||||
return func(opts *options) {
|
||||
opts.httpLoader = httpLoader
|
||||
}
|
||||
}
|
||||
|
||||
func LoggerOption(logger logger.Logger) Option {
|
||||
return func(opts *options) {
|
||||
opts.logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
type localIngress struct {
|
||||
rules map[string]*ingress.Rule
|
||||
cancelFunc context.CancelFunc
|
||||
options options
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewIngress creates and initializes a new Ingress.
|
||||
func NewIngress(opts ...Option) ingress.Ingress {
|
||||
var options options
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
|
||||
ing := &localIngress{
|
||||
cancelFunc: cancel,
|
||||
options: options,
|
||||
}
|
||||
|
||||
if err := ing.reload(ctx); err != nil {
|
||||
options.logger.Warnf("reload: %v", err)
|
||||
}
|
||||
if ing.options.period > 0 {
|
||||
go ing.periodReload(ctx)
|
||||
}
|
||||
|
||||
return ing
|
||||
}
|
||||
|
||||
func (ing *localIngress) periodReload(ctx context.Context) error {
|
||||
period := ing.options.period
|
||||
if period < time.Second {
|
||||
period = time.Second
|
||||
}
|
||||
ticker := time.NewTicker(period)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if err := ing.reload(ctx); err != nil {
|
||||
ing.options.logger.Warnf("reload: %v", err)
|
||||
// return err
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ing *localIngress) reload(ctx context.Context) error {
|
||||
rules := make(map[string]*ingress.Rule)
|
||||
|
||||
fn := func(rule *ingress.Rule) {
|
||||
if rule.Hostname == "" || rule.Endpoint == "" {
|
||||
return
|
||||
}
|
||||
host := rule.Hostname
|
||||
if host[0] == '*' {
|
||||
host = host[1:]
|
||||
}
|
||||
rules[host] = rule
|
||||
}
|
||||
|
||||
for _, rule := range ing.options.rules {
|
||||
fn(rule)
|
||||
}
|
||||
|
||||
v, err := ing.load(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, rule := range v {
|
||||
fn(rule)
|
||||
}
|
||||
|
||||
ing.options.logger.Debugf("load items %d", len(rules))
|
||||
|
||||
ing.mu.Lock()
|
||||
defer ing.mu.Unlock()
|
||||
|
||||
ing.rules = rules
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ing *localIngress) load(ctx context.Context) (rules []*ingress.Rule, err error) {
|
||||
if ing.options.fileLoader != nil {
|
||||
if lister, ok := ing.options.fileLoader.(loader.Lister); ok {
|
||||
list, er := lister.List(ctx)
|
||||
if er != nil {
|
||||
ing.options.logger.Warnf("file loader: %v", er)
|
||||
}
|
||||
for _, s := range list {
|
||||
rules = append(rules, ing.parseLine(s))
|
||||
}
|
||||
} else {
|
||||
r, er := ing.options.fileLoader.Load(ctx)
|
||||
if er != nil {
|
||||
ing.options.logger.Warnf("file loader: %v", er)
|
||||
}
|
||||
if v, _ := ing.parseRules(r); v != nil {
|
||||
rules = append(rules, v...)
|
||||
}
|
||||
}
|
||||
}
|
||||
if ing.options.redisLoader != nil {
|
||||
if lister, ok := ing.options.redisLoader.(loader.Lister); ok {
|
||||
list, er := lister.List(ctx)
|
||||
if er != nil {
|
||||
ing.options.logger.Warnf("redis loader: %v", er)
|
||||
}
|
||||
for _, v := range list {
|
||||
rules = append(rules, ing.parseLine(v))
|
||||
}
|
||||
} else {
|
||||
r, er := ing.options.redisLoader.Load(ctx)
|
||||
if er != nil {
|
||||
ing.options.logger.Warnf("redis loader: %v", er)
|
||||
}
|
||||
v, _ := ing.parseRules(r)
|
||||
rules = append(rules, v...)
|
||||
}
|
||||
}
|
||||
if ing.options.httpLoader != nil {
|
||||
r, er := ing.options.httpLoader.Load(ctx)
|
||||
if er != nil {
|
||||
ing.options.logger.Warnf("http loader: %v", er)
|
||||
}
|
||||
v, _ := ing.parseRules(r)
|
||||
rules = append(rules, v...)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (ing *localIngress) parseRules(r io.Reader) (rules []*ingress.Rule, err error) {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
if rule := ing.parseLine(scanner.Text()); rule.Hostname != "" {
|
||||
rules = append(rules, rule)
|
||||
}
|
||||
}
|
||||
|
||||
err = scanner.Err()
|
||||
return
|
||||
}
|
||||
|
||||
func (ing *localIngress) GetRule(ctx context.Context, host string, opts ...ingress.Option) *ingress.Rule {
|
||||
if host == "" || ing == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// try to strip the port
|
||||
if v, _, _ := net.SplitHostPort(host); v != "" {
|
||||
host = v
|
||||
}
|
||||
|
||||
ing.options.logger.Debugf("ingress: lookup %s", host)
|
||||
ep := ing.lookup(host)
|
||||
if ep == nil {
|
||||
ep = ing.lookup("." + host)
|
||||
}
|
||||
if ep == nil {
|
||||
s := host
|
||||
for {
|
||||
if index := strings.IndexByte(s, '.'); index > 0 {
|
||||
ep = ing.lookup(s[index:])
|
||||
s = s[index+1:]
|
||||
if ep == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if ep != nil {
|
||||
ing.options.logger.Debugf("ingress: %s -> %s:%s", host, ep.Hostname, ep.Endpoint)
|
||||
}
|
||||
|
||||
return ep
|
||||
}
|
||||
|
||||
func (ing *localIngress) SetRule(ctx context.Context, rule *ingress.Rule, opts ...ingress.Option) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (ing *localIngress) lookup(host string) *ingress.Rule {
|
||||
if ing == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ing.mu.RLock()
|
||||
defer ing.mu.RUnlock()
|
||||
|
||||
return ing.rules[host]
|
||||
}
|
||||
|
||||
func (ing *localIngress) parseLine(s string) (rule *ingress.Rule) {
|
||||
line := strings.Replace(s, "\t", " ", -1)
|
||||
line = strings.TrimSpace(line)
|
||||
if n := strings.IndexByte(line, '#'); n >= 0 {
|
||||
line = line[:n]
|
||||
}
|
||||
var sp []string
|
||||
for _, s := range strings.Split(line, " ") {
|
||||
if s = strings.TrimSpace(s); s != "" {
|
||||
sp = append(sp, s)
|
||||
}
|
||||
}
|
||||
if len(sp) < 2 {
|
||||
return // invalid lines are ignored
|
||||
}
|
||||
|
||||
return &ingress.Rule{
|
||||
Hostname: sp[0],
|
||||
Endpoint: sp[1],
|
||||
}
|
||||
}
|
||||
|
||||
func (ing *localIngress) Close() error {
|
||||
ing.cancelFunc()
|
||||
if ing.options.fileLoader != nil {
|
||||
ing.options.fileLoader.Close()
|
||||
}
|
||||
if ing.options.redisLoader != nil {
|
||||
ing.options.redisLoader.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package ingress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/go-gost/core/ingress"
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/plugin/ingress/proto"
|
||||
"github.com/go-gost/x/internal/plugin"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type grpcPlugin struct {
|
||||
conn grpc.ClientConnInterface
|
||||
client proto.IngressClient
|
||||
log logger.Logger
|
||||
}
|
||||
|
||||
// NewGRPCPlugin creates an Ingress plugin based on gRPC.
|
||||
func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) ingress.Ingress {
|
||||
var options plugin.Options
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
log := logger.Default().WithFields(map[string]any{
|
||||
"kind": "ingress",
|
||||
"ingress": name,
|
||||
})
|
||||
conn, err := plugin.NewGRPCConn(addr, &options)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
p := &grpcPlugin{
|
||||
conn: conn,
|
||||
log: log,
|
||||
}
|
||||
if conn != nil {
|
||||
p.client = proto.NewIngressClient(conn)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) GetRule(ctx context.Context, host string, opts ...ingress.Option) *ingress.Rule {
|
||||
if p.client == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
r, err := p.client.GetRule(ctx,
|
||||
&proto.GetRuleRequest{
|
||||
Host: host,
|
||||
})
|
||||
if err != nil {
|
||||
p.log.Error(err)
|
||||
return nil
|
||||
}
|
||||
if r.Endpoint == "" {
|
||||
return nil
|
||||
}
|
||||
return &ingress.Rule{
|
||||
Hostname: host,
|
||||
Endpoint: r.Endpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) SetRule(ctx context.Context, rule *ingress.Rule, opts ...ingress.Option) bool {
|
||||
if p.client == nil || rule == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
r, _ := p.client.SetRule(ctx, &proto.SetRuleRequest{
|
||||
Host: rule.Hostname,
|
||||
Endpoint: rule.Endpoint,
|
||||
})
|
||||
if r == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return r.Ok
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) Close() error {
|
||||
if p.conn == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if closer, ok := p.conn.(io.Closer); ok {
|
||||
return closer.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package ingress
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-gost/core/ingress"
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/x/internal/plugin"
|
||||
)
|
||||
|
||||
type httpPluginGetRuleRequest struct {
|
||||
Host string `json:"host"`
|
||||
}
|
||||
|
||||
type httpPluginGetRuleResponse struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
}
|
||||
|
||||
type httpPluginSetRuleRequest struct {
|
||||
Host string `json:"host"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
}
|
||||
|
||||
type httpPluginSetRuleResponse struct {
|
||||
OK bool `json:"ok"`
|
||||
}
|
||||
|
||||
type httpPlugin struct {
|
||||
url string
|
||||
client *http.Client
|
||||
header http.Header
|
||||
log logger.Logger
|
||||
}
|
||||
|
||||
// NewHTTPPlugin creates an Ingress plugin based on HTTP.
|
||||
func NewHTTPPlugin(name string, url string, opts ...plugin.Option) ingress.Ingress {
|
||||
var options plugin.Options
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &httpPlugin{
|
||||
url: url,
|
||||
client: plugin.NewHTTPClient(&options),
|
||||
header: options.Header,
|
||||
log: logger.Default().WithFields(map[string]any{
|
||||
"kind": "ingress",
|
||||
"ingress": name,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *httpPlugin) GetRule(ctx context.Context, host string, opts ...ingress.Option) *ingress.Rule {
|
||||
if p.client == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.url, nil)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if p.header != nil {
|
||||
req.Header = p.header.Clone()
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Set("host", host)
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
resp, err := p.client.Do(req)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
|
||||
res := httpPluginGetRuleResponse{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
|
||||
return nil
|
||||
}
|
||||
if res.Endpoint == "" {
|
||||
return nil
|
||||
}
|
||||
return &ingress.Rule{
|
||||
Hostname: host,
|
||||
Endpoint: res.Endpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *httpPlugin) SetRule(ctx context.Context, rule *ingress.Rule, opts ...ingress.Option) bool {
|
||||
if p.client == nil || rule == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
rb := httpPluginSetRuleRequest{
|
||||
Host: rule.Hostname,
|
||||
Endpoint: rule.Endpoint,
|
||||
}
|
||||
v, err := json.Marshal(&rb)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, p.url, bytes.NewReader(v))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
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 false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return false
|
||||
}
|
||||
|
||||
res := httpPluginSetRuleResponse{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
|
||||
return false
|
||||
}
|
||||
return res.OK
|
||||
}
|
||||
Reference in New Issue
Block a user