transports.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2014, Yawning Angel <yawning at schwanenlied dot me>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Package transports provides a interface to query supported pluggable
  28. // transports.
  29. package transports // import "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports"
  30. import (
  31. "fmt"
  32. "sync"
  33. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/snowflake"
  34. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/webtunnel"
  35. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/base"
  36. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/meeklite"
  37. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/obfs2"
  38. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/obfs3"
  39. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/obfs4"
  40. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/scramblesuit"
  41. )
  42. var transportMapLock sync.Mutex
  43. var transportMap map[string]base.Transport = make(map[string]base.Transport)
  44. // Register registers a transport protocol.
  45. func Register(transport base.Transport) error {
  46. transportMapLock.Lock()
  47. defer transportMapLock.Unlock()
  48. name := transport.Name()
  49. _, registered := transportMap[name]
  50. if registered {
  51. return fmt.Errorf("transport '%s' already registered", name)
  52. }
  53. transportMap[name] = transport
  54. return nil
  55. }
  56. // Transports returns the list of registered transport protocols.
  57. func Transports() []string {
  58. transportMapLock.Lock()
  59. defer transportMapLock.Unlock()
  60. var ret []string
  61. for name := range transportMap {
  62. ret = append(ret, name)
  63. }
  64. return ret
  65. }
  66. // Get returns a transport protocol implementation by name.
  67. func Get(name string) base.Transport {
  68. transportMapLock.Lock()
  69. defer transportMapLock.Unlock()
  70. t := transportMap[name]
  71. return t
  72. }
  73. // Init initializes all of the integrated transports.
  74. func Init() error {
  75. for _, v := range []base.Transport{
  76. new(meeklite.Transport),
  77. new(obfs2.Transport),
  78. new(obfs3.Transport),
  79. new(obfs4.Transport),
  80. new(scramblesuit.Transport),
  81. new(snowflake.Transport),
  82. webtunnel.Transport,
  83. } {
  84. if err := Register(v); err != nil {
  85. return err
  86. }
  87. }
  88. return nil
  89. }