https.go 2.3 KB
Newer Older
F
fatedier 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright 2018 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sub

import (
	"fmt"
	"os"
	"strings"

	"github.com/spf13/cobra"

F
fatedier 已提交
24 25
	"github.com/fatedier/frp/pkg/config"
	"github.com/fatedier/frp/pkg/consts"
F
fatedier 已提交
26 27 28
)

func init() {
T
Tank 已提交
29
	RegisterCommonFlags(httpsCmd)
F
fatedier 已提交
30 31

	httpsCmd.PersistentFlags().StringVarP(&proxyName, "proxy_name", "n", "", "proxy name")
F
fatedier 已提交
32
	httpsCmd.PersistentFlags().StringVarP(&localIP, "local_ip", "i", "127.0.0.1", "local ip")
F
fatedier 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
	httpsCmd.PersistentFlags().IntVarP(&localPort, "local_port", "l", 0, "local port")
	httpsCmd.PersistentFlags().StringVarP(&customDomains, "custom_domain", "d", "", "custom domain")
	httpsCmd.PersistentFlags().StringVarP(&subDomain, "sd", "", "", "sub domain")
	httpsCmd.PersistentFlags().BoolVarP(&useEncryption, "ue", "", false, "use encryption")
	httpsCmd.PersistentFlags().BoolVarP(&useCompression, "uc", "", false, "use compression")

	rootCmd.AddCommand(httpsCmd)
}

var httpsCmd = &cobra.Command{
	Use:   "https",
	Short: "Run frpc with a single https proxy",
	RunE: func(cmd *cobra.Command, args []string) error {
46
		clientCfg, err := parseClientCommonCfg(CfgFileTypeCmd, "")
F
fatedier 已提交
47 48 49 50 51
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

F
fatedier 已提交
52
		cfg := &config.HTTPSProxyConf{}
F
fatedier 已提交
53 54 55 56 57
		var prefix string
		if user != "" {
			prefix = user + "."
		}
		cfg.ProxyName = prefix + proxyName
F
fatedier 已提交
58 59
		cfg.ProxyType = consts.HTTPSProxy
		cfg.LocalIP = localIP
F
fatedier 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
		cfg.LocalPort = localPort
		cfg.CustomDomains = strings.Split(customDomains, ",")
		cfg.SubDomain = subDomain
		cfg.UseEncryption = useEncryption
		cfg.UseCompression = useCompression

		err = cfg.CheckForCli()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		proxyConfs := map[string]config.ProxyConf{
			cfg.ProxyName: cfg,
		}
75
		err = startService(clientCfg, proxyConfs, nil, "")
F
fatedier 已提交
76 77 78 79 80 81 82
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		return nil
	},
}