proxy.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. "github.com/fatedier/frp/pkg/consts"
  22. "github.com/fatedier/frp/pkg/msg"
  23. "gopkg.in/ini.v1"
  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. func (cfg *BaseProxyConf) checkForSvr(conf ServerCommonConf) error {
  364. return nil
  365. }
  366. // DomainConf
  367. func (cfg *DomainConf) check() (err error) {
  368. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  369. err = fmt.Errorf("custom_domains and subdomain should set at least one of them")
  370. return
  371. }
  372. return
  373. }
  374. func (cfg *DomainConf) checkForCli() (err error) {
  375. if err = cfg.check(); err != nil {
  376. return
  377. }
  378. return
  379. }
  380. func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) {
  381. if err = cfg.check(); err != nil {
  382. return
  383. }
  384. for _, domain := range cfg.CustomDomains {
  385. if serverCfg.SubDomainHost != "" && len(strings.Split(serverCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
  386. if strings.Contains(domain, serverCfg.SubDomainHost) {
  387. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, serverCfg.SubDomainHost)
  388. }
  389. }
  390. }
  391. if cfg.SubDomain != "" {
  392. if serverCfg.SubDomainHost == "" {
  393. return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
  394. }
  395. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  396. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  397. }
  398. }
  399. return nil
  400. }
  401. // LocalSvrConf
  402. func (cfg *LocalSvrConf) checkForCli() (err error) {
  403. if cfg.Plugin == "" {
  404. if cfg.LocalIP == "" {
  405. err = fmt.Errorf("local ip or plugin is required")
  406. return
  407. }
  408. if cfg.LocalPort <= 0 {
  409. err = fmt.Errorf("error local_port")
  410. return
  411. }
  412. }
  413. return
  414. }
  415. // HealthCheckConf
  416. func (cfg *HealthCheckConf) checkForCli() error {
  417. if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
  418. return fmt.Errorf("unsupport health check type")
  419. }
  420. if cfg.HealthCheckType != "" {
  421. if cfg.HealthCheckType == "http" && cfg.HealthCheckURL == "" {
  422. return fmt.Errorf("health_check_url is required for health check type 'http'")
  423. }
  424. }
  425. return nil
  426. }
  427. func preUnmarshalFromIni(cfg ProxyConf, prefix string, name string, section *ini.Section) error {
  428. err := section.MapTo(cfg)
  429. if err != nil {
  430. return err
  431. }
  432. err = cfg.GetBaseInfo().decorate(prefix, name, section)
  433. if err != nil {
  434. return err
  435. }
  436. return nil
  437. }
  438. // TCP
  439. func (cfg *TCPProxyConf) Compare(cmp ProxyConf) bool {
  440. cmpConf, ok := cmp.(*TCPProxyConf)
  441. if !ok {
  442. return false
  443. }
  444. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
  445. return false
  446. }
  447. // Add custom logic equal if exists.
  448. if cfg.RemotePort != cmpConf.RemotePort {
  449. return false
  450. }
  451. return true
  452. }
  453. func (cfg *TCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  454. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  455. // Add custom logic unmarshal if exists
  456. cfg.RemotePort = pMsg.RemotePort
  457. }
  458. func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  459. err := preUnmarshalFromIni(cfg, prefix, name, section)
  460. if err != nil {
  461. return err
  462. }
  463. // Add custom logic unmarshal if exists
  464. return nil
  465. }
  466. func (cfg *TCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  467. cfg.BaseProxyConf.marshalToMsg(pMsg)
  468. // Add custom logic marshal if exists
  469. pMsg.RemotePort = cfg.RemotePort
  470. }
  471. func (cfg *TCPProxyConf) CheckForCli() (err error) {
  472. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  473. return
  474. }
  475. // Add custom logic check if exists
  476. return
  477. }
  478. func (cfg *TCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
  479. return nil
  480. }
  481. // TCPMux
  482. func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool {
  483. cmpConf, ok := cmp.(*TCPMuxProxyConf)
  484. if !ok {
  485. return false
  486. }
  487. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
  488. return false
  489. }
  490. // Add custom logic equal if exists.
  491. if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
  492. return false
  493. }
  494. if cfg.Multiplexer != cmpConf.Multiplexer || cfg.RouteByHTTPUser != cmpConf.RouteByHTTPUser {
  495. return false
  496. }
  497. return true
  498. }
  499. func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  500. err := preUnmarshalFromIni(cfg, prefix, name, section)
  501. if err != nil {
  502. return err
  503. }
  504. // Add custom logic unmarshal if exists
  505. return nil
  506. }
  507. func (cfg *TCPMuxProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  508. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  509. // Add custom logic unmarshal if exists
  510. cfg.CustomDomains = pMsg.CustomDomains
  511. cfg.SubDomain = pMsg.SubDomain
  512. cfg.Multiplexer = pMsg.Multiplexer
  513. cfg.RouteByHTTPUser = pMsg.RouteByHTTPUser
  514. }
  515. func (cfg *TCPMuxProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  516. cfg.BaseProxyConf.marshalToMsg(pMsg)
  517. // Add custom logic marshal if exists
  518. pMsg.CustomDomains = cfg.CustomDomains
  519. pMsg.SubDomain = cfg.SubDomain
  520. pMsg.Multiplexer = cfg.Multiplexer
  521. pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser
  522. }
  523. func (cfg *TCPMuxProxyConf) CheckForCli() (err error) {
  524. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  525. return
  526. }
  527. // Add custom logic check if exists
  528. if err = cfg.DomainConf.checkForCli(); err != nil {
  529. return
  530. }
  531. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  532. return fmt.Errorf("parse conf error: incorrect multiplexer [%s]", cfg.Multiplexer)
  533. }
  534. return
  535. }
  536. func (cfg *TCPMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  537. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  538. return fmt.Errorf("proxy [%s] incorrect multiplexer [%s]", cfg.ProxyName, cfg.Multiplexer)
  539. }
  540. if cfg.Multiplexer == consts.HTTPConnectTCPMultiplexer && serverCfg.TCPMuxHTTPConnectPort == 0 {
  541. return fmt.Errorf("proxy [%s] type [tcpmux] with multiplexer [httpconnect] requires tcpmux_httpconnect_port configuration", cfg.ProxyName)
  542. }
  543. if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
  544. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  545. return
  546. }
  547. return
  548. }
  549. // UDP
  550. func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool {
  551. cmpConf, ok := cmp.(*UDPProxyConf)
  552. if !ok {
  553. return false
  554. }
  555. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
  556. return false
  557. }
  558. // Add custom logic equal if exists.
  559. if cfg.RemotePort != cmpConf.RemotePort {
  560. return false
  561. }
  562. return true
  563. }
  564. func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  565. err := preUnmarshalFromIni(cfg, prefix, name, section)
  566. if err != nil {
  567. return err
  568. }
  569. // Add custom logic unmarshal if exists
  570. return nil
  571. }
  572. func (cfg *UDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  573. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  574. // Add custom logic unmarshal if exists
  575. cfg.RemotePort = pMsg.RemotePort
  576. }
  577. func (cfg *UDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  578. cfg.BaseProxyConf.marshalToMsg(pMsg)
  579. // Add custom logic marshal if exists
  580. pMsg.RemotePort = cfg.RemotePort
  581. }
  582. func (cfg *UDPProxyConf) CheckForCli() (err error) {
  583. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  584. return
  585. }
  586. // Add custom logic check if exists
  587. return
  588. }
  589. func (cfg *UDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
  590. return nil
  591. }
  592. // HTTP
  593. func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool {
  594. cmpConf, ok := cmp.(*HTTPProxyConf)
  595. if !ok {
  596. return false
  597. }
  598. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
  599. return false
  600. }
  601. // Add custom logic equal if exists.
  602. if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
  603. return false
  604. }
  605. if !reflect.DeepEqual(cfg.Locations, cmpConf.Locations) ||
  606. cfg.HTTPUser != cmpConf.HTTPUser ||
  607. cfg.HTTPPwd != cmpConf.HTTPPwd ||
  608. cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
  609. cfg.RouteByHTTPUser != cmpConf.RouteByHTTPUser ||
  610. !reflect.DeepEqual(cfg.Headers, cmpConf.Headers) {
  611. return false
  612. }
  613. return true
  614. }
  615. func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  616. err := preUnmarshalFromIni(cfg, prefix, name, section)
  617. if err != nil {
  618. return err
  619. }
  620. // Add custom logic unmarshal if exists
  621. cfg.Headers = GetMapWithoutPrefix(section.KeysHash(), "header_")
  622. return nil
  623. }
  624. func (cfg *HTTPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  625. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  626. // Add custom logic unmarshal if exists
  627. cfg.CustomDomains = pMsg.CustomDomains
  628. cfg.SubDomain = pMsg.SubDomain
  629. cfg.Locations = pMsg.Locations
  630. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  631. cfg.HTTPUser = pMsg.HTTPUser
  632. cfg.HTTPPwd = pMsg.HTTPPwd
  633. cfg.Headers = pMsg.Headers
  634. cfg.RouteByHTTPUser = pMsg.RouteByHTTPUser
  635. }
  636. func (cfg *HTTPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  637. cfg.BaseProxyConf.marshalToMsg(pMsg)
  638. // Add custom logic marshal if exists
  639. pMsg.CustomDomains = cfg.CustomDomains
  640. pMsg.SubDomain = cfg.SubDomain
  641. pMsg.Locations = cfg.Locations
  642. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  643. pMsg.HTTPUser = cfg.HTTPUser
  644. pMsg.HTTPPwd = cfg.HTTPPwd
  645. pMsg.Headers = cfg.Headers
  646. pMsg.RouteByHTTPUser = cfg.RouteByHTTPUser
  647. }
  648. func (cfg *HTTPProxyConf) CheckForCli() (err error) {
  649. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  650. return
  651. }
  652. // Add custom logic check if exists
  653. if err = cfg.DomainConf.checkForCli(); err != nil {
  654. return
  655. }
  656. return
  657. }
  658. func (cfg *HTTPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  659. if serverCfg.VhostHTTPPort == 0 {
  660. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  661. }
  662. if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
  663. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  664. return
  665. }
  666. return
  667. }
  668. // HTTPS
  669. func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool {
  670. cmpConf, ok := cmp.(*HTTPSProxyConf)
  671. if !ok {
  672. return false
  673. }
  674. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
  675. return false
  676. }
  677. // Add custom logic equal if exists.
  678. if !reflect.DeepEqual(cfg.DomainConf, cmpConf.DomainConf) {
  679. return false
  680. }
  681. return true
  682. }
  683. func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  684. err := preUnmarshalFromIni(cfg, prefix, name, section)
  685. if err != nil {
  686. return err
  687. }
  688. // Add custom logic unmarshal if exists
  689. return nil
  690. }
  691. func (cfg *HTTPSProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  692. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  693. // Add custom logic unmarshal if exists
  694. cfg.CustomDomains = pMsg.CustomDomains
  695. cfg.SubDomain = pMsg.SubDomain
  696. }
  697. func (cfg *HTTPSProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  698. cfg.BaseProxyConf.marshalToMsg(pMsg)
  699. // Add custom logic marshal if exists
  700. pMsg.CustomDomains = cfg.CustomDomains
  701. pMsg.SubDomain = cfg.SubDomain
  702. }
  703. func (cfg *HTTPSProxyConf) CheckForCli() (err error) {
  704. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  705. return
  706. }
  707. // Add custom logic check if exists
  708. if err = cfg.DomainConf.checkForCli(); err != nil {
  709. return
  710. }
  711. return
  712. }
  713. func (cfg *HTTPSProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  714. if serverCfg.VhostHTTPSPort == 0 {
  715. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  716. }
  717. if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
  718. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  719. return
  720. }
  721. return
  722. }
  723. // SUDP
  724. func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool {
  725. cmpConf, ok := cmp.(*SUDPProxyConf)
  726. if !ok {
  727. return false
  728. }
  729. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
  730. return false
  731. }
  732. // Add custom logic equal if exists.
  733. if cfg.Role != cmpConf.Role ||
  734. cfg.Sk != cmpConf.Sk {
  735. return false
  736. }
  737. return true
  738. }
  739. func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  740. err := preUnmarshalFromIni(cfg, prefix, name, section)
  741. if err != nil {
  742. return err
  743. }
  744. // Add custom logic unmarshal if exists
  745. return nil
  746. }
  747. // Only for role server.
  748. func (cfg *SUDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  749. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  750. // Add custom logic unmarshal if exists
  751. cfg.Sk = pMsg.Sk
  752. }
  753. func (cfg *SUDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  754. cfg.BaseProxyConf.marshalToMsg(pMsg)
  755. // Add custom logic marshal if exists
  756. pMsg.Sk = cfg.Sk
  757. }
  758. func (cfg *SUDPProxyConf) CheckForCli() (err error) {
  759. if err := cfg.BaseProxyConf.checkForCli(); err != nil {
  760. return err
  761. }
  762. // Add custom logic check if exists
  763. if cfg.Role != "server" {
  764. return fmt.Errorf("role should be 'server'")
  765. }
  766. return nil
  767. }
  768. func (cfg *SUDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
  769. return nil
  770. }
  771. // STCP
  772. func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool {
  773. cmpConf, ok := cmp.(*STCPProxyConf)
  774. if !ok {
  775. return false
  776. }
  777. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
  778. return false
  779. }
  780. // Add custom logic equal if exists.
  781. if cfg.Role != cmpConf.Role ||
  782. cfg.Sk != cmpConf.Sk {
  783. return false
  784. }
  785. return true
  786. }
  787. func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  788. err := preUnmarshalFromIni(cfg, prefix, name, section)
  789. if err != nil {
  790. return err
  791. }
  792. // Add custom logic unmarshal if exists
  793. if cfg.Role == "" {
  794. cfg.Role = "server"
  795. }
  796. return nil
  797. }
  798. // Only for role server.
  799. func (cfg *STCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  800. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  801. // Add custom logic unmarshal if exists
  802. cfg.Sk = pMsg.Sk
  803. }
  804. func (cfg *STCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  805. cfg.BaseProxyConf.marshalToMsg(pMsg)
  806. // Add custom logic marshal if exists
  807. pMsg.Sk = cfg.Sk
  808. }
  809. func (cfg *STCPProxyConf) CheckForCli() (err error) {
  810. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  811. return
  812. }
  813. // Add custom logic check if exists
  814. if cfg.Role != "server" {
  815. return fmt.Errorf("role should be 'server'")
  816. }
  817. return
  818. }
  819. func (cfg *STCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
  820. return nil
  821. }
  822. // XTCP
  823. func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool {
  824. cmpConf, ok := cmp.(*XTCPProxyConf)
  825. if !ok {
  826. return false
  827. }
  828. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) {
  829. return false
  830. }
  831. // Add custom logic equal if exists.
  832. if cfg.Role != cmpConf.Role ||
  833. cfg.Sk != cmpConf.Sk {
  834. return false
  835. }
  836. return true
  837. }
  838. func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section *ini.Section) error {
  839. err := preUnmarshalFromIni(cfg, prefix, name, section)
  840. if err != nil {
  841. return err
  842. }
  843. // Add custom logic unmarshal if exists
  844. if cfg.Role == "" {
  845. cfg.Role = "server"
  846. }
  847. return nil
  848. }
  849. // Only for role server.
  850. func (cfg *XTCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  851. cfg.BaseProxyConf.unmarshalFromMsg(pMsg)
  852. // Add custom logic unmarshal if exists
  853. cfg.Sk = pMsg.Sk
  854. }
  855. func (cfg *XTCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  856. cfg.BaseProxyConf.marshalToMsg(pMsg)
  857. // Add custom logic marshal if exists
  858. pMsg.Sk = cfg.Sk
  859. }
  860. func (cfg *XTCPProxyConf) CheckForCli() (err error) {
  861. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  862. return
  863. }
  864. // Add custom logic check if exists
  865. if cfg.Role != "server" {
  866. return fmt.Errorf("role should be 'server'")
  867. }
  868. return
  869. }
  870. func (cfg *XTCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error {
  871. return nil
  872. }