proxy.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. // HTTP
  162. type HTTPProxyConf struct {
  163. BaseProxyConf `ini:",extends"`
  164. DomainConf `ini:",extends"`
  165. Locations []string `ini:"locations" json:"locations"`
  166. HTTPUser string `ini:"http_user" json:"http_user"`
  167. HTTPPwd string `ini:"http_pwd" json:"http_pwd"`
  168. HostHeaderRewrite string `ini:"host_header_rewrite" json:"host_header_rewrite"`
  169. Headers map[string]string `ini:"-" json:"headers"`
  170. RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
  171. }
  172. // HTTPS
  173. type HTTPSProxyConf struct {
  174. BaseProxyConf `ini:",extends"`
  175. DomainConf `ini:",extends"`
  176. }
  177. // TCP
  178. type TCPProxyConf struct {
  179. BaseProxyConf `ini:",extends"`
  180. RemotePort int `ini:"remote_port" json:"remote_port"`
  181. }
  182. // UDP
  183. type UDPProxyConf struct {
  184. BaseProxyConf `ini:",extends"`
  185. RemotePort int `ini:"remote_port" json:"remote_port"`
  186. }
  187. // TCPMux
  188. type TCPMuxProxyConf struct {
  189. BaseProxyConf `ini:",extends"`
  190. DomainConf `ini:",extends"`
  191. HTTPUser string `ini:"http_user" json:"http_user,omitempty"`
  192. HTTPPwd string `ini:"http_pwd" json:"http_pwd,omitempty"`
  193. RouteByHTTPUser string `ini:"route_by_http_user" json:"route_by_http_user"`
  194. Multiplexer string `ini:"multiplexer"`
  195. }
  196. // STCP
  197. type STCPProxyConf struct {
  198. BaseProxyConf `ini:",extends"`
  199. RoleServerCommonConf `ini:",extends"`
  200. }
  201. // XTCP
  202. type XTCPProxyConf struct {
  203. BaseProxyConf `ini:",extends"`
  204. RoleServerCommonConf `ini:",extends"`
  205. }
  206. // SUDP
  207. type SUDPProxyConf struct {
  208. BaseProxyConf `ini:",extends"`
  209. RoleServerCommonConf `ini:",extends"`
  210. }
  211. // Proxy Conf Loader
  212. // DefaultProxyConf creates a empty ProxyConf object by proxyType.
  213. // If proxyType doesn't exist, return nil.
  214. func DefaultProxyConf(proxyType string) ProxyConf {
  215. conf := NewConfByType(proxyType)
  216. if conf != nil {
  217. conf.SetDefaultValues()
  218. }
  219. return conf
  220. }
  221. // Proxy loaded from ini
  222. func NewProxyConfFromIni(prefix, name string, section *ini.Section) (ProxyConf, error) {
  223. // section.Key: if key not exists, section will set it with default value.
  224. proxyType := section.Key("type").String()
  225. if proxyType == "" {
  226. proxyType = consts.TCPProxy
  227. }
  228. conf := DefaultProxyConf(proxyType)
  229. if conf == nil {
  230. return nil, fmt.Errorf("proxy %s has invalid type [%s]", name, proxyType)
  231. }
  232. if err := conf.UnmarshalFromIni(prefix, name, section); err != nil {
  233. return nil, err
  234. }
  235. if err := conf.ValidateForClient(); err != nil {
  236. return nil, err
  237. }
  238. return conf, nil
  239. }
  240. // Proxy loaded from msg
  241. func NewProxyConfFromMsg(pMsg *msg.NewProxy, serverCfg ServerCommonConf) (ProxyConf, error) {
  242. if pMsg.ProxyType == "" {
  243. pMsg.ProxyType = consts.TCPProxy
  244. }
  245. conf := DefaultProxyConf(pMsg.ProxyType)
  246. if conf == nil {
  247. return nil, fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
  248. }
  249. conf.UnmarshalFromMsg(pMsg)
  250. err := conf.ValidateForServer(serverCfg)
  251. if err != nil {
  252. return nil, err
  253. }
  254. return conf, nil
  255. }
  256. // Base
  257. func (cfg *BaseProxyConf) GetBaseConfig() *BaseProxyConf {
  258. return cfg
  259. }
  260. func (cfg *BaseProxyConf) SetDefaultValues() {
  261. cfg.LocalSvrConf = LocalSvrConf{
  262. LocalIP: "127.0.0.1",
  263. }
  264. cfg.BandwidthLimitMode = BandwidthLimitModeClient
  265. }
  266. // BaseProxyConf apply custom logic changes.
  267. func (cfg *BaseProxyConf) decorate(prefix string, name string, section *ini.Section) error {
  268. // proxy_name
  269. cfg.ProxyName = prefix + name
  270. // metas_xxx
  271. cfg.Metas = GetMapWithoutPrefix(section.KeysHash(), "meta_")
  272. // bandwidth_limit
  273. if bandwidth, err := section.GetKey("bandwidth_limit"); err == nil {
  274. cfg.BandwidthLimit, err = NewBandwidthQuantity(bandwidth.String())
  275. if err != nil {
  276. return err
  277. }
  278. }
  279. // plugin_xxx
  280. cfg.LocalSvrConf.PluginParams = GetMapByPrefix(section.KeysHash(), "plugin_")
  281. // custom logic code
  282. if cfg.HealthCheckType == "tcp" && cfg.Plugin == "" {
  283. cfg.HealthCheckAddr = cfg.LocalIP + fmt.Sprintf(":%d", cfg.LocalPort)
  284. }
  285. if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckURL != "" {
  286. s := "http://" + net.JoinHostPort(cfg.LocalIP, strconv.Itoa(cfg.LocalPort))
  287. if !strings.HasPrefix(cfg.HealthCheckURL, "/") {
  288. s += "/"
  289. }
  290. cfg.HealthCheckURL = s + cfg.HealthCheckURL
  291. }
  292. return nil
  293. }
  294. func (cfg *BaseProxyConf) marshalToMsg(pMsg *msg.NewProxy) {
  295. pMsg.ProxyName = cfg.ProxyName
  296. pMsg.ProxyType = cfg.ProxyType
  297. pMsg.UseEncryption = cfg.UseEncryption
  298. pMsg.UseCompression = cfg.UseCompression
  299. pMsg.BandwidthLimit = cfg.BandwidthLimit.String()
  300. // leave it empty for default value to reduce traffic
  301. if cfg.BandwidthLimitMode != "client" {
  302. pMsg.BandwidthLimitMode = cfg.BandwidthLimitMode
  303. }
  304. pMsg.Group = cfg.Group
  305. pMsg.GroupKey = cfg.GroupKey
  306. pMsg.Metas = cfg.Metas
  307. }
  308. func (cfg *BaseProxyConf) unmarshalFromMsg(pMsg *msg.NewProxy) {
  309. cfg.ProxyName = pMsg.ProxyName
  310. cfg.ProxyType = pMsg.ProxyType
  311. cfg.UseEncryption = pMsg.UseEncryption
  312. cfg.UseCompression = pMsg.UseCompression
  313. if pMsg.BandwidthLimit != "" {
  314. cfg.BandwidthLimit, _ = NewBandwidthQuantity(pMsg.BandwidthLimit)
  315. }
  316. if pMsg.BandwidthLimitMode != "" {
  317. cfg.BandwidthLimitMode = pMsg.BandwidthLimitMode
  318. }
  319. cfg.Group = pMsg.Group
  320. cfg.GroupKey = pMsg.GroupKey
  321. cfg.Metas = pMsg.Metas
  322. }
  323. func (cfg *BaseProxyConf) validateForClient() (err error) {
  324. if cfg.ProxyProtocolVersion != "" {
  325. if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" {
  326. return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion)
  327. }
  328. }
  329. if cfg.BandwidthLimitMode != "client" && cfg.BandwidthLimitMode != "server" {
  330. return fmt.Errorf("bandwidth_limit_mode should be client or server")
  331. }
  332. if err = cfg.LocalSvrConf.validateForClient(); err != nil {
  333. return
  334. }
  335. if err = cfg.HealthCheckConf.validateForClient(); err != nil {
  336. return
  337. }
  338. return nil
  339. }
  340. func (cfg *BaseProxyConf) validateForServer() (err error) {
  341. if cfg.BandwidthLimitMode != "client" && cfg.BandwidthLimitMode != "server" {
  342. return fmt.Errorf("bandwidth_limit_mode should be client or server")
  343. }
  344. return nil
  345. }
  346. // DomainConf
  347. func (cfg *DomainConf) check() (err error) {
  348. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  349. err = fmt.Errorf("custom_domains and subdomain should set at least one of them")
  350. return
  351. }
  352. return
  353. }
  354. func (cfg *DomainConf) validateForClient() (err error) {
  355. if err = cfg.check(); err != nil {
  356. return
  357. }
  358. return
  359. }
  360. func (cfg *DomainConf) validateForServer(serverCfg ServerCommonConf) (err error) {
  361. if err = cfg.check(); err != nil {
  362. return
  363. }
  364. for _, domain := range cfg.CustomDomains {
  365. if serverCfg.SubDomainHost != "" && len(strings.Split(serverCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
  366. if strings.Contains(domain, serverCfg.SubDomainHost) {
  367. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, serverCfg.SubDomainHost)
  368. }
  369. }
  370. }
  371. if cfg.SubDomain != "" {
  372. if serverCfg.SubDomainHost == "" {
  373. return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
  374. }
  375. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  376. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  377. }
  378. }
  379. return nil
  380. }
  381. // LocalSvrConf
  382. func (cfg *LocalSvrConf) validateForClient() (err error) {
  383. if cfg.Plugin == "" {
  384. if cfg.LocalIP == "" {
  385. err = fmt.Errorf("local ip or plugin is required")
  386. return
  387. }
  388. if cfg.LocalPort <= 0 {
  389. err = fmt.Errorf("error local_port")
  390. return
  391. }
  392. }
  393. return
  394. }
  395. // HealthCheckConf
  396. func (cfg *HealthCheckConf) validateForClient() error {
  397. if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
  398. return fmt.Errorf("unsupport health check type")
  399. }
  400. if cfg.HealthCheckType != "" {
  401. if cfg.HealthCheckType == "http" && cfg.HealthCheckURL == "" {
  402. return fmt.Errorf("health_check_url is required for health check type 'http'")
  403. }
  404. }
  405. return nil
  406. }
  407. func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini.Section) error {
  408. err := section.MapTo(cfg)
  409. if err != nil {
  410. return err
  411. }
  412. err = cfg.GetBaseConfig().decorate(prefix, name, section)
  413. if err != nil {
  414. return err
  415. }
  416. return nil
  417. }
  418. // TCP
  419. func (cfg *TCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  420. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  421. // Add custom logic unmarshal if exists
  422. cfg.RemotePort = pMsg.RemotePort
  423. }
  424. func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  425. err := preUnmarshalFromIni(cfg, prefix, name, section)
  426. if err != nil {
  427. return err
  428. }
  429. // Add custom logic unmarshal if exists
  430. return nil
  431. }
  432. func (cfg *TCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  433. cfg.BaseProxyConf.marshalToMsg(pMsg)
  434. // Add custom logic marshal if exists
  435. pMsg.RemotePort = cfg.RemotePort
  436. }
  437. func (cfg *TCPProxyConf) ValidateForClient() (err error) {
  438. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  439. return
  440. }
  441. // Add custom logic check if exists
  442. return
  443. }
  444. func (cfg *TCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  445. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  446. return err
  447. }
  448. return nil
  449. }
  450. // TCPMux
  451. func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  452. err := preUnmarshalFromIni(cfg, prefix, name, section)
  453. if err != nil {
  454. return err
  455. }
  456. // Add custom logic unmarshal if exists
  457. return nil
  458. }
  459. func (cfg *TCPMuxProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  460. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  461. // Add custom logic unmarshal if exists
  462. cfg.CustomDomains = pMsg.CustomDomains
  463. cfg.SubDomain = pMsg.SubDomain
  464. cfg.Multiplexer = pMsg.Multiplexer
  465. cfg.HTTPUser = pMsg.HTTPUser
  466. cfg.HTTPPwd = pMsg.HTTPPwd
  467. cfg.RouteByHTTPUser = pMsg.RouteByHTTPUser
  468. }
  469. func (cfg *TCPMuxProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  470. cfg.BaseProxyConf.marshalToMsg(pMsg)
  471. // Add custom logic marshal if exists
  472. pMsg.CustomDomains = cfg.CustomDomains
  473. pMsg.SubDomain = cfg.SubDomain
  474. pMsg.Multiplexer = cfg.Multiplexer
  475. pMsg.HTTPUser = cfg.HTTPUser
  476. pMsg.HTTPPwd = cfg.HTTPPwd
  477. pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser
  478. }
  479. func (cfg *TCPMuxProxyConf) ValidateForClient() (err error) {
  480. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  481. return
  482. }
  483. // Add custom logic check if exists
  484. if err = cfg.DomainConf.validateForClient(); err != nil {
  485. return
  486. }
  487. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  488. return fmt.Errorf("parse conf error: incorrect multiplexer [%s]", cfg.Multiplexer)
  489. }
  490. return
  491. }
  492. func (cfg *TCPMuxProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
  493. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  494. return err
  495. }
  496. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  497. return fmt.Errorf("proxy [%s] incorrect multiplexer [%s]", cfg.ProxyName, cfg.Multiplexer)
  498. }
  499. if cfg.Multiplexer == consts.HTTPConnectTCPMultiplexer && serverCfg.TCPMuxHTTPConnectPort == 0 {
  500. return fmt.Errorf("proxy [%s] type [tcpmux] with multiplexer [httpconnect] requires tcpmux_httpconnect_port configuration", cfg.ProxyName)
  501. }
  502. if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
  503. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  504. return
  505. }
  506. return
  507. }
  508. // UDP
  509. func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  510. err := preUnmarshalFromIni(cfg, prefix, name, section)
  511. if err != nil {
  512. return err
  513. }
  514. // Add custom logic unmarshal if exists
  515. return nil
  516. }
  517. func (cfg *UDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  518. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  519. // Add custom logic unmarshal if exists
  520. cfg.RemotePort = pMsg.RemotePort
  521. }
  522. func (cfg *UDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  523. cfg.BaseProxyConf.marshalToMsg(pMsg)
  524. // Add custom logic marshal if exists
  525. pMsg.RemotePort = cfg.RemotePort
  526. }
  527. func (cfg *UDPProxyConf) ValidateForClient() (err error) {
  528. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  529. return
  530. }
  531. // Add custom logic check if exists
  532. return
  533. }
  534. func (cfg *UDPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  535. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  536. return err
  537. }
  538. return nil
  539. }
  540. // HTTP
  541. func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  542. err := preUnmarshalFromIni(cfg, prefix, name, section)
  543. if err != nil {
  544. return err
  545. }
  546. // Add custom logic unmarshal if exists
  547. cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_")
  548. return nil
  549. }
  550. func (cfg *HTTPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  551. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  552. // Add custom logic unmarshal if exists
  553. cfg.CustomDomains = pMsg.CustomDomains
  554. cfg.SubDomain = pMsg.SubDomain
  555. cfg.Locations = pMsg.Locations
  556. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  557. cfg.HTTPUser = pMsg.HTTPUser
  558. cfg.HTTPPwd = pMsg.HTTPPwd
  559. cfg.Headers = pMsg.Headers
  560. cfg.RouteByHTTPUser = pMsg.RouteByHTTPUser
  561. }
  562. func (cfg *HTTPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  563. cfg.BaseProxyConf.marshalToMsg(pMsg)
  564. // Add custom logic marshal if exists
  565. pMsg.CustomDomains = cfg.CustomDomains
  566. pMsg.SubDomain = cfg.SubDomain
  567. pMsg.Locations = cfg.Locations
  568. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  569. pMsg.HTTPUser = cfg.HTTPUser
  570. pMsg.HTTPPwd = cfg.HTTPPwd
  571. pMsg.Headers = cfg.Headers
  572. pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser
  573. }
  574. func (cfg *HTTPProxyConf) ValidateForClient() (err error) {
  575. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  576. return
  577. }
  578. // Add custom logic check if exists
  579. if err = cfg.DomainConf.validateForClient(); err != nil {
  580. return
  581. }
  582. return
  583. }
  584. func (cfg *HTTPProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
  585. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  586. return err
  587. }
  588. if serverCfg.VhostHTTPPort == 0 {
  589. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  590. }
  591. if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
  592. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  593. return
  594. }
  595. return
  596. }
  597. // HTTPS
  598. func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  599. err := preUnmarshalFromIni(cfg, prefix, name, section)
  600. if err != nil {
  601. return err
  602. }
  603. // Add custom logic unmarshal if exists
  604. return nil
  605. }
  606. func (cfg *HTTPSProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  607. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  608. // Add custom logic unmarshal if exists
  609. cfg.CustomDomains = pMsg.CustomDomains
  610. cfg.SubDomain = pMsg.SubDomain
  611. }
  612. func (cfg *HTTPSProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  613. cfg.BaseProxyConf.marshalToMsg(pMsg)
  614. // Add custom logic marshal if exists
  615. pMsg.CustomDomains = cfg.CustomDomains
  616. pMsg.SubDomain = cfg.SubDomain
  617. }
  618. func (cfg *HTTPSProxyConf) ValidateForClient() (err error) {
  619. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  620. return
  621. }
  622. // Add custom logic check if exists
  623. if err = cfg.DomainConf.validateForClient(); err != nil {
  624. return
  625. }
  626. return
  627. }
  628. func (cfg *HTTPSProxyConf) ValidateForServer(serverCfg ServerCommonConf) (err error) {
  629. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  630. return err
  631. }
  632. if serverCfg.VhostHTTPSPort == 0 {
  633. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  634. }
  635. if err = cfg.DomainConf.validateForServer(serverCfg); err != nil {
  636. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  637. return
  638. }
  639. return
  640. }
  641. // SUDP
  642. func (cfg *SUDPProxyConf) SetDefaultValues() {
  643. cfg.BaseProxyConf.SetDefaultValues()
  644. cfg.RoleServerCommonConf.setDefaultValues()
  645. }
  646. func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  647. err := preUnmarshalFromIni(cfg, prefix, name, section)
  648. if err != nil {
  649. return err
  650. }
  651. // Add custom logic unmarshal if exists
  652. return nil
  653. }
  654. // Only for role server.
  655. func (cfg *SUDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  656. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  657. // Add custom logic unmarshal if exists
  658. cfg.Sk = pMsg.Sk
  659. }
  660. func (cfg *SUDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  661. cfg.BaseProxyConf.marshalToMsg(pMsg)
  662. // Add custom logic marshal if exists
  663. pMsg.Sk = cfg.Sk
  664. }
  665. func (cfg *SUDPProxyConf) ValidateForClient() (err error) {
  666. if err := cfg.BaseProxyConf.validateForClient(); err != nil {
  667. return err
  668. }
  669. // Add custom logic check if exists
  670. if cfg.Role != "server" {
  671. return fmt.Errorf("role should be 'server'")
  672. }
  673. return nil
  674. }
  675. func (cfg *SUDPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  676. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  677. return err
  678. }
  679. return nil
  680. }
  681. // STCP
  682. func (cfg *STCPProxyConf) SetDefaultValues() {
  683. cfg.BaseProxyConf.SetDefaultValues()
  684. cfg.RoleServerCommonConf.setDefaultValues()
  685. }
  686. func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  687. err := preUnmarshalFromIni(cfg, prefix, name, section)
  688. if err != nil {
  689. return err
  690. }
  691. // Add custom logic unmarshal if exists
  692. if cfg.Role == "" {
  693. cfg.Role = "server"
  694. }
  695. return nil
  696. }
  697. // Only for role server.
  698. func (cfg *STCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  699. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  700. // Add custom logic unmarshal if exists
  701. cfg.Sk = pMsg.Sk
  702. }
  703. func (cfg *STCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  704. cfg.BaseProxyConf.marshalToMsg(pMsg)
  705. // Add custom logic marshal if exists
  706. pMsg.Sk = cfg.Sk
  707. }
  708. func (cfg *STCPProxyConf) ValidateForClient() (err error) {
  709. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  710. return
  711. }
  712. // Add custom logic check if exists
  713. if cfg.Role != "server" {
  714. return fmt.Errorf("role should be 'server'")
  715. }
  716. return
  717. }
  718. func (cfg *STCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  719. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  720. return err
  721. }
  722. return nil
  723. }
  724. // XTCP
  725. func (cfg *XTCPProxyConf) SetDefaultValues() {
  726. cfg.BaseProxyConf.SetDefaultValues()
  727. cfg.RoleServerCommonConf.setDefaultValues()
  728. }
  729. func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  730. err := preUnmarshalFromIni(cfg, prefix, name, section)
  731. if err != nil {
  732. return err
  733. }
  734. // Add custom logic unmarshal if exists
  735. if cfg.Role == "" {
  736. cfg.Role = "server"
  737. }
  738. return nil
  739. }
  740. // Only for role server.
  741. func (cfg *XTCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  742. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  743. // Add custom logic unmarshal if exists
  744. cfg.Sk = pMsg.Sk
  745. }
  746. func (cfg *XTCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  747. cfg.BaseProxyConf.marshalToMsg(pMsg)
  748. // Add custom logic marshal if exists
  749. pMsg.Sk = cfg.Sk
  750. }
  751. func (cfg *XTCPProxyConf) ValidateForClient() (err error) {
  752. if err = cfg.BaseProxyConf.validateForClient(); err != nil {
  753. return
  754. }
  755. // Add custom logic check if exists
  756. if cfg.Role != "server" {
  757. return fmt.Errorf("role should be 'server'")
  758. }
  759. return
  760. }
  761. func (cfg *XTCPProxyConf) ValidateForServer(serverCfg ServerCommonConf) error {
  762. if err := cfg.BaseProxyConf.validateForServer(); err != nil {
  763. return err
  764. }
  765. return nil
  766. }