mirror of
https://github.com/fatedier/frp.git
synced 2025-07-07 00:44:16 +07:00
server: adjust code structure
This commit is contained in:
46
server/controller/resource.go
Normal file
46
server/controller/resource.go
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2019 fatedier, fatedier@gmail.com
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/fatedier/frp/models/nathole"
|
||||
"github.com/fatedier/frp/server/group"
|
||||
"github.com/fatedier/frp/server/ports"
|
||||
"github.com/fatedier/frp/utils/vhost"
|
||||
)
|
||||
|
||||
// All resource managers and controllers
|
||||
type ResourceController struct {
|
||||
// Manage all visitor listeners
|
||||
VisitorManager *VisitorManager
|
||||
|
||||
// Tcp Group Controller
|
||||
TcpGroupCtl *group.TcpGroupCtl
|
||||
|
||||
// Manage all tcp ports
|
||||
TcpPortManager *ports.PortManager
|
||||
|
||||
// Manage all udp ports
|
||||
UdpPortManager *ports.PortManager
|
||||
|
||||
// For http proxies, forwarding http requests
|
||||
HttpReverseProxy *vhost.HttpReverseProxy
|
||||
|
||||
// For https proxies, route requests to different clients by hostname and other infomation
|
||||
VhostHttpsMuxer *vhost.HttpsMuxer
|
||||
|
||||
// Controller for nat hole connections
|
||||
NatHoleController *nathole.NatHoleController
|
||||
}
|
95
server/controller/visitor.go
Normal file
95
server/controller/visitor.go
Normal file
@ -0,0 +1,95 @@
|
||||
// Copyright 2019 fatedier, fatedier@gmail.com
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
frpNet "github.com/fatedier/frp/utils/net"
|
||||
"github.com/fatedier/frp/utils/util"
|
||||
|
||||
frpIo "github.com/fatedier/golib/io"
|
||||
)
|
||||
|
||||
// Manager for visitor listeners.
|
||||
type VisitorManager struct {
|
||||
visitorListeners map[string]*frpNet.CustomListener
|
||||
skMap map[string]string
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewVisitorManager() *VisitorManager {
|
||||
return &VisitorManager{
|
||||
visitorListeners: make(map[string]*frpNet.CustomListener),
|
||||
skMap: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *VisitorManager) Listen(name string, sk string) (l *frpNet.CustomListener, err error) {
|
||||
vm.mu.Lock()
|
||||
defer vm.mu.Unlock()
|
||||
|
||||
if _, ok := vm.visitorListeners[name]; ok {
|
||||
err = fmt.Errorf("custom listener for [%s] is repeated", name)
|
||||
return
|
||||
}
|
||||
|
||||
l = frpNet.NewCustomListener()
|
||||
vm.visitorListeners[name] = l
|
||||
vm.skMap[name] = sk
|
||||
return
|
||||
}
|
||||
|
||||
func (vm *VisitorManager) NewConn(name string, conn frpNet.Conn, timestamp int64, signKey string,
|
||||
useEncryption bool, useCompression bool) (err error) {
|
||||
|
||||
vm.mu.RLock()
|
||||
defer vm.mu.RUnlock()
|
||||
|
||||
if l, ok := vm.visitorListeners[name]; ok {
|
||||
var sk string
|
||||
if sk = vm.skMap[name]; util.GetAuthKey(sk, timestamp) != signKey {
|
||||
err = fmt.Errorf("visitor connection of [%s] auth failed", name)
|
||||
return
|
||||
}
|
||||
|
||||
var rwc io.ReadWriteCloser = conn
|
||||
if useEncryption {
|
||||
if rwc, err = frpIo.WithEncryption(rwc, []byte(sk)); err != nil {
|
||||
err = fmt.Errorf("create encryption connection failed: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if useCompression {
|
||||
rwc = frpIo.WithCompression(rwc)
|
||||
}
|
||||
err = l.PutConn(frpNet.WrapReadWriteCloserToConn(rwc, conn))
|
||||
} else {
|
||||
err = fmt.Errorf("custom listener for [%s] doesn't exist", name)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (vm *VisitorManager) CloseListener(name string) {
|
||||
vm.mu.Lock()
|
||||
defer vm.mu.Unlock()
|
||||
|
||||
delete(vm.visitorListeners, name)
|
||||
delete(vm.skMap, name)
|
||||
}
|
Reference in New Issue
Block a user