proxy.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. HealthCheckIntervalS int `json:"health_check_interval_s"`
  337. HealthCheckUrl string `json:"health_check_url"`
  338. // local_ip + local_port
  339. HealthCheckAddr string `json:"-"`
  340. }
  341. func (cfg *HealthCheckConf) compare(cmp *HealthCheckConf) bool {
  342. if cfg.HealthCheckType != cmp.HealthCheckType ||
  343. cfg.HealthCheckUrl != cmp.HealthCheckUrl ||
  344. cfg.HealthCheckIntervalS != cmp.HealthCheckIntervalS {
  345. return false
  346. }
  347. return true
  348. }
  349. func (cfg *HealthCheckConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  350. cfg.HealthCheckType = section["health_check_type"]
  351. cfg.HealthCheckUrl = section["health_check_url"]
  352. if tmpStr, ok := section["health_check_interval_s"]; ok {
  353. if cfg.HealthCheckIntervalS, err = strconv.Atoi(tmpStr); err != nil {
  354. return fmt.Errorf("Parse conf error: proxy [%s] health_check_interval_s error", name)
  355. }
  356. }
  357. return
  358. }
  359. func (cfg *HealthCheckConf) checkForCli() error {
  360. if cfg.HealthCheckType != "" && cfg.HealthCheckType != "tcp" && cfg.HealthCheckType != "http" {
  361. return fmt.Errorf("unsupport health check type")
  362. }
  363. if cfg.HealthCheckType != "" {
  364. if cfg.HealthCheckType == "http" && cfg.HealthCheckUrl == "" {
  365. return fmt.Errorf("health_check_url is required for health check type 'http'")
  366. }
  367. if cfg.HealthCheckIntervalS <= 0 {
  368. return fmt.Errorf("health_check_interval_s is required and should greater than 0")
  369. }
  370. }
  371. return nil
  372. }
  373. // TCP
  374. type TcpProxyConf struct {
  375. BaseProxyConf
  376. BindInfoConf
  377. }
  378. func (cfg *TcpProxyConf) Compare(cmp ProxyConf) bool {
  379. cmpConf, ok := cmp.(*TcpProxyConf)
  380. if !ok {
  381. return false
  382. }
  383. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  384. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
  385. return false
  386. }
  387. return true
  388. }
  389. func (cfg *TcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  390. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  391. cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
  392. }
  393. func (cfg *TcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  394. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  395. return
  396. }
  397. if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
  398. return
  399. }
  400. return
  401. }
  402. func (cfg *TcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  403. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  404. cfg.BindInfoConf.MarshalToMsg(pMsg)
  405. }
  406. func (cfg *TcpProxyConf) CheckForCli() (err error) {
  407. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  408. return err
  409. }
  410. return
  411. }
  412. func (cfg *TcpProxyConf) CheckForSvr() error { return nil }
  413. // UDP
  414. type UdpProxyConf struct {
  415. BaseProxyConf
  416. BindInfoConf
  417. }
  418. func (cfg *UdpProxyConf) Compare(cmp ProxyConf) bool {
  419. cmpConf, ok := cmp.(*UdpProxyConf)
  420. if !ok {
  421. return false
  422. }
  423. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  424. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) {
  425. return false
  426. }
  427. return true
  428. }
  429. func (cfg *UdpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  430. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  431. cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
  432. }
  433. func (cfg *UdpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  434. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  435. return
  436. }
  437. if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
  438. return
  439. }
  440. return
  441. }
  442. func (cfg *UdpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  443. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  444. cfg.BindInfoConf.MarshalToMsg(pMsg)
  445. }
  446. func (cfg *UdpProxyConf) CheckForCli() (err error) {
  447. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  448. return
  449. }
  450. return
  451. }
  452. func (cfg *UdpProxyConf) CheckForSvr() error { return nil }
  453. // HTTP
  454. type HttpProxyConf struct {
  455. BaseProxyConf
  456. DomainConf
  457. Locations []string `json:"locations"`
  458. HttpUser string `json:"http_user"`
  459. HttpPwd string `json:"http_pwd"`
  460. HostHeaderRewrite string `json:"host_header_rewrite"`
  461. Headers map[string]string `json:"headers"`
  462. }
  463. func (cfg *HttpProxyConf) Compare(cmp ProxyConf) bool {
  464. cmpConf, ok := cmp.(*HttpProxyConf)
  465. if !ok {
  466. return false
  467. }
  468. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  469. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  470. strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
  471. cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
  472. cfg.HttpUser != cmpConf.HttpUser ||
  473. cfg.HttpPwd != cmpConf.HttpPwd ||
  474. len(cfg.Headers) != len(cmpConf.Headers) {
  475. return false
  476. }
  477. for k, v := range cfg.Headers {
  478. if v2, ok := cmpConf.Headers[k]; !ok {
  479. return false
  480. } else {
  481. if v != v2 {
  482. return false
  483. }
  484. }
  485. }
  486. return true
  487. }
  488. func (cfg *HttpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  489. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  490. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  491. cfg.Locations = pMsg.Locations
  492. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  493. cfg.HttpUser = pMsg.HttpUser
  494. cfg.HttpPwd = pMsg.HttpPwd
  495. cfg.Headers = pMsg.Headers
  496. }
  497. func (cfg *HttpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  498. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  499. return
  500. }
  501. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  502. return
  503. }
  504. var (
  505. tmpStr string
  506. ok bool
  507. )
  508. if tmpStr, ok = section["locations"]; ok {
  509. cfg.Locations = strings.Split(tmpStr, ",")
  510. } else {
  511. cfg.Locations = []string{""}
  512. }
  513. cfg.HostHeaderRewrite = section["host_header_rewrite"]
  514. cfg.HttpUser = section["http_user"]
  515. cfg.HttpPwd = section["http_pwd"]
  516. cfg.Headers = make(map[string]string)
  517. for k, v := range section {
  518. if strings.HasPrefix(k, "header_") {
  519. cfg.Headers[strings.TrimPrefix(k, "header_")] = v
  520. }
  521. }
  522. return
  523. }
  524. func (cfg *HttpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  525. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  526. cfg.DomainConf.MarshalToMsg(pMsg)
  527. pMsg.Locations = cfg.Locations
  528. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  529. pMsg.HttpUser = cfg.HttpUser
  530. pMsg.HttpPwd = cfg.HttpPwd
  531. pMsg.Headers = cfg.Headers
  532. }
  533. func (cfg *HttpProxyConf) CheckForCli() (err error) {
  534. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  535. return
  536. }
  537. if err = cfg.DomainConf.checkForCli(); err != nil {
  538. return
  539. }
  540. return
  541. }
  542. func (cfg *HttpProxyConf) CheckForSvr() (err error) {
  543. if vhostHttpPort == 0 {
  544. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  545. }
  546. if err = cfg.DomainConf.checkForSvr(); err != nil {
  547. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  548. return
  549. }
  550. return
  551. }
  552. // HTTPS
  553. type HttpsProxyConf struct {
  554. BaseProxyConf
  555. DomainConf
  556. }
  557. func (cfg *HttpsProxyConf) Compare(cmp ProxyConf) bool {
  558. cmpConf, ok := cmp.(*HttpsProxyConf)
  559. if !ok {
  560. return false
  561. }
  562. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  563. !cfg.DomainConf.compare(&cmpConf.DomainConf) {
  564. return false
  565. }
  566. return true
  567. }
  568. func (cfg *HttpsProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  569. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  570. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  571. }
  572. func (cfg *HttpsProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  573. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  574. return
  575. }
  576. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  577. return
  578. }
  579. return
  580. }
  581. func (cfg *HttpsProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  582. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  583. cfg.DomainConf.MarshalToMsg(pMsg)
  584. }
  585. func (cfg *HttpsProxyConf) CheckForCli() (err error) {
  586. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  587. return
  588. }
  589. if err = cfg.DomainConf.checkForCli(); err != nil {
  590. return
  591. }
  592. return
  593. }
  594. func (cfg *HttpsProxyConf) CheckForSvr() (err error) {
  595. if vhostHttpsPort == 0 {
  596. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  597. }
  598. if err = cfg.DomainConf.checkForSvr(); err != nil {
  599. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  600. return
  601. }
  602. return
  603. }
  604. // STCP
  605. type StcpProxyConf struct {
  606. BaseProxyConf
  607. Role string `json:"role"`
  608. Sk string `json:"sk"`
  609. }
  610. func (cfg *StcpProxyConf) Compare(cmp ProxyConf) bool {
  611. cmpConf, ok := cmp.(*StcpProxyConf)
  612. if !ok {
  613. return false
  614. }
  615. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  616. cfg.Role != cmpConf.Role ||
  617. cfg.Sk != cmpConf.Sk {
  618. return false
  619. }
  620. return true
  621. }
  622. // Only for role server.
  623. func (cfg *StcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  624. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  625. cfg.Sk = pMsg.Sk
  626. }
  627. func (cfg *StcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  628. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  629. return
  630. }
  631. cfg.Role = section["role"]
  632. if cfg.Role != "server" {
  633. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  634. }
  635. cfg.Sk = section["sk"]
  636. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  637. return
  638. }
  639. return
  640. }
  641. func (cfg *StcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  642. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  643. pMsg.Sk = cfg.Sk
  644. }
  645. func (cfg *StcpProxyConf) CheckForCli() (err error) {
  646. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  647. return
  648. }
  649. if cfg.Role != "server" {
  650. err = fmt.Errorf("role should be 'server'")
  651. return
  652. }
  653. return
  654. }
  655. func (cfg *StcpProxyConf) CheckForSvr() (err error) {
  656. return
  657. }
  658. // XTCP
  659. type XtcpProxyConf struct {
  660. BaseProxyConf
  661. Role string `json:"role"`
  662. Sk string `json:"sk"`
  663. }
  664. func (cfg *XtcpProxyConf) Compare(cmp ProxyConf) bool {
  665. cmpConf, ok := cmp.(*XtcpProxyConf)
  666. if !ok {
  667. return false
  668. }
  669. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  670. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  671. cfg.Role != cmpConf.Role ||
  672. cfg.Sk != cmpConf.Sk {
  673. return false
  674. }
  675. return true
  676. }
  677. // Only for role server.
  678. func (cfg *XtcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  679. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  680. cfg.Sk = pMsg.Sk
  681. }
  682. func (cfg *XtcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  683. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  684. return
  685. }
  686. cfg.Role = section["role"]
  687. if cfg.Role != "server" {
  688. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, cfg.Role)
  689. }
  690. cfg.Sk = section["sk"]
  691. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  692. return
  693. }
  694. return
  695. }
  696. func (cfg *XtcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  697. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  698. pMsg.Sk = cfg.Sk
  699. }
  700. func (cfg *XtcpProxyConf) CheckForCli() (err error) {
  701. if err = cfg.BaseProxyConf.checkForCli(); err != nil {
  702. return
  703. }
  704. if cfg.Role != "server" {
  705. err = fmt.Errorf("role should be 'server'")
  706. return
  707. }
  708. return
  709. }
  710. func (cfg *XtcpProxyConf) CheckForSvr() (err error) {
  711. return
  712. }
  713. func ParseRangeSection(name string, section ini.Section) (sections map[string]ini.Section, err error) {
  714. localPorts, errRet := util.ParseRangeNumbers(section["local_port"])
  715. if errRet != nil {
  716. err = fmt.Errorf("Parse conf error: range section [%s] local_port invalid, %v", name, errRet)
  717. return
  718. }
  719. remotePorts, errRet := util.ParseRangeNumbers(section["remote_port"])
  720. if errRet != nil {
  721. err = fmt.Errorf("Parse conf error: range section [%s] remote_port invalid, %v", name, errRet)
  722. return
  723. }
  724. if len(localPorts) != len(remotePorts) {
  725. err = fmt.Errorf("Parse conf error: range section [%s] local ports number should be same with remote ports number", name)
  726. return
  727. }
  728. if len(localPorts) == 0 {
  729. err = fmt.Errorf("Parse conf error: range section [%s] local_port and remote_port is necessary", name)
  730. return
  731. }
  732. sections = make(map[string]ini.Section)
  733. for i, port := range localPorts {
  734. subName := fmt.Sprintf("%s_%d", name, i)
  735. subSection := copySection(section)
  736. subSection["local_port"] = fmt.Sprintf("%d", port)
  737. subSection["remote_port"] = fmt.Sprintf("%d", remotePorts[i])
  738. sections[subName] = subSection
  739. }
  740. return
  741. }
  742. // if len(startProxy) is 0, start all
  743. // otherwise just start proxies in startProxy map
  744. func LoadAllConfFromIni(prefix string, conf ini.File, startProxy map[string]struct{}) (
  745. proxyConfs map[string]ProxyConf, visitorConfs map[string]VisitorConf, err error) {
  746. if prefix != "" {
  747. prefix += "."
  748. }
  749. startAll := true
  750. if len(startProxy) > 0 {
  751. startAll = false
  752. }
  753. proxyConfs = make(map[string]ProxyConf)
  754. visitorConfs = make(map[string]VisitorConf)
  755. for name, section := range conf {
  756. if name == "common" {
  757. continue
  758. }
  759. _, shouldStart := startProxy[name]
  760. if !startAll && !shouldStart {
  761. continue
  762. }
  763. subSections := make(map[string]ini.Section)
  764. if strings.HasPrefix(name, "range:") {
  765. // range section
  766. rangePrefix := strings.TrimSpace(strings.TrimPrefix(name, "range:"))
  767. subSections, err = ParseRangeSection(rangePrefix, section)
  768. if err != nil {
  769. return
  770. }
  771. } else {
  772. subSections[name] = section
  773. }
  774. for subName, subSection := range subSections {
  775. if subSection["role"] == "" {
  776. subSection["role"] = "server"
  777. }
  778. role := subSection["role"]
  779. if role == "server" {
  780. cfg, errRet := NewProxyConfFromIni(prefix, subName, subSection)
  781. if errRet != nil {
  782. err = errRet
  783. return
  784. }
  785. proxyConfs[prefix+subName] = cfg
  786. } else if role == "visitor" {
  787. cfg, errRet := NewVisitorConfFromIni(prefix, subName, subSection)
  788. if errRet != nil {
  789. err = errRet
  790. return
  791. }
  792. visitorConfs[prefix+subName] = cfg
  793. } else {
  794. err = fmt.Errorf("role should be 'server' or 'visitor'")
  795. return
  796. }
  797. }
  798. }
  799. return
  800. }
  801. func copySection(section ini.Section) (out ini.Section) {
  802. out = make(ini.Section)
  803. for k, v := range section {
  804. out[k] = v
  805. }
  806. return
  807. }