transports.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.com/yawning/obfs4.git/transports"
  30. import (
  31. "fmt"
  32. "sync"
  33. "gitlab.com/yawning/obfs4.git/transports/base"
  34. "gitlab.com/yawning/obfs4.git/transports/meeklite"
  35. "gitlab.com/yawning/obfs4.git/transports/obfs2"
  36. "gitlab.com/yawning/obfs4.git/transports/obfs3"
  37. "gitlab.com/yawning/obfs4.git/transports/obfs4"
  38. "gitlab.com/yawning/obfs4.git/transports/scramblesuit"
  39. )
  40. var transportMapLock sync.Mutex
  41. var transportMap map[string]base.Transport = make(map[string]base.Transport)
  42. // Register registers a transport protocol.
  43. func Register(transport base.Transport) error {
  44. transportMapLock.Lock()
  45. defer transportMapLock.Unlock()
  46. name := transport.Name()
  47. _, registered := transportMap[name]
  48. if registered {
  49. return fmt.Errorf("transport '%s' already registered", name)
  50. }
  51. transportMap[name] = transport
  52. return nil
  53. }
  54. // Transports returns the list of registered transport protocols.
  55. func Transports() []string {
  56. transportMapLock.Lock()
  57. defer transportMapLock.Unlock()
  58. var ret []string
  59. for name := range transportMap {
  60. ret = append(ret, name)
  61. }
  62. return ret
  63. }
  64. // Get returns a transport protocol implementation by name.
  65. func Get(name string) base.Transport {
  66. transportMapLock.Lock()
  67. defer transportMapLock.Unlock()
  68. t := transportMap[name]
  69. return t
  70. }
  71. // Init initializes all of the integrated transports.
  72. func Init() error {
  73. for _, v := range []base.Transport{
  74. new(meeklite.Transport),
  75. new(obfs2.Transport),
  76. new(obfs3.Transport),
  77. new(obfs4.Transport),
  78. new(scramblesuit.Transport),
  79. } {
  80. if err := Register(v); err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }