proxy.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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 in remote 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. return fmt.Errorf("type [http] not support when vhost_http_port is not set")
  440. }
  441. if err = cfg.DomainConf.checkForSvr(); err != nil {
  442. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  443. return
  444. }
  445. return
  446. }
  447. // HTTPS
  448. type HttpsProxyConf struct {
  449. BaseProxyConf
  450. DomainConf
  451. LocalSvrConf
  452. }
  453. func (cfg *HttpsProxyConf) Compare(cmp ProxyConf) bool {
  454. cmpConf, ok := cmp.(*HttpsProxyConf)
  455. if !ok {
  456. return false
  457. }
  458. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  459. !cfg.DomainConf.compare(&cmpConf.DomainConf) ||
  460. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) {
  461. return false
  462. }
  463. return true
  464. }
  465. func (cfg *HttpsProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  466. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  467. cfg.DomainConf.UnmarshalFromMsg(pMsg)
  468. }
  469. func (cfg *HttpsProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  470. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  471. return
  472. }
  473. if err = cfg.DomainConf.UnmarshalFromIni(prefix, name, section); err != nil {
  474. return
  475. }
  476. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  477. return
  478. }
  479. return
  480. }
  481. func (cfg *HttpsProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  482. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  483. cfg.DomainConf.MarshalToMsg(pMsg)
  484. }
  485. func (cfg *HttpsProxyConf) CheckForCli() (err error) {
  486. if err = cfg.DomainConf.checkForCli(); err != nil {
  487. return
  488. }
  489. return
  490. }
  491. func (cfg *HttpsProxyConf) CheckForSvr() (err error) {
  492. if vhostHttpsPort == 0 {
  493. return fmt.Errorf("type [https] not support when vhost_https_port is not set")
  494. }
  495. if err = cfg.DomainConf.checkForSvr(); err != nil {
  496. err = fmt.Errorf("proxy [%s] domain conf check error: %v", cfg.ProxyName, err)
  497. return
  498. }
  499. return
  500. }
  501. // STCP
  502. type StcpProxyConf struct {
  503. BaseProxyConf
  504. Role string `json:"role"`
  505. Sk string `json:"sk"`
  506. // used in role server
  507. LocalSvrConf
  508. // used in role visitor
  509. ServerName string `json:"server_name"`
  510. BindAddr string `json:"bind_addr"`
  511. BindPort int `json:"bind_port"`
  512. }
  513. func (cfg *StcpProxyConf) Compare(cmp ProxyConf) bool {
  514. cmpConf, ok := cmp.(*StcpProxyConf)
  515. if !ok {
  516. return false
  517. }
  518. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  519. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  520. cfg.Role != cmpConf.Role ||
  521. cfg.Sk != cmpConf.Sk ||
  522. cfg.ServerName != cmpConf.ServerName ||
  523. cfg.BindAddr != cmpConf.BindAddr ||
  524. cfg.BindPort != cmpConf.BindPort {
  525. return false
  526. }
  527. return true
  528. }
  529. // Only for role server.
  530. func (cfg *StcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  531. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  532. cfg.Sk = pMsg.Sk
  533. }
  534. func (cfg *StcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  535. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  536. return
  537. }
  538. tmpStr := section["role"]
  539. if tmpStr == "" {
  540. tmpStr = "server"
  541. }
  542. if tmpStr == "server" || tmpStr == "visitor" {
  543. cfg.Role = tmpStr
  544. } else {
  545. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
  546. }
  547. cfg.Sk = section["sk"]
  548. if tmpStr == "visitor" {
  549. prefix := section["prefix"]
  550. cfg.ServerName = prefix + section["server_name"]
  551. if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
  552. cfg.BindAddr = "127.0.0.1"
  553. }
  554. if tmpStr, ok := section["bind_port"]; ok {
  555. if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
  556. return fmt.Errorf("Parse conf error: proxy [%s] bind_port error", name)
  557. }
  558. } else {
  559. return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
  560. }
  561. } else {
  562. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  563. return
  564. }
  565. }
  566. return
  567. }
  568. func (cfg *StcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  569. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  570. pMsg.Sk = cfg.Sk
  571. }
  572. func (cfg *StcpProxyConf) CheckForCli() (err error) {
  573. if cfg.Role != "server" && cfg.Role != "visitor" {
  574. err = fmt.Errorf("role should be 'server' or 'visitor'")
  575. return
  576. }
  577. if cfg.Role == "visitor" {
  578. if cfg.BindAddr == "" {
  579. err = fmt.Errorf("bind_addr shouldn't be empty")
  580. return
  581. }
  582. if cfg.BindPort == 0 {
  583. err = fmt.Errorf("bind_port should be set")
  584. return
  585. }
  586. }
  587. return
  588. }
  589. func (cfg *StcpProxyConf) CheckForSvr() (err error) {
  590. return
  591. }
  592. // XTCP
  593. type XtcpProxyConf struct {
  594. BaseProxyConf
  595. Role string `json:"role"`
  596. Sk string `json:"sk"`
  597. // used in role server
  598. LocalSvrConf
  599. // used in role visitor
  600. ServerName string `json:"server_name"`
  601. BindAddr string `json:"bind_addr"`
  602. BindPort int `json:"bind_port"`
  603. }
  604. func (cfg *XtcpProxyConf) Compare(cmp ProxyConf) bool {
  605. cmpConf, ok := cmp.(*XtcpProxyConf)
  606. if !ok {
  607. return false
  608. }
  609. if !cfg.BaseProxyConf.compare(&cmpConf.BaseProxyConf) ||
  610. !cfg.LocalSvrConf.compare(&cmpConf.LocalSvrConf) ||
  611. cfg.Role != cmpConf.Role ||
  612. cfg.Sk != cmpConf.Sk ||
  613. cfg.ServerName != cmpConf.ServerName ||
  614. cfg.BindAddr != cmpConf.BindAddr ||
  615. cfg.BindPort != cmpConf.BindPort {
  616. return false
  617. }
  618. return true
  619. }
  620. // Only for role server.
  621. func (cfg *XtcpProxyConf) UnmarshalFromMsg(pMsg *msg.NewProxy) {
  622. cfg.BaseProxyConf.UnmarshalFromMsg(pMsg)
  623. cfg.Sk = pMsg.Sk
  624. }
  625. func (cfg *XtcpProxyConf) UnmarshalFromIni(prefix string, name string, section ini.Section) (err error) {
  626. if err = cfg.BaseProxyConf.UnmarshalFromIni(prefix, name, section); err != nil {
  627. return
  628. }
  629. tmpStr := section["role"]
  630. if tmpStr == "" {
  631. tmpStr = "server"
  632. }
  633. if tmpStr == "server" || tmpStr == "visitor" {
  634. cfg.Role = tmpStr
  635. } else {
  636. return fmt.Errorf("Parse conf error: proxy [%s] incorrect role [%s]", name, tmpStr)
  637. }
  638. cfg.Sk = section["sk"]
  639. if tmpStr == "visitor" {
  640. prefix := section["prefix"]
  641. cfg.ServerName = prefix + section["server_name"]
  642. if cfg.BindAddr = section["bind_addr"]; cfg.BindAddr == "" {
  643. cfg.BindAddr = "127.0.0.1"
  644. }
  645. if tmpStr, ok := section["bind_port"]; ok {
  646. if cfg.BindPort, err = strconv.Atoi(tmpStr); err != nil {
  647. return fmt.Errorf("Parse conf error: proxy [%s] bind_port error", name)
  648. }
  649. } else {
  650. return fmt.Errorf("Parse conf error: proxy [%s] bind_port not found", name)
  651. }
  652. } else {
  653. if err = cfg.LocalSvrConf.UnmarshalFromIni(prefix, name, section); err != nil {
  654. return
  655. }
  656. }
  657. return
  658. }
  659. func (cfg *XtcpProxyConf) MarshalToMsg(pMsg *msg.NewProxy) {
  660. cfg.BaseProxyConf.MarshalToMsg(pMsg)
  661. pMsg.Sk = cfg.Sk
  662. }
  663. func (cfg *XtcpProxyConf) CheckForCli() (err error) {
  664. if cfg.Role != "server" && cfg.Role != "visitor" {
  665. err = fmt.Errorf("role should be 'server' or 'visitor'")
  666. return
  667. }
  668. if cfg.Role == "visitor" {
  669. if cfg.BindAddr == "" {
  670. err = fmt.Errorf("bind_addr shouldn't be empty")
  671. return
  672. }
  673. if cfg.BindPort == 0 {
  674. err = fmt.Errorf("bind_port should be set")
  675. return
  676. }
  677. }
  678. return
  679. }
  680. func (cfg *XtcpProxyConf) CheckForSvr() (err error) {
  681. return
  682. }
  683. func ParseRangeSection(name string, section ini.Section) (sections map[string]ini.Section, err error) {
  684. localPorts, errRet := util.ParseRangeNumbers(section["local_port"])
  685. if errRet != nil {
  686. err = fmt.Errorf("Parse conf error: range section [%s] local_port invalid, %v", name, errRet)
  687. return
  688. }
  689. remotePorts, errRet := util.ParseRangeNumbers(section["remote_port"])
  690. if errRet != nil {
  691. err = fmt.Errorf("Parse conf error: range section [%s] remote_port invalid, %v", name, errRet)
  692. return
  693. }
  694. if len(localPorts) != len(remotePorts) {
  695. err = fmt.Errorf("Parse conf error: range section [%s] local ports number should be same with remote ports number", name)
  696. return
  697. }
  698. if len(localPorts) == 0 {
  699. err = fmt.Errorf("Parse conf error: range section [%s] local_port and remote_port is necessary", name)
  700. return
  701. }
  702. sections = make(map[string]ini.Section)
  703. for i, port := range localPorts {
  704. subName := fmt.Sprintf("%s_%d", name, i)
  705. subSection := copySection(section)
  706. subSection["local_port"] = fmt.Sprintf("%d", port)
  707. subSection["remote_port"] = fmt.Sprintf("%d", remotePorts[i])
  708. sections[subName] = subSection
  709. }
  710. return
  711. }
  712. // if len(startProxy) is 0, start all
  713. // otherwise just start proxies in startProxy map
  714. func LoadProxyConfFromIni(prefix string, conf ini.File, startProxy map[string]struct{}) (
  715. proxyConfs map[string]ProxyConf, visitorConfs map[string]ProxyConf, err error) {
  716. if prefix != "" {
  717. prefix += "."
  718. }
  719. startAll := true
  720. if len(startProxy) > 0 {
  721. startAll = false
  722. }
  723. proxyConfs = make(map[string]ProxyConf)
  724. visitorConfs = make(map[string]ProxyConf)
  725. for name, section := range conf {
  726. if name == "common" {
  727. continue
  728. }
  729. _, shouldStart := startProxy[name]
  730. if !startAll && !shouldStart {
  731. continue
  732. }
  733. subSections := make(map[string]ini.Section)
  734. if strings.HasPrefix(name, "range:") {
  735. // range section
  736. rangePrefix := strings.TrimSpace(strings.TrimPrefix(name, "range:"))
  737. subSections, err = ParseRangeSection(rangePrefix, section)
  738. if err != nil {
  739. return
  740. }
  741. } else {
  742. subSections[name] = section
  743. }
  744. for subName, subSection := range subSections {
  745. cfg, err := NewProxyConfFromIni(prefix, subName, subSection)
  746. if err != nil {
  747. return proxyConfs, visitorConfs, err
  748. }
  749. role := subSection["role"]
  750. if role == "visitor" {
  751. visitorConfs[prefix+subName] = cfg
  752. } else {
  753. proxyConfs[prefix+subName] = cfg
  754. }
  755. }
  756. }
  757. return
  758. }
  759. func copySection(section ini.Section) (out ini.Section) {
  760. out = make(ini.Section)
  761. for k, v := range section {
  762. out[k] = v
  763. }
  764. return
  765. }