proxy.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strconv"
  19. "strings"
  20. "github.com/fatedier/frp/models/consts"
  21. "github.com/fatedier/frp/models/msg"
  22. "github.com/fatedier/frp/utils/util"
  23. ini "github.com/vaughan0/go-ini"
  24. )
  25. var (
  26. proxyConfTypeMap map[string]reflect.Type
  27. )
  28. func init() {
  29. proxyConfTypeMap = make(map[string]reflect.Type)
  30. proxyConfTypeMap[consts.TcpProxy] = reflect.TypeOf(TcpProxyConf{})
  31. proxyConfTypeMap[consts.UdpProxy] = reflect.TypeOf(UdpProxyConf{})
  32. proxyConfTypeMap[consts.HttpProxy] = reflect.TypeOf(HttpProxyConf{})
  33. proxyConfTypeMap[consts.HttpsProxy] = reflect.TypeOf(HttpsProxyConf{})
  34. proxyConfTypeMap[consts.StcpProxy] = reflect.TypeOf(StcpProxyConf{})
  35. proxyConfTypeMap[consts.XtcpProxy] = reflect.TypeOf(XtcpProxyConf{})
  36. }
  37. // NewConfByType creates a empty ProxyConf object by proxyType.
  38. // If proxyType isn't exist, return nil.
  39. func NewConfByType(proxyType string) ProxyConf {
  40. v, ok := proxyConfTypeMap[proxyType]
  41. if !ok {
  42. return nil
  43. }
  44. cfg := reflect.New(v).Interface().(ProxyConf)
  45. return cfg
  46. }
  47. type ProxyConf interface {
  48. GetBaseInfo() *BaseProxyConf
  49. UnmarshalFromMsg(pMsg *msg.NewProxy)
  50. UnmarshalFromIni(prefix string, name string, conf ini.Section) error
  51. MarshalToMsg(pMsg *msg.NewProxy)
  52. CheckForCli() error
  53. CheckForSvr() error
  54. Compare(conf ProxyConf) bool
  55. }
  56. func NewProxyConfFromMsg(pMsg *msg.NewProxy) (cfg ProxyConf, err error) {
  57. if pMsg.ProxyType == "" {
  58. pMsg.ProxyType = consts.TcpProxy
  59. }
  60. cfg = NewConfByType(pMsg.ProxyType)
  61. if cfg == nil {
  62. err = fmt.Errorf("proxy [%s] type [%s] error", pMsg.ProxyName, pMsg.ProxyType)
  63. return
  64. }
  65. cfg.UnmarshalFromMsg(pMsg)
  66. err = cfg.CheckForSvr()
  67. return
  68. }
  69. func NewProxyConfFromIni(prefix string, name string, section ini.Section) (cfg ProxyConf, err error) {
  70. proxyType := section["type"]
  71. if proxyType == "" {
  72. proxyType = consts.TcpProxy
  73. section["type"] = consts.TcpProxy
  74. }
  75. cfg = NewConfByType(proxyType)
  76. if cfg == nil {
  77. err = fmt.Errorf("proxy [%s] type [%s] error", name, proxyType)
  78. return
  79. }
  80. if err = cfg.UnmarshalFromIni(prefix, name, section); err != nil {
  81. return
  82. }
  83. err = cfg.CheckForCli()
  84. return
  85. }
  86. // BaseProxy info
  87. type BaseProxyConf struct {
  88. ProxyName string `json:"proxy_name"`
  89. ProxyType string `json:"proxy_type"`
  90. UseEncryption bool `json:"use_encryption"`
  91. UseCompression bool `json:"use_compression"`
  92. }
  93. func (cfg *BaseProxyConf) GetBaseInfo() *BaseProxyConf {
  94. return cfg
  95. }
  96. func (cfg *BaseProxyConf) compare(cmp *BaseProxyConf) bool {
  97. if cfg.ProxyName != cmp.ProxyName ||
  98. cfg.ProxyType != cmp.ProxyType ||
  99. cfg.UseEncryption != cmp.UseEncryption ||
  100. cfg.UseCompression != cmp.UseCompression {
  101. return false
  102. }
  103. return true
  104. }
  105. func (cfg *BaseProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  106. cfg.ProxyName = pMsg.ProxyName
  107. cfg.ProxyType = pMsg.ProxyType
  108. cfg.UseEncryption = pMsg.UseEncryption
  109. cfg.UseCompression = pMsg.UseCompression
  110. }
  111. func (cfg *BaseProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) error {
  112. var (
  113. tmpStr string
  114. ok bool
  115. )
  116. cfg.ProxyName = prefix + name
  117. cfg.ProxyType = section["type"]
  118. tmpStr, ok = section["use_encryption"]
  119. if ok && tmpStr == "true" {
  120. cfg.UseEncryption = true
  121. }
  122. tmpStr, ok = section["use_compression"]
  123. if ok && tmpStr == "true" {
  124. cfg.UseCompression = true
  125. }
  126. return nil
  127. }
  128. func (cfg *BaseProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  129. pMsg.ProxyName = cfg.ProxyName
  130. pMsg.ProxyType = cfg.ProxyType
  131. pMsg.UseEncryption = cfg.UseEncryption
  132. pMsg.UseCompression = cfg.UseCompression
  133. }
  134. // Bind info
  135. type BindInfoConf struct {
  136. RemotePort int `json:"remote_port"`
  137. }
  138. func (cfg *BindInfoConf) compare(cmp *BindInfoConf) bool {
  139. if cfg.RemotePort != cmp.RemotePort {
  140. return false
  141. }
  142. return true
  143. }
  144. func (cfg *BindInfoConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  145. cfg.RemotePort = pMsg.RemotePort
  146. }
  147. func (cfg *BindInfoConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  148. var (
  149. tmpStr string
  150. ok bool
  151. v int64
  152. )
  153. if tmpStr, ok = section["remote_port"]; ok {
  154. if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
  155. return fmt.Errorf("Parse conf error: proxy [%s] remote_port error", name)
  156. } else {
  157. cfg.RemotePort = int(v)
  158. }
  159. } else {
  160. return fmt.Errorf("Parse conf error: proxy [%s] remote_port not found", name)
  161. }
  162. return nil
  163. }
  164. func (cfg *BindInfoConf) MarshalToMsg(pMsg *msg.NewProxy) {
  165. pMsg.RemotePort = cfg.RemotePort
  166. }
  167. // Domain info
  168. type DomainConf struct {
  169. CustomDomains []string `json:"custom_domains"`
  170. SubDomain string `json:"sub_domain"`
  171. }
  172. func (cfg *DomainConf) compare(cmp *DomainConf) bool {
  173. if strings.Join(cfg.CustomDomains, " ") != strings.Join(cmp.CustomDomains, " ") ||
  174. cfg.SubDomain != cmp.SubDomain {
  175. return false
  176. }
  177. return true
  178. }
  179. func (cfg *DomainConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  180. cfg.CustomDomains = pMsg.CustomDomains
  181. cfg.SubDomain = pMsg.SubDomain
  182. }
  183. func (cfg *DomainConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  184. var (
  185. tmpStr string
  186. ok bool
  187. )
  188. if tmpStr, ok = section["custom_domains"]; ok {
  189. cfg.CustomDomains = strings.Split(tmpStr, ",")
  190. for i, domain := range cfg.CustomDomains {
  191. cfg.CustomDomains[i] = strings.ToLower(strings.TrimSpace(domain))
  192. }
  193. }
  194. if tmpStr, ok = section["subdomain"]; ok {
  195. cfg.SubDomain = tmpStr
  196. }
  197. return
  198. }
  199. func (cfg *DomainConf) MarshalToMsg(pMsg *msg.NewProxy) {
  200. pMsg.CustomDomains = cfg.CustomDomains
  201. pMsg.SubDomain = cfg.SubDomain
  202. }
  203. func (cfg *DomainConf) check() (err error) {
  204. if len(cfg.CustomDomains) == 0 && cfg.SubDomain == "" {
  205. err = fmt.Errorf("custom_domains and subdomain should set at least one of them")
  206. return
  207. }
  208. return
  209. }
  210. func (cfg *DomainConf) checkForCli() (err error) {
  211. if err = cfg.check(); err != nil {
  212. return
  213. }
  214. return
  215. }
  216. func (cfg *DomainConf) checkForSvr() (err error) {
  217. if err = cfg.check(); err != nil {
  218. return
  219. }
  220. for _, domain := range cfg.CustomDomains {
  221. if subDomainHost != "" && len(strings.Split(subDomainHost, ".")) < len(strings.Split(domain, ".")) {
  222. if strings.Contains(domain, subDomainHost) {
  223. return fmt.Errorf("custom domain [%s] should not belong to subdomain_host [%s]", domain, subDomainHost)
  224. }
  225. }
  226. }
  227. if cfg.SubDomain != "" {
  228. if subDomainHost == "" {
  229. return fmt.Errorf("subdomain is not supported because this feature is not enabled by frps")
  230. }
  231. if strings.Contains(cfg.SubDomain, ".") || strings.Contains(cfg.SubDomain, "*") {
  232. return fmt.Errorf("'.' and '*' is not supported in subdomain")
  233. }
  234. }
  235. return
  236. }
  237. // Local service info
  238. type LocalSvrConf struct {
  239. LocalIp string `json:"local_ip"`
  240. LocalPort int `json:"local_port"`
  241. Plugin string `json:"plugin"`
  242. PluginParams map[string]string `json:"plugin_params"`
  243. }
  244. func (cfg *LocalSvrConf) compare(cmp *LocalSvrConf) bool {
  245. if cfg.LocalIp != cmp.LocalIp ||
  246. cfg.LocalPort != cmp.LocalPort {
  247. return false
  248. }
  249. if cfg.Plugin != cmp.Plugin ||
  250. len(cfg.PluginParams) != len(cmp.PluginParams) {
  251. return false
  252. }
  253. for k, v := range cfg.PluginParams {
  254. value, ok := cmp.PluginParams[k]
  255. if !ok || v != value {
  256. return false
  257. }
  258. }
  259. return true
  260. }
  261. func (cfg *LocalSvrConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  262. cfg.Plugin = section["plugin"]
  263. cfg.PluginParams = make(map[string]string)
  264. if cfg.Plugin != "" {
  265. // get params begin with "plugin_"
  266. for k, v := range section {
  267. if strings.HasPrefix(k, "plugin_") {
  268. cfg.PluginParams[k] = v
  269. }
  270. }
  271. } else {
  272. if cfg.LocalIp = section["local_ip"]; cfg.LocalIp == "" {
  273. cfg.LocalIp = "127.0.0.1"
  274. }
  275. if tmpStr, ok := section["local_port"]; ok {
  276. if cfg.LocalPort, err = strconv.Atoi(tmpStr); err != nil {
  277. return fmt.Errorf("Parse conf error: proxy [%s] local_port error", name)
  278. }
  279. } else {
  280. return fmt.Errorf("Parse conf error: proxy [%s] local_port not found", name)
  281. }
  282. }
  283. return
  284. }
  285. // TCP
  286. type TcpProxyConf struct {
  287. BaseProxyConf
  288. BindInfoConf
  289. LocalSvrConf
  290. }
  291. func (cfg *TcpProxyConf) Compare(cmp ProxyConf) bool {
  292. cmpConf, ok := cmp.(*TcpProxyConf)
  293. if !ok {
  294. return false
  295. }
  296. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  297. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) ||
  298. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) {
  299. return false
  300. }
  301. return true
  302. }
  303. func (cfg *TcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  304. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  305. cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
  306. }
  307. func (cfg *TcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  308. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  309. return
  310. }
  311. if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
  312. return
  313. }
  314. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  315. return
  316. }
  317. return
  318. }
  319. func (cfg *TcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  320. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  321. cfg.BindInfoConf.MarshalToMsg(pMsg)
  322. }
  323. func (cfg *TcpProxyConf) CheckForCli() error { return nil }
  324. func (cfg *TcpProxyConf) CheckForSvr() error { return nil }
  325. // UDP
  326. type UdpProxyConf struct {
  327. BaseProxyConf
  328. BindInfoConf
  329. LocalSvrConf
  330. }
  331. func (cfg *UdpProxyConf) Compare(cmp ProxyConf) bool {
  332. cmpConf, ok := cmp.(*UdpProxyConf)
  333. if !ok {
  334. return false
  335. }
  336. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  337. !cfg.BindInfoConf.compare(&cmpConf.BindInfoConf) ||
  338. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) {
  339. return false
  340. }
  341. return true
  342. }
  343. func (cfg *UdpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  344. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  345. cfg.BindInfoConf.UnmarshalFromMsg(pMsg)
  346. }
  347. func (cfg *UdpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  348. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  349. return
  350. }
  351. if err = cfg.BindInfoConf.UnmarshalFromIni(prefix, name, section); err != nil {
  352. return
  353. }
  354. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  355. return
  356. }
  357. return
  358. }
  359. func (cfg *UdpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  360. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  361. cfg.BindInfoConf.MarshalToMsg(pMsg)
  362. }
  363. func (cfg *UdpProxyConf) CheckForCli() error { return nil }
  364. func (cfg *UdpProxyConf) CheckForSvr() error { return nil }
  365. // HTTP
  366. type HttpProxyConf struct {
  367. BaseProxyConf
  368. DomainConf
  369. LocalSvrConf
  370. Locations []string `json:"locations"`
  371. HostHeaderRewrite string `json:"host_header_rewrite"`
  372. HttpUser string `json:"http_user"`
  373. HttpPwd string `json:"http_pwd"`
  374. }
  375. func (cfg *HttpProxyConf) Compare(cmp ProxyConf) bool {
  376. cmpConf, ok := cmp.(*HttpProxyConf)
  377. if !ok {
  378. return false
  379. }
  380. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  381. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  382. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  383. strings.Join(cfg.Locations, " ") != strings.Join(cmpConf.Locations, " ") ||
  384. cfg.HostHeaderRewrite != cmpConf.HostHeaderRewrite ||
  385. cfg.HttpUser != cmpConf.HttpUser ||
  386. cfg.HttpPwd != cmpConf.HttpPwd {
  387. return false
  388. }
  389. return true
  390. }
  391. func (cfg *HttpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  392. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  393. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  394. cfg.Locations = pMsg.Locations
  395. cfg.HostHeaderRewrite = pMsg.HostHeaderRewrite
  396. cfg.HttpUser = pMsg.HttpUser
  397. cfg.HttpPwd = pMsg.HttpPwd
  398. }
  399. func (cfg *HttpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  400. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  401. return
  402. }
  403. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  404. return
  405. }
  406. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  407. return
  408. }
  409. var (
  410. tmpStr string
  411. ok bool
  412. )
  413. if tmpStr, ok = section["locations"]; ok {
  414. cfg.Locations = strings.Split(tmpStr, ",")
  415. } else {
  416. cfg.Locations = []string{""}
  417. }
  418. cfg.HostHeaderRewrite = section["host_header_rewrite"]
  419. cfg.HttpUser = section["http_user"]
  420. cfg.HttpPwd = section["http_pwd"]
  421. return
  422. }
  423. func (cfg *HttpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  424. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  425. cfg.DomainConf.MarshalToMsg(pMsg)
  426. pMsg.Locations = cfg.Locations
  427. pMsg.HostHeaderRewrite = cfg.HostHeaderRewrite
  428. pMsg.HttpUser = cfg.HttpUser
  429. pMsg.HttpPwd = cfg.HttpPwd
  430. }
  431. func (cfg *HttpProxyConf) CheckForCli() (err error) {
  432. if err = cfg.DomainConf.checkForCli(); err != nil {
  433. return
  434. }
  435. return
  436. }
  437. func (cfg *HttpProxyConf) CheckForSvr() (err error) {
  438. if vhostHttpPort == 0 {
  439. err = fmt.Errorf("type [http] not support when vhost_http_port is not set")
  440. }
  441. if err = cfg.DomainConf.checkForSvr(); err != nil {
  442. return
  443. }
  444. return
  445. }
  446. // HTTPS
  447. type HttpsProxyConf struct {
  448. BaseProxyConf
  449. DomainConf
  450. LocalSvrConf
  451. }
  452. func (cfg *HttpsProxyConf) Compare(cmp ProxyConf) bool {
  453. cmpConf, ok := cmp.(*HttpsProxyConf)
  454. if !ok {
  455. return false
  456. }
  457. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  458. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  459. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) {
  460. return false
  461. }
  462. return true
  463. }
  464. func (cfg *HttpsProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  465. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  466. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  467. }
  468. func (cfg *HttpsProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  469. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  470. return
  471. }
  472. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  473. return
  474. }
  475. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  476. return
  477. }
  478. return
  479. }
  480. func (cfg *HttpsProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  481. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  482. cfg.DomainConf.MarshalToMsg(pMsg)
  483. }
  484. func (cfg *HttpsProxyConf) CheckForCli() (err error) {
  485. if err = cfg.DomainConf.checkForCli(); err != nil {
  486. return
  487. }
  488. return
  489. }
  490. func (cfg *HttpsProxyConf) CheckForSvr() (err error) {
  491. if vhostHttpsPort == 0 {
  492. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  493. }
  494. if err = cfg.DomainConf.checkForSvr(); err != nil {
  495. return
  496. }
  497. return
  498. }
  499. // STCP
  500. type StcpProxyConf struct {
  501. BaseProxyConf
  502. Role string `json:"role"`
  503. Sk string `json:"sk"`
  504. // used in role server
  505. LocalSvrConf
  506. // used in role visitor
  507. ServerName string `json:"server_name"`
  508. BindAddr string `json:"bind_addr"`
  509. BindPort int `json:"bind_port"`
  510. }
  511. func (cfg *StcpProxyConf) Compare(cmp ProxyConf) bool {
  512. cmpConf, ok := cmp.(*StcpProxyConf)
  513. if !ok {
  514. return false
  515. }
  516. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  517. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  518. cfg.Role != cmpConf.Role ||
  519. cfg.Sk != cmpConf.Sk ||
  520. cfg.ServerName != cmpConf.ServerName ||
  521. cfg.BindAddr != cmpConf.BindAddr ||
  522. cfg.BindPort != cmpConf.BindPort {
  523. return false
  524. }
  525. return true
  526. }
  527. // Only for role server.
  528. func (cfg *StcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  529. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  530. cfg.Sk = pMsg.Sk
  531. }
  532. func (cfg *StcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  533. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  534. return
  535. }
  536. tmpStr := section["role"]
  537. if tmpStr == "" {
  538. tmpStr = "server"
  539. }
  540. if tmpStr == "server" || tmpStr == "visitor" {
  541. cfg.Role = tmpStr
  542. } else {
  543. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
  544. }
  545. cfg.Sk = section["sk"]
  546. if tmpStr == "visitor" {
  547. prefix := section["prefix"]
  548. cfg.ServerName = prefix + section["server_name"]
  549. if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
  550. cfg.BindAddr = "127.0.0.1"
  551. }
  552. if tmpStr, ok := section["bind_port"]; ok {
  553. if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
  554. return fmt.Errorf("Parse conf error: proxy [%s] bind_port error", name)
  555. }
  556. } else {
  557. return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
  558. }
  559. } else {
  560. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  561. return
  562. }
  563. }
  564. return
  565. }
  566. func (cfg *StcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  567. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  568. pMsg.Sk = cfg.Sk
  569. }
  570. func (cfg *StcpProxyConf) CheckForCli() (err error) {
  571. if cfg.Role != "server" && cfg.Role != "visitor" {
  572. err = fmt.Errorf("role should be 'server' or 'visitor'")
  573. return
  574. }
  575. if cfg.Role == "visitor" {
  576. if cfg.BindAddr == "" {
  577. err = fmt.Errorf("bind_addr shouldn't be empty")
  578. return
  579. }
  580. }
  581. return
  582. }
  583. func (cfg *StcpProxyConf) CheckForSvr() (err error) {
  584. return
  585. }
  586. // XTCP
  587. type XtcpProxyConf struct {
  588. BaseProxyConf
  589. Role string `json:"role"`
  590. Sk string `json:"sk"`
  591. // used in role server
  592. LocalSvrConf
  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.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) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  616. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  617. cfg.Sk = pMsg.Sk
  618. }
  619. func (cfg *XtcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  620. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, 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.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  648. return
  649. }
  650. }
  651. return
  652. }
  653. func (cfg *XtcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  654. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  655. pMsg.Sk = cfg.Sk
  656. }
  657. func (cfg *XtcpProxyConf) CheckForCli() (err error) {
  658. if cfg.Role != "server" && cfg.Role != "visitor" {
  659. err = fmt.Errorf("role should be 'server' or 'visitor'")
  660. return
  661. }
  662. if cfg.Role == "visitor" {
  663. if cfg.BindAddr == "" {
  664. err = fmt.Errorf("bind_addr shouldn't be empty")
  665. return
  666. }
  667. }
  668. return
  669. }
  670. func (cfg *XtcpProxyConf) CheckForSvr() (err error) {
  671. return
  672. }
  673. func ParseRangeSection(name string, section ini.Section) (sections map[string]ini.Section, err error) {
  674. localPorts, errRet := util.ParseRangeNumbers(section["local_port"])
  675. if errRet != nil {
  676. err = fmt.Errorf("Parse conf error: range section [%s] local_port invalid, %v", name, errRet)
  677. return
  678. }
  679. remotePorts, errRet := util.ParseRangeNumbers(section["remote_port"])
  680. if errRet != nil {
  681. err = fmt.Errorf("Parse conf error: range section [%s] remote_port invalid, %v", name, errRet)
  682. return
  683. }
  684. if len(localPorts) != len(remotePorts) {
  685. err = fmt.Errorf("Parse conf error: range section [%s] local ports number should be same with remote ports number", name)
  686. return
  687. }
  688. if len(localPorts) == 0 {
  689. err = fmt.Errorf("Parse conf error: range section [%s] local_port and remote_port is necessary", name)
  690. return
  691. }
  692. sections = make(map[string]ini.Section)
  693. for i, port := range localPorts {
  694. subName := fmt.Sprintf("%s_%d", name, i)
  695. subSection := copySection(section)
  696. subSection["local_port"] = fmt.Sprintf("%d", port)
  697. subSection["remote_port"] = fmt.Sprintf("%d", remotePorts[i])
  698. sections[subName] = subSection
  699. }
  700. return
  701. }
  702. // if len(startProxy) is 0, start all
  703. // otherwise just start proxies in startProxy map
  704. func LoadProxyConfFromIni(prefix string, conf ini.File, startProxy map[string]struct{}) (
  705. proxyConfs map[string]ProxyConf, visitorConfs map[string]ProxyConf, err error) {
  706. if prefix != "" {
  707. prefix += "."
  708. }
  709. startAll := true
  710. if len(startProxy) > 0 {
  711. startAll = false
  712. }
  713. proxyConfs = make(map[string]ProxyConf)
  714. visitorConfs = make(map[string]ProxyConf)
  715. for name, section := range conf {
  716. if name == "common" {
  717. continue
  718. }
  719. _, shouldStart := startProxy[name]
  720. if !startAll && !shouldStart {
  721. continue
  722. }
  723. subSections := make(map[string]ini.Section)
  724. if strings.HasPrefix(name, "range:") {
  725. // range section
  726. rangePrefix := strings.TrimSpace(strings.TrimPrefix(name, "range:"))
  727. subSections, err = ParseRangeSection(rangePrefix, section)
  728. if err != nil {
  729. return
  730. }
  731. } else {
  732. subSections[name] = section
  733. }
  734. for subName, subSection := range subSections {
  735. cfg, err := NewProxyConfFromIni(prefix, subName, subSection)
  736. if err != nil {
  737. return proxyConfs, visitorConfs, err
  738. }
  739. role := subSection["role"]
  740. if role == "visitor" {
  741. visitorConfs[prefix+subName] = cfg
  742. } else {
  743. proxyConfs[prefix+subName] = cfg
  744. }
  745. }
  746. }
  747. return
  748. }
  749. func copySection(section ini.Section) (out ini.Section) {
  750. out = make(ini.Section)
  751. for k, v := range section {
  752. out[k] = v
  753. }
  754. return
  755. }