proxy.go 20 KB

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