proxy.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. "strconv"
  19. "strings"
  20. "github.com/fatedier/frp/pkg/consts"
  21. "github.com/fatedier/frp/pkg/msg"
  22. "github.com/fatedier/frp/pkg/util/util"
  23. ini "github.com/vaughan0/go-ini"
  24. )
  25. var (
  26. proxyConfTypeMap map[string]reflect.Type
  27. )
  28. func init() {
  29. proxyConfTypeMap = make(map[string]reflect.Type)
  30. proxyConfTypeMap[consts.TCPProxy] = reflect.TypeOf(TCPProxyConf{})
  31. proxyConfTypeMap[consts.TCPMuxProxy] = reflect.TypeOf(TCPMuxProxyConf{})
  32. proxyConfTypeMap[consts.UDPProxy] = reflect.TypeOf(UDPProxyConf{})
  33. proxyConfTypeMap[consts.HTTPProxy] = reflect.TypeOf(HTTPProxyConf{})
  34. proxyConfTypeMap[consts.HTTPSProxy] = reflect.TypeOf(HTTPSProxyConf{})
  35. proxyConfTypeMap[consts.STCPProxy] = reflect.TypeOf(STCPProxyConf{})
  36. proxyConfTypeMap[consts.XTCPProxy] = reflect.TypeOf(XTCPProxyConf{})
  37. proxyConfTypeMap[consts.SUDPProxy] = reflect.TypeOf(SUDPProxyConf{})
  38. }
  39. // NewConfByType creates a empty ProxyConf object by proxyType.
  40. // If proxyType isn't exist, return nil.
  41. func NewConfByType(proxyType string) ProxyConf {
  42. v, ok := proxyConfTypeMap[proxyType]
  43. if !ok {
  44. return nil
  45. }
  46. cfg := reflect.New(v).Interface().(ProxyConf)
  47. return cfg
  48. }
  49. type ProxyConf interface {
  50. GetBaseInfo() *BaseProxyConf
  51. UnmarshalFromMsg(pMsg *msg.NewProxy)
  52. UnmarshalFromIni(prefix string, name string, conf ini.Section) error
  53. MarshalToMsg(pMsg *msg.NewProxy)
  54. CheckForCli() error
  55. CheckForSvr(serverCfg ServerCommonConf) error
  56. Compare(conf ProxyConf) bool
  57. }
  58. func NewProxyConfFromMsg(pMsg *msg.NewProxy, serverCfg ServerCommonConf) (cfg ProxyConf, err error) {
  59. if pMsg.ProxyType == "" {
  60. pMsg.ProxyType = consts.TCPProxy
  61. }
  62. cfg = NewConfByType(pMsg.ProxyType)
  63. if cfg == nil {
  64. err = fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
  65. return
  66. }
  67. cfg.UnmarshalFromMsg(pMsg)
  68. err = cfg.CheckForSvr(serverCfg)
  69. return
  70. }
  71. func NewProxyConfFromIni(prefix string, name string, section ini.Section) (cfg ProxyConf, err error) {
  72. proxyType := section["type"]
  73. if proxyType == "" {
  74. proxyType = consts.TCPProxy
  75. section["type"] = consts.TCPProxy
  76. }
  77. cfg = NewConfByType(proxyType)
  78. if cfg == nil {
  79. err = fmt.Errorf("proxy [%s] type [%s] error", name, proxyType)
  80. return
  81. }
  82. if err = cfg.UnmarshalFromIni(prefix, name, section); err != nil {
  83. return
  84. }
  85. if err = cfg.CheckForCli(); err != nil {
  86. return
  87. }
  88. return
  89. }
  90. // BaseProxyConf provides configuration info that is common to all proxy types.
  91. type BaseProxyConf struct {
  92. // ProxyName is the name of this proxy.
  93. ProxyName string `json:"proxy_name"`
  94. // ProxyType specifies the type of this proxy. Valid values include "tcp",
  95. // "udp", "http", "https", "stcp", and "xtcp". By default, this value is
  96. // "tcp".
  97. ProxyType string `json:"proxy_type"`
  98. // UseEncryption controls whether or not communication with the server will
  99. // be encrypted. Encryption is done using the tokens supplied in the server
  100. // and client configuration. By default, this value is false.
  101. UseEncryption bool `json:"use_encryption"`
  102. // UseCompression controls whether or not communication with the server
  103. // will be compressed. By default, this value is false.
  104. UseCompression bool `json:"use_compression"`
  105. // Group specifies which group the proxy is a part of. The server will use
  106. // this information to load balance proxies in the same group. If the value
  107. // is "", this proxy will not be in a group. By default, this value is "".
  108. Group string `json:"group"`
  109. // GroupKey specifies a group key, which should be the same among proxies
  110. // of the same group. By default, this value is "".
  111. GroupKey string `json:"group_key"`
  112. // ProxyProtocolVersion specifies which protocol version to use. Valid
  113. // values include "v1", "v2", and "". If the value is "", a protocol
  114. // version will be automatically selected. By default, this value is "".
  115. ProxyProtocolVersion string `json:"proxy_protocol_version"`
  116. // BandwidthLimit limit the proxy bandwidth
  117. // 0 means no limit
  118. BandwidthLimit BandwidthQuantity `json:"bandwidth_limit"`
  119. // meta info for each proxy
  120. Metas map[string]string `json:"metas"`
  121. LocalSvrConf
  122. HealthCheckConf
  123. }
  124. func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
  125. return cfg
  126. }
  127. func (cfg *BaseProxyConf) compare(cmp *BaseProxyConf) bool {
  128. if cfg.ProxyName != cmp.ProxyName ||
  129. cfg.ProxyType != cmp.ProxyType ||
  130. cfg.UseEncryption != cmp.UseEncryption ||
  131. cfg.UseCompression != cmp.UseCompression ||
  132. cfg.Group != cmp.Group ||
  133. cfg.GroupKey != cmp.GroupKey ||
  134. cfg.ProxyProtocolVersion != cmp.ProxyProtocolVersion ||
  135. !cfg.BandwidthLimit.Equal(&cmp.BandwidthLimit) ||
  136. !reflect.DeepEqual(cfg.Metas, cmp.Metas) {
  137. return false
  138. }
  139. if !cfg.LocalSvrConf.compare(&cmp.LocalSvrConf) {
  140. return false
  141. }
  142. if !cfg.HealthCheckConf.compare(&cmp.HealthCheckConf) {
  143. return false
  144. }
  145. return true
  146. }
  147. func (cfg *BaseProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  148. cfg.ProxyName = pMsg.ProxyName
  149. cfg.ProxyType = pMsg.ProxyType
  150. cfg.UseEncryption = pMsg.UseEncryption
  151. cfg.UseCompression = pMsg.UseCompression
  152. cfg.Group = pMsg.Group
  153. cfg.GroupKey = pMsg.GroupKey
  154. cfg.Metas = pMsg.Metas
  155. }
  156. func (cfg *BaseProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) error {
  157. var (
  158. tmpStr string
  159. ok bool
  160. err error
  161. )
  162. cfg.ProxyName = prefix + name
  163. cfg.ProxyType = section["type"]
  164. tmpStr, ok = section["use_encryption"]
  165. if ok && tmpStr == "true" {
  166. cfg.UseEncryption = true
  167. }
  168. tmpStr, ok = section["use_compression"]
  169. if ok && tmpStr == "true" {
  170. cfg.UseCompression = true
  171. }
  172. cfg.Group = section["group"]
  173. cfg.GroupKey = section["group_key"]
  174. cfg.ProxyProtocolVersion = section["proxy_protocol_version"]
  175. if cfg.BandwidthLimit, err = NewBandwidthQuantity(section["bandwidth_limit"]); err != nil {
  176. return err
  177. }
  178. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  179. return err
  180. }
  181. if err = cfg.HealthCheckConf.UnmarshalFromIni(prefix, name, section); err != nil {
  182. return err
  183. }
  184. if cfg.HealthCheckType == "tcp" && cfg.Plugin == "" {
  185. cfg.HealthCheckAddr = cfg.LocalIP + fmt.Sprintf(":%d", cfg.LocalPort)
  186. }
  187. if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckURL != "" {
  188. s := fmt.Sprintf("http://%s:%d", cfg.LocalIP, cfg.LocalPort)
  189. if !strings.HasPrefix(cfg.HealthCheckURL, "/") {
  190. s += "/"
  191. }
  192. cfg.HealthCheckURL = s + cfg.HealthCheckURL
  193. }
  194. cfg.Metas = make(map[string]string)
  195. for k, v := range section {
  196. if strings.HasPrefix(k, "meta_") {
  197. cfg.Metas[strings.TrimPrefix(k, "meta_")] = v
  198. }
  199. }
  200. return nil
  201. }
  202. func (cfg *BaseProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  203. pMsg.ProxyName = cfg.ProxyName
  204. pMsg.ProxyType = cfg.ProxyType
  205. pMsg.UseEncryption = cfg.UseEncryption
  206. pMsg.UseCompression = cfg.UseCompression
  207. pMsg.Group = cfg.Group
  208. pMsg.GroupKey = cfg.GroupKey
  209. pMsg.Metas = cfg.Metas
  210. }
  211. func (cfg *BaseProxyConf) checkForCli() (err error) {
  212. if cfg.ProxyProtocolVersion != "" {
  213. if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" {
  214. return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion)
  215. }
  216. }
  217. if err = cfg.LocalSvrConf.checkForCli(); err != nil {
  218. return
  219. }
  220. if err = cfg.HealthCheckConf.checkForCli(); err != nil {
  221. return
  222. }
  223. return nil
  224. }
  225. // Bind info
  226. type BindInfoConf struct {
  227. RemotePort int `json:"remote_port"`
  228. }
  229. func (cfg *BindInfoConf) compare(cmp *BindInfoConf) bool {
  230. if cfg.RemotePort != cmp.RemotePort {
  231. return false
  232. }
  233. return true
  234. }
  235. func (cfg *BindInfoConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  236. cfg.RemotePort = pMsg.RemotePort
  237. }
  238. func (cfg *BindInfoConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  239. var (
  240. tmpStr string
  241. ok bool
  242. v int64
  243. )
  244. if tmpStr, ok = section["remote_port"]; ok {
  245. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  246. return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
  247. }
  248. cfg.RemotePort = int(v)
  249. } else {
  250. return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
  251. }
  252. return nil
  253. }
  254. func (cfg *BindInfoConf) MarshalToMsg(pMsg *msg.NewProxy) {
  255. pMsg.RemotePort = cfg.RemotePort
  256. }
  257. // Domain info
  258. type DomainConf struct {
  259. CustomDomains []string `json:"custom_domains"`
  260. SubDomain string `json:"sub_domain"`
  261. }
  262. func (cfg *DomainConf) compare(cmp *DomainConf) bool {
  263. if strings.Join(cfg.CustomDomains, " ") != strings.Join(cmp.CustomDomains, " ") ||
  264. cfg.SubDomain != cmp.SubDomain {
  265. return false
  266. }
  267. return true
  268. }
  269. func (cfg *DomainConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  270. cfg.CustomDomains = pMsg.CustomDomains
  271. cfg.SubDomain = pMsg.SubDomain
  272. }
  273. func (cfg *DomainConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  274. var (
  275. tmpStr string
  276. ok bool
  277. )
  278. if tmpStr, ok = section["custom_domains"]; ok {
  279. cfg.CustomDomains = strings.Split(tmpStr, ",")
  280. for i, domain := range cfg.CustomDomains {
  281. cfg.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  282. }
  283. }
  284. if tmpStr, ok = section["subdomain"]; ok {
  285. cfg.SubDomain = tmpStr
  286. }
  287. return
  288. }
  289. func (cfg *DomainConf) MarshalToMsg(pMsg *msg.NewProxy) {
  290. pMsg.CustomDomains = cfg.CustomDomains
  291. pMsg.SubDomain = cfg.SubDomain
  292. }
  293. func (cfg *DomainConf) check() (err error) {
  294. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  295. err = fmt.Errorf("custom_domains and subdomain should set at least one of them")
  296. return
  297. }
  298. return
  299. }
  300. func (cfg *DomainConf) checkForCli() (err error) {
  301. if err = cfg.check(); err != nil {
  302. return
  303. }
  304. return
  305. }
  306. func (cfg *DomainConf) checkForSvr(serverCfg ServerCommonConf) (err error) {
  307. if err = cfg.check(); err != nil {
  308. return
  309. }
  310. for _, domain := range cfg.CustomDomains {
  311. if serverCfg.SubDomainHost != "" && len(strings.Split(serverCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
  312. if strings.Contains(domain, serverCfg.SubDomainHost) {
  313. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, serverCfg.SubDomainHost)
  314. }
  315. }
  316. }
  317. if cfg.SubDomain != "" {
  318. if serverCfg.SubDomainHost == "" {
  319. return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
  320. }
  321. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  322. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  323. }
  324. }
  325. return
  326. }
  327. // LocalSvrConf configures what location the client will proxy to, or what
  328. // plugin will be used.
  329. type LocalSvrConf struct {
  330. // LocalIP specifies the IP address or host name to proxy to.
  331. LocalIP string `json:"local_ip"`
  332. // LocalPort specifies the port to proxy to.
  333. LocalPort int `json:"local_port"`
  334. // Plugin specifies what plugin should be used for proxying. If this value
  335. // is set, the LocalIp and LocalPort values will be ignored. By default,
  336. // this value is "".
  337. Plugin string `json:"plugin"`
  338. // PluginParams specify parameters to be passed to the plugin, if one is
  339. // being used. By default, this value is an empty map.
  340. PluginParams map[string]string `json:"plugin_params"`
  341. }
  342. func (cfg *LocalSvrConf) compare(cmp *LocalSvrConf) bool {
  343. if cfg.LocalIP != cmp.LocalIP ||
  344. cfg.LocalPort != cmp.LocalPort {
  345. return false
  346. }
  347. if cfg.Plugin != cmp.Plugin ||
  348. len(cfg.PluginParams) != len(cmp.PluginParams) {
  349. return false
  350. }
  351. for k, v := range cfg.PluginParams {
  352. value, ok := cmp.PluginParams[k]
  353. if !ok || v != value {
  354. return false
  355. }
  356. }
  357. return true
  358. }
  359. func (cfg *LocalSvrConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  360. cfg.Plugin = section["plugin"]
  361. cfg.PluginParams = make(map[string]string)
  362. if cfg.Plugin != "" {
  363. // get params begin with "plugin_"
  364. for k, v := range section {
  365. if strings.HasPrefix(k, "plugin_") {
  366. cfg.PluginParams[k] = v
  367. }
  368. }
  369. } else {
  370. if cfg.LocalIP = section["local_ip"]; cfg.LocalIP == "" {
  371. cfg.LocalIP = "127.0.0.1"
  372. }
  373. if tmpStr, ok := section["local_port"]; ok {
  374. if cfg.LocalPort, err = strconv.Atoi(tmpStr); err != nil {
  375. return fmt.Errorf("Parse conf error: proxy [%s] local_port error", name)
  376. }
  377. } else {
  378. return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", name)
  379. }
  380. }
  381. return
  382. }
  383. func (cfg *LocalSvrConf) checkForCli() (err error) {
  384. if cfg.Plugin == "" {
  385. if cfg.LocalIP == "" {
  386. err = fmt.Errorf("local ip or plugin is required")
  387. return
  388. }
  389. if cfg.LocalPort <= 0 {
  390. err = fmt.Errorf("error local_port")
  391. return
  392. }
  393. }
  394. return
  395. }
  396. // HealthCheckConf configures health checking. This can be useful for load
  397. // balancing purposes to detect and remove proxies to failing services.
  398. type HealthCheckConf struct {
  399. // HealthCheckType specifies what protocol to use for health checking.
  400. // Valid values include "tcp", "http", and "". If this value is "", health
  401. // checking will not be performed. By default, this value is "".
  402. //
  403. // If the type is "tcp", a connection will be attempted to the target
  404. // server. If a connection cannot be established, the health check fails.
  405. //
  406. // If the type is "http", a GET request will be made to the endpoint
  407. // specified by HealthCheckURL. If the response is not a 200, the health
  408. // check fails.
  409. HealthCheckType string `json:"health_check_type"` // tcp | http
  410. // HealthCheckTimeoutS specifies the number of seconds to wait for a health
  411. // check attempt to connect. If the timeout is reached, this counts as a
  412. // health check failure. By default, this value is 3.
  413. HealthCheckTimeoutS int `json:"health_check_timeout_s"`
  414. // HealthCheckMaxFailed specifies the number of allowed failures before the
  415. // proxy is stopped. By default, this value is 1.
  416. HealthCheckMaxFailed int `json:"health_check_max_failed"`
  417. // HealthCheckIntervalS specifies the time in seconds between health
  418. // checks. By default, this value is 10.
  419. HealthCheckIntervalS int `json:"health_check_interval_s"`
  420. // HealthCheckURL specifies the address to send health checks to if the
  421. // health check type is "http".
  422. HealthCheckURL string `json:"health_check_url"`
  423. // HealthCheckAddr specifies the address to connect to if the health check
  424. // type is "tcp".
  425. HealthCheckAddr string `json:"-"`
  426. }
  427. func (cfg *HealthCheckConf) compare(cmp *HealthCheckConf) bool {
  428. if cfg.HealthCheckType != cmp.HealthCheckType ||
  429. cfg.HealthCheckTimeoutS != cmp.HealthCheckTimeoutS ||
  430. cfg.HealthCheckMaxFailed != cmp.HealthCheckMaxFailed ||
  431. cfg.HealthCheckIntervalS != cmp.HealthCheckIntervalS ||
  432. cfg.HealthCheckURL != cmp.HealthCheckURL {
  433. return false
  434. }
  435. return true
  436. }
  437. func (cfg *HealthCheckConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  438. cfg.HealthCheckType = section["health_check_type"]
  439. cfg.HealthCheckURL = section["health_check_url"]
  440. if tmpStr, ok := section["health_check_timeout_s"]; ok {
  441. if cfg.HealthCheckTimeoutS, err = strconv.Atoi(tmpStr); err != nil {
  442. return fmt.Errorf("Parse conf error: proxy [%s] health_check_timeout_s error", name)
  443. }
  444. }
  445. if tmpStr, ok := section["health_check_max_failed"]; ok {
  446. if cfg.HealthCheckMaxFailed, err = strconv.Atoi(tmpStr); err != nil {
  447. return fmt.Errorf("Parse conf error: proxy [%s] health_check_max_failed error", name)
  448. }
  449. }
  450. if tmpStr, ok := section["health_check_interval_s"]; ok {
  451. if cfg.HealthCheckIntervalS, err = strconv.Atoi(tmpStr); err != nil {
  452. return fmt.Errorf("Parse conf error: proxy [%s] health_check_interval_s error", name)
  453. }
  454. }
  455. return
  456. }
  457. func (cfg *HealthCheckConf) checkForCli() error {
  458. if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
  459. return fmt.Errorf("unsupport health check type")
  460. }
  461. if cfg.HealthCheckType != "" {
  462. if cfg.HealthCheckType == "http" && cfg.HealthCheckURL == "" {
  463. return fmt.Errorf("health_check_url is required for health check type 'http'")
  464. }
  465. }
  466. return nil
  467. }
  468. // TCP
  469. type TCPProxyConf struct {
  470. BaseProxyConf
  471. BindInfoConf
  472. }
  473. func (cfg *TCPProxyConf) Compare(cmp ProxyConf) bool {
  474. cmpConf, ok := cmp.(*TCPProxyConf)
  475. if !ok {
  476. return false
  477. }
  478. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  479. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
  480. return false
  481. }
  482. return true
  483. }
  484. func (cfg *TCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  485. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  486. cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
  487. }
  488. func (cfg *TCPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  489. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  490. return
  491. }
  492. if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
  493. return
  494. }
  495. return
  496. }
  497. func (cfg *TCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  498. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  499. cfg.BindInfoConf.MarshalToMsg(pMsg)
  500. }
  501. func (cfg *TCPProxyConf) CheckForCli() (err error) {
  502. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  503. return err
  504. }
  505. return
  506. }
  507. func (cfg *TCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }
  508. // TCP Multiplexer
  509. type TCPMuxProxyConf struct {
  510. BaseProxyConf
  511. DomainConf
  512. Multiplexer string `json:"multiplexer"`
  513. }
  514. func (cfg *TCPMuxProxyConf) Compare(cmp ProxyConf) bool {
  515. cmpConf, ok := cmp.(*TCPMuxProxyConf)
  516. if !ok {
  517. return false
  518. }
  519. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  520. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  521. cfg.Multiplexer != cmpConf.Multiplexer {
  522. return false
  523. }
  524. return true
  525. }
  526. func (cfg *TCPMuxProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  527. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  528. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  529. cfg.Multiplexer = pMsg.Multiplexer
  530. }
  531. func (cfg *TCPMuxProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  532. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  533. return
  534. }
  535. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  536. return
  537. }
  538. cfg.Multiplexer = section["multiplexer"]
  539. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  540. return fmt.Errorf("parse conf error: proxy [%s] incorrect multiplexer [%s]", name, cfg.Multiplexer)
  541. }
  542. return
  543. }
  544. func (cfg *TCPMuxProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  545. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  546. cfg.DomainConf.MarshalToMsg(pMsg)
  547. pMsg.Multiplexer = cfg.Multiplexer
  548. }
  549. func (cfg *TCPMuxProxyConf) CheckForCli() (err error) {
  550. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  551. return err
  552. }
  553. if err = cfg.DomainConf.checkForCli(); err != nil {
  554. return err
  555. }
  556. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  557. return fmt.Errorf("parse conf error: incorrect multiplexer [%s]", cfg.Multiplexer)
  558. }
  559. return
  560. }
  561. func (cfg *TCPMuxProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  562. if cfg.Multiplexer != consts.HTTPConnectTCPMultiplexer {
  563. return fmt.Errorf("proxy [%s] incorrect multiplexer [%s]", cfg.ProxyName, cfg.Multiplexer)
  564. }
  565. if cfg.Multiplexer == consts.HTTPConnectTCPMultiplexer && serverCfg.TCPMuxHTTPConnectPort == 0 {
  566. return fmt.Errorf("proxy [%s] type [tcpmux] with multiplexer [httpconnect] requires tcpmux_httpconnect_port configuration", cfg.ProxyName)
  567. }
  568. if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
  569. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  570. return
  571. }
  572. return
  573. }
  574. // UDP
  575. type UDPProxyConf struct {
  576. BaseProxyConf
  577. BindInfoConf
  578. }
  579. func (cfg *UDPProxyConf) Compare(cmp ProxyConf) bool {
  580. cmpConf, ok := cmp.(*UDPProxyConf)
  581. if !ok {
  582. return false
  583. }
  584. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  585. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
  586. return false
  587. }
  588. return true
  589. }
  590. func (cfg *UDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  591. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  592. cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
  593. }
  594. func (cfg *UDPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  595. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  596. return
  597. }
  598. if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
  599. return
  600. }
  601. return
  602. }
  603. func (cfg *UDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  604. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  605. cfg.BindInfoConf.MarshalToMsg(pMsg)
  606. }
  607. func (cfg *UDPProxyConf) CheckForCli() (err error) {
  608. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  609. return
  610. }
  611. return
  612. }
  613. func (cfg *UDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) error { return nil }
  614. // HTTP
  615. type HTTPProxyConf struct {
  616. BaseProxyConf
  617. DomainConf
  618. Locations []string `json:"locations"`
  619. HTTPUser string `json:"http_user"`
  620. HTTPPwd string `json:"http_pwd"`
  621. HostHeaderRewrite string `json:"host_header_rewrite"`
  622. Headers map[string]string `json:"headers"`
  623. }
  624. func (cfg *HTTPProxyConf) Compare(cmp ProxyConf) bool {
  625. cmpConf, ok := cmp.(*HTTPProxyConf)
  626. if !ok {
  627. return false
  628. }
  629. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  630. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  631. strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
  632. cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
  633. cfg.HTTPUser != cmpConf.HTTPUser ||
  634. cfg.HTTPPwd != cmpConf.HTTPPwd ||
  635. len(cfg.Headers) != len(cmpConf.Headers) {
  636. return false
  637. }
  638. for k, v := range cfg.Headers {
  639. v2, ok := cmpConf.Headers[k]
  640. if !ok {
  641. return false
  642. }
  643. if v != v2 {
  644. return false
  645. }
  646. }
  647. return true
  648. }
  649. func (cfg *HTTPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  650. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  651. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  652. cfg.Locations = pMsg.Locations
  653. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  654. cfg.HTTPUser = pMsg.HTTPUser
  655. cfg.HTTPPwd = pMsg.HTTPPwd
  656. cfg.Headers = pMsg.Headers
  657. }
  658. func (cfg *HTTPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  659. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  660. return
  661. }
  662. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  663. return
  664. }
  665. var (
  666. tmpStr string
  667. ok bool
  668. )
  669. if tmpStr, ok = section["locations"]; ok {
  670. cfg.Locations = strings.Split(tmpStr, ",")
  671. } else {
  672. cfg.Locations = []string{""}
  673. }
  674. cfg.HostHeaderRewrite = section["host_header_rewrite"]
  675. cfg.HTTPUser = section["http_user"]
  676. cfg.HTTPPwd = section["http_pwd"]
  677. cfg.Headers = make(map[string]string)
  678. for k, v := range section {
  679. if strings.HasPrefix(k, "header_") {
  680. cfg.Headers[strings.TrimPrefix(k, "header_")] = v
  681. }
  682. }
  683. return
  684. }
  685. func (cfg *HTTPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  686. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  687. cfg.DomainConf.MarshalToMsg(pMsg)
  688. pMsg.Locations = cfg.Locations
  689. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  690. pMsg.HTTPUser = cfg.HTTPUser
  691. pMsg.HTTPPwd = cfg.HTTPPwd
  692. pMsg.Headers = cfg.Headers
  693. }
  694. func (cfg *HTTPProxyConf) CheckForCli() (err error) {
  695. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  696. return
  697. }
  698. if err = cfg.DomainConf.checkForCli(); err != nil {
  699. return
  700. }
  701. return
  702. }
  703. func (cfg *HTTPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  704. if serverCfg.VhostHTTPPort == 0 {
  705. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  706. }
  707. if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
  708. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  709. return
  710. }
  711. return
  712. }
  713. // HTTPS
  714. type HTTPSProxyConf struct {
  715. BaseProxyConf
  716. DomainConf
  717. }
  718. func (cfg *HTTPSProxyConf) Compare(cmp ProxyConf) bool {
  719. cmpConf, ok := cmp.(*HTTPSProxyConf)
  720. if !ok {
  721. return false
  722. }
  723. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  724. !cfg.DomainConf.compare(&cmpConf.DomainConf) {
  725. return false
  726. }
  727. return true
  728. }
  729. func (cfg *HTTPSProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  730. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  731. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  732. }
  733. func (cfg *HTTPSProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  734. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  735. return
  736. }
  737. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  738. return
  739. }
  740. return
  741. }
  742. func (cfg *HTTPSProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  743. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  744. cfg.DomainConf.MarshalToMsg(pMsg)
  745. }
  746. func (cfg *HTTPSProxyConf) CheckForCli() (err error) {
  747. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  748. return
  749. }
  750. if err = cfg.DomainConf.checkForCli(); err != nil {
  751. return
  752. }
  753. return
  754. }
  755. func (cfg *HTTPSProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  756. if serverCfg.VhostHTTPSPort == 0 {
  757. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  758. }
  759. if err = cfg.DomainConf.checkForSvr(serverCfg); err != nil {
  760. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  761. return
  762. }
  763. return
  764. }
  765. // SUDP
  766. type SUDPProxyConf struct {
  767. BaseProxyConf
  768. Role string `json:"role"`
  769. Sk string `json:"sk"`
  770. }
  771. func (cfg *SUDPProxyConf) Compare(cmp ProxyConf) bool {
  772. cmpConf, ok := cmp.(*SUDPProxyConf)
  773. if !ok {
  774. return false
  775. }
  776. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  777. cfg.Role != cmpConf.Role ||
  778. cfg.Sk != cmpConf.Sk {
  779. return false
  780. }
  781. return true
  782. }
  783. func (cfg *SUDPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  784. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  785. return
  786. }
  787. cfg.Role = section["role"]
  788. if cfg.Role != "server" {
  789. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  790. }
  791. cfg.Sk = section["sk"]
  792. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  793. return
  794. }
  795. return
  796. }
  797. func (cfg *SUDPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  798. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  799. pMsg.Sk = cfg.Sk
  800. }
  801. func (cfg *SUDPProxyConf) CheckForCli() (err error) {
  802. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  803. return
  804. }
  805. if cfg.Role != "server" {
  806. err = fmt.Errorf("role should be 'server'")
  807. return
  808. }
  809. return
  810. }
  811. func (cfg *SUDPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  812. return
  813. }
  814. // Only for role server.
  815. func (cfg *SUDPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  816. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  817. cfg.Sk = pMsg.Sk
  818. }
  819. // STCP
  820. type STCPProxyConf struct {
  821. BaseProxyConf
  822. Role string `json:"role"`
  823. Sk string `json:"sk"`
  824. }
  825. func (cfg *STCPProxyConf) Compare(cmp ProxyConf) bool {
  826. cmpConf, ok := cmp.(*STCPProxyConf)
  827. if !ok {
  828. return false
  829. }
  830. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  831. cfg.Role != cmpConf.Role ||
  832. cfg.Sk != cmpConf.Sk {
  833. return false
  834. }
  835. return true
  836. }
  837. // Only for role server.
  838. func (cfg *STCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  839. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  840. cfg.Sk = pMsg.Sk
  841. }
  842. func (cfg *STCPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  843. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  844. return
  845. }
  846. cfg.Role = section["role"]
  847. if cfg.Role != "server" {
  848. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  849. }
  850. cfg.Sk = section["sk"]
  851. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  852. return
  853. }
  854. return
  855. }
  856. func (cfg *STCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  857. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  858. pMsg.Sk = cfg.Sk
  859. }
  860. func (cfg *STCPProxyConf) CheckForCli() (err error) {
  861. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  862. return
  863. }
  864. if cfg.Role != "server" {
  865. err = fmt.Errorf("role should be 'server'")
  866. return
  867. }
  868. return
  869. }
  870. func (cfg *STCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  871. return
  872. }
  873. // XTCP
  874. type XTCPProxyConf struct {
  875. BaseProxyConf
  876. Role string `json:"role"`
  877. Sk string `json:"sk"`
  878. }
  879. func (cfg *XTCPProxyConf) Compare(cmp ProxyConf) bool {
  880. cmpConf, ok := cmp.(*XTCPProxyConf)
  881. if !ok {
  882. return false
  883. }
  884. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  885. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  886. cfg.Role != cmpConf.Role ||
  887. cfg.Sk != cmpConf.Sk {
  888. return false
  889. }
  890. return true
  891. }
  892. // Only for role server.
  893. func (cfg *XTCPProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  894. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  895. cfg.Sk = pMsg.Sk
  896. }
  897. func (cfg *XTCPProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  898. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  899. return
  900. }
  901. cfg.Role = section["role"]
  902. if cfg.Role != "server" {
  903. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  904. }
  905. cfg.Sk = section["sk"]
  906. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  907. return
  908. }
  909. return
  910. }
  911. func (cfg *XTCPProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  912. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  913. pMsg.Sk = cfg.Sk
  914. }
  915. func (cfg *XTCPProxyConf) CheckForCli() (err error) {
  916. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  917. return
  918. }
  919. if cfg.Role != "server" {
  920. err = fmt.Errorf("role should be 'server'")
  921. return
  922. }
  923. return
  924. }
  925. func (cfg *XTCPProxyConf) CheckForSvr(serverCfg ServerCommonConf) (err error) {
  926. return
  927. }
  928. func ParseRangeSection(name string, section ini.Section) (sections map[string]ini.Section, err error) {
  929. localPorts, errRet := util.ParseRangeNumbers(section["local_port"])
  930. if errRet != nil {
  931. err = fmt.Errorf("Parse conf error: range section [%s] local_port invalid, %v", name, errRet)
  932. return
  933. }
  934. remotePorts, errRet := util.ParseRangeNumbers(section["remote_port"])
  935. if errRet != nil {
  936. err = fmt.Errorf("Parse conf error: range section [%s] remote_port invalid, %v", name, errRet)
  937. return
  938. }
  939. if len(localPorts) != len(remotePorts) {
  940. err = fmt.Errorf("Parse conf error: range section [%s] local ports number should be same with remote ports number", name)
  941. return
  942. }
  943. if len(localPorts) == 0 {
  944. err = fmt.Errorf("Parse conf error: range section [%s] local_port and remote_port is necessary", name)
  945. return
  946. }
  947. sections = make(map[string]ini.Section)
  948. for i, port := range localPorts {
  949. subName := fmt.Sprintf("%s_%d", name, i)
  950. subSection := copySection(section)
  951. subSection["local_port"] = fmt.Sprintf("%d", port)
  952. subSection["remote_port"] = fmt.Sprintf("%d", remotePorts[i])
  953. sections[subName] = subSection
  954. }
  955. return
  956. }
  957. // if len(startProxy) is 0, start all
  958. // otherwise just start proxies in startProxy map
  959. func LoadAllConfFromIni(prefix string, content string, startProxy map[string]struct{}) (
  960. proxyConfs map[string]ProxyConf, visitorConfs map[string]VisitorConf, err error) {
  961. conf, errRet := ini.Load(strings.NewReader(content))
  962. if errRet != nil {
  963. err = errRet
  964. return
  965. }
  966. if prefix != "" {
  967. prefix += "."
  968. }
  969. startAll := true
  970. if len(startProxy) > 0 {
  971. startAll = false
  972. }
  973. proxyConfs = make(map[string]ProxyConf)
  974. visitorConfs = make(map[string]VisitorConf)
  975. for name, section := range conf {
  976. if name == "common" {
  977. continue
  978. }
  979. _, shouldStart := startProxy[name]
  980. if !startAll && !shouldStart {
  981. continue
  982. }
  983. subSections := make(map[string]ini.Section)
  984. if strings.HasPrefix(name, "range:") {
  985. // range section
  986. rangePrefix := strings.TrimSpace(strings.TrimPrefix(name, "range:"))
  987. subSections, err = ParseRangeSection(rangePrefix, section)
  988. if err != nil {
  989. return
  990. }
  991. } else {
  992. subSections[name] = section
  993. }
  994. for subName, subSection := range subSections {
  995. if subSection["role"] == "" {
  996. subSection["role"] = "server"
  997. }
  998. role := subSection["role"]
  999. if role == "server" {
  1000. cfg, errRet := NewProxyConfFromIni(prefix, subName, subSection)
  1001. if errRet != nil {
  1002. err = errRet
  1003. return
  1004. }
  1005. proxyConfs[prefix+subName] = cfg
  1006. } else if role == "visitor" {
  1007. cfg, errRet := NewVisitorConfFromIni(prefix, subName, subSection)
  1008. if errRet != nil {
  1009. err = errRet
  1010. return
  1011. }
  1012. visitorConfs[prefix+subName] = cfg
  1013. } else {
  1014. err = fmt.Errorf("role should be 'server' or 'visitor'")
  1015. return
  1016. }
  1017. }
  1018. }
  1019. return
  1020. }
  1021. func copySection(section ini.Section) (out ini.Section) {
  1022. out = make(ini.Section)
  1023. for k, v := range section {
  1024. out[k] = v
  1025. }
  1026. return
  1027. }