server.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package plugin
  2. import (
  3. "fmt"
  4. "time"
  5. plugin "github.com/fatedier/frp/pkg/plugin/server"
  6. "github.com/fatedier/frp/pkg/transport"
  7. "github.com/fatedier/frp/test/e2e/framework"
  8. "github.com/fatedier/frp/test/e2e/framework/consts"
  9. . "github.com/onsi/ginkgo"
  10. )
  11. var _ = Describe("[Feature: Server-Plugins]", func() {
  12. f := framework.NewDefaultFramework()
  13. Describe("Login", func() {
  14. newFunc := func() *plugin.Request {
  15. var r plugin.Request
  16. r.Content = &plugin.LoginContent{}
  17. return &r
  18. }
  19. It("Auth for custom meta token", func() {
  20. localPort := f.AllocPort()
  21. clientAddressGot := false
  22. handler := func(req *plugin.Request) *plugin.Response {
  23. var ret plugin.Response
  24. content := req.Content.(*plugin.LoginContent)
  25. if content.ClientAddress != "" {
  26. clientAddressGot = true
  27. }
  28. if content.Metas["token"] == "123" {
  29. ret.Unchange = true
  30. } else {
  31. ret.Reject = true
  32. ret.RejectReason = "invalid token"
  33. }
  34. return &ret
  35. }
  36. pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
  37. f.RunServer("", pluginServer)
  38. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  39. [plugin.user-manager]
  40. addr = 127.0.0.1:%d
  41. path = /handler
  42. ops = Login
  43. `, localPort)
  44. clientConf := consts.DefaultClientConfig
  45. remotePort := f.AllocPort()
  46. clientConf += fmt.Sprintf(`
  47. meta_token = 123
  48. [tcp]
  49. type = tcp
  50. local_port = {{ .%s }}
  51. remote_port = %d
  52. `, framework.TCPEchoServerPort, remotePort)
  53. remotePort2 := f.AllocPort()
  54. invalidTokenClientConf := consts.DefaultClientConfig + fmt.Sprintf(`
  55. [tcp2]
  56. type = tcp
  57. local_port = {{ .%s }}
  58. remote_port = %d
  59. `, framework.TCPEchoServerPort, remotePort2)
  60. f.RunProcesses([]string{serverConf}, []string{clientConf, invalidTokenClientConf})
  61. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  62. framework.NewRequestExpect(f).Port(remotePort2).ExpectError(true).Ensure()
  63. framework.ExpectTrue(clientAddressGot)
  64. })
  65. })
  66. Describe("NewProxy", func() {
  67. newFunc := func() *plugin.Request {
  68. var r plugin.Request
  69. r.Content = &plugin.NewProxyContent{}
  70. return &r
  71. }
  72. It("Validate Info", func() {
  73. localPort := f.AllocPort()
  74. handler := func(req *plugin.Request) *plugin.Response {
  75. var ret plugin.Response
  76. content := req.Content.(*plugin.NewProxyContent)
  77. if content.ProxyName == "tcp" {
  78. ret.Unchange = true
  79. } else {
  80. ret.Reject = true
  81. }
  82. return &ret
  83. }
  84. pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
  85. f.RunServer("", pluginServer)
  86. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  87. [plugin.test]
  88. addr = 127.0.0.1:%d
  89. path = /handler
  90. ops = NewProxy
  91. `, localPort)
  92. clientConf := consts.DefaultClientConfig
  93. remotePort := f.AllocPort()
  94. clientConf += fmt.Sprintf(`
  95. [tcp]
  96. type = tcp
  97. local_port = {{ .%s }}
  98. remote_port = %d
  99. `, framework.TCPEchoServerPort, remotePort)
  100. f.RunProcesses([]string{serverConf}, []string{clientConf})
  101. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  102. })
  103. It("Mofify RemotePort", func() {
  104. localPort := f.AllocPort()
  105. remotePort := f.AllocPort()
  106. handler := func(req *plugin.Request) *plugin.Response {
  107. var ret plugin.Response
  108. content := req.Content.(*plugin.NewProxyContent)
  109. content.RemotePort = remotePort
  110. ret.Content = content
  111. return &ret
  112. }
  113. pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
  114. f.RunServer("", pluginServer)
  115. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  116. [plugin.test]
  117. addr = 127.0.0.1:%d
  118. path = /handler
  119. ops = NewProxy
  120. `, localPort)
  121. clientConf := consts.DefaultClientConfig
  122. clientConf += fmt.Sprintf(`
  123. [tcp]
  124. type = tcp
  125. local_port = {{ .%s }}
  126. remote_port = 0
  127. `, framework.TCPEchoServerPort, remotePort)
  128. f.RunProcesses([]string{serverConf}, []string{clientConf})
  129. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  130. })
  131. })
  132. Describe("Ping", func() {
  133. newFunc := func() *plugin.Request {
  134. var r plugin.Request
  135. r.Content = &plugin.PingContent{}
  136. return &r
  137. }
  138. It("Validate Info", func() {
  139. localPort := f.AllocPort()
  140. var record string
  141. handler := func(req *plugin.Request) *plugin.Response {
  142. var ret plugin.Response
  143. content := req.Content.(*plugin.PingContent)
  144. record = content.Ping.PrivilegeKey
  145. ret.Unchange = true
  146. return &ret
  147. }
  148. pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
  149. f.RunServer("", pluginServer)
  150. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  151. [plugin.test]
  152. addr = 127.0.0.1:%d
  153. path = /handler
  154. ops = Ping
  155. `, localPort)
  156. remotePort := f.AllocPort()
  157. clientConf := consts.DefaultClientConfig
  158. clientConf += fmt.Sprintf(`
  159. heartbeat_interval = 1
  160. authenticate_heartbeats = true
  161. [tcp]
  162. type = tcp
  163. local_port = {{ .%s }}
  164. remote_port = %d
  165. `, framework.TCPEchoServerPort, remotePort)
  166. f.RunProcesses([]string{serverConf}, []string{clientConf})
  167. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  168. time.Sleep(3 * time.Second)
  169. framework.ExpectNotEqual("", record)
  170. })
  171. })
  172. Describe("NewWorkConn", func() {
  173. newFunc := func() *plugin.Request {
  174. var r plugin.Request
  175. r.Content = &plugin.NewWorkConnContent{}
  176. return &r
  177. }
  178. It("Validate Info", func() {
  179. localPort := f.AllocPort()
  180. var record string
  181. handler := func(req *plugin.Request) *plugin.Response {
  182. var ret plugin.Response
  183. content := req.Content.(*plugin.NewWorkConnContent)
  184. record = content.NewWorkConn.RunID
  185. ret.Unchange = true
  186. return &ret
  187. }
  188. pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
  189. f.RunServer("", pluginServer)
  190. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  191. [plugin.test]
  192. addr = 127.0.0.1:%d
  193. path = /handler
  194. ops = NewWorkConn
  195. `, localPort)
  196. remotePort := f.AllocPort()
  197. clientConf := consts.DefaultClientConfig
  198. clientConf += fmt.Sprintf(`
  199. [tcp]
  200. type = tcp
  201. local_port = {{ .%s }}
  202. remote_port = %d
  203. `, framework.TCPEchoServerPort, remotePort)
  204. f.RunProcesses([]string{serverConf}, []string{clientConf})
  205. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  206. framework.ExpectNotEqual("", record)
  207. })
  208. })
  209. Describe("NewUserConn", func() {
  210. newFunc := func() *plugin.Request {
  211. var r plugin.Request
  212. r.Content = &plugin.NewUserConnContent{}
  213. return &r
  214. }
  215. It("Validate Info", func() {
  216. localPort := f.AllocPort()
  217. var record string
  218. handler := func(req *plugin.Request) *plugin.Response {
  219. var ret plugin.Response
  220. content := req.Content.(*plugin.NewUserConnContent)
  221. record = content.RemoteAddr
  222. ret.Unchange = true
  223. return &ret
  224. }
  225. pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, nil)
  226. f.RunServer("", pluginServer)
  227. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  228. [plugin.test]
  229. addr = 127.0.0.1:%d
  230. path = /handler
  231. ops = NewUserConn
  232. `, localPort)
  233. remotePort := f.AllocPort()
  234. clientConf := consts.DefaultClientConfig
  235. clientConf += fmt.Sprintf(`
  236. [tcp]
  237. type = tcp
  238. local_port = {{ .%s }}
  239. remote_port = %d
  240. `, framework.TCPEchoServerPort, remotePort)
  241. f.RunProcesses([]string{serverConf}, []string{clientConf})
  242. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  243. framework.ExpectNotEqual("", record)
  244. })
  245. })
  246. Describe("HTTPS Protocol", func() {
  247. newFunc := func() *plugin.Request {
  248. var r plugin.Request
  249. r.Content = &plugin.NewUserConnContent{}
  250. return &r
  251. }
  252. It("Validate Login Info, disable tls verify", func() {
  253. localPort := f.AllocPort()
  254. var record string
  255. handler := func(req *plugin.Request) *plugin.Response {
  256. var ret plugin.Response
  257. content := req.Content.(*plugin.NewUserConnContent)
  258. record = content.RemoteAddr
  259. ret.Unchange = true
  260. return &ret
  261. }
  262. tlsConfig, err := transport.NewServerTLSConfig("", "", "")
  263. framework.ExpectNoError(err)
  264. pluginServer := NewHTTPPluginServer(localPort, newFunc, handler, tlsConfig)
  265. f.RunServer("", pluginServer)
  266. serverConf := consts.DefaultServerConfig + fmt.Sprintf(`
  267. [plugin.test]
  268. addr = https://127.0.0.1:%d
  269. path = /handler
  270. ops = NewUserConn
  271. `, localPort)
  272. remotePort := f.AllocPort()
  273. clientConf := consts.DefaultClientConfig
  274. clientConf += fmt.Sprintf(`
  275. [tcp]
  276. type = tcp
  277. local_port = {{ .%s }}
  278. remote_port = %d
  279. `, framework.TCPEchoServerPort, remotePort)
  280. f.RunProcesses([]string{serverConf}, []string{clientConf})
  281. framework.NewRequestExpect(f).Port(remotePort).Ensure()
  282. framework.ExpectNotEqual("", record)
  283. })
  284. })
  285. })