rename stream to plugin

This commit is contained in:
ICKelin
2021-04-29 20:45:47 +08:00
parent e95d962d36
commit 27a9f09cf8
7 changed files with 31 additions and 28 deletions

View File

@@ -5,9 +5,14 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/ICKelin/opennotr/opennotrd/stream" "github.com/ICKelin/opennotr/opennotrd/plugin"
"github.com/ICKelin/opennotr/pkg/device" "github.com/ICKelin/opennotr/pkg/device"
"github.com/ICKelin/opennotr/pkg/logs" "github.com/ICKelin/opennotr/pkg/logs"
// plugin import to register
_ "github.com/ICKelin/opennotr/opennotrd/plugin/restyproxy"
_ "github.com/ICKelin/opennotr/opennotrd/plugin/tcpproxy"
_ "github.com/ICKelin/opennotr/opennotrd/plugin/udpproxy"
) )
func main() { func main() {
@@ -51,7 +56,7 @@ func main() {
return return
} }
stream.Setup(cfg.Plugins) plugin.Setup(cfg.Plugins)
// initial resolver // initial resolver
// currently resolver use coredns and etcd // currently resolver use coredns and etcd

View File

@@ -1,4 +1,4 @@
package stream package plugin
import ( import (
"encoding/json" "encoding/json"

View File

@@ -1,4 +1,4 @@
package stream package plugin
import ( import (
"net" "net"

View File

@@ -1,4 +1,4 @@
package stream package restyproxy
import ( import (
"bytes" "bytes"
@@ -9,15 +9,16 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/ICKelin/opennotr/opennotrd/plugin"
"github.com/ICKelin/opennotr/pkg/logs" "github.com/ICKelin/opennotr/pkg/logs"
) )
var restyAdminUrl string var restyAdminUrl string
func init() { func init() {
RegisterProxier("http", &RestyProxy{}) plugin.RegisterProxier("http", &RestyProxy{})
RegisterProxier("https", &RestyProxy{}) plugin.RegisterProxier("https", &RestyProxy{})
RegisterProxier("h2c", &RestyProxy{}) plugin.RegisterProxier("h2c", &RestyProxy{})
} }
type AddUpstreamBody struct { type AddUpstreamBody struct {
@@ -45,11 +46,11 @@ func (p *RestyProxy) Setup(config json.RawMessage) error {
return nil return nil
} }
func (p *RestyProxy) StopProxy(item *ProxyItem) { func (p *RestyProxy) StopProxy(item *plugin.ProxyItem) {
p.sendDeleteReq(item.Host, item.Protocol) p.sendDeleteReq(item.Host, item.Protocol)
} }
func (p *RestyProxy) RunProxy(item *ProxyItem) error { func (p *RestyProxy) RunProxy(item *plugin.ProxyItem) error {
vip, port, err := net.SplitHostPort(item.To) vip, port, err := net.SplitHostPort(item.To)
if err != nil { if err != nil {
return err return err

View File

@@ -1,4 +1,4 @@
package stream package tcpproxy
import ( import (
"encoding/json" "encoding/json"
@@ -7,25 +7,26 @@ import (
"sync" "sync"
"time" "time"
"github.com/ICKelin/opennotr/opennotrd/plugin"
"github.com/ICKelin/opennotr/pkg/logs" "github.com/ICKelin/opennotr/pkg/logs"
) )
func init() { func init() {
RegisterProxier("tcp", &TCPProxy{}) plugin.RegisterProxier("tcp", &TCPProxy{})
} }
type TCPProxy struct{} type TCPProxy struct{}
func (t *TCPProxy) Setup(config json.RawMessage) error { return nil } func (t *TCPProxy) Setup(config json.RawMessage) error { return nil }
func (t *TCPProxy) StopProxy(item *ProxyItem) { func (t *TCPProxy) StopProxy(item *plugin.ProxyItem) {
select { select {
case item.RecycleSignal <- struct{}{}: case item.RecycleSignal <- struct{}{}:
default: default:
} }
} }
func (t *TCPProxy) RunProxy(item *ProxyItem) error { func (t *TCPProxy) RunProxy(item *plugin.ProxyItem) error {
from, to := item.From, item.To from, to := item.From, item.To
lis, err := net.Listen("tcp", from) lis, err := net.Listen("tcp", from)
if err != nil { if err != nil {

View File

@@ -1,4 +1,4 @@
package stream package udpproxy
import ( import (
"context" "context"
@@ -6,25 +6,26 @@ import (
"net" "net"
"sync" "sync"
"github.com/ICKelin/opennotr/opennotrd/plugin"
"github.com/ICKelin/opennotr/pkg/logs" "github.com/ICKelin/opennotr/pkg/logs"
) )
func init() { func init() {
RegisterProxier("udp", &UDPProxy{}) plugin.RegisterProxier("udp", &UDPProxy{})
} }
type UDPProxy struct{} type UDPProxy struct{}
func (p *UDPProxy) Setup(config json.RawMessage) error { return nil } func (p *UDPProxy) Setup(config json.RawMessage) error { return nil }
func (p *UDPProxy) StopProxy(item *ProxyItem) { func (p *UDPProxy) StopProxy(item *plugin.ProxyItem) {
select { select {
case item.RecycleSignal <- struct{}{}: case item.RecycleSignal <- struct{}{}:
default: default:
} }
} }
func (p *UDPProxy) RunProxy(item *ProxyItem) error { func (p *UDPProxy) RunProxy(item *plugin.ProxyItem) error {
from := item.From from := item.From
laddr, err := net.ResolveUDPAddr("udp", from) laddr, err := net.ResolveUDPAddr("udp", from)
if err != nil { if err != nil {
@@ -40,7 +41,7 @@ func (p *UDPProxy) RunProxy(item *ProxyItem) error {
return nil return nil
} }
func (p *UDPProxy) doProxy(lis *net.UDPConn, item *ProxyItem) { func (p *UDPProxy) doProxy(lis *net.UDPConn, item *plugin.ProxyItem) {
defer lis.Close() defer lis.Close()
from := item.From from := item.From

View File

@@ -11,7 +11,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/ICKelin/opennotr/opennotrd/stream" "github.com/ICKelin/opennotr/opennotrd/plugin"
"github.com/ICKelin/opennotr/pkg/device" "github.com/ICKelin/opennotr/pkg/device"
"github.com/ICKelin/opennotr/pkg/logs" "github.com/ICKelin/opennotr/pkg/logs"
"github.com/ICKelin/opennotr/pkg/proto" "github.com/ICKelin/opennotr/pkg/proto"
@@ -46,13 +46,8 @@ type Server struct {
// dhcp manager select/release ip for client // dhcp manager select/release ip for client
dhcp *DHCP dhcp *DHCP
// call resty-upstream for dynamic upstream
// for http, https, grpc, websocket
// Ref: https://github.com/ICKelin/resty-upstream
// upstreamMgr *UpstreamManager
// call stream proxy for dynamic add/del tcp/udp proxy // call stream proxy for dynamic add/del tcp/udp proxy
streamProxy *stream.Stream streamProxy *plugin.Stream
// tun device wraper // tun device wraper
dev *device.Device dev *device.Device
@@ -76,7 +71,7 @@ func NewServer(cfg ServerConfig,
domain: cfg.Domain, domain: cfg.Domain,
publicIP: publicIP(), publicIP: publicIP(),
dhcp: dhcp, dhcp: dhcp,
streamProxy: stream.DefaultStream(), streamProxy: plugin.DefaultStream(),
dev: dev, dev: dev,
resolver: resolver, resolver: resolver,
} }
@@ -162,7 +157,7 @@ func (s *Server) onConn(conn net.Conn) {
// Host is only use for restyproxy // Host is only use for restyproxy
for _, forward := range auth.Forward { for _, forward := range auth.Forward {
for localPort, upstreamPort := range forward.Ports { for localPort, upstreamPort := range forward.Ports {
item := &stream.ProxyItem{ item := &plugin.ProxyItem{
Protocol: forward.Protocol, Protocol: forward.Protocol,
From: fmt.Sprintf("0.0.0.0:%d", localPort), From: fmt.Sprintf("0.0.0.0:%d", localPort),
To: fmt.Sprintf("%s:%d", vip, upstreamPort), To: fmt.Sprintf("%s:%d", vip, upstreamPort),