proxy.go 24 KB

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