base.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2015, 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 meeklite provides an implementation of the Meek circumvention
  28. // protocol. Only a client implementation is provided, and no effort is
  29. // made to normalize the TLS fingerprint.
  30. //
  31. // It borrows quite liberally from the real meek-client code.
  32. package meeklite // import "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/meeklite"
  33. import (
  34. "fmt"
  35. "net"
  36. pt "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib"
  37. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/base"
  38. )
  39. const transportName = "meek_lite"
  40. // Transport is the Meek implementation of the base.Transport interface.
  41. type Transport struct{}
  42. // Name returns the name of the Meek transport protocol.
  43. func (t *Transport) Name() string {
  44. return transportName
  45. }
  46. // ClientFactory returns a new meekClientFactory instance.
  47. func (t *Transport) ClientFactory(stateDir string) (base.ClientFactory, error) {
  48. cf := &meekClientFactory{transport: t}
  49. return cf, nil
  50. }
  51. // ServerFactory will one day return a new meekServerFactory instance.
  52. func (t *Transport) ServerFactory(stateDir string, args *pt.Args) (base.ServerFactory, error) {
  53. // TODO: Fill this in eventually, though for servers people should
  54. // just use the real thing.
  55. return nil, fmt.Errorf("server not supported")
  56. }
  57. type meekClientFactory struct {
  58. transport base.Transport
  59. }
  60. func (cf *meekClientFactory) Transport() base.Transport {
  61. return cf.transport
  62. }
  63. func (cf *meekClientFactory) ParseArgs(args *pt.Args) (interface{}, error) {
  64. return newClientArgs(args)
  65. }
  66. func (cf *meekClientFactory) Dial(network, addr string, dialFn base.DialFunc, args interface{}) (net.Conn, error) {
  67. // Validate args before opening outgoing connection.
  68. ca, ok := args.(*meekClientArgs)
  69. if !ok {
  70. return nil, fmt.Errorf("invalid argument type for args")
  71. }
  72. return newMeekConn(network, addr, dialFn, ca)
  73. }
  74. // Not yet implemented
  75. func (cf *meekClientFactory) OnEvent(f func(base.TransportEvent)) {}
  76. var (
  77. _ base.ClientFactory = (*meekClientFactory)(nil)
  78. _ base.Transport = (*Transport)(nil)
  79. )