proxy.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2023 The frp Authors
  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 legacy
  15. import (
  16. "fmt"
  17. "reflect"
  18. "gopkg.in/ini.v1"
  19. "github.com/fatedier/frp/pkg/config/types"
  20. "github.com/fatedier/frp/pkg/consts"
  21. )
  22. // Proxy
  23. var (
  24. proxyConfTypeMap = map[string]reflect.Type{
  25. consts.TCPProxy: reflect.TypeOf(TCPProxyConf{}),
  26. consts.TCPMuxProxy: reflect.TypeOf(TCPMuxProxyConf{}),
  27. consts.UDPProxy: reflect.TypeOf(UDPProxyConf{}),
  28. consts.HTTPProxy: reflect.TypeOf(HTTPProxyConf{}),
  29. consts.HTTPSProxy: reflect.TypeOf(HTTPSProxyConf{}),
  30. consts.STCPProxy: reflect.TypeOf(STCPProxyConf{}),
  31. consts.XTCPProxy: reflect.TypeOf(XTCPProxyConf{}),
  32. consts.SUDPProxy: reflect.TypeOf(SUDPProxyConf{}),
  33. }
  34. )
  35. type ProxyConf interface {
  36. // GetBaseConfig returns the BaseProxyConf for this config.
  37. GetBaseConfig() *BaseProxyConf
  38. // UnmarshalFromIni unmarshals a ini.Section into this config. This function
  39. // will be called on the frpc side.
  40. UnmarshalFromIni(string, string, *ini.Section) error
  41. }
  42. func NewConfByType(proxyType string) ProxyConf {
  43. v, ok := proxyConfTypeMap[proxyType]
  44. if !ok {
  45. return nil
  46. }
  47. cfg := reflect.New(v).Interface().(ProxyConf)
  48. return cfg
  49. }
  50. // Proxy Conf Loader
  51. // DefaultProxyConf creates a empty ProxyConf object by proxyType.
  52. // If proxyType doesn't exist, return nil.
  53. func DefaultProxyConf(proxyType string) ProxyConf {
  54. return NewConfByType(proxyType)
  55. }
  56. // Proxy loaded from ini
  57. func NewProxyConfFromIni(prefix, name string, section *ini.Section) (ProxyConf, error) {
  58. // section.Key: if key not exists, section will set it with default value.
  59. proxyType := section.Key("type").String()
  60. if proxyType == "" {
  61. proxyType = consts.TCPProxy
  62. }
  63. conf := DefaultProxyConf(proxyType)
  64. if conf == nil {
  65. return nil, fmt.Errorf("invalid type [%s]", proxyType)
  66. }
  67. if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
  68. return nil, err
  69. }
  70. return conf, nil
  71. }
  72. // LocalSvrConf configures what location the client will to, or what
  73. // plugin will be used.
  74. type LocalSvrConf struct {
  75. // LocalIP specifies the IP address or host name to to.
  76. LocalIP string `ini:"local_ip" json:"local_ip"`
  77. // LocalPort specifies the port to to.
  78. LocalPort int `ini:"local_port" json:"local_port"`
  79. // Plugin specifies what plugin should be used for ng. If this value
  80. // is set, the LocalIp and LocalPort values will be ignored. By default,
  81. // this value is "".
  82. Plugin string `ini:"plugin" json:"plugin"`
  83. // PluginParams specify parameters to be passed to the plugin, if one is
  84. // being used. By default, this value is an empty map.
  85. PluginParams map[string]string `ini:"-"`
  86. }
  87. // HealthCheckConf configures health checking. This can be useful for load
  88. // balancing purposes to detect and remove proxies to failing services.
  89. type HealthCheckConf struct {
  90. // HealthCheckType specifies what protocol to use for health checking.
  91. // Valid values include "tcp", "http", and "". If this value is "", health
  92. // checking will not be performed. By default, this value is "".
  93. //
  94. // If the type is "tcp", a connection will be attempted to the target
  95. // server. If a connection cannot be established, the health check fails.
  96. //
  97. // If the type is "http", a GET request will be made to the endpoint
  98. // specified by HealthCheckURL. If the response is not a 200, the health
  99. // check fails.
  100. HealthCheckType string `ini:"health_check_type" json:"health_check_type"` // tcp | http
  101. // HealthCheckTimeoutS specifies the number of seconds to wait for a health
  102. // check attempt to connect. If the timeout is reached, this counts as a
  103. // health check failure. By default, this value is 3.
  104. HealthCheckTimeoutS int `ini:"health_check_timeout_s" json:"health_check_timeout_s"`
  105. // HealthCheckMaxFailed specifies the number of allowed failures before the
  106. // is stopped. By default, this value is 1.
  107. HealthCheckMaxFailed int `ini:"health_check_max_failed" json:"health_check_max_failed"`
  108. // HealthCheckIntervalS specifies the time in seconds between health
  109. // checks. By default, this value is 10.
  110. HealthCheckIntervalS int `ini:"health_check_interval_s" json:"health_check_interval_s"`
  111. // HealthCheckURL specifies the address to send health checks to if the
  112. // health check type is "http".
  113. HealthCheckURL string `ini:"health_check_url" json:"health_check_url"`
  114. // HealthCheckAddr specifies the address to connect to if the health check
  115. // type is "tcp".
  116. HealthCheckAddr string `ini:"-"`
  117. }
  118. // BaseProxyConf provides configuration info that is common to all types.
  119. type BaseProxyConf struct {
  120. // ProxyName is the name of this
  121. ProxyName string `ini:"name" json:"name"`
  122. // ProxyType specifies the type of this Valid values include "tcp",
  123. // "udp", "http", "https", "stcp", and "xtcp". By default, this value is
  124. // "tcp".
  125. ProxyType string `ini:"type" json:"type"`
  126. // UseEncryption controls whether or not communication with the server will
  127. // be encrypted. Encryption is done using the tokens supplied in the server
  128. // and client configuration. By default, this value is false.
  129. UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
  130. // UseCompression controls whether or not communication with the server
  131. // will be compressed. By default, this value is false.
  132. UseCompression bool `ini:"use_compression" json:"use_compression"`
  133. // Group specifies which group the is a part of. The server will use
  134. // this information to load balance proxies in the same group. If the value
  135. // is "", this will not be in a group. By default, this value is "".
  136. Group string `ini:"group" json:"group"`
  137. // GroupKey specifies a group key, which should be the same among proxies
  138. // of the same group. By default, this value is "".
  139. GroupKey string `ini:"group_key" json:"group_key"`
  140. // ProxyProtocolVersion specifies which protocol version to use. Valid
  141. // values include "v1", "v2", and "". If the value is "", a protocol
  142. // version will be automatically selected. By default, this value is "".
  143. ProxyProtocolVersion string `ini:"proxy_protocol_version" json:"proxy_protocol_version"`
  144. // BandwidthLimit limit the bandwidth
  145. // 0 means no limit
  146. BandwidthLimit types.BandwidthQuantity `ini:"bandwidth_limit" json:"bandwidth_limit"`
  147. // BandwidthLimitMode specifies whether to limit the bandwidth on the
  148. // client or server side. Valid values include "client" and "server".
  149. // By default, this value is "client".
  150. BandwidthLimitMode string `ini:"bandwidth_limit_mode" json:"bandwidth_limit_mode"`
  151. // meta info for each proxy
  152. Metas map[string]string `ini:"-" json:"metas"`
  153. LocalSvrConf `ini:",extends"`
  154. HealthCheckConf `ini:",extends"`
  155. }
  156. // Base
  157. func (cfg *BaseProxyConf) GetBaseConfig() *BaseProxyConf {
  158. return cfg
  159. }
  160. // BaseProxyConf apply custom logic changes.
  161. func (cfg *BaseProxyConf) decorate(_ string, name string, section *ini.Section) error {
  162. cfg.ProxyName = name
  163. // metas_xxx
  164. cfg.Metas = GetMapWithoutPrefix(section.KeysHash(), "meta_")
  165. // bandwidth_limit
  166. if bandwidth, err := section.GetKey("bandwidth_limit"); err == nil {
  167. cfg.BandwidthLimit, err = types.NewBandwidthQuantity(bandwidth.String())
  168. if err != nil {
  169. return err
  170. }
  171. }
  172. // plugin_xxx
  173. cfg.LocalSvrConf.PluginParams = GetMapByPrefix(section.KeysHash(), "plugin_")
  174. return nil
  175. }
  176. type DomainConf struct {
  177. CustomDomains []string `ini:"custom_domains" json:"custom_domains"`
  178. SubDomain string `ini:"subdomain" json:"subdomain"`
  179. }
  180. type RoleServerCommonConf struct {
  181. Role string `ini:"role" json:"role"`
  182. Sk string `ini:"sk" json:"sk"`
  183. AllowUsers []string `ini:"allow_users" json:"allow_users"`
  184. }
  185. // HTTP
  186. type HTTPProxyConf struct {
  187. BaseProxyConf `ini:",extends"`
  188. DomainConf `ini:",extends"`
  189. Locations []string `ini:"locations" json:"locations"`
  190. HTTPUser string `ini:"http_user" json:"http_user"`
  191. HTTPPwd string `ini:"http_pwd" json:"http_pwd"`
  192. HostHeaderRewrite string `ini:"host_header_rewrite" json:"host_header_rewrite"`
  193. Headers map[string]string `ini:"-" json:"headers"`
  194. RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
  195. }
  196. func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  197. err := preUnmarshalFromIni(cfg, prefix, name, section)
  198. if err != nil {
  199. return err
  200. }
  201. // Add custom logic unmarshal if exists
  202. cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_")
  203. return nil
  204. }
  205. // HTTPS
  206. type HTTPSProxyConf struct {
  207. BaseProxyConf `ini:",extends"`
  208. DomainConf `ini:",extends"`
  209. }
  210. func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  211. err := preUnmarshalFromIni(cfg, prefix, name, section)
  212. if err != nil {
  213. return err
  214. }
  215. // Add custom logic unmarshal if exists
  216. return nil
  217. }
  218. // TCP
  219. type TCPProxyConf struct {
  220. BaseProxyConf `ini:",extends"`
  221. RemotePort int `ini:"remote_port" json:"remote_port"`
  222. }
  223. func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  224. err := preUnmarshalFromIni(cfg, prefix, name, section)
  225. if err != nil {
  226. return err
  227. }
  228. // Add custom logic unmarshal if exists
  229. return nil
  230. }
  231. // UDP
  232. type UDPProxyConf struct {
  233. BaseProxyConf `ini:",extends"`
  234. RemotePort int `ini:"remote_port" json:"remote_port"`
  235. }
  236. func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  237. err := preUnmarshalFromIni(cfg, prefix, name, section)
  238. if err != nil {
  239. return err
  240. }
  241. // Add custom logic unmarshal if exists
  242. return nil
  243. }
  244. // TCPMux
  245. type TCPMuxProxyConf struct {
  246. BaseProxyConf `ini:",extends"`
  247. DomainConf `ini:",extends"`
  248. HTTPUser string `ini:"http_user" json:"http_user,omitempty"`
  249. HTTPPwd string `ini:"http_pwd" json:"http_pwd,omitempty"`
  250. RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
  251. Multiplexer string `ini:"multiplexer"`
  252. }
  253. func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  254. err := preUnmarshalFromIni(cfg, prefix, name, section)
  255. if err != nil {
  256. return err
  257. }
  258. // Add custom logic unmarshal if exists
  259. return nil
  260. }
  261. // STCP
  262. type STCPProxyConf struct {
  263. BaseProxyConf `ini:",extends"`
  264. RoleServerCommonConf `ini:",extends"`
  265. }
  266. func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  267. err := preUnmarshalFromIni(cfg, prefix, name, section)
  268. if err != nil {
  269. return err
  270. }
  271. // Add custom logic unmarshal if exists
  272. if cfg.Role == "" {
  273. cfg.Role = "server"
  274. }
  275. return nil
  276. }
  277. // XTCP
  278. type XTCPProxyConf struct {
  279. BaseProxyConf `ini:",extends"`
  280. RoleServerCommonConf `ini:",extends"`
  281. }
  282. func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  283. err := preUnmarshalFromIni(cfg, prefix, name, section)
  284. if err != nil {
  285. return err
  286. }
  287. // Add custom logic unmarshal if exists
  288. if cfg.Role == "" {
  289. cfg.Role = "server"
  290. }
  291. return nil
  292. }
  293. // SUDP
  294. type SUDPProxyConf struct {
  295. BaseProxyConf `ini:",extends"`
  296. RoleServerCommonConf `ini:",extends"`
  297. }
  298. func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  299. err := preUnmarshalFromIni(cfg, prefix, name, section)
  300. if err != nil {
  301. return err
  302. }
  303. // Add custom logic unmarshal if exists
  304. return nil
  305. }
  306. func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini.Section) error {
  307. err := section.MapTo(cfg)
  308. if err != nil {
  309. return err
  310. }
  311. err = cfg.GetBaseConfig().decorate(prefix, name, section)
  312. if err != nil {
  313. return err
  314. }
  315. return nil
  316. }