proxy.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. ini "github.com/vaughan0/go-ini"
  23. )
  24. var proxyConfTypeMap map[string]reflect.Type
  25. func init() {
  26. proxyConfTypeMap = make(map[string]reflect.Type)
  27. proxyConfTypeMap[consts.TcpProxy] = reflect.TypeOf(TcpProxyConf{})
  28. proxyConfTypeMap[consts.UdpProxy] = reflect.TypeOf(UdpProxyConf{})
  29. proxyConfTypeMap[consts.HttpProxy] = reflect.TypeOf(HttpProxyConf{})
  30. proxyConfTypeMap[consts.HttpsProxy] = reflect.TypeOf(HttpsProxyConf{})
  31. proxyConfTypeMap[consts.StcpProxy] = reflect.TypeOf(StcpProxyConf{})
  32. proxyConfTypeMap[consts.XtcpProxy] = reflect.TypeOf(XtcpProxyConf{})
  33. }
  34. // NewConfByType creates a empty ProxyConf object by proxyType.
  35. // If proxyType isn't exist, return nil.
  36. func NewConfByType(proxyType string) ProxyConf {
  37. v, ok := proxyConfTypeMap[proxyType]
  38. if !ok {
  39. return nil
  40. }
  41. cfg := reflect.New(v).Interface().(ProxyConf)
  42. return cfg
  43. }
  44. type ProxyConf interface {
  45. GetName() string
  46. GetType() string
  47. GetBaseInfo() *BaseProxyConf
  48. LoadFromMsg(pMsg *msg.NewProxy)
  49. LoadFromFile(name string, conf ini.Section) error
  50. UnMarshalToMsg(pMsg *msg.NewProxy)
  51. Check() error
  52. Compare(conf ProxyConf) bool
  53. }
  54. func NewProxyConf(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
  55. if pMsg.ProxyType == "" {
  56. pMsg.ProxyType = consts.TcpProxy
  57. }
  58. cfg = NewConfByType(pMsg.ProxyType)
  59. if cfg == nil {
  60. err = fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
  61. return
  62. }
  63. cfg.LoadFromMsg(pMsg)
  64. err = cfg.Check()
  65. return
  66. }
  67. func NewProxyConfFromFile(name string, section ini.Section) (cfg ProxyConf, err error) {
  68. proxyType := section["type"]
  69. if proxyType == "" {
  70. proxyType = consts.TcpProxy
  71. section["type"] = consts.TcpProxy
  72. }
  73. cfg = NewConfByType(proxyType)
  74. if cfg == nil {
  75. err = fmt.Errorf("proxy [%s] type [%s] error", name, proxyType)
  76. return
  77. }
  78. err = cfg.LoadFromFile(name, section)
  79. return
  80. }
  81. // BaseProxy info
  82. type BaseProxyConf struct {
  83. ProxyName string `json:"proxy_name"`
  84. ProxyType string `json:"proxy_type"`
  85. UseEncryption bool `json:"use_encryption"`
  86. UseCompression bool `json:"use_compression"`
  87. }
  88. func (cfg *BaseProxyConf) GetName() string {
  89. return cfg.ProxyName
  90. }
  91. func (cfg *BaseProxyConf) GetType() string {
  92. return cfg.ProxyType
  93. }
  94. func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
  95. return cfg
  96. }
  97. func (cfg *BaseProxyConf) compare(cmp *BaseProxyConf) bool {
  98. if cfg.ProxyName != cmp.ProxyName ||
  99. cfg.ProxyType != cmp.ProxyType ||
  100. cfg.UseEncryption != cmp.UseEncryption ||
  101. cfg.UseCompression != cmp.UseCompression {
  102. return false
  103. }
  104. return true
  105. }
  106. func (cfg *BaseProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  107. cfg.ProxyName = pMsg.ProxyName
  108. cfg.ProxyType = pMsg.ProxyType
  109. cfg.UseEncryption = pMsg.UseEncryption
  110. cfg.UseCompression = pMsg.UseCompression
  111. }
  112. func (cfg *BaseProxyConf) LoadFromFile(name string, section ini.Section) error {
  113. var (
  114. tmpStr string
  115. ok bool
  116. )
  117. if ClientCommonCfg.User != "" {
  118. cfg.ProxyName = ClientCommonCfg.User + "." + name
  119. } else {
  120. cfg.ProxyName = name
  121. }
  122. cfg.ProxyType = section["type"]
  123. tmpStr, ok = section["use_encryption"]
  124. if ok && tmpStr == "true" {
  125. cfg.UseEncryption = true
  126. }
  127. tmpStr, ok = section["use_compression"]
  128. if ok && tmpStr == "true" {
  129. cfg.UseCompression = true
  130. }
  131. return nil
  132. }
  133. func (cfg *BaseProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  134. pMsg.ProxyName = cfg.ProxyName
  135. pMsg.ProxyType = cfg.ProxyType
  136. pMsg.UseEncryption = cfg.UseEncryption
  137. pMsg.UseCompression = cfg.UseCompression
  138. }
  139. // Bind info
  140. type BindInfoConf struct {
  141. BindAddr string `json:"bind_addr"`
  142. RemotePort int `json:"remote_port"`
  143. }
  144. func (cfg *BindInfoConf) compare(cmp *BindInfoConf) bool {
  145. if cfg.BindAddr != cmp.BindAddr ||
  146. cfg.RemotePort != cmp.RemotePort {
  147. return false
  148. }
  149. return true
  150. }
  151. func (cfg *BindInfoConf) LoadFromMsg(pMsg *msg.NewProxy) {
  152. cfg.BindAddr = ServerCommonCfg.ProxyBindAddr
  153. cfg.RemotePort = pMsg.RemotePort
  154. }
  155. func (cfg *BindInfoConf) LoadFromFile(name string, section ini.Section) (err error) {
  156. var (
  157. tmpStr string
  158. ok bool
  159. v int64
  160. )
  161. if tmpStr, ok = section["remote_port"]; ok {
  162. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  163. return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
  164. } else {
  165. cfg.RemotePort = int(v)
  166. }
  167. } else {
  168. return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
  169. }
  170. return nil
  171. }
  172. func (cfg *BindInfoConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  173. pMsg.RemotePort = cfg.RemotePort
  174. }
  175. func (cfg *BindInfoConf) check() (err error) {
  176. return nil
  177. }
  178. // Domain info
  179. type DomainConf struct {
  180. CustomDomains []string `json:"custom_domains"`
  181. SubDomain string `json:"sub_domain"`
  182. }
  183. func (cfg *DomainConf) compare(cmp *DomainConf) bool {
  184. if strings.Join(cfg.CustomDomains, " ") != strings.Join(cmp.CustomDomains, " ") ||
  185. cfg.SubDomain != cmp.SubDomain {
  186. return false
  187. }
  188. return true
  189. }
  190. func (cfg *DomainConf) LoadFromMsg(pMsg *msg.NewProxy) {
  191. cfg.CustomDomains = pMsg.CustomDomains
  192. cfg.SubDomain = pMsg.SubDomain
  193. }
  194. func (cfg *DomainConf) LoadFromFile(name string, section ini.Section) (err error) {
  195. var (
  196. tmpStr string
  197. ok bool
  198. )
  199. if tmpStr, ok = section["custom_domains"]; ok {
  200. cfg.CustomDomains = strings.Split(tmpStr, ",")
  201. for i, domain := range cfg.CustomDomains {
  202. cfg.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  203. }
  204. }
  205. if tmpStr, ok = section["subdomain"]; ok {
  206. cfg.SubDomain = tmpStr
  207. }
  208. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  209. return fmt.Errorf("Parse conf error: proxy [%s] custom_domains and subdomain should set at least one of them", name)
  210. }
  211. return
  212. }
  213. func (cfg *DomainConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  214. pMsg.CustomDomains = cfg.CustomDomains
  215. pMsg.SubDomain = cfg.SubDomain
  216. }
  217. func (cfg *DomainConf) check() (err error) {
  218. for _, domain := range cfg.CustomDomains {
  219. if ServerCommonCfg.SubDomainHost != "" && len(strings.Split(ServerCommonCfg.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
  220. if strings.Contains(domain, ServerCommonCfg.SubDomainHost) {
  221. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, ServerCommonCfg.SubDomainHost)
  222. }
  223. }
  224. }
  225. if cfg.SubDomain != "" {
  226. if ServerCommonCfg.SubDomainHost == "" {
  227. return fmt.Errorf("subdomain is not supported because this feature is not enabled by frps")
  228. }
  229. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  230. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  231. }
  232. }
  233. return nil
  234. }
  235. // Local service info
  236. type LocalSvrConf struct {
  237. LocalIp string `json:"-"`
  238. LocalPort int `json:"-"`
  239. }
  240. func (cfg *LocalSvrConf) compare(cmp *LocalSvrConf) bool {
  241. if cfg.LocalIp != cmp.LocalIp ||
  242. cfg.LocalPort != cmp.LocalPort {
  243. return false
  244. }
  245. return true
  246. }
  247. func (cfg *LocalSvrConf) LoadFromFile(name string, section ini.Section) (err error) {
  248. if cfg.LocalIp = section["local_ip"]; cfg.LocalIp == "" {
  249. cfg.LocalIp = "127.0.0.1"
  250. }
  251. if tmpStr, ok := section["local_port"]; ok {
  252. if cfg.LocalPort, err = strconv.Atoi(tmpStr); err != nil {
  253. return fmt.Errorf("Parse conf error: proxy [%s] local_port error", name)
  254. }
  255. } else {
  256. return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", name)
  257. }
  258. return nil
  259. }
  260. type PluginConf struct {
  261. Plugin string `json:"-"`
  262. PluginParams map[string]string `json:"-"`
  263. }
  264. func (cfg *PluginConf) compare(cmp *PluginConf) bool {
  265. if cfg.Plugin != cmp.Plugin ||
  266. len(cfg.PluginParams) != len(cmp.PluginParams) {
  267. return false
  268. }
  269. for k, v := range cfg.PluginParams {
  270. value, ok := cmp.PluginParams[k]
  271. if !ok || v != value {
  272. return false
  273. }
  274. }
  275. return true
  276. }
  277. func (cfg *PluginConf) LoadFromFile(name string, section ini.Section) (err error) {
  278. cfg.Plugin = section["plugin"]
  279. cfg.PluginParams = make(map[string]string)
  280. if cfg.Plugin != "" {
  281. // get params begin with "plugin_"
  282. for k, v := range section {
  283. if strings.HasPrefix(k, "plugin_") {
  284. cfg.PluginParams[k] = v
  285. }
  286. }
  287. } else {
  288. return fmt.Errorf("Parse conf error: proxy [%s] no plugin info found", name)
  289. }
  290. return
  291. }
  292. // TCP
  293. type TcpProxyConf struct {
  294. BaseProxyConf
  295. BindInfoConf
  296. LocalSvrConf
  297. PluginConf
  298. }
  299. func (cfg *TcpProxyConf) Compare(cmp ProxyConf) bool {
  300. cmpConf, ok := cmp.(*TcpProxyConf)
  301. if !ok {
  302. return false
  303. }
  304. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  305. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) ||
  306. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  307. !cfg.PluginConf.compare(&cmpConf.PluginConf) {
  308. return false
  309. }
  310. return true
  311. }
  312. func (cfg *TcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  313. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  314. cfg.BindInfoConf.LoadFromMsg(pMsg)
  315. }
  316. func (cfg *TcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  317. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  318. return
  319. }
  320. if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
  321. return
  322. }
  323. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  324. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  325. return
  326. }
  327. }
  328. return
  329. }
  330. func (cfg *TcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  331. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  332. cfg.BindInfoConf.UnMarshalToMsg(pMsg)
  333. }
  334. func (cfg *TcpProxyConf) Check() (err error) {
  335. err = cfg.BindInfoConf.check()
  336. return
  337. }
  338. // UDP
  339. type UdpProxyConf struct {
  340. BaseProxyConf
  341. BindInfoConf
  342. LocalSvrConf
  343. }
  344. func (cfg *UdpProxyConf) Compare(cmp ProxyConf) bool {
  345. cmpConf, ok := cmp.(*UdpProxyConf)
  346. if !ok {
  347. return false
  348. }
  349. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  350. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) ||
  351. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) {
  352. return false
  353. }
  354. return true
  355. }
  356. func (cfg *UdpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  357. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  358. cfg.BindInfoConf.LoadFromMsg(pMsg)
  359. }
  360. func (cfg *UdpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  361. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  362. return
  363. }
  364. if err = cfg.BindInfoConf.LoadFromFile(name, section); err != nil {
  365. return
  366. }
  367. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  368. return
  369. }
  370. return
  371. }
  372. func (cfg *UdpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  373. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  374. cfg.BindInfoConf.UnMarshalToMsg(pMsg)
  375. }
  376. func (cfg *UdpProxyConf) Check() (err error) {
  377. err = cfg.BindInfoConf.check()
  378. return
  379. }
  380. // HTTP
  381. type HttpProxyConf struct {
  382. BaseProxyConf
  383. DomainConf
  384. LocalSvrConf
  385. PluginConf
  386. Locations []string `json:"locations"`
  387. HostHeaderRewrite string `json:"host_header_rewrite"`
  388. HttpUser string `json:"-"`
  389. HttpPwd string `json:"-"`
  390. }
  391. func (cfg *HttpProxyConf) Compare(cmp ProxyConf) bool {
  392. cmpConf, ok := cmp.(*HttpProxyConf)
  393. if !ok {
  394. return false
  395. }
  396. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  397. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  398. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  399. !cfg.PluginConf.compare(&cmpConf.PluginConf) ||
  400. strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
  401. cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
  402. cfg.HttpUser != cmpConf.HttpUser ||
  403. cfg.HttpPwd != cmpConf.HttpPwd {
  404. return false
  405. }
  406. return true
  407. }
  408. func (cfg *HttpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  409. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  410. cfg.DomainConf.LoadFromMsg(pMsg)
  411. cfg.Locations = pMsg.Locations
  412. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  413. cfg.HttpUser = pMsg.HttpUser
  414. cfg.HttpPwd = pMsg.HttpPwd
  415. }
  416. func (cfg *HttpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  417. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  418. return
  419. }
  420. if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
  421. return
  422. }
  423. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  424. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  425. return
  426. }
  427. }
  428. var (
  429. tmpStr string
  430. ok bool
  431. )
  432. if tmpStr, ok = section["locations"]; ok {
  433. cfg.Locations = strings.Split(tmpStr, ",")
  434. } else {
  435. cfg.Locations = []string{""}
  436. }
  437. cfg.HostHeaderRewrite = section["host_header_rewrite"]
  438. cfg.HttpUser = section["http_user"]
  439. cfg.HttpPwd = section["http_pwd"]
  440. return
  441. }
  442. func (cfg *HttpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  443. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  444. cfg.DomainConf.UnMarshalToMsg(pMsg)
  445. pMsg.Locations = cfg.Locations
  446. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  447. pMsg.HttpUser = cfg.HttpUser
  448. pMsg.HttpPwd = cfg.HttpPwd
  449. }
  450. func (cfg *HttpProxyConf) Check() (err error) {
  451. if ServerCommonCfg.VhostHttpPort == 0 {
  452. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  453. }
  454. err = cfg.DomainConf.check()
  455. return
  456. }
  457. // HTTPS
  458. type HttpsProxyConf struct {
  459. BaseProxyConf
  460. DomainConf
  461. LocalSvrConf
  462. PluginConf
  463. }
  464. func (cfg *HttpsProxyConf) Compare(cmp ProxyConf) bool {
  465. cmpConf, ok := cmp.(*HttpsProxyConf)
  466. if !ok {
  467. return false
  468. }
  469. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  470. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  471. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  472. !cfg.PluginConf.compare(&cmpConf.PluginConf) {
  473. return false
  474. }
  475. return true
  476. }
  477. func (cfg *HttpsProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  478. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  479. cfg.DomainConf.LoadFromMsg(pMsg)
  480. }
  481. func (cfg *HttpsProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  482. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  483. return
  484. }
  485. if err = cfg.DomainConf.LoadFromFile(name, section); err != nil {
  486. return
  487. }
  488. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  489. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  490. return
  491. }
  492. }
  493. return
  494. }
  495. func (cfg *HttpsProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  496. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  497. cfg.DomainConf.UnMarshalToMsg(pMsg)
  498. }
  499. func (cfg *HttpsProxyConf) Check() (err error) {
  500. if ServerCommonCfg.VhostHttpsPort == 0 {
  501. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  502. }
  503. err = cfg.DomainConf.check()
  504. return
  505. }
  506. // STCP
  507. type StcpProxyConf struct {
  508. BaseProxyConf
  509. Role string `json:"role"`
  510. Sk string `json:"sk"`
  511. // used in role server
  512. LocalSvrConf
  513. PluginConf
  514. // used in role visitor
  515. ServerName string `json:"server_name"`
  516. BindAddr string `json:"bind_addr"`
  517. BindPort int `json:"bind_port"`
  518. }
  519. func (cfg *StcpProxyConf) Compare(cmp ProxyConf) bool {
  520. cmpConf, ok := cmp.(*StcpProxyConf)
  521. if !ok {
  522. return false
  523. }
  524. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  525. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  526. !cfg.PluginConf.compare(&cmpConf.PluginConf) ||
  527. cfg.Role != cmpConf.Role ||
  528. cfg.Sk != cmpConf.Sk ||
  529. cfg.ServerName != cmpConf.ServerName ||
  530. cfg.BindAddr != cmpConf.BindAddr ||
  531. cfg.BindPort != cmpConf.BindPort {
  532. return false
  533. }
  534. return true
  535. }
  536. // Only for role server.
  537. func (cfg *StcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  538. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  539. cfg.Sk = pMsg.Sk
  540. }
  541. func (cfg *StcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  542. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  543. return
  544. }
  545. tmpStr := section["role"]
  546. if tmpStr == "" {
  547. tmpStr = "server"
  548. }
  549. if tmpStr == "server" || tmpStr == "visitor" {
  550. cfg.Role = tmpStr
  551. } else {
  552. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
  553. }
  554. cfg.Sk = section["sk"]
  555. if tmpStr == "visitor" {
  556. prefix := section["prefix"]
  557. cfg.ServerName = prefix + section["server_name"]
  558. if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
  559. cfg.BindAddr = "127.0.0.1"
  560. }
  561. if tmpStr, ok := section["bind_port"]; ok {
  562. if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
  563. return fmt.Errorf("Parse conf error: proxy [%s] bind_port error", name)
  564. }
  565. } else {
  566. return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
  567. }
  568. } else {
  569. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  570. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  571. return
  572. }
  573. }
  574. }
  575. return
  576. }
  577. func (cfg *StcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  578. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  579. pMsg.Sk = cfg.Sk
  580. }
  581. func (cfg *StcpProxyConf) Check() (err error) {
  582. return
  583. }
  584. // XTCP
  585. type XtcpProxyConf struct {
  586. BaseProxyConf
  587. Role string `json:"role"`
  588. Sk string `json:"sk"`
  589. // used in role server
  590. LocalSvrConf
  591. PluginConf
  592. // used in role visitor
  593. ServerName string `json:"server_name"`
  594. BindAddr string `json:"bind_addr"`
  595. BindPort int `json:"bind_port"`
  596. }
  597. func (cfg *XtcpProxyConf) Compare(cmp ProxyConf) bool {
  598. cmpConf, ok := cmp.(*XtcpProxyConf)
  599. if !ok {
  600. return false
  601. }
  602. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  603. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  604. !cfg.PluginConf.compare(&cmpConf.PluginConf) ||
  605. cfg.Role != cmpConf.Role ||
  606. cfg.Sk != cmpConf.Sk ||
  607. cfg.ServerName != cmpConf.ServerName ||
  608. cfg.BindAddr != cmpConf.BindAddr ||
  609. cfg.BindPort != cmpConf.BindPort {
  610. return false
  611. }
  612. return true
  613. }
  614. // Only for role server.
  615. func (cfg *XtcpProxyConf) LoadFromMsg(pMsg *msg.NewProxy) {
  616. cfg.BaseProxyConf.LoadFromMsg(pMsg)
  617. cfg.Sk = pMsg.Sk
  618. }
  619. func (cfg *XtcpProxyConf) LoadFromFile(name string, section ini.Section) (err error) {
  620. if err = cfg.BaseProxyConf.LoadFromFile(name, section); err != nil {
  621. return
  622. }
  623. tmpStr := section["role"]
  624. if tmpStr == "" {
  625. tmpStr = "server"
  626. }
  627. if tmpStr == "server" || tmpStr == "visitor" {
  628. cfg.Role = tmpStr
  629. } else {
  630. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
  631. }
  632. cfg.Sk = section["sk"]
  633. if tmpStr == "visitor" {
  634. prefix := section["prefix"]
  635. cfg.ServerName = prefix + section["server_name"]
  636. if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
  637. cfg.BindAddr = "127.0.0.1"
  638. }
  639. if tmpStr, ok := section["bind_port"]; ok {
  640. if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
  641. return fmt.Errorf("Parse conf error: proxy [%s] bind_port error", name)
  642. }
  643. } else {
  644. return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
  645. }
  646. } else {
  647. if err = cfg.PluginConf.LoadFromFile(name, section); err != nil {
  648. if err = cfg.LocalSvrConf.LoadFromFile(name, section); err != nil {
  649. return
  650. }
  651. }
  652. }
  653. return
  654. }
  655. func (cfg *XtcpProxyConf) UnMarshalToMsg(pMsg *msg.NewProxy) {
  656. cfg.BaseProxyConf.UnMarshalToMsg(pMsg)
  657. pMsg.Sk = cfg.Sk
  658. }
  659. func (cfg *XtcpProxyConf) Check() (err error) {
  660. return
  661. }
  662. // if len(startProxy) is 0, start all
  663. // otherwise just start proxies in startProxy map
  664. func LoadProxyConfFromFile(prefix string, conf ini.File, startProxy map[string]struct{}) (
  665. proxyConfs map[string]ProxyConf, visitorConfs map[string]ProxyConf, err error) {
  666. if prefix != "" {
  667. prefix += "."
  668. }
  669. startAll := true
  670. if len(startProxy) > 0 {
  671. startAll = false
  672. }
  673. proxyConfs = make(map[string]ProxyConf)
  674. visitorConfs = make(map[string]ProxyConf)
  675. for name, section := range conf {
  676. _, shouldStart := startProxy[name]
  677. if name != "common" && (startAll || shouldStart) {
  678. // some proxy or visotr configure may be used this prefix
  679. section["prefix"] = prefix
  680. cfg, err := NewProxyConfFromFile(name, section)
  681. if err != nil {
  682. return proxyConfs, visitorConfs, err
  683. }
  684. role := section["role"]
  685. if role == "visitor" {
  686. visitorConfs[prefix+name] = cfg
  687. } else {
  688. proxyConfs[prefix+name] = cfg
  689. }
  690. }
  691. }
  692. return
  693. }