proxy.go 27 KB

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