1
0

proxy.go 23 KB

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