Esempio n. 1
0
func startCommand(ctx *cli.Context) {
	workdir := ctx.GlobalString("workdir")
	if workdir == "" {
		fmt.Println("unknown working directory, please use -w to provide.")
		os.Exit(1)
	}
	// daemon
	daemon := ctx.GlobalBool("daemon")

	// options
	options := app.NewOptions(ctx.GlobalString("http"), ctx.GlobalString("api"))
	if err := options.Prepare(workdir); err != nil {
		fmt.Println("options prepare failed:", err)
		os.Exit(1)
	}

	refresh := ctx.GlobalString("refresh")
	options.SetRefreshDefault(refresh, time.Minute*10)

	// configs
	configs, err := app.LoadCONFIG(path.Join(options.Root, "h2forward.conf"))
	if err != nil {
		configs = app.DefaultCONFIG()
		if err := configs.Save(path.Join(options.Root, "h2forward.conf")); err != nil {
			fmt.Println("h2forward.conf saving failed:", err)
			os.Exit(1)
		}
	}

	logger := log.NewH2OLogger()
	defer logger.Close()
	logger.SetConsole(true)

	configs.SetSection("logs")
	fenable := configs.BoolDefault("file.enable", false)
	fname := configs.StringDefault("file.name", "h2o.log")
	flevel := configs.StringDefault("file.level", "info")
	fsize := configs.IntDefault("file.rotate_max_size", 1024*1024*1024)
	fline := configs.IntDefault("file.rotate_max_line", 102400)
	fdaily := configs.BoolDefault("file.rotate_daily", true)
	fn := path.Join(options.LogsRoot, fname)
	if fenable == true {
		logger.SetFileLog(fn, flevel, fsize, fline, fdaily)
	}

	application := app.NewApplication(options, configs, logger)

	if err := application.Init(); err != nil {
		fmt.Println("[h2forward] init failed:", err)
		os.Exit(1)
	}

	start(application, daemon)
}
Esempio n. 2
0
func vhostDelCommand(ctx *cli.Context) {
	workdir := ctx.GlobalString("workdir")
	if workdir == "" {
		fmt.Println("unknown working directory, please use -w to provide.")
		os.Exit(1)
	}

	// directory
	directory, err := filepath.Abs(workdir)
	if err != nil {
		fmt.Println("workdir:", err)
		return
	}

	configs, err := app.LoadCONFIG(path.Join(directory, "h2forward.conf"))
	if err != nil {
		fmt.Println("load configure failed:", err)
		return
	}

	addr := ctx.GlobalString("api")

	configs.SetSection("forward")
	AppID := configs.StringDefault("appid", "")
	Secret := configs.StringDefault("secret", "")

	client := api.NewClient(addr)
	auth := api.NewAdminAuth(AppID, Secret)

	args := ctx.Args()
	if len(args) == 0 {
		fmt.Println("command args: <hostname1> <hostname2> ...")
		return
	}

	for _, host := range args {
		if err := client.DelHost(nil, auth, host); err != nil {
			fmt.Println("det host failed:", err)
			return
		}
		fmt.Printf("det host (%s) ok.\n", host)
	}
	return
}
Esempio n. 3
0
func vhostGetCommand(ctx *cli.Context) {
	workdir := ctx.GlobalString("workdir")
	if workdir == "" {
		fmt.Println("unknown working directory, please use -w to provide.")
		os.Exit(1)
	}

	// directory
	directory, err := filepath.Abs(workdir)
	if err != nil {
		fmt.Println("workdir:", err)
		return
	}

	configs, err := app.LoadCONFIG(path.Join(directory, "h2forward.conf"))
	if err != nil {
		fmt.Println("load configure failed:", err)
		return
	}

	addr := ctx.GlobalString("api")

	configs.SetSection("forward")
	AppID := configs.StringDefault("appid", "")
	Secret := configs.StringDefault("secret", "")

	client := api.NewClient(addr)
	auth := api.NewAdminAuth(AppID, Secret)

	hosts := ctx.Args()
	ret := map[string]interface{}{}
	if err := client.GetHost(nil, auth, hosts, &ret); err != nil {
		fmt.Println("get host failed:", err)
		return
	}

	b, _ := json.MarshalIndent(ret, "", " ")
	fmt.Println(string(b))
	return
}