Example #1
0
func main() {
	laddr := flag.String("listen", ":8001", "listen address")
	baddr := flag.String("backend", "127.0.0.1:1234", "backend address")
	secret := flag.String("secret", "the answer to life, the universe and everything", "tunnel secret")
	tunnels := flag.Uint("tunnels", 0, "low level tunnel count, 0 if work as server")
	flag.Int64Var(&tunnel.Timeout, "timeout", 3, "tunnel read/write timeout")
	flag.UintVar(&tunnel.LogLevel, "log", 1, "log level")

	flag.Usage = usage
	flag.Parse()

	var app tunnel.Service
	var err error
	if *tunnels == 0 {
		app, err = tunnel.NewServer(*laddr, *baddr, *secret)
	} else {
		app, err = tunnel.NewClient(*laddr, *baddr, *secret, *tunnels)
	}

	if err != nil {
		fmt.Fprintf(os.Stderr, "create service failed:%s\n", err.Error())
		return
	}

	if err = app.Start(); err != nil {
		fmt.Fprintf(os.Stderr, "start failed:%s\n", err.Error())
		return
	}
	handleSignal(app)
}
Example #2
0
func handleSignal(app tunnel.Service) {
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGHUP)

	for sig := range c {
		switch sig {
		case syscall.SIGHUP:
			app.Status()
			tunnel.Log("total goroutines:%d", runtime.NumGoroutine())
		default:
			tunnel.Log("catch signal:%v, exit", sig)
			os.Exit(1)
		}
	}
}