example_udpproxy_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // The MIT License (MIT)
  2. //
  3. // # Copyright (c) 2021 Winlin
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package vnet_test
  22. import (
  23. "net"
  24. vnet_proxy "github.com/ossrs/srs-bench/vnet"
  25. "github.com/pion/logging"
  26. "github.com/pion/transport/v2/vnet"
  27. )
  28. // Proxy many vnet endpoint to one real server endpoint.
  29. // For example:
  30. //
  31. // vnet(10.0.0.11:5787) => proxy => 192.168.1.10:8000
  32. // vnet(10.0.0.11:5788) => proxy => 192.168.1.10:8000
  33. // vnet(10.0.0.11:5789) => proxy => 192.168.1.10:8000
  34. func ExampleUDPProxyManyToOne() { // nolint:govet
  35. var clientNetwork *vnet.Net
  36. var serverAddr *net.UDPAddr
  37. if addr, err := net.ResolveUDPAddr("udp4", "192.168.1.10:8000"); err != nil {
  38. // handle error
  39. } else {
  40. serverAddr = addr
  41. }
  42. // Setup the network and proxy.
  43. if true {
  44. // Create vnet WAN with one endpoint, please read from
  45. // https://github.com/pion/transport/tree/master/vnet#example-wan-with-one-endpoint-vnet
  46. router, err := vnet.NewRouter(&vnet.RouterConfig{
  47. CIDR: "0.0.0.0/0",
  48. LoggerFactory: logging.NewDefaultLoggerFactory(),
  49. })
  50. if err != nil {
  51. // handle error
  52. }
  53. // Create a network and add to router, for example, for client.
  54. clientNetwork = vnet.NewNet(&vnet.NetConfig{
  55. StaticIP: "10.0.0.11",
  56. })
  57. if err = router.AddNet(clientNetwork); err != nil {
  58. // handle error
  59. }
  60. // Start the router.
  61. if err = router.Start(); err != nil {
  62. // handle error
  63. }
  64. defer router.Stop() // nolint:errcheck
  65. // Create a proxy, bind to the router.
  66. proxy, err := vnet_proxy.NewProxy(router)
  67. if err != nil {
  68. // handle error
  69. }
  70. defer proxy.Close() // nolint:errcheck
  71. // Start to proxy some addresses, clientNetwork is a hit for proxy,
  72. // that the client in vnet is from this network.
  73. if err := proxy.Proxy(clientNetwork, serverAddr); err != nil {
  74. // handle error
  75. }
  76. }
  77. // Now, all packets from client, will be proxy to real server, vice versa.
  78. client0, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5787")
  79. if err != nil {
  80. // handle error
  81. }
  82. _, _ = client0.WriteTo([]byte("Hello"), serverAddr)
  83. client1, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5788")
  84. if err != nil {
  85. // handle error
  86. }
  87. _, _ = client1.WriteTo([]byte("Hello"), serverAddr)
  88. client2, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5789")
  89. if err != nil {
  90. // handle error
  91. }
  92. _, _ = client2.WriteTo([]byte("Hello"), serverAddr)
  93. }
  94. // Proxy many vnet endpoint to one real server endpoint.
  95. // For example:
  96. //
  97. // vnet(10.0.0.11:5787) => proxy => 192.168.1.10:8000
  98. // vnet(10.0.0.11:5788) => proxy => 192.168.1.10:8000
  99. func ExampleUDPProxyMultileTimes() { // nolint:govet
  100. var clientNetwork *vnet.Net
  101. var serverAddr *net.UDPAddr
  102. if addr, err := net.ResolveUDPAddr("udp4", "192.168.1.10:8000"); err != nil {
  103. // handle error
  104. } else {
  105. serverAddr = addr
  106. }
  107. // Setup the network and proxy.
  108. var proxy *vnet_proxy.UDPProxy
  109. if true {
  110. // Create vnet WAN with one endpoint, please read from
  111. // https://github.com/pion/transport/tree/master/vnet#example-wan-with-one-endpoint-vnet
  112. router, err := vnet.NewRouter(&vnet.RouterConfig{
  113. CIDR: "0.0.0.0/0",
  114. LoggerFactory: logging.NewDefaultLoggerFactory(),
  115. })
  116. if err != nil {
  117. // handle error
  118. }
  119. // Create a network and add to router, for example, for client.
  120. clientNetwork = vnet.NewNet(&vnet.NetConfig{
  121. StaticIP: "10.0.0.11",
  122. })
  123. if err = router.AddNet(clientNetwork); err != nil {
  124. // handle error
  125. }
  126. // Start the router.
  127. if err = router.Start(); err != nil {
  128. // handle error
  129. }
  130. defer router.Stop() // nolint:errcheck
  131. // Create a proxy, bind to the router.
  132. proxy, err = vnet_proxy.NewProxy(router)
  133. if err != nil {
  134. // handle error
  135. }
  136. defer proxy.Close() // nolint:errcheck
  137. }
  138. if true {
  139. // Start to proxy some addresses, clientNetwork is a hit for proxy,
  140. // that the client in vnet is from this network.
  141. if err := proxy.Proxy(clientNetwork, serverAddr); err != nil {
  142. // handle error
  143. }
  144. // Now, all packets from client, will be proxy to real server, vice versa.
  145. client0, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5787")
  146. if err != nil {
  147. // handle error
  148. }
  149. _, _ = client0.WriteTo([]byte("Hello"), serverAddr)
  150. }
  151. if true {
  152. // It's ok to proxy multiple times, for example, the publisher and player
  153. // might need to proxy when got answer.
  154. if err := proxy.Proxy(clientNetwork, serverAddr); err != nil {
  155. // handle error
  156. }
  157. client1, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5788")
  158. if err != nil {
  159. // handle error
  160. }
  161. _, _ = client1.WriteTo([]byte("Hello"), serverAddr)
  162. }
  163. }
  164. // Proxy one vnet endpoint to one real server endpoint.
  165. // For example:
  166. //
  167. // vnet(10.0.0.11:5787) => proxy0 => 192.168.1.10:8000
  168. // vnet(10.0.0.11:5788) => proxy1 => 192.168.1.10:8001
  169. // vnet(10.0.0.11:5789) => proxy2 => 192.168.1.10:8002
  170. func ExampleUDPProxyOneToOne() { // nolint:govet
  171. var clientNetwork *vnet.Net
  172. var serverAddr0 *net.UDPAddr
  173. if addr, err := net.ResolveUDPAddr("udp4", "192.168.1.10:8000"); err != nil {
  174. // handle error
  175. } else {
  176. serverAddr0 = addr
  177. }
  178. var serverAddr1 *net.UDPAddr
  179. if addr, err := net.ResolveUDPAddr("udp4", "192.168.1.10:8001"); err != nil {
  180. // handle error
  181. } else {
  182. serverAddr1 = addr
  183. }
  184. var serverAddr2 *net.UDPAddr
  185. if addr, err := net.ResolveUDPAddr("udp4", "192.168.1.10:8002"); err != nil {
  186. // handle error
  187. } else {
  188. serverAddr2 = addr
  189. }
  190. // Setup the network and proxy.
  191. if true {
  192. // Create vnet WAN with one endpoint, please read from
  193. // https://github.com/pion/transport/tree/master/vnet#example-wan-with-one-endpoint-vnet
  194. router, err := vnet.NewRouter(&vnet.RouterConfig{
  195. CIDR: "0.0.0.0/0",
  196. LoggerFactory: logging.NewDefaultLoggerFactory(),
  197. })
  198. if err != nil {
  199. // handle error
  200. }
  201. // Create a network and add to router, for example, for client.
  202. clientNetwork = vnet.NewNet(&vnet.NetConfig{
  203. StaticIP: "10.0.0.11",
  204. })
  205. if err = router.AddNet(clientNetwork); err != nil {
  206. // handle error
  207. }
  208. // Start the router.
  209. if err = router.Start(); err != nil {
  210. // handle error
  211. }
  212. defer router.Stop() // nolint:errcheck
  213. // Create a proxy, bind to the router.
  214. proxy, err := vnet_proxy.NewProxy(router)
  215. if err != nil {
  216. // handle error
  217. }
  218. defer proxy.Close() // nolint:errcheck
  219. // Start to proxy some addresses, clientNetwork is a hit for proxy,
  220. // that the client in vnet is from this network.
  221. if err := proxy.Proxy(clientNetwork, serverAddr0); err != nil {
  222. // handle error
  223. }
  224. if err := proxy.Proxy(clientNetwork, serverAddr1); err != nil {
  225. // handle error
  226. }
  227. if err := proxy.Proxy(clientNetwork, serverAddr2); err != nil {
  228. // handle error
  229. }
  230. }
  231. // Now, all packets from client, will be proxy to real server, vice versa.
  232. client0, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5787")
  233. if err != nil {
  234. // handle error
  235. }
  236. _, _ = client0.WriteTo([]byte("Hello"), serverAddr0)
  237. client1, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5788")
  238. if err != nil {
  239. // handle error
  240. }
  241. _, _ = client1.WriteTo([]byte("Hello"), serverAddr1)
  242. client2, err := clientNetwork.ListenPacket("udp4", "10.0.0.11:5789")
  243. if err != nil {
  244. // handle error
  245. }
  246. _, _ = client2.WriteTo([]byte("Hello"), serverAddr2)
  247. }