proxy.go 24 KB

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