proxy.go 27 KB

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