1
0

proxy.go 23 KB

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