sudp.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. v1 "github.com/fatedier/frp/pkg/config/v1"
  18. )
  19. func init() {
  20. RegisterProxyFactory(reflect.TypeOf(&v1.SUDPProxyConfig{}), NewSUDPProxy)
  21. }
  22. type SUDPProxy struct {
  23. *BaseProxy
  24. cfg *v1.SUDPProxyConfig
  25. }
  26. func NewSUDPProxy(baseProxy *BaseProxy) Proxy {
  27. unwrapped, ok := baseProxy.GetConfigurer().(*v1.SUDPProxyConfig)
  28. if !ok {
  29. return nil
  30. }
  31. return &SUDPProxy{
  32. BaseProxy: baseProxy,
  33. cfg: unwrapped,
  34. }
  35. }
  36. func (pxy *SUDPProxy) 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.Secretkey, allowUsers)
  44. if errRet != nil {
  45. err = errRet
  46. return
  47. }
  48. pxy.listeners = append(pxy.listeners, listener)
  49. xl.Infof("sudp proxy custom listen success")
  50. pxy.startCommonTCPListenersHandler()
  51. return
  52. }
  53. func (pxy *SUDPProxy) Close() {
  54. pxy.BaseProxy.Close()
  55. pxy.rc.VisitorManager.CloseListener(pxy.GetName())
  56. }