server.go 8.4 KB

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