1
0

proxy.go 29 KB

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