cfg.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package base
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "reflect"
  7. )
  8. const (
  9. LinkModeTUN = "tun"
  10. LinkModeTAP = "tap"
  11. LinkModeMacvtap = "macvtap"
  12. LinkModeIpvtap = "ipvtap"
  13. )
  14. var (
  15. Cfg = &ServerConfig{}
  16. )
  17. // # ReKey time (in seconds)
  18. // rekey-time = 172800
  19. // # ReKey method
  20. // # Valid options: ssl, new-tunnel
  21. // # ssl: Will perform an efficient rehandshake on the channel allowing
  22. // # a seamless connection during rekey.
  23. // # new-tunnel: Will instruct the client to discard and re-establish the channel.
  24. // # Use this option only if the connecting clients have issues with the ssl
  25. // # option.
  26. // rekey-method = ssl
  27. type ServerConfig struct {
  28. // LinkAddr string `json:"link_addr"`
  29. Conf string `json:"conf"`
  30. Profile string `json:"profile"`
  31. ServerAddr string `json:"server_addr"`
  32. ServerDTLSAddr string `json:"server_dtls_addr"`
  33. ServerDTLS bool `json:"server_dtls"`
  34. AdminAddr string `json:"admin_addr"`
  35. ProxyProtocol bool `json:"proxy_protocol"`
  36. DbType string `json:"db_type"`
  37. DbSource string `json:"db_source"`
  38. CertFile string `json:"cert_file"`
  39. CertKey string `json:"cert_key"`
  40. FilesPath string `json:"files_path"`
  41. LogPath string `json:"log_path"`
  42. LogLevel string `json:"log_level"`
  43. Pprof bool `json:"pprof"`
  44. Issuer string `json:"issuer"`
  45. AdminUser string `json:"admin_user"`
  46. AdminPass string `json:"admin_pass"`
  47. JwtSecret string `json:"jwt_secret"`
  48. LinkMode string `json:"link_mode"` // tun tap macvtap ipvtap
  49. Ipv4Master string `json:"ipv4_master"` // eth0
  50. Ipv4CIDR string `json:"ipv4_cidr"` // 192.168.10.0/24
  51. Ipv4Gateway string `json:"ipv4_gateway"` // 192.168.10.1
  52. Ipv4Start string `json:"ipv4_start"` // 192.168.10.100
  53. Ipv4End string `json:"ipv4_end"` // 192.168.10.200
  54. IpLease int `json:"ip_lease"`
  55. MaxClient int `json:"max_client"`
  56. MaxUserClient int `json:"max_user_client"`
  57. DefaultGroup string `json:"default_group"`
  58. CstpKeepalive int `json:"cstp_keepalive"` // in seconds
  59. CstpDpd int `json:"cstp_dpd"` // Dead peer detection in seconds
  60. MobileKeepalive int `json:"mobile_keepalive"`
  61. MobileDpd int `json:"mobile_dpd"`
  62. Mtu int `json:"mtu"`
  63. DefaultDomain string `json:"default_domain"`
  64. SessionTimeout int `json:"session_timeout"` // in seconds
  65. // AuthTimeout int `json:"auth_timeout"` // in seconds
  66. AuditInterval int `json:"audit_interval"` // in seconds
  67. ShowSQL bool `json:"show_sql"` // bool
  68. }
  69. func initServerCfg() {
  70. // TODO 取消绝对地址转换
  71. // sf, _ := filepath.Abs(cfgFile)
  72. // base := filepath.Dir(sf)
  73. // 转换成绝对路径
  74. // Cfg.DbFile = getAbsPath(base, Cfg.DbFile)
  75. // Cfg.CertFile = getAbsPath(base, Cfg.CertFile)
  76. // Cfg.CertKey = getAbsPath(base, Cfg.CertKey)
  77. // Cfg.UiPath = getAbsPath(base, Cfg.UiPath)
  78. // Cfg.FilesPath = getAbsPath(base, Cfg.FilesPath)
  79. // Cfg.LogPath = getAbsPath(base, Cfg.LogPath)
  80. if Cfg.AdminPass == defaultPwd {
  81. fmt.Fprintln(os.Stderr, "=== 使用默认的admin_pass有安全风险,请设置新的admin_pass ===")
  82. }
  83. if Cfg.JwtSecret == defaultJwt {
  84. fmt.Fprintln(os.Stderr, "=== 使用默认的jwt_secret有安全风险,请设置新的jwt_secret ===")
  85. }
  86. fmt.Printf("ServerCfg: %+v \n", Cfg)
  87. }
  88. func getAbsPath(base, cfile string) string {
  89. if cfile == "" {
  90. return ""
  91. }
  92. abs := filepath.IsAbs(cfile)
  93. if abs {
  94. return cfile
  95. }
  96. return filepath.Join(base, cfile)
  97. }
  98. func initCfg() {
  99. ref := reflect.ValueOf(Cfg)
  100. s := ref.Elem()
  101. typ := s.Type()
  102. numFields := s.NumField()
  103. for i := 0; i < numFields; i++ {
  104. field := typ.Field(i)
  105. value := s.Field(i)
  106. tag := field.Tag.Get("json")
  107. for _, v := range configs {
  108. if v.Name == tag {
  109. if v.Typ == cfgStr {
  110. value.SetString(linkViper.GetString(v.Name))
  111. }
  112. if v.Typ == cfgInt {
  113. value.SetInt(int64(linkViper.GetInt(v.Name)))
  114. }
  115. if v.Typ == cfgBool {
  116. value.SetBool(linkViper.GetBool(v.Name))
  117. }
  118. }
  119. }
  120. }
  121. initServerCfg()
  122. }
  123. type SCfg struct {
  124. Name string `json:"name"`
  125. Env string `json:"env"`
  126. Info string `json:"info"`
  127. Data interface{} `json:"data"`
  128. }
  129. func ServerCfg2Slice() []SCfg {
  130. ref := reflect.ValueOf(Cfg)
  131. s := ref.Elem()
  132. var datas []SCfg
  133. typ := s.Type()
  134. numFields := s.NumField()
  135. for i := 0; i < numFields; i++ {
  136. field := typ.Field(i)
  137. value := s.Field(i)
  138. tag := field.Tag.Get("json")
  139. usage, env := getUsageEnv(tag)
  140. datas = append(datas, SCfg{Name: tag, Env: env, Info: usage, Data: value.Interface()})
  141. }
  142. return datas
  143. }
  144. func getUsageEnv(name string) (usage, env string) {
  145. for _, v := range configs {
  146. if v.Name == name {
  147. usage = v.Usage
  148. }
  149. }
  150. if e, ok := envs[name]; ok {
  151. env = e
  152. }
  153. return
  154. }