2018-04-10 17:46:49 +08:00
// 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"
2020-09-23 13:49:14 +08:00
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/consts"
2018-04-10 17:46:49 +08:00
2020-09-23 13:49:14 +08:00
"github.com/spf13/cobra"
2018-04-10 17:46:49 +08:00
)
func init ( ) {
2020-05-19 10:49:29 +08:00
RegisterCommonFlags ( httpCmd )
2018-04-10 17:46:49 +08:00
httpCmd . PersistentFlags ( ) . StringVarP ( & proxyName , "proxy_name" , "n" , "" , "proxy name" )
2020-05-24 17:48:37 +08:00
httpCmd . PersistentFlags ( ) . StringVarP ( & localIP , "local_ip" , "i" , "127.0.0.1" , "local ip" )
2018-04-10 17:46:49 +08:00
httpCmd . PersistentFlags ( ) . IntVarP ( & localPort , "local_port" , "l" , 0 , "local port" )
httpCmd . PersistentFlags ( ) . StringVarP ( & customDomains , "custom_domain" , "d" , "" , "custom domain" )
httpCmd . PersistentFlags ( ) . StringVarP ( & subDomain , "sd" , "" , "" , "sub domain" )
httpCmd . PersistentFlags ( ) . StringVarP ( & locations , "locations" , "" , "" , "locations" )
2018-05-03 10:20:09 +08:00
httpCmd . PersistentFlags ( ) . StringVarP ( & httpUser , "http_user" , "" , "" , "http auth user" )
httpCmd . PersistentFlags ( ) . StringVarP ( & httpPwd , "http_pwd" , "" , "" , "http auth password" )
2022-09-25 15:49:07 +08:00
httpCmd . PersistentFlags ( ) . StringVarP ( & ipsAllowList , "ips_allow_list" , "" , "" , "ips allow list - supports subnet mask for example - 192.168.0.0/16, 255.255.0.0" )
2018-04-10 17:46:49 +08:00
httpCmd . PersistentFlags ( ) . StringVarP ( & hostHeaderRewrite , "host_header_rewrite" , "" , "" , "host header rewrite" )
httpCmd . PersistentFlags ( ) . BoolVarP ( & useEncryption , "ue" , "" , false , "use encryption" )
httpCmd . PersistentFlags ( ) . BoolVarP ( & useCompression , "uc" , "" , false , "use compression" )
rootCmd . AddCommand ( httpCmd )
}
var httpCmd = & cobra . Command {
Use : "http" ,
Short : "Run frpc with a single http proxy" ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2021-06-02 23:54:22 +08:00
clientCfg , err := parseClientCommonCfgFromCmd ( )
2018-04-10 17:46:49 +08:00
if err != nil {
fmt . Println ( err )
os . Exit ( 1 )
}
2020-05-24 17:48:37 +08:00
cfg := & config . HTTPProxyConf { }
2018-04-10 17:46:49 +08:00
var prefix string
if user != "" {
prefix = user + "."
}
cfg . ProxyName = prefix + proxyName
2020-05-24 17:48:37 +08:00
cfg . ProxyType = consts . HTTPProxy
cfg . LocalIP = localIP
2018-04-10 17:46:49 +08:00
cfg . LocalPort = localPort
cfg . CustomDomains = strings . Split ( customDomains , "," )
cfg . SubDomain = subDomain
cfg . Locations = strings . Split ( locations , "," )
2020-05-24 17:48:37 +08:00
cfg . HTTPUser = httpUser
cfg . HTTPPwd = httpPwd
2022-09-25 15:49:07 +08:00
cfg . IpsAllowList = strings . Split ( ipsAllowList , "," )
2018-04-10 17:46:49 +08:00
cfg . HostHeaderRewrite = hostHeaderRewrite
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 ,
}
2019-08-21 04:53:27 +08:00
err = startService ( clientCfg , proxyConfs , nil , "" )
2018-04-10 17:46:49 +08:00
if err != nil {
fmt . Println ( err )
os . Exit ( 1 )
}
return nil
} ,
}