1
0

proxy.go 21 KB

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