proxy.go 23 KB

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