proxy.go 20 KB

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