frp/server/visitor/visitor.go

97 lines
2.4 KiB
Go
Raw Normal View History

2019-01-14 17:11:08 +01:00
// Copyright 2019 fatedier, fatedier@gmail.com
2017-03-22 19:01:25 +01: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 05:28:57 +02:00
package visitor
2017-03-08 19:03:47 +01:00
import (
"fmt"
2017-06-25 21:02:33 +02:00
"io"
2019-10-12 14:13:12 +02:00
"net"
2017-03-08 19:03:47 +01:00
"sync"
2017-06-25 21:02:33 +02:00
2020-09-23 07:49:14 +02:00
frpNet "github.com/fatedier/frp/pkg/util/net"
"github.com/fatedier/frp/pkg/util/util"
2018-05-08 17:42:04 +02:00
frpIo "github.com/fatedier/golib/io"
2017-03-08 19:03:47 +01:00
)
2017-12-04 18:34:33 +01:00
// Manager for visitor listeners.
2020-05-24 11:48:37 +02:00
type Manager struct {
2017-12-04 18:34:33 +01:00
visitorListeners map[string]*frpNet.CustomListener
skMap map[string]string
2017-06-25 21:02:33 +02:00
mu sync.RWMutex
}
2020-05-24 11:48:37 +02:00
func NewManager() *Manager {
return &Manager{
2017-12-04 18:34:33 +01:00
visitorListeners: make(map[string]*frpNet.CustomListener),
skMap: make(map[string]string),
2017-06-25 21:02:33 +02:00
}
}
2020-05-24 11:48:37 +02:00
func (vm *Manager) Listen(name string, sk string) (l *frpNet.CustomListener, err error) {
2017-06-25 21:02:33 +02:00
vm.mu.Lock()
defer vm.mu.Unlock()
2017-12-04 18:34:33 +01:00
if _, ok := vm.visitorListeners[name]; ok {
2017-06-25 21:02:33 +02:00
err = fmt.Errorf("custom listener for [%s] is repeated", name)
return
}
l = frpNet.NewCustomListener()
2017-12-04 18:34:33 +01:00
vm.visitorListeners[name] = l
2017-06-25 21:02:33 +02:00
vm.skMap[name] = sk
return
}
2020-05-24 11:48:37 +02:00
func (vm *Manager) NewConn(name string, conn net.Conn, timestamp int64, signKey string,
2017-06-25 21:02:33 +02:00
useEncryption bool, useCompression bool) (err error) {
vm.mu.RLock()
defer vm.mu.RUnlock()
2017-12-04 18:34:33 +01:00
if l, ok := vm.visitorListeners[name]; ok {
2017-06-25 21:02:33 +02:00
var sk string
if sk = vm.skMap[name]; util.GetAuthKey(sk, timestamp) != signKey {
2017-12-04 18:34:33 +01:00
err = fmt.Errorf("visitor connection of [%s] auth failed", name)
2017-06-25 21:02:33 +02:00
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))
2017-06-25 21:02:33 +02:00
} else {
err = fmt.Errorf("custom listener for [%s] doesn't exist", name)
return
}
return
}
2020-05-24 11:48:37 +02:00
func (vm *Manager) CloseListener(name string) {
2017-06-25 21:02:33 +02:00
vm.mu.Lock()
defer vm.mu.Unlock()
2017-12-04 18:34:33 +01:00
delete(vm.visitorListeners, name)
2017-06-25 21:02:33 +02:00
delete(vm.skMap, name)
}