optimize: rename some structs

This commit is contained in:
ICKelin
2021-04-30 23:22:01 +08:00
parent 0064d10fdc
commit e59425baeb
6 changed files with 56 additions and 55 deletions

View File

@@ -20,4 +20,5 @@ forwards:
- protocol: h2c
ports:
0: 50052
0: 50052

View File

@@ -47,7 +47,7 @@ type Server struct {
dhcp *DHCP
// call stream proxy for dynamic add/del tcp/udp proxy
streamProxy *plugin.Stream
pluginMgr *plugin.PluginManager
// tun device wraper
dev *device.Device
@@ -66,14 +66,14 @@ func NewServer(cfg ServerConfig,
dev *device.Device,
resolver *Resolver) *Server {
return &Server{
addr: cfg.ListenAddr,
authKey: cfg.AuthKey,
domain: cfg.Domain,
publicIP: publicIP(),
dhcp: dhcp,
streamProxy: plugin.DefaultStream(),
dev: dev,
resolver: resolver,
addr: cfg.ListenAddr,
authKey: cfg.AuthKey,
domain: cfg.Domain,
publicIP: publicIP(),
dhcp: dhcp,
pluginMgr: plugin.DefaultPluginManager(),
dev: dev,
resolver: resolver,
}
}
@@ -157,7 +157,7 @@ func (s *Server) onConn(conn net.Conn) {
// Domain is only use for restyproxy
for _, forward := range auth.Forward {
for localPort, upstreamPort := range forward.Ports {
item := &plugin.ProxyItem{
item := &plugin.PluginMeta{
Protocol: forward.Protocol,
From: fmt.Sprintf("0.0.0.0:%d", localPort),
To: fmt.Sprintf("%s:%d", vip, upstreamPort),
@@ -165,8 +165,8 @@ func (s *Server) onConn(conn net.Conn) {
RecycleSignal: make(chan struct{}),
}
s.streamProxy.AddProxy(item)
defer s.streamProxy.DelProxy(item)
s.pluginMgr.AddProxy(item)
defer s.pluginMgr.DelProxy(item)
}
}

View File

@@ -8,64 +8,64 @@ import (
"github.com/ICKelin/opennotr/pkg/logs"
)
var stream = &Stream{
routes: make(map[string]*ProxyItem),
proxier: make(map[string]Proxier),
var pluginMgr = &PluginManager{
routes: make(map[string]*PluginMeta),
plugins: make(map[string]IPlugin),
}
type ProxyItem struct {
type PluginMeta struct {
Protocol string
From string
To string
Domain string
Ctx interface{} // data pass to proxier
Ctx interface{} // data pass to plugin
RecycleSignal chan struct{}
}
func (item *ProxyItem) identify() string {
func (item *PluginMeta) identify() string {
return fmt.Sprintf("%s:%s:%s:%s", item.Protocol, item.From, item.To, item.Domain)
}
// Proxier defines stream proxy API
type Proxier interface {
// IPlugin defines proxy plugin API
type IPlugin interface {
Setup(json.RawMessage) error
StopProxy(item *ProxyItem)
RunProxy(item *ProxyItem) error
StopProxy(item *PluginMeta)
RunProxy(item *PluginMeta) error
}
type Stream struct {
type PluginManager struct {
mu sync.Mutex
// routes stores proxier of localAddress
// key: proxyItem.identify()
// value: proxyItem
routes map[string]*ProxyItem
// key: pluginMeta.identify()
// value: pluginMeta
routes map[string]*PluginMeta
// proxier stores proxier info of each registerd proxier
// by call RegisterProxier function.
// plugins store plugin information
// by call plugin.Register function.
// key: protocol, eg: tcp, udp
// value: proxy implement
proxier map[string]Proxier
// value: plugin implement
plugins map[string]IPlugin
}
func DefaultStream() *Stream {
return stream
func DefaultPluginManager() *PluginManager {
return pluginMgr
}
func RegisterProxier(protocol string, proxier Proxier) {
stream.proxier[protocol] = proxier
func Register(protocol string, p IPlugin) {
pluginMgr.plugins[protocol] = p
}
func Setup(plugins map[string]string) error {
for protocol, cfg := range plugins {
logs.Info("setup for %s with configuration:\n%s", protocol, cfg)
proxier, ok := stream.proxier[protocol]
plug, ok := pluginMgr.plugins[protocol]
if !ok {
logs.Error("protocol %s not register", protocol)
return fmt.Errorf("protocol %s not register", protocol)
}
err := proxier.Setup([]byte(cfg))
err := plug.Setup([]byte(cfg))
if err != nil {
logs.Error("setup protocol %s fail: %v", protocol, err)
return err
@@ -75,7 +75,7 @@ func Setup(plugins map[string]string) error {
return nil
}
func (p *Stream) AddProxy(item *ProxyItem) error {
func (p *PluginManager) AddProxy(item *PluginMeta) error {
p.mu.Lock()
defer p.mu.Unlock()
key := item.identify()
@@ -83,12 +83,12 @@ func (p *Stream) AddProxy(item *ProxyItem) error {
return fmt.Errorf("port %s is in used", key)
}
proxier, ok := p.proxier[item.Protocol]
plug, ok := p.plugins[item.Protocol]
if !ok {
return fmt.Errorf("proxy %s not register", item.Protocol)
}
err := proxier.RunProxy(item)
err := plug.RunProxy(item)
if err != nil {
logs.Error("run proxy fail: %v", err)
return err
@@ -97,14 +97,14 @@ func (p *Stream) AddProxy(item *ProxyItem) error {
return nil
}
func (p *Stream) DelProxy(item *ProxyItem) {
func (p *PluginManager) DelProxy(item *PluginMeta) {
p.mu.Lock()
defer p.mu.Unlock()
key := item.identify()
proxier, ok := p.proxier[item.Protocol]
plug, ok := p.plugins[item.Protocol]
if ok {
proxier.StopProxy(item)
plug.StopProxy(item)
}
delete(p.routes, key)

View File

@@ -16,9 +16,9 @@ import (
var restyAdminUrl string
func init() {
plugin.RegisterProxier("http", &RestyProxy{})
plugin.RegisterProxier("https", &RestyProxy{})
plugin.RegisterProxier("h2c", &RestyProxy{})
plugin.Register("http", &RestyProxy{})
plugin.Register("https", &RestyProxy{})
plugin.Register("h2c", &RestyProxy{})
}
type AddUpstreamBody struct {
@@ -46,11 +46,11 @@ func (p *RestyProxy) Setup(config json.RawMessage) error {
return nil
}
func (p *RestyProxy) StopProxy(item *plugin.ProxyItem) {
func (p *RestyProxy) StopProxy(item *plugin.PluginMeta) {
p.sendDeleteReq(item.Domain, item.Protocol)
}
func (p *RestyProxy) RunProxy(item *plugin.ProxyItem) error {
func (p *RestyProxy) RunProxy(item *plugin.PluginMeta) error {
vip, port, err := net.SplitHostPort(item.To)
if err != nil {
return err

View File

@@ -12,21 +12,21 @@ import (
)
func init() {
plugin.RegisterProxier("tcp", &TCPProxy{})
plugin.Register("tcp", &TCPProxy{})
}
type TCPProxy struct{}
func (t *TCPProxy) Setup(config json.RawMessage) error { return nil }
func (t *TCPProxy) StopProxy(item *plugin.ProxyItem) {
func (t *TCPProxy) StopProxy(item *plugin.PluginMeta) {
select {
case item.RecycleSignal <- struct{}{}:
default:
}
}
func (t *TCPProxy) RunProxy(item *plugin.ProxyItem) error {
func (t *TCPProxy) RunProxy(item *plugin.PluginMeta) error {
from, to := item.From, item.To
lis, err := net.Listen("tcp", from)
if err != nil {

View File

@@ -11,21 +11,21 @@ import (
)
func init() {
plugin.RegisterProxier("udp", &UDPProxy{})
plugin.Register("udp", &UDPProxy{})
}
type UDPProxy struct{}
func (p *UDPProxy) Setup(config json.RawMessage) error { return nil }
func (p *UDPProxy) StopProxy(item *plugin.ProxyItem) {
func (p *UDPProxy) StopProxy(item *plugin.PluginMeta) {
select {
case item.RecycleSignal <- struct{}{}:
default:
}
}
func (p *UDPProxy) RunProxy(item *plugin.ProxyItem) error {
func (p *UDPProxy) RunProxy(item *plugin.PluginMeta) error {
from := item.From
laddr, err := net.ResolveUDPAddr("udp", from)
if err != nil {
@@ -41,7 +41,7 @@ func (p *UDPProxy) RunProxy(item *plugin.ProxyItem) error {
return nil
}
func (p *UDPProxy) doProxy(lis *net.UDPConn, item *plugin.ProxyItem) {
func (p *UDPProxy) doProxy(lis *net.UDPConn, item *plugin.PluginMeta) {
defer lis.Close()
from := item.From