1
0

proxy.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. // Copyright 2016 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 config
  15. import (
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "gopkg.in/ini.v1"
  22. "github.com/fatedier/frp/pkg/consts"
  23. "github.com/fatedier/frp/pkg/msg"
  24. )
  25. // Proxy
  26. var (
  27. proxyConfTypeMap = map[string]reflect.Type{
  28. consts.TCPProxy: reflect.TypeOf(TCPProxyConf{}),
  29. consts.TCPMuxProxy: reflect.TypeOf(TCPMuxProxyConf{}),
  30. consts.UDPProxy: reflect.TypeOf(UDPProxyConf{}),
  31. consts.HTTPProxy: reflect.TypeOf(HTTPProxyConf{}),
  32. consts.HTTPSProxy: reflect.TypeOf(HTTPSProxyConf{}),
  33. consts.STCPProxy: reflect.TypeOf(STCPProxyConf{}),
  34. consts.XTCPProxy: reflect.TypeOf(XTCPProxyConf{}),
  35. consts.SUDPProxy: reflect.TypeOf(SUDPProxyConf{}),
  36. }
  37. )
  38. func NewConfByType(proxyType string) ProxyConf {
  39. v, ok := proxyConfTypeMap[proxyType]
  40. if !ok {
  41. return nil
  42. }
  43. cfg := reflect.New(v).Interface().(ProxyConf)
  44. return cfg
  45. }
  46. type ProxyConf interface {
  47. // GetBaseConfig returns the BaseProxyConf for this config.
  48. GetBaseConfig() *BaseProxyConf
  49. // SetDefaultValues sets the default values for this config.
  50. SetDefaultValues()
  51. // UnmarshalFromMsg unmarshals a msg.NewProxy message into this config.
  52. // This function will be called on the frps side.
  53. UnmarshalFromMsg(*msg.NewProxy)
  54. // UnmarshalFromIni unmarshals a ini.Section into this config. This function
  55. // will be called on the frpc side.
  56. UnmarshalFromIni(string, string, *ini.Section) error
  57. // MarshalToMsg marshals this config into a msg.NewProxy message. This
  58. // function will be called on the frpc side.
  59. MarshalToMsg(*msg.NewProxy)
  60. // ValidateForClient checks that the config is valid for the frpc side.
  61. ValidateForClient() error
  62. // ValidateForServer checks that the config is valid for the frps side.
  63. ValidateForServer(ServerCommonConf) error
  64. }
  65. // LocalSvrConf configures what location the client will to, or what
  66. // plugin will be used.
  67. type LocalSvrConf struct {
  68. // LocalIP specifies the IP address or host name to to.
  69. LocalIP string `ini:"local_ip" json:"local_ip"`
  70. // LocalPort specifies the port to to.
  71. LocalPort int `ini:"local_port" json:"local_port"`
  72. // Plugin specifies what plugin should be used for ng. If this value
  73. // is set, the LocalIp and LocalPort values will be ignored. By default,
  74. // this value is "".
  75. Plugin string `ini:"plugin" json:"plugin"`
  76. // PluginParams specify parameters to be passed to the plugin, if one is
  77. // being used. By default, this value is an empty map.
  78. PluginParams map[string]string `ini:"-"`
  79. }
  80. // HealthCheckConf configures health checking. This can be useful for load
  81. // balancing purposes to detect and remove proxies to failing services.
  82. type HealthCheckConf struct {
  83. // HealthCheckType specifies what protocol to use for health checking.
  84. // Valid values include "tcp", "http", and "". If this value is "", health
  85. // checking will not be performed. By default, this value is "".
  86. //
  87. // If the type is "tcp", a connection will be attempted to the target
  88. // server. If a connection cannot be established, the health check fails.
  89. //
  90. // If the type is "http", a GET request will be made to the endpoint
  91. // specified by HealthCheckURL. If the response is not a 200, the health
  92. // check fails.
  93. HealthCheckType string `ini:"health_check_type" json:"health_check_type"` // tcp | http
  94. // HealthCheckTimeoutS specifies the number of seconds to wait for a health
  95. // check attempt to connect. If the timeout is reached, this counts as a
  96. // health check failure. By default, this value is 3.
  97. HealthCheckTimeoutS int `ini:"health_check_timeout_s" json:"health_check_timeout_s"`
  98. // HealthCheckMaxFailed specifies the number of allowed failures before the
  99. // is stopped. By default, this value is 1.
  100. HealthCheckMaxFailed int `ini:"health_check_max_failed" json:"health_check_max_failed"`
  101. // HealthCheckIntervalS specifies the time in seconds between health
  102. // checks. By default, this value is 10.
  103. HealthCheckIntervalS int `ini:"health_check_interval_s" json:"health_check_interval_s"`
  104. // HealthCheckURL specifies the address to send health checks to if the
  105. // health check type is "http".
  106. HealthCheckURL string `ini:"health_check_url" json:"health_check_url"`
  107. // HealthCheckAddr specifies the address to connect to if the health check
  108. // type is "tcp".
  109. HealthCheckAddr string `ini:"-"`
  110. }
  111. // BaseProxyConf provides configuration info that is common to all types.
  112. type BaseProxyConf struct {
  113. // ProxyName is the name of this
  114. ProxyName string `ini:"name" json:"name"`
  115. // ProxyType specifies the type of this Valid values include "tcp",
  116. // "udp", "http", "https", "stcp", and "xtcp". By default, this value is
  117. // "tcp".
  118. ProxyType string `ini:"type" json:"type"`
  119. // UseEncryption controls whether or not communication with the server will
  120. // be encrypted. Encryption is done using the tokens supplied in the server
  121. // and client configuration. By default, this value is false.
  122. UseEncryption bool `ini:"use_encryption" json:"use_encryption"`
  123. // UseCompression controls whether or not communication with the server
  124. // will be compressed. By default, this value is false.
  125. UseCompression bool `ini:"use_compression" json:"use_compression"`
  126. // Group specifies which group the is a part of. The server will use
  127. // this information to load balance proxies in the same group. If the value
  128. // is "", this will not be in a group. By default, this value is "".
  129. Group string `ini:"group" json:"group"`
  130. // GroupKey specifies a group key, which should be the same among proxies
  131. // of the same group. By default, this value is "".
  132. GroupKey string `ini:"group_key" json:"group_key"`
  133. // ProxyProtocolVersion specifies which protocol version to use. Valid
  134. // values include "v1", "v2", and "". If the value is "", a protocol
  135. // version will be automatically selected. By default, this value is "".
  136. ProxyProtocolVersion string `ini:"proxy_protocol_version" json:"proxy_protocol_version"`
  137. // BandwidthLimit limit the bandwidth
  138. // 0 means no limit
  139. BandwidthLimit BandwidthQuantity `ini:"bandwidth_limit" json:"bandwidth_limit"`
  140. // BandwidthLimitMode specifies whether to limit the bandwidth on the
  141. // client or server side. Valid values include "client" and "server".
  142. // By default, this value is "client".
  143. BandwidthLimitMode string `ini:"bandwidth_limit_mode" json:"bandwidth_limit_mode"`
  144. // meta info for each proxy
  145. Metas map[string]string `ini:"-" json:"metas"`
  146. LocalSvrConf `ini:",extends"`
  147. HealthCheckConf `ini:",extends"`
  148. }
  149. type DomainConf struct {
  150. CustomDomains []string `ini:"custom_domains" json:"custom_domains"`
  151. SubDomain string `ini:"subdomain" json:"subdomain"`
  152. }
  153. type RoleServerCommonConf struct {
  154. Role string `ini:"role" json:"role"`
  155. Sk string `ini:"sk" json:"sk"`
  156. AllowUsers []string `ini:"allow_users" json:"allow_users"`
  157. }
  158. func (cfg *RoleServerCommonConf) setDefaultValues() {
  159. cfg.Role = "server"
  160. }
  161. func (cfg *RoleServerCommonConf) marshalToMsg(m *msg.NewProxy) {
  162. m.Sk = cfg.Sk
  163. m.AllowUsers = cfg.AllowUsers
  164. }
  165. func (cfg *RoleServerCommonConf) unmarshalFromMsg(m *msg.NewProxy) {
  166. cfg.Sk = m.Sk
  167. cfg.AllowUsers = m.AllowUsers
  168. }
  169. // HTTP
  170. type HTTPProxyConf struct {
  171. BaseProxyConf `ini:",extends"`
  172. DomainConf `ini:",extends"`
  173. Locations []string `ini:"locations" json:"locations"`
  174. HTTPUser string `ini:"http_user" json:"http_user"`
  175. HTTPPwd string `ini:"http_pwd" json:"http_pwd"`
  176. HostHeaderRewrite string `ini:"host_header_rewrite" json:"host_header_rewrite"`
  177. Headers map[string]string `ini:"-" json:"headers"`
  178. RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
  179. }
  180. // HTTPS
  181. type HTTPSProxyConf struct {
  182. BaseProxyConf `ini:",extends"`
  183. DomainConf `ini:",extends"`
  184. }
  185. // TCP
  186. type TCPProxyConf struct {
  187. BaseProxyConf `ini:",extends"`
  188. RemotePort int `ini:"remote_port" json:"remote_port"`
  189. }
  190. // UDP
  191. type UDPProxyConf struct {
  192. BaseProxyConf `ini:",extends"`
  193. RemotePort int `ini:"remote_port" json:"remote_port"`
  194. }
  195. // TCPMux
  196. type TCPMuxProxyConf struct {
  197. BaseProxyConf `ini:",extends"`
  198. DomainConf `ini:",extends"`
  199. HTTPUser string `ini:"http_user" json:"http_user,omitempty"`
  200. HTTPPwd string `ini:"http_pwd" json:"http_pwd,omitempty"`
  201. RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
  202. Multiplexer string `ini:"multiplexer"`
  203. }
  204. // STCP
  205. type STCPProxyConf struct {
  206. BaseProxyConf `ini:",extends"`
  207. RoleServerCommonConf `ini:",extends"`
  208. }
  209. // XTCP
  210. type XTCPProxyConf struct {
  211. BaseProxyConf `ini:",extends"`
  212. RoleServerCommonConf `ini:",extends"`
  213. }
  214. // SUDP
  215. type SUDPProxyConf struct {
  216. BaseProxyConf `ini:",extends"`
  217. RoleServerCommonConf `ini:",extends"`
  218. }
  219. // Proxy Conf Loader
  220. // DefaultProxyConf creates a empty ProxyConf object by proxyType.
  221. // If proxyType doesn't exist, return nil.
  222. func DefaultProxyConf(proxyType string) ProxyConf {
  223. conf := NewConfByType(proxyType)
  224. if conf != nil {
  225. conf.SetDefaultValues()
  226. }
  227. return conf
  228. }
  229. // Proxy loaded from ini
  230. func NewProxyConfFromIni(prefix, name string, section *ini.Section) (ProxyConf, error) {
  231. // section.Key: if key not exists, section will set it with default value.
  232. proxyType := section.Key("type").String()
  233. if proxyType == "" {
  234. proxyType = consts.TCPProxy
  235. }
  236. conf := DefaultProxyConf(proxyType)
  237. if conf == nil {
  238. return nil, fmt.Errorf("invalid type [%s]", proxyType)
  239. }
  240. if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
  241. return nil, err
  242. }
  243. if err := conf.ValidateForClient(); err != nil {
  244. return nil, err
  245. }
  246. return conf, nil
  247. }
  248. // Proxy loaded from msg
  249. func NewProxyConfFromMsg(m *msg.NewProxy, serverCfg ServerCommonConf) (ProxyConf, error) {
  250. if m.ProxyType == "" {
  251. m.ProxyType = consts.TCPProxy
  252. }
  253. conf := DefaultProxyConf(m.ProxyType)
  254. if conf == nil {
  255. return nil, fmt.Errorf("proxy [%s] type [%s] error", m.ProxyName, m.ProxyType)
  256. }
  257. conf.UnmarshalFromMsg(m)
  258. err := conf.ValidateForServer(serverCfg)
  259. if err != nil {
  260. return nil, err
  261. }
  262. return conf, nil
  263. }
  264. // Base
  265. func (cfg *BaseProxyConf) GetBaseConfig() *BaseProxyConf {
  266. return cfg
  267. }
  268. func (cfg *BaseProxyConf) SetDefaultValues() {
  269. cfg.LocalSvrConf = LocalSvrConf{
  270. LocalIP: "127.0.0.1",
  271. }
  272. cfg.BandwidthLimitMode = BandwidthLimitModeClient
  273. }
  274. // BaseProxyConf apply custom logic changes.
  275. func (cfg *BaseProxyConf) decorate(prefix string, name string, section *ini.Section) error {
  276. // proxy_name
  277. cfg.ProxyName = prefix + name
  278. // metas_xxx
  279. cfg.Metas = GetMapWithoutPrefix(section.KeysHash(), "meta_")
  280. // bandwidth_limit
  281. if bandwidth, err := section.GetKey("bandwidth_limit"); err == nil {
  282. cfg.BandwidthLimit, err = NewBandwidthQuantity(bandwidth.String())
  283. if err != nil {
  284. return err
  285. }
  286. }
  287. // plugin_xxx
  288. cfg.LocalSvrConf.PluginParams = GetMapByPrefix(section.KeysHash(), "plugin_")
  289. // custom logic code
  290. if cfg.HealthCheckType == "tcp" && cfg.Plugin == "" {
  291. cfg.HealthCheckAddr = cfg.LocalIP + fmt.Sprintf(":%d", cfg.LocalPort)
  292. }
  293. if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckURL != "" {
  294. s := "http://" + net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort))
  295. if !strings.HasPrefix(cfg.HealthCheckURL, "/") {
  296. s += "/"
  297. }
  298. cfg.HealthCheckURL = s + cfg.HealthCheckURL
  299. }
  300. return nil
  301. }
  302. func (cfg *BaseProxyConf) marshalToMsg(m *msg.NewProxy) {
  303. m.ProxyName = cfg.ProxyName
  304. m.ProxyType = cfg.ProxyType
  305. m.UseEncryption = cfg.UseEncryption
  306. m.UseCompression = cfg.UseCompression
  307. m.BandwidthLimit = cfg.BandwidthLimit.String()
  308. // leave it empty for default value to reduce traffic
  309. if cfg.BandwidthLimitMode != "client" {
  310. m.BandwidthLimitMode = cfg.BandwidthLimitMode
  311. }
  312. m.Group = cfg.Group
  313. m.GroupKey = cfg.GroupKey
  314. m.Metas = cfg.Metas
  315. }
  316. func (cfg *BaseProxyConf) unmarshalFromMsg(m *msg.NewProxy) {
  317. cfg.ProxyName = m.ProxyName
  318. cfg.ProxyType = m.ProxyType
  319. cfg.UseEncryption = m.UseEncryption
  320. cfg.UseCompression = m.UseCompression
  321. if m.BandwidthLimit != "" {
  322. cfg.BandwidthLimit, _ = NewBandwidthQuantity(m.BandwidthLimit)
  323. }
  324. if m.BandwidthLimitMode != "" {
  325. cfg.BandwidthLimitMode = m.BandwidthLimitMode
  326. }
  327. cfg.Group = m.Group
  328. cfg.GroupKey = m.GroupKey
  329. cfg.Metas = m.Metas
  330. }
  331. func (cfg *BaseProxyConf) validateForClient() (err error) {
  332. if cfg.ProxyProtocolVersion != "" {
  333. if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" {
  334. return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion)
  335. }
  336. }
  337. if cfg.BandwidthLimitMode != "client" && cfg.BandwidthLimitMode != "server" {
  338. return fmt.Errorf("bandwidth_limit_mode should be client or server")
  339. }
  340. if err = cfg.LocalSvrConf.validateForClient(); err != nil {
  341. return
  342. }
  343. if err = cfg.HealthCheckConf.validateForClient(); err != nil {
  344. return
  345. }
  346. return nil
  347. }
  348. func (cfg *BaseProxyConf) validateForServer() (err error) {
  349. if cfg.BandwidthLimitMode != "client" && cfg.BandwidthLimitMode != "server" {
  350. return fmt.Errorf("bandwidth_limit_mode should be client or server")
  351. }
  352. return nil
  353. }
  354. // DomainConf
  355. func (cfg *DomainConf) check() (err error) {
  356. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  357. err = fmt.Errorf("custom_domains and subdomain should set at least one of them")
  358. return
  359. }
  360. return
  361. }
  362. func (cfg *DomainConf) validateForClient() (err error) {
  363. if err = cfg.check(); err != nil {
  364. return
  365. }
  366. return
  367. }
  368. func (cfg *DomainConf) validateForServer(serverCfg ServerCommonConf) (err error) {
  369. if err = cfg.check(); err != nil {
  370. return
  371. }
  372. for _, domain := range cfg.CustomDomains {
  373. if serverCfg.SubDomainHost != "" && len(strings.Split(serverCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
  374. if strings.Contains(domain, serverCfg.SubDomainHost) {
  375. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, serverCfg.SubDomainHost)
  376. }
  377. }
  378. }
  379. if cfg.SubDomain != "" {
  380. if serverCfg.SubDomainHost == "" {
  381. return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
  382. }
  383. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  384. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  385. }
  386. }
  387. return nil
  388. }
  389. // LocalSvrConf
  390. func (cfg *LocalSvrConf) validateForClient() (err error) {
  391. if cfg.Plugin == "" {
  392. if cfg.LocalIP == "" {
  393. err = fmt.Errorf("local ip or plugin is required")
  394. return
  395. }
  396. if cfg.LocalPort <= 0 {
  397. err = fmt.Errorf("error local_port")
  398. return
  399. }
  400. }
  401. return
  402. }
  403. // HealthCheckConf
  404. func (cfg *HealthCheckConf) validateForClient() error {
  405. if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
  406. return fmt.Errorf("unsupport health check type")
  407. }
  408. if cfg.HealthCheckType != "" {
  409. if cfg.HealthCheckType == "http" && cfg.HealthCheckURL == "" {
  410. return fmt.Errorf("health_check_url is required for health check type 'http'")
  411. }
  412. }
  413. return nil
  414. }
  415. func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini.Section) error {
  416. err := section.MapTo(cfg)
  417. if err != nil {
  418. return err
  419. }
  420. err = cfg.GetBaseConfig().decorate(prefix, name, section)
  421. if err != nil {
  422. return err
  423. }
  424. return nil
  425. }
  426. // TCP
  427. func (cfg *TCPProxyConf) UnmarshalFromMsg(m *msg.NewProxy) {
  428. cfg.BaseProxyConf.unmarshalFromMsg(m)
  429. // Add custom logic unmarshal if exists
  430. cfg.RemotePort = m.RemotePort
  431. }
  432. func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  433. err := preUnmarshalFromIni(cfg, prefix, name, section)
  434. if err != nil {
  435. return err
  436. }
  437. // Add custom logic unmarshal if exists
  438. return nil
  439. }
  440. func (cfg *TCPProxyConf) MarshalToMsg(m *msg.NewProxy) {
  441. cfg.BaseProxyConf.marshalToMsg(m)
  442. // Add custom logic marshal if exists
  443. m.RemotePort = cfg.RemotePort
  444. }
  445. func (cfg *TCPProxyConf) ValidateForClient() (err error) {
  446. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  447. return
  448. }
  449. // Add custom logic check if exists
  450. return
  451. }
  452. func (cfg *TCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  453. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  454. return err
  455. }
  456. return nil
  457. }
  458. // TCPMux
  459. func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  460. err := preUnmarshalFromIni(cfg, prefix, name, section)
  461. if err != nil {
  462. return err
  463. }
  464. // Add custom logic unmarshal if exists
  465. return nil
  466. }
  467. func (cfg *TCPMuxProxyConf) UnmarshalFromMsg(m *msg.NewProxy) {
  468. cfg.BaseProxyConf.unmarshalFromMsg(m)
  469. // Add custom logic unmarshal if exists
  470. cfg.CustomDomains = m.CustomDomains
  471. cfg.SubDomain = m.SubDomain
  472. cfg.Multiplexer = m.Multiplexer
  473. cfg.HTTPUser = m.HTTPUser
  474. cfg.HTTPPwd = m.HTTPPwd
  475. cfg.RouteByHTTPUser = m.RouteByHTTPUser
  476. }
  477. func (cfg *TCPMuxProxyConf) MarshalToMsg(m *msg.NewProxy) {
  478. cfg.BaseProxyConf.marshalToMsg(m)
  479. // Add custom logic marshal if exists
  480. m.CustomDomains = cfg.CustomDomains
  481. m.SubDomain = cfg.SubDomain
  482. m.Multiplexer = cfg.Multiplexer
  483. m.HTTPUser = cfg.HTTPUser
  484. m.HTTPPwd = cfg.HTTPPwd
  485. m.RouteByHTTPUser = cfg.RouteByHTTPUser
  486. }
  487. func (cfg *TCPMuxProxyConf) ValidateForClient() (err error) {
  488. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  489. return
  490. }
  491. // Add custom logic check if exists
  492. if err = cfg.DomainConf.validateForClient(); err != nil {
  493. return
  494. }
  495. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  496. return fmt.Errorf("parse conf error: incorrect multiplexer [%s]", cfg.Multiplexer)
  497. }
  498. return
  499. }
  500. func (cfg *TCPMuxProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
  501. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  502. return err
  503. }
  504. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  505. return fmt.Errorf("proxy [%s] incorrect multiplexer [%s]", cfg.ProxyName, cfg.Multiplexer)
  506. }
  507. if cfg.Multiplexer == consts.HTTPConnectTCPMultiplexer && serverCfg.TCPMuxHTTPConnectPort == 0 {
  508. return fmt.Errorf("proxy [%s] type [tcpmux] with multiplexer [httpconnect] requires tcpmux_httpconnect_port configuration", cfg.ProxyName)
  509. }
  510. if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
  511. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  512. return
  513. }
  514. return
  515. }
  516. // UDP
  517. func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  518. err := preUnmarshalFromIni(cfg, prefix, name, section)
  519. if err != nil {
  520. return err
  521. }
  522. // Add custom logic unmarshal if exists
  523. return nil
  524. }
  525. func (cfg *UDPProxyConf) UnmarshalFromMsg(m *msg.NewProxy) {
  526. cfg.BaseProxyConf.unmarshalFromMsg(m)
  527. // Add custom logic unmarshal if exists
  528. cfg.RemotePort = m.RemotePort
  529. }
  530. func (cfg *UDPProxyConf) MarshalToMsg(m *msg.NewProxy) {
  531. cfg.BaseProxyConf.marshalToMsg(m)
  532. // Add custom logic marshal if exists
  533. m.RemotePort = cfg.RemotePort
  534. }
  535. func (cfg *UDPProxyConf) ValidateForClient() (err error) {
  536. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  537. return
  538. }
  539. // Add custom logic check if exists
  540. return
  541. }
  542. func (cfg *UDPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  543. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  544. return err
  545. }
  546. return nil
  547. }
  548. // HTTP
  549. func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  550. err := preUnmarshalFromIni(cfg, prefix, name, section)
  551. if err != nil {
  552. return err
  553. }
  554. // Add custom logic unmarshal if exists
  555. cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_")
  556. return nil
  557. }
  558. func (cfg *HTTPProxyConf) UnmarshalFromMsg(m *msg.NewProxy) {
  559. cfg.BaseProxyConf.unmarshalFromMsg(m)
  560. // Add custom logic unmarshal if exists
  561. cfg.CustomDomains = m.CustomDomains
  562. cfg.SubDomain = m.SubDomain
  563. cfg.Locations = m.Locations
  564. cfg.HostHeaderRewrite = m.HostHeaderRewrite
  565. cfg.HTTPUser = m.HTTPUser
  566. cfg.HTTPPwd = m.HTTPPwd
  567. cfg.Headers = m.Headers
  568. cfg.RouteByHTTPUser = m.RouteByHTTPUser
  569. }
  570. func (cfg *HTTPProxyConf) MarshalToMsg(m *msg.NewProxy) {
  571. cfg.BaseProxyConf.marshalToMsg(m)
  572. // Add custom logic marshal if exists
  573. m.CustomDomains = cfg.CustomDomains
  574. m.SubDomain = cfg.SubDomain
  575. m.Locations = cfg.Locations
  576. m.HostHeaderRewrite = cfg.HostHeaderRewrite
  577. m.HTTPUser = cfg.HTTPUser
  578. m.HTTPPwd = cfg.HTTPPwd
  579. m.Headers = cfg.Headers
  580. m.RouteByHTTPUser = cfg.RouteByHTTPUser
  581. }
  582. func (cfg *HTTPProxyConf) ValidateForClient() (err error) {
  583. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  584. return
  585. }
  586. // Add custom logic check if exists
  587. if err = cfg.DomainConf.validateForClient(); err != nil {
  588. return
  589. }
  590. return
  591. }
  592. func (cfg *HTTPProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
  593. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  594. return err
  595. }
  596. if serverCfg.VhostHTTPPort == 0 {
  597. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  598. }
  599. if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
  600. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  601. return
  602. }
  603. return
  604. }
  605. // HTTPS
  606. func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  607. err := preUnmarshalFromIni(cfg, prefix, name, section)
  608. if err != nil {
  609. return err
  610. }
  611. // Add custom logic unmarshal if exists
  612. return nil
  613. }
  614. func (cfg *HTTPSProxyConf) UnmarshalFromMsg(m *msg.NewProxy) {
  615. cfg.BaseProxyConf.unmarshalFromMsg(m)
  616. // Add custom logic unmarshal if exists
  617. cfg.CustomDomains = m.CustomDomains
  618. cfg.SubDomain = m.SubDomain
  619. }
  620. func (cfg *HTTPSProxyConf) MarshalToMsg(m *msg.NewProxy) {
  621. cfg.BaseProxyConf.marshalToMsg(m)
  622. // Add custom logic marshal if exists
  623. m.CustomDomains = cfg.CustomDomains
  624. m.SubDomain = cfg.SubDomain
  625. }
  626. func (cfg *HTTPSProxyConf) ValidateForClient() (err error) {
  627. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  628. return
  629. }
  630. // Add custom logic check if exists
  631. if err = cfg.DomainConf.validateForClient(); err != nil {
  632. return
  633. }
  634. return
  635. }
  636. func (cfg *HTTPSProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
  637. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  638. return err
  639. }
  640. if serverCfg.VhostHTTPSPort == 0 {
  641. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  642. }
  643. if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
  644. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  645. return
  646. }
  647. return
  648. }
  649. // SUDP
  650. func (cfg *SUDPProxyConf) SetDefaultValues() {
  651. cfg.BaseProxyConf.SetDefaultValues()
  652. cfg.RoleServerCommonConf.setDefaultValues()
  653. }
  654. func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  655. err := preUnmarshalFromIni(cfg, prefix, name, section)
  656. if err != nil {
  657. return err
  658. }
  659. // Add custom logic unmarshal if exists
  660. return nil
  661. }
  662. // Only for role server.
  663. func (cfg *SUDPProxyConf) UnmarshalFromMsg(m *msg.NewProxy) {
  664. cfg.BaseProxyConf.unmarshalFromMsg(m)
  665. // Add custom logic unmarshal if exists
  666. cfg.RoleServerCommonConf.unmarshalFromMsg(m)
  667. }
  668. func (cfg *SUDPProxyConf) MarshalToMsg(m *msg.NewProxy) {
  669. cfg.BaseProxyConf.marshalToMsg(m)
  670. // Add custom logic marshal if exists
  671. cfg.RoleServerCommonConf.marshalToMsg(m)
  672. }
  673. func (cfg *SUDPProxyConf) ValidateForClient() (err error) {
  674. if err := cfg.BaseProxyConf.validateForClient(); err != nil {
  675. return err
  676. }
  677. // Add custom logic check if exists
  678. if cfg.Role != "server" {
  679. return fmt.Errorf("role should be 'server'")
  680. }
  681. return nil
  682. }
  683. func (cfg *SUDPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  684. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  685. return err
  686. }
  687. return nil
  688. }
  689. // STCP
  690. func (cfg *STCPProxyConf) SetDefaultValues() {
  691. cfg.BaseProxyConf.SetDefaultValues()
  692. cfg.RoleServerCommonConf.setDefaultValues()
  693. }
  694. func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  695. err := preUnmarshalFromIni(cfg, prefix, name, section)
  696. if err != nil {
  697. return err
  698. }
  699. // Add custom logic unmarshal if exists
  700. if cfg.Role == "" {
  701. cfg.Role = "server"
  702. }
  703. return nil
  704. }
  705. // Only for role server.
  706. func (cfg *STCPProxyConf) UnmarshalFromMsg(m *msg.NewProxy) {
  707. cfg.BaseProxyConf.unmarshalFromMsg(m)
  708. // Add custom logic unmarshal if exists
  709. cfg.RoleServerCommonConf.unmarshalFromMsg(m)
  710. }
  711. func (cfg *STCPProxyConf) MarshalToMsg(m *msg.NewProxy) {
  712. cfg.BaseProxyConf.marshalToMsg(m)
  713. // Add custom logic marshal if exists
  714. cfg.RoleServerCommonConf.marshalToMsg(m)
  715. }
  716. func (cfg *STCPProxyConf) ValidateForClient() (err error) {
  717. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  718. return
  719. }
  720. // Add custom logic check if exists
  721. if cfg.Role != "server" {
  722. return fmt.Errorf("role should be 'server'")
  723. }
  724. return
  725. }
  726. func (cfg *STCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  727. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  728. return err
  729. }
  730. return nil
  731. }
  732. // XTCP
  733. func (cfg *XTCPProxyConf) SetDefaultValues() {
  734. cfg.BaseProxyConf.SetDefaultValues()
  735. cfg.RoleServerCommonConf.setDefaultValues()
  736. }
  737. func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  738. err := preUnmarshalFromIni(cfg, prefix, name, section)
  739. if err != nil {
  740. return err
  741. }
  742. // Add custom logic unmarshal if exists
  743. if cfg.Role == "" {
  744. cfg.Role = "server"
  745. }
  746. return nil
  747. }
  748. // Only for role server.
  749. func (cfg *XTCPProxyConf) UnmarshalFromMsg(m *msg.NewProxy) {
  750. cfg.BaseProxyConf.unmarshalFromMsg(m)
  751. // Add custom logic unmarshal if exists
  752. cfg.RoleServerCommonConf.unmarshalFromMsg(m)
  753. }
  754. func (cfg *XTCPProxyConf) MarshalToMsg(m *msg.NewProxy) {
  755. cfg.BaseProxyConf.marshalToMsg(m)
  756. // Add custom logic marshal if exists
  757. cfg.RoleServerCommonConf.marshalToMsg(m)
  758. }
  759. func (cfg *XTCPProxyConf) ValidateForClient() (err error) {
  760. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  761. return
  762. }
  763. // Add custom logic check if exists
  764. if cfg.Role != "server" {
  765. return fmt.Errorf("role should be 'server'")
  766. }
  767. return
  768. }
  769. func (cfg *XTCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  770. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  771. return err
  772. }
  773. return nil
  774. }