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"
"log"
"github.com/ICKelin/opennotr/opennotrd/stream"
"github.com/ICKelin/opennotr/opennotrd/plugin"
"github.com/ICKelin/opennotr/pkg/device"
"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() {
@@ -51,7 +56,7 @@ func main() {
return
}
stream.Setup(cfg.Plugins)
plugin.Setup(cfg.Plugins)
// initial resolver
// currently resolver use coredns and etcd

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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