| 70 | } |
| 71 | |
| 72 | func start(c *cli.Context) error { |
| 73 | ctx, cancel := context.WithCancel(context.Background()) |
| 74 | defer cancel() |
| 75 | wg := sync.WaitGroup{} |
| 76 | errSignal := make(chan error) |
| 77 | p, err := proxy.New() |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | wg.Add(1) |
| 83 | fmt.Println("listener port:", config.Get().Proxy.LocalPort) |
| 84 | utils.GoWithRecover(func() { |
| 85 | defer wg.Done() |
| 86 | p.Run(ctx, errSignal) |
| 87 | }, nil) |
| 88 | err = <-errSignal |
| 89 | |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | // Listening to the offline |
| 95 | sigs := make(chan os.Signal, 1) |
| 96 | signal.Notify(sigs, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) |
| 97 | for sig := range sigs { |
| 98 | switch sig { |
| 99 | case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT: |
| 100 | cancel() |
| 101 | ok := make(chan struct{}) |
| 102 | go func() { |
| 103 | wg.Wait() |
| 104 | ok <- struct{}{} |
| 105 | }() |
| 106 | select { |
| 107 | case <-ok: |
| 108 | logrus.Info("shutdown proxy") |
| 109 | case <-time.After(time.Second * 5): |
| 110 | logrus.Info("context deadline exceeded") |
| 111 | } |
| 112 | os.Exit(0) |
| 113 | case syscall.SIGHUP: |
| 114 | logrus.Info("catch syscall.SIGHUP") |
| 115 | } |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func initConfig(c *cli.Context) error { |
| 121 | // Read configuration file configuration |