service.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2017 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 client
  15. import (
  16. "github.com/fatedier/frp/g"
  17. "github.com/fatedier/frp/models/config"
  18. "github.com/fatedier/frp/utils/log"
  19. )
  20. type Service struct {
  21. // manager control connection with server
  22. ctl *Control
  23. closedCh chan int
  24. }
  25. func NewService(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.VisitorConf) (svr *Service) {
  26. svr = &Service{
  27. closedCh: make(chan int),
  28. }
  29. ctl := NewControl(svr, pxyCfgs, visitorCfgs)
  30. svr.ctl = ctl
  31. return
  32. }
  33. func (svr *Service) Run() error {
  34. err := svr.ctl.Run()
  35. if err != nil {
  36. return err
  37. }
  38. if g.GlbClientCfg.AdminPort != 0 {
  39. err = svr.RunAdminServer(g.GlbClientCfg.AdminAddr, g.GlbClientCfg.AdminPort)
  40. if err != nil {
  41. log.Warn("run admin server error: %v", err)
  42. }
  43. log.Info("admin server listen on %s:%d", g.GlbClientCfg.AdminAddr, g.GlbClientCfg.AdminPort)
  44. }
  45. <-svr.closedCh
  46. return nil
  47. }
  48. func (svr *Service) Close() {
  49. svr.ctl.Close()
  50. }