2019-01-15 00:11:08 +08:00
|
|
|
// Copyright 2019 fatedier, fatedier@gmail.com
|
2017-03-23 02:01:25 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-05-24 11:28:57 +08:00
|
|
|
package visitor
|
2017-03-09 02:03:47 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-26 03:02:33 +08:00
|
|
|
"io"
|
2019-10-12 20:13:12 +08:00
|
|
|
"net"
|
2017-03-09 02:03:47 +08:00
|
|
|
"sync"
|
2017-06-26 03:02:33 +08:00
|
|
|
|
2023-05-29 14:10:34 +08:00
|
|
|
libio "github.com/fatedier/golib/io"
|
2023-06-02 16:06:29 +08:00
|
|
|
"github.com/samber/lo"
|
2022-08-29 01:02:53 +08:00
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
2017-03-09 02:03:47 +08:00
|
|
|
)
|
|
|
|
|
2023-06-02 16:06:29 +08:00
|
|
|
type listenerBundle struct {
|
2023-11-27 15:47:49 +08:00
|
|
|
l *netpkg.InternalListener
|
2023-06-02 16:06:29 +08:00
|
|
|
sk string
|
|
|
|
allowUsers []string
|
|
|
|
}
|
|
|
|
|
2017-12-05 01:34:33 +08:00
|
|
|
// Manager for visitor listeners.
|
2020-05-24 17:48:37 +08:00
|
|
|
type Manager struct {
|
2023-06-02 16:06:29 +08:00
|
|
|
listeners map[string]*listenerBundle
|
2017-06-26 03:02:33 +08:00
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func NewManager() *Manager {
|
|
|
|
return &Manager{
|
2023-06-02 16:06:29 +08:00
|
|
|
listeners: make(map[string]*listenerBundle),
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
func (vm *Manager) Listen(name string, sk string, allowUsers []string) (*netpkg.InternalListener, error) {
|
2017-06-26 03:02:33 +08:00
|
|
|
vm.mu.Lock()
|
|
|
|
defer vm.mu.Unlock()
|
|
|
|
|
2023-06-02 16:06:29 +08:00
|
|
|
if _, ok := vm.listeners[name]; ok {
|
2023-11-27 15:47:49 +08:00
|
|
|
return nil, fmt.Errorf("custom listener for [%s] is repeated", name)
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
l := netpkg.NewInternalListener()
|
2023-06-02 16:06:29 +08:00
|
|
|
vm.listeners[name] = &listenerBundle{
|
|
|
|
l: l,
|
|
|
|
sk: sk,
|
|
|
|
allowUsers: allowUsers,
|
|
|
|
}
|
2023-11-27 15:47:49 +08:00
|
|
|
return l, nil
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (vm *Manager) NewConn(name string, conn net.Conn, timestamp int64, signKey string,
|
2023-06-02 16:06:29 +08:00
|
|
|
useEncryption bool, useCompression bool, visitorUser string,
|
2022-08-29 01:02:53 +08:00
|
|
|
) (err error) {
|
2017-06-26 03:02:33 +08:00
|
|
|
vm.mu.RLock()
|
|
|
|
defer vm.mu.RUnlock()
|
|
|
|
|
2023-06-02 16:06:29 +08:00
|
|
|
if l, ok := vm.listeners[name]; ok {
|
|
|
|
if util.GetAuthKey(l.sk, timestamp) != signKey {
|
2017-12-05 01:34:33 +08:00
|
|
|
err = fmt.Errorf("visitor connection of [%s] auth failed", name)
|
2017-06-26 03:02:33 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-02 16:06:29 +08:00
|
|
|
if !lo.Contains(l.allowUsers, visitorUser) && !lo.Contains(l.allowUsers, "*") {
|
|
|
|
err = fmt.Errorf("visitor connection of [%s] user [%s] not allowed", name, visitorUser)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:02:33 +08:00
|
|
|
var rwc io.ReadWriteCloser = conn
|
|
|
|
if useEncryption {
|
2023-06-02 16:06:29 +08:00
|
|
|
if rwc, err = libio.WithEncryption(rwc, []byte(l.sk)); err != nil {
|
2017-06-26 03:02:33 +08:00
|
|
|
err = fmt.Errorf("create encryption connection failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if useCompression {
|
2023-05-29 14:10:34 +08:00
|
|
|
rwc = libio.WithCompression(rwc)
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|
2023-11-27 15:47:49 +08:00
|
|
|
err = l.l.PutConn(netpkg.WrapReadWriteCloserToConn(rwc, conn))
|
2017-06-26 03:02:33 +08:00
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("custom listener for [%s] doesn't exist", name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (vm *Manager) CloseListener(name string) {
|
2017-06-26 03:02:33 +08:00
|
|
|
vm.mu.Lock()
|
|
|
|
defer vm.mu.Unlock()
|
|
|
|
|
2023-06-02 16:06:29 +08:00
|
|
|
delete(vm.listeners, name)
|
2017-06-26 03:02:33 +08:00
|
|
|
}
|