base.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 base provides the common interface that each supported transport
  28. // protocol must implement.
  29. package base // import "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/base"
  30. import (
  31. "net"
  32. pt "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib"
  33. )
  34. type DialFunc func(string, string) (net.Conn, error)
  35. // ClientFactory is the interface that defines the factory for creating
  36. // pluggable transport protocol client instances.
  37. type ClientFactory interface {
  38. // Transport returns the Transport instance that this ClientFactory belongs
  39. // to.
  40. Transport() Transport
  41. // ParseArgs parses the supplied arguments into an internal representation
  42. // for use with WrapConn. This routine is called before the outgoing
  43. // TCP/IP connection is created to allow doing things (like keypair
  44. // generation) to be hidden from third parties.
  45. ParseArgs(args *pt.Args) (interface{}, error)
  46. // Dial creates an outbound net.Conn, and does whatever is required
  47. // (eg: handshaking) to get the connection to the point where it is
  48. // ready to relay data.
  49. Dial(network, address string, dialFn DialFunc, args interface{}) (net.Conn, error)
  50. // OnEvent sets a callback that can be called by transports when notable
  51. // events in the connection happen. This is especially useful for logging
  52. // and UX purposes.
  53. OnEvent(f func(TransportEvent))
  54. }
  55. // ServerFactory is the interface that defines the factory for creating
  56. // plugable transport protocol server instances. As the arguments are the
  57. // property of the factory, validation is done at factory creation time.
  58. type ServerFactory interface {
  59. // Transport returns the Transport instance that this ServerFactory belongs
  60. // to.
  61. Transport() Transport
  62. // Args returns the Args required on the client side to handshake with
  63. // server connections created by this factory.
  64. Args() *pt.Args
  65. // WrapConn wraps the provided net.Conn with a transport protocol
  66. // implementation, and does whatever is required (eg: handshaking) to get
  67. // the connection to a point where it is ready to relay data.
  68. WrapConn(conn net.Conn) (net.Conn, error)
  69. }
  70. // Transport is an interface that defines a pluggable transport protocol.
  71. type Transport interface {
  72. // Name returns the name of the transport protocol. It MUST be a valid C
  73. // identifier.
  74. Name() string
  75. // ClientFactory returns a ClientFactory instance for this transport
  76. // protocol.
  77. ClientFactory(stateDir string) (ClientFactory, error)
  78. // ServerFactory returns a ServerFactory instance for this transport
  79. // protocol. This can fail if the provided arguments are invalid.
  80. ServerFactory(stateDir string, args *pt.Args) (ServerFactory, error)
  81. }
  82. type TransportEvent interface {
  83. String() string
  84. }