stcp.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package proxy
  15. import (
  16. "reflect"
  17. "github.com/fatedier/frp/pkg/config"
  18. )
  19. func init() {
  20. RegisterProxyFactory(reflect.TypeOf(&config.STCPProxyConf{}), NewSTCPProxy)
  21. }
  22. type STCPProxy struct {
  23. *BaseProxy
  24. cfg *config.STCPProxyConf
  25. }
  26. func NewSTCPProxy(baseProxy *BaseProxy, cfg config.ProxyConf) Proxy {
  27. unwrapped, ok := cfg.(*config.STCPProxyConf)
  28. if !ok {
  29. return nil
  30. }
  31. return &STCPProxy{
  32. BaseProxy: baseProxy,
  33. cfg: unwrapped,
  34. }
  35. }
  36. func (pxy *STCPProxy) Run() (remoteAddr string, err error) {
  37. xl := pxy.xl
  38. allowUsers := pxy.cfg.AllowUsers
  39. // if allowUsers is empty, only allow same user from proxy
  40. if len(allowUsers) == 0 {
  41. allowUsers = []string{pxy.GetUserInfo().User}
  42. }
  43. listener, errRet := pxy.rc.VisitorManager.Listen(pxy.GetName(), pxy.cfg.Sk, allowUsers)
  44. if errRet != nil {
  45. err = errRet
  46. return
  47. }
  48. pxy.listeners = append(pxy.listeners, listener)
  49. xl.Info("stcp proxy custom listen success")
  50. pxy.startCommonTCPListenersHandler()
  51. return
  52. }
  53. func (pxy *STCPProxy) GetConf() config.ProxyConf {
  54. return pxy.cfg
  55. }
  56. func (pxy *STCPProxy) Close() {
  57. pxy.BaseProxy.Close()
  58. pxy.rc.VisitorManager.CloseListener(pxy.GetName())
  59. }