root.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2018 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sub
  15. import (
  16. "context"
  17. "fmt"
  18. "io/ioutil"
  19. "net"
  20. "os"
  21. "os/signal"
  22. "strconv"
  23. "strings"
  24. "syscall"
  25. "time"
  26. "github.com/spf13/cobra"
  27. ini "github.com/vaughan0/go-ini"
  28. "github.com/fatedier/frp/client"
  29. "github.com/fatedier/frp/g"
  30. "github.com/fatedier/frp/models/config"
  31. "github.com/fatedier/frp/utils/log"
  32. "github.com/fatedier/frp/utils/version"
  33. )
  34. const (
  35. CfgFileTypeIni = iota
  36. CfgFileTypeCmd
  37. )
  38. var (
  39. cfgFile string
  40. showVersion bool
  41. serverAddr string
  42. user string
  43. protocol string
  44. token string
  45. logLevel string
  46. logFile string
  47. logMaxDays int
  48. proxyName string
  49. localIp string
  50. localPort int
  51. remotePort int
  52. useEncryption bool
  53. useCompression bool
  54. customDomains string
  55. subDomain string
  56. httpUser string
  57. httpPwd string
  58. locations string
  59. hostHeaderRewrite string
  60. role string
  61. sk string
  62. serverName string
  63. bindAddr string
  64. )
  65. func init() {
  66. rootCmd.PersistentFlags().StringVarP(&cfgFile, "", "c", "./frpc.ini", "config file of frpc")
  67. rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "version of frpc")
  68. }
  69. var rootCmd = &cobra.Command{
  70. Use: "frpc",
  71. Short: "frpc is the client of frp (https://github.com/fatedier/frp)",
  72. RunE: func(cmd *cobra.Command, args []string) error {
  73. if showVersion {
  74. fmt.Println(version.Full())
  75. return nil
  76. }
  77. // Do not show command usage here.
  78. err := runClient(cfgFile)
  79. if err != nil {
  80. fmt.Println(err)
  81. os.Exit(1)
  82. }
  83. return nil
  84. },
  85. }
  86. func Execute() {
  87. if err := rootCmd.Execute(); err != nil {
  88. os.Exit(1)
  89. }
  90. }
  91. func handleSignal(svr *client.Service) {
  92. ch := make(chan os.Signal)
  93. signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
  94. <-ch
  95. svr.Close()
  96. time.Sleep(250 * time.Millisecond)
  97. os.Exit(0)
  98. }
  99. func parseClientCommonCfg(fileType int, filePath string) (err error) {
  100. if fileType == CfgFileTypeIni {
  101. err = parseClientCommonCfgFromIni(filePath)
  102. } else if fileType == CfgFileTypeCmd {
  103. err = parseClientCommonCfgFromCmd()
  104. }
  105. if err != nil {
  106. return
  107. }
  108. g.GlbClientCfg.CfgFile = cfgFile
  109. err = g.GlbClientCfg.ClientCommonConf.Check()
  110. if err != nil {
  111. return
  112. }
  113. return
  114. }
  115. func parseClientCommonCfgFromIni(filePath string) (err error) {
  116. b, err := ioutil.ReadFile(filePath)
  117. if err != nil {
  118. return err
  119. }
  120. content := string(b)
  121. cfg, err := config.UnmarshalClientConfFromIni(&g.GlbClientCfg.ClientCommonConf, content)
  122. if err != nil {
  123. return err
  124. }
  125. g.GlbClientCfg.ClientCommonConf = *cfg
  126. return
  127. }
  128. func parseClientCommonCfgFromCmd() (err error) {
  129. strs := strings.Split(serverAddr, ":")
  130. if len(strs) < 2 {
  131. err = fmt.Errorf("invalid server_addr")
  132. return
  133. }
  134. if strs[0] != "" {
  135. g.GlbClientCfg.ServerAddr = strs[0]
  136. }
  137. g.GlbClientCfg.ServerPort, err = strconv.Atoi(strs[1])
  138. if err != nil {
  139. err = fmt.Errorf("invalid server_addr")
  140. return
  141. }
  142. g.GlbClientCfg.User = user
  143. g.GlbClientCfg.Protocol = protocol
  144. g.GlbClientCfg.Token = token
  145. g.GlbClientCfg.LogLevel = logLevel
  146. g.GlbClientCfg.LogFile = logFile
  147. g.GlbClientCfg.LogMaxDays = int64(logMaxDays)
  148. return nil
  149. }
  150. func runClient(cfgFilePath string) (err error) {
  151. err = parseClientCommonCfg(CfgFileTypeIni, cfgFilePath)
  152. if err != nil {
  153. return
  154. }
  155. conf, err := ini.LoadFile(cfgFilePath)
  156. if err != nil {
  157. return err
  158. }
  159. pxyCfgs, visitorCfgs, err := config.LoadProxyConfFromIni(g.GlbClientCfg.User, conf, g.GlbClientCfg.Start)
  160. if err != nil {
  161. return err
  162. }
  163. err = startService(pxyCfgs, visitorCfgs)
  164. return
  165. }
  166. func startService(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.ProxyConf) (err error) {
  167. log.InitLog(g.GlbClientCfg.LogWay, g.GlbClientCfg.LogFile, g.GlbClientCfg.LogLevel, g.GlbClientCfg.LogMaxDays)
  168. if g.GlbClientCfg.DnsServer != "" {
  169. s := g.GlbClientCfg.DnsServer
  170. if !strings.Contains(s, ":") {
  171. s += ":53"
  172. }
  173. // Change default dns server for frpc
  174. net.DefaultResolver = &net.Resolver{
  175. PreferGo: true,
  176. Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
  177. return net.Dial("udp", s)
  178. },
  179. }
  180. }
  181. svr := client.NewService(pxyCfgs, visitorCfgs)
  182. // Capture the exit signal if we use kcp.
  183. if g.GlbClientCfg.Protocol == "kcp" {
  184. go handleSignal(svr)
  185. }
  186. err = svr.Run()
  187. return
  188. }