1
0

proxy.go 29 KB

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