proxy.go 27 KB

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