proxy.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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/models/consts"
  21. "github.com/fatedier/frp/models/msg"
  22. "github.com/fatedier/frp/utils/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.UdpProxy] = reflect.TypeOf(UdpProxyConf{})
  32. proxyConfTypeMap[consts.HttpProxy] = reflect.TypeOf(HttpProxyConf{})
  33. proxyConfTypeMap[consts.HttpsProxy] = reflect.TypeOf(HttpsProxyConf{})
  34. proxyConfTypeMap[consts.StcpProxy] = reflect.TypeOf(StcpProxyConf{})
  35. proxyConfTypeMap[consts.XtcpProxy] = reflect.TypeOf(XtcpProxyConf{})
  36. }
  37. // NewConfByType creates a empty ProxyConf object by proxyType.
  38. // If proxyType isn't exist, return nil.
  39. func NewConfByType(proxyType string) ProxyConf {
  40. v, ok := proxyConfTypeMap[proxyType]
  41. if !ok {
  42. return nil
  43. }
  44. cfg := reflect.New(v).Interface().(ProxyConf)
  45. return cfg
  46. }
  47. type ProxyConf interface {
  48. GetBaseInfo() *BaseProxyConf
  49. UnmarshalFromMsg(pMsg *msg.NewProxy)
  50. UnmarshalFromIni(prefix string, name string, conf ini.Section) error
  51. MarshalToMsg(pMsg *msg.NewProxy)
  52. CheckForCli() error
  53. CheckForSvr() error
  54. Compare(conf ProxyConf) bool
  55. }
  56. func NewProxyConfFromMsg(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
  57. if pMsg.ProxyType == "" {
  58. pMsg.ProxyType = consts.TcpProxy
  59. }
  60. cfg = NewConfByType(pMsg.ProxyType)
  61. if cfg == nil {
  62. err = fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
  63. return
  64. }
  65. cfg.UnmarshalFromMsg(pMsg)
  66. err = cfg.CheckForSvr()
  67. return
  68. }
  69. func NewProxyConfFromIni(prefix string, name string, section ini.Section) (cfg ProxyConf, err error) {
  70. proxyType := section["type"]
  71. if proxyType == "" {
  72. proxyType = consts.TcpProxy
  73. section["type"] = consts.TcpProxy
  74. }
  75. cfg = NewConfByType(proxyType)
  76. if cfg == nil {
  77. err = fmt.Errorf("proxy [%s] type [%s] error", name, proxyType)
  78. return
  79. }
  80. if err = cfg.UnmarshalFromIni(prefix, name, section); err != nil {
  81. return
  82. }
  83. if err = cfg.CheckForCli(); err != nil {
  84. return
  85. }
  86. return
  87. }
  88. // BaseProxy info
  89. type BaseProxyConf struct {
  90. ProxyName string `json:"proxy_name"`
  91. ProxyType string `json:"proxy_type"`
  92. UseEncryption bool `json:"use_encryption"`
  93. UseCompression bool `json:"use_compression"`
  94. Group string `json:"group"`
  95. GroupKey string `json:"group_key"`
  96. // only used for client
  97. ProxyProtocolVersion string `json:"proxy_protocol_version"`
  98. LocalSvrConf
  99. HealthCheckConf
  100. }
  101. func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
  102. return cfg
  103. }
  104. func (cfg *BaseProxyConf) compare(cmp *BaseProxyConf) bool {
  105. if cfg.ProxyName != cmp.ProxyName ||
  106. cfg.ProxyType != cmp.ProxyType ||
  107. cfg.UseEncryption != cmp.UseEncryption ||
  108. cfg.UseCompression != cmp.UseCompression ||
  109. cfg.Group != cmp.Group ||
  110. cfg.GroupKey != cmp.GroupKey ||
  111. cfg.ProxyProtocolVersion != cmp.ProxyProtocolVersion {
  112. return false
  113. }
  114. if !cfg.LocalSvrConf.compare(&cmp.LocalSvrConf) {
  115. return false
  116. }
  117. if !cfg.HealthCheckConf.compare(&cmp.HealthCheckConf) {
  118. return false
  119. }
  120. return true
  121. }
  122. func (cfg *BaseProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  123. cfg.ProxyName = pMsg.ProxyName
  124. cfg.ProxyType = pMsg.ProxyType
  125. cfg.UseEncryption = pMsg.UseEncryption
  126. cfg.UseCompression = pMsg.UseCompression
  127. cfg.Group = pMsg.Group
  128. cfg.GroupKey = pMsg.GroupKey
  129. }
  130. func (cfg *BaseProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) error {
  131. var (
  132. tmpStr string
  133. ok bool
  134. )
  135. cfg.ProxyName = prefix + name
  136. cfg.ProxyType = section["type"]
  137. tmpStr, ok = section["use_encryption"]
  138. if ok && tmpStr == "true" {
  139. cfg.UseEncryption = true
  140. }
  141. tmpStr, ok = section["use_compression"]
  142. if ok && tmpStr == "true" {
  143. cfg.UseCompression = true
  144. }
  145. cfg.Group = section["group"]
  146. cfg.GroupKey = section["group_key"]
  147. cfg.ProxyProtocolVersion = section["proxy_protocol_version"]
  148. if err := cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  149. return err
  150. }
  151. if err := cfg.HealthCheckConf.UnmarshalFromIni(prefix, name, section); err != nil {
  152. return err
  153. }
  154. if cfg.HealthCheckType == "tcp" && cfg.Plugin == "" {
  155. cfg.HealthCheckAddr = cfg.LocalIp + fmt.Sprintf(":%d", cfg.LocalPort)
  156. }
  157. if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckUrl != "" {
  158. s := fmt.Sprintf("http://%s:%d", cfg.LocalIp, cfg.LocalPort)
  159. if !strings.HasPrefix(cfg.HealthCheckUrl, "/") {
  160. s += "/"
  161. }
  162. cfg.HealthCheckUrl = s + cfg.HealthCheckUrl
  163. }
  164. return nil
  165. }
  166. func (cfg *BaseProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  167. pMsg.ProxyName = cfg.ProxyName
  168. pMsg.ProxyType = cfg.ProxyType
  169. pMsg.UseEncryption = cfg.UseEncryption
  170. pMsg.UseCompression = cfg.UseCompression
  171. pMsg.Group = cfg.Group
  172. pMsg.GroupKey = cfg.GroupKey
  173. }
  174. func (cfg *BaseProxyConf) checkForCli() (err error) {
  175. if cfg.ProxyProtocolVersion != "" {
  176. if cfg.ProxyProtocolVersion != "v1" && cfg.ProxyProtocolVersion != "v2" {
  177. return fmt.Errorf("no support proxy protocol version: %s", cfg.ProxyProtocolVersion)
  178. }
  179. }
  180. if err = cfg.LocalSvrConf.checkForCli(); err != nil {
  181. return
  182. }
  183. if err = cfg.HealthCheckConf.checkForCli(); err != nil {
  184. return
  185. }
  186. return nil
  187. }
  188. // Bind info
  189. type BindInfoConf struct {
  190. RemotePort int `json:"remote_port"`
  191. }
  192. func (cfg *BindInfoConf) compare(cmp *BindInfoConf) bool {
  193. if cfg.RemotePort != cmp.RemotePort {
  194. return false
  195. }
  196. return true
  197. }
  198. func (cfg *BindInfoConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  199. cfg.RemotePort = pMsg.RemotePort
  200. }
  201. func (cfg *BindInfoConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  202. var (
  203. tmpStr string
  204. ok bool
  205. v int64
  206. )
  207. if tmpStr, ok = section["remote_port"]; ok {
  208. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  209. return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
  210. } else {
  211. cfg.RemotePort = int(v)
  212. }
  213. } else {
  214. return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
  215. }
  216. return nil
  217. }
  218. func (cfg *BindInfoConf) MarshalToMsg(pMsg *msg.NewProxy) {
  219. pMsg.RemotePort = cfg.RemotePort
  220. }
  221. // Domain info
  222. type DomainConf struct {
  223. CustomDomains []string `json:"custom_domains"`
  224. SubDomain string `json:"sub_domain"`
  225. }
  226. func (cfg *DomainConf) compare(cmp *DomainConf) bool {
  227. if strings.Join(cfg.CustomDomains, " ") != strings.Join(cmp.CustomDomains, " ") ||
  228. cfg.SubDomain != cmp.SubDomain {
  229. return false
  230. }
  231. return true
  232. }
  233. func (cfg *DomainConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  234. cfg.CustomDomains = pMsg.CustomDomains
  235. cfg.SubDomain = pMsg.SubDomain
  236. }
  237. func (cfg *DomainConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  238. var (
  239. tmpStr string
  240. ok bool
  241. )
  242. if tmpStr, ok = section["custom_domains"]; ok {
  243. cfg.CustomDomains = strings.Split(tmpStr, ",")
  244. for i, domain := range cfg.CustomDomains {
  245. cfg.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  246. }
  247. }
  248. if tmpStr, ok = section["subdomain"]; ok {
  249. cfg.SubDomain = tmpStr
  250. }
  251. return
  252. }
  253. func (cfg *DomainConf) MarshalToMsg(pMsg *msg.NewProxy) {
  254. pMsg.CustomDomains = cfg.CustomDomains
  255. pMsg.SubDomain = cfg.SubDomain
  256. }
  257. func (cfg *DomainConf) check() (err error) {
  258. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  259. err = fmt.Errorf("custom_domains and subdomain should set at least one of them")
  260. return
  261. }
  262. return
  263. }
  264. func (cfg *DomainConf) checkForCli() (err error) {
  265. if err = cfg.check(); err != nil {
  266. return
  267. }
  268. return
  269. }
  270. func (cfg *DomainConf) checkForSvr() (err error) {
  271. if err = cfg.check(); err != nil {
  272. return
  273. }
  274. for _, domain := range cfg.CustomDomains {
  275. if subDomainHost != "" && len(strings.Split(subDomainHost, ".")) < len(strings.Split(domain, ".")) {
  276. if strings.Contains(domain, subDomainHost) {
  277. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, subDomainHost)
  278. }
  279. }
  280. }
  281. if cfg.SubDomain != "" {
  282. if subDomainHost == "" {
  283. return fmt.Errorf("subdomain is not supported because this feature is not enabled in remote frps")
  284. }
  285. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  286. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  287. }
  288. }
  289. return
  290. }
  291. // Local service info
  292. type LocalSvrConf struct {
  293. LocalIp string `json:"local_ip"`
  294. LocalPort int `json:"local_port"`
  295. Plugin string `json:"plugin"`
  296. PluginParams map[string]string `json:"plugin_params"`
  297. }
  298. func (cfg *LocalSvrConf) compare(cmp *LocalSvrConf) bool {
  299. if cfg.LocalIp != cmp.LocalIp ||
  300. cfg.LocalPort != cmp.LocalPort {
  301. return false
  302. }
  303. if cfg.Plugin != cmp.Plugin ||
  304. len(cfg.PluginParams) != len(cmp.PluginParams) {
  305. return false
  306. }
  307. for k, v := range cfg.PluginParams {
  308. value, ok := cmp.PluginParams[k]
  309. if !ok || v != value {
  310. return false
  311. }
  312. }
  313. return true
  314. }
  315. func (cfg *LocalSvrConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  316. cfg.Plugin = section["plugin"]
  317. cfg.PluginParams = make(map[string]string)
  318. if cfg.Plugin != "" {
  319. // get params begin with "plugin_"
  320. for k, v := range section {
  321. if strings.HasPrefix(k, "plugin_") {
  322. cfg.PluginParams[k] = v
  323. }
  324. }
  325. } else {
  326. if cfg.LocalIp = section["local_ip"]; cfg.LocalIp == "" {
  327. cfg.LocalIp = "127.0.0.1"
  328. }
  329. if tmpStr, ok := section["local_port"]; ok {
  330. if cfg.LocalPort, err = strconv.Atoi(tmpStr); err != nil {
  331. return fmt.Errorf("Parse conf error: proxy [%s] local_port error", name)
  332. }
  333. } else {
  334. return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", name)
  335. }
  336. }
  337. return
  338. }
  339. func (cfg *LocalSvrConf) checkForCli() (err error) {
  340. if cfg.Plugin == "" {
  341. if cfg.LocalIp == "" {
  342. err = fmt.Errorf("local ip or plugin is required")
  343. return
  344. }
  345. if cfg.LocalPort <= 0 {
  346. err = fmt.Errorf("error local_port")
  347. return
  348. }
  349. }
  350. return
  351. }
  352. // Health check info
  353. type HealthCheckConf struct {
  354. HealthCheckType string `json:"health_check_type"` // tcp | http
  355. HealthCheckTimeoutS int `json:"health_check_timeout_s"`
  356. HealthCheckMaxFailed int `json:"health_check_max_failed"`
  357. HealthCheckIntervalS int `json:"health_check_interval_s"`
  358. HealthCheckUrl string `json:"health_check_url"`
  359. // local_ip + local_port
  360. HealthCheckAddr string `json:"-"`
  361. }
  362. func (cfg *HealthCheckConf) compare(cmp *HealthCheckConf) bool {
  363. if cfg.HealthCheckType != cmp.HealthCheckType ||
  364. cfg.HealthCheckTimeoutS != cmp.HealthCheckTimeoutS ||
  365. cfg.HealthCheckMaxFailed != cmp.HealthCheckMaxFailed ||
  366. cfg.HealthCheckIntervalS != cmp.HealthCheckIntervalS ||
  367. cfg.HealthCheckUrl != cmp.HealthCheckUrl {
  368. return false
  369. }
  370. return true
  371. }
  372. func (cfg *HealthCheckConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  373. cfg.HealthCheckType = section["health_check_type"]
  374. cfg.HealthCheckUrl = section["health_check_url"]
  375. if tmpStr, ok := section["health_check_timeout_s"]; ok {
  376. if cfg.HealthCheckTimeoutS, err = strconv.Atoi(tmpStr); err != nil {
  377. return fmt.Errorf("Parse conf error: proxy [%s] health_check_timeout_s error", name)
  378. }
  379. }
  380. if tmpStr, ok := section["health_check_max_failed"]; ok {
  381. if cfg.HealthCheckMaxFailed, err = strconv.Atoi(tmpStr); err != nil {
  382. return fmt.Errorf("Parse conf error: proxy [%s] health_check_max_failed error", name)
  383. }
  384. }
  385. if tmpStr, ok := section["health_check_interval_s"]; ok {
  386. if cfg.HealthCheckIntervalS, err = strconv.Atoi(tmpStr); err != nil {
  387. return fmt.Errorf("Parse conf error: proxy [%s] health_check_interval_s error", name)
  388. }
  389. }
  390. return
  391. }
  392. func (cfg *HealthCheckConf) checkForCli() error {
  393. if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
  394. return fmt.Errorf("unsupport health check type")
  395. }
  396. if cfg.HealthCheckType != "" {
  397. if cfg.HealthCheckType == "http" && cfg.HealthCheckUrl == "" {
  398. return fmt.Errorf("health_check_url is required for health check type 'http'")
  399. }
  400. }
  401. return nil
  402. }
  403. // TCP
  404. type TcpProxyConf struct {
  405. BaseProxyConf
  406. BindInfoConf
  407. }
  408. func (cfg *TcpProxyConf) Compare(cmp ProxyConf) bool {
  409. cmpConf, ok := cmp.(*TcpProxyConf)
  410. if !ok {
  411. return false
  412. }
  413. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  414. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
  415. return false
  416. }
  417. return true
  418. }
  419. func (cfg *TcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  420. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  421. cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
  422. }
  423. func (cfg *TcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  424. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  425. return
  426. }
  427. if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
  428. return
  429. }
  430. return
  431. }
  432. func (cfg *TcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  433. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  434. cfg.BindInfoConf.MarshalToMsg(pMsg)
  435. }
  436. func (cfg *TcpProxyConf) CheckForCli() (err error) {
  437. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  438. return err
  439. }
  440. return
  441. }
  442. func (cfg *TcpProxyConf) CheckForSvr() error { return nil }
  443. // UDP
  444. type UdpProxyConf struct {
  445. BaseProxyConf
  446. BindInfoConf
  447. }
  448. func (cfg *UdpProxyConf) Compare(cmp ProxyConf) bool {
  449. cmpConf, ok := cmp.(*UdpProxyConf)
  450. if !ok {
  451. return false
  452. }
  453. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  454. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
  455. return false
  456. }
  457. return true
  458. }
  459. func (cfg *UdpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  460. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  461. cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
  462. }
  463. func (cfg *UdpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  464. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  465. return
  466. }
  467. if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
  468. return
  469. }
  470. return
  471. }
  472. func (cfg *UdpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  473. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  474. cfg.BindInfoConf.MarshalToMsg(pMsg)
  475. }
  476. func (cfg *UdpProxyConf) CheckForCli() (err error) {
  477. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  478. return
  479. }
  480. return
  481. }
  482. func (cfg *UdpProxyConf) CheckForSvr() error { return nil }
  483. // HTTP
  484. type HttpProxyConf struct {
  485. BaseProxyConf
  486. DomainConf
  487. Locations []string `json:"locations"`
  488. HttpUser string `json:"http_user"`
  489. HttpPwd string `json:"http_pwd"`
  490. HostHeaderRewrite string `json:"host_header_rewrite"`
  491. Headers map[string]string `json:"headers"`
  492. }
  493. func (cfg *HttpProxyConf) Compare(cmp ProxyConf) bool {
  494. cmpConf, ok := cmp.(*HttpProxyConf)
  495. if !ok {
  496. return false
  497. }
  498. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  499. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  500. strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
  501. cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
  502. cfg.HttpUser != cmpConf.HttpUser ||
  503. cfg.HttpPwd != cmpConf.HttpPwd ||
  504. len(cfg.Headers) != len(cmpConf.Headers) {
  505. return false
  506. }
  507. for k, v := range cfg.Headers {
  508. if v2, ok := cmpConf.Headers[k]; !ok {
  509. return false
  510. } else {
  511. if v != v2 {
  512. return false
  513. }
  514. }
  515. }
  516. return true
  517. }
  518. func (cfg *HttpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  519. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  520. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  521. cfg.Locations = pMsg.Locations
  522. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  523. cfg.HttpUser = pMsg.HttpUser
  524. cfg.HttpPwd = pMsg.HttpPwd
  525. cfg.Headers = pMsg.Headers
  526. }
  527. func (cfg *HttpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  528. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  529. return
  530. }
  531. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  532. return
  533. }
  534. var (
  535. tmpStr string
  536. ok bool
  537. )
  538. if tmpStr, ok = section["locations"]; ok {
  539. cfg.Locations = strings.Split(tmpStr, ",")
  540. } else {
  541. cfg.Locations = []string{""}
  542. }
  543. cfg.HostHeaderRewrite = section["host_header_rewrite"]
  544. cfg.HttpUser = section["http_user"]
  545. cfg.HttpPwd = section["http_pwd"]
  546. cfg.Headers = make(map[string]string)
  547. for k, v := range section {
  548. if strings.HasPrefix(k, "header_") {
  549. cfg.Headers[strings.TrimPrefix(k, "header_")] = v
  550. }
  551. }
  552. return
  553. }
  554. func (cfg *HttpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  555. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  556. cfg.DomainConf.MarshalToMsg(pMsg)
  557. pMsg.Locations = cfg.Locations
  558. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  559. pMsg.HttpUser = cfg.HttpUser
  560. pMsg.HttpPwd = cfg.HttpPwd
  561. pMsg.Headers = cfg.Headers
  562. }
  563. func (cfg *HttpProxyConf) CheckForCli() (err error) {
  564. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  565. return
  566. }
  567. if err = cfg.DomainConf.checkForCli(); err != nil {
  568. return
  569. }
  570. return
  571. }
  572. func (cfg *HttpProxyConf) CheckForSvr() (err error) {
  573. if vhostHttpPort == 0 {
  574. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  575. }
  576. if err = cfg.DomainConf.checkForSvr(); err != nil {
  577. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  578. return
  579. }
  580. return
  581. }
  582. // HTTPS
  583. type HttpsProxyConf struct {
  584. BaseProxyConf
  585. DomainConf
  586. }
  587. func (cfg *HttpsProxyConf) Compare(cmp ProxyConf) bool {
  588. cmpConf, ok := cmp.(*HttpsProxyConf)
  589. if !ok {
  590. return false
  591. }
  592. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  593. !cfg.DomainConf.compare(&cmpConf.DomainConf) {
  594. return false
  595. }
  596. return true
  597. }
  598. func (cfg *HttpsProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  599. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  600. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  601. }
  602. func (cfg *HttpsProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  603. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  604. return
  605. }
  606. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  607. return
  608. }
  609. return
  610. }
  611. func (cfg *HttpsProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  612. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  613. cfg.DomainConf.MarshalToMsg(pMsg)
  614. }
  615. func (cfg *HttpsProxyConf) CheckForCli() (err error) {
  616. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  617. return
  618. }
  619. if err = cfg.DomainConf.checkForCli(); err != nil {
  620. return
  621. }
  622. return
  623. }
  624. func (cfg *HttpsProxyConf) CheckForSvr() (err error) {
  625. if vhostHttpsPort == 0 {
  626. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  627. }
  628. if err = cfg.DomainConf.checkForSvr(); err != nil {
  629. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  630. return
  631. }
  632. return
  633. }
  634. // STCP
  635. type StcpProxyConf struct {
  636. BaseProxyConf
  637. Role string `json:"role"`
  638. Sk string `json:"sk"`
  639. }
  640. func (cfg *StcpProxyConf) Compare(cmp ProxyConf) bool {
  641. cmpConf, ok := cmp.(*StcpProxyConf)
  642. if !ok {
  643. return false
  644. }
  645. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  646. cfg.Role != cmpConf.Role ||
  647. cfg.Sk != cmpConf.Sk {
  648. return false
  649. }
  650. return true
  651. }
  652. // Only for role server.
  653. func (cfg *StcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  654. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  655. cfg.Sk = pMsg.Sk
  656. }
  657. func (cfg *StcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  658. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  659. return
  660. }
  661. cfg.Role = section["role"]
  662. if cfg.Role != "server" {
  663. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  664. }
  665. cfg.Sk = section["sk"]
  666. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  667. return
  668. }
  669. return
  670. }
  671. func (cfg *StcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  672. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  673. pMsg.Sk = cfg.Sk
  674. }
  675. func (cfg *StcpProxyConf) CheckForCli() (err error) {
  676. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  677. return
  678. }
  679. if cfg.Role != "server" {
  680. err = fmt.Errorf("role should be 'server'")
  681. return
  682. }
  683. return
  684. }
  685. func (cfg *StcpProxyConf) CheckForSvr() (err error) {
  686. return
  687. }
  688. // XTCP
  689. type XtcpProxyConf struct {
  690. BaseProxyConf
  691. Role string `json:"role"`
  692. Sk string `json:"sk"`
  693. }
  694. func (cfg *XtcpProxyConf) Compare(cmp ProxyConf) bool {
  695. cmpConf, ok := cmp.(*XtcpProxyConf)
  696. if !ok {
  697. return false
  698. }
  699. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  700. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  701. cfg.Role != cmpConf.Role ||
  702. cfg.Sk != cmpConf.Sk {
  703. return false
  704. }
  705. return true
  706. }
  707. // Only for role server.
  708. func (cfg *XtcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  709. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  710. cfg.Sk = pMsg.Sk
  711. }
  712. func (cfg *XtcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  713. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  714. return
  715. }
  716. cfg.Role = section["role"]
  717. if cfg.Role != "server" {
  718. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  719. }
  720. cfg.Sk = section["sk"]
  721. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  722. return
  723. }
  724. return
  725. }
  726. func (cfg *XtcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  727. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  728. pMsg.Sk = cfg.Sk
  729. }
  730. func (cfg *XtcpProxyConf) CheckForCli() (err error) {
  731. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  732. return
  733. }
  734. if cfg.Role != "server" {
  735. err = fmt.Errorf("role should be 'server'")
  736. return
  737. }
  738. return
  739. }
  740. func (cfg *XtcpProxyConf) CheckForSvr() (err error) {
  741. return
  742. }
  743. func ParseRangeSection(name string, section ini.Section) (sections map[string]ini.Section, err error) {
  744. localPorts, errRet := util.ParseRangeNumbers(section["local_port"])
  745. if errRet != nil {
  746. err = fmt.Errorf("Parse conf error: range section [%s] local_port invalid, %v", name, errRet)
  747. return
  748. }
  749. remotePorts, errRet := util.ParseRangeNumbers(section["remote_port"])
  750. if errRet != nil {
  751. err = fmt.Errorf("Parse conf error: range section [%s] remote_port invalid, %v", name, errRet)
  752. return
  753. }
  754. if len(localPorts) != len(remotePorts) {
  755. err = fmt.Errorf("Parse conf error: range section [%s] local ports number should be same with remote ports number", name)
  756. return
  757. }
  758. if len(localPorts) == 0 {
  759. err = fmt.Errorf("Parse conf error: range section [%s] local_port and remote_port is necessary", name)
  760. return
  761. }
  762. sections = make(map[string]ini.Section)
  763. for i, port := range localPorts {
  764. subName := fmt.Sprintf("%s_%d", name, i)
  765. subSection := copySection(section)
  766. subSection["local_port"] = fmt.Sprintf("%d", port)
  767. subSection["remote_port"] = fmt.Sprintf("%d", remotePorts[i])
  768. sections[subName] = subSection
  769. }
  770. return
  771. }
  772. // if len(startProxy) is 0, start all
  773. // otherwise just start proxies in startProxy map
  774. func LoadAllConfFromIni(prefix string, content string, startProxy map[string]struct{}) (
  775. proxyConfs map[string]ProxyConf, visitorConfs map[string]VisitorConf, err error) {
  776. conf, errRet := ini.Load(strings.NewReader(content))
  777. if errRet != nil {
  778. err = errRet
  779. return
  780. }
  781. if prefix != "" {
  782. prefix += "."
  783. }
  784. startAll := true
  785. if len(startProxy) > 0 {
  786. startAll = false
  787. }
  788. proxyConfs = make(map[string]ProxyConf)
  789. visitorConfs = make(map[string]VisitorConf)
  790. for name, section := range conf {
  791. if name == "common" {
  792. continue
  793. }
  794. _, shouldStart := startProxy[name]
  795. if !startAll && !shouldStart {
  796. continue
  797. }
  798. subSections := make(map[string]ini.Section)
  799. if strings.HasPrefix(name, "range:") {
  800. // range section
  801. rangePrefix := strings.TrimSpace(strings.TrimPrefix(name, "range:"))
  802. subSections, err = ParseRangeSection(rangePrefix, section)
  803. if err != nil {
  804. return
  805. }
  806. } else {
  807. subSections[name] = section
  808. }
  809. for subName, subSection := range subSections {
  810. if subSection["role"] == "" {
  811. subSection["role"] = "server"
  812. }
  813. role := subSection["role"]
  814. if role == "server" {
  815. cfg, errRet := NewProxyConfFromIni(prefix, subName, subSection)
  816. if errRet != nil {
  817. err = errRet
  818. return
  819. }
  820. proxyConfs[prefix+subName] = cfg
  821. } else if role == "visitor" {
  822. cfg, errRet := NewVisitorConfFromIni(prefix, subName, subSection)
  823. if errRet != nil {
  824. err = errRet
  825. return
  826. }
  827. visitorConfs[prefix+subName] = cfg
  828. } else {
  829. err = fmt.Errorf("role should be 'server' or 'visitor'")
  830. return
  831. }
  832. }
  833. }
  834. return
  835. }
  836. func copySection(section ini.Section) (out ini.Section) {
  837. out = make(ini.Section)
  838. for k, v := range section {
  839. out[k] = v
  840. }
  841. return
  842. }