proxy.go 29 KB

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