1
0

proxy.go 27 KB

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