base.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 scramblesuit provides an implementation of the ScrambleSuit
  28. // obfuscation protocol. The implementation is client only.
  29. package scramblesuit // import "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/scramblesuit"
  30. import (
  31. "fmt"
  32. "net"
  33. pt "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/goptlib"
  34. "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/lyrebird/transports/base"
  35. )
  36. const transportName = "scramblesuit"
  37. // Transport is the ScrambleSuit implementation of the base.Transport interface.
  38. type Transport struct{}
  39. // Name returns the name of the ScrambleSuit transport protocol.
  40. func (t *Transport) Name() string {
  41. return transportName
  42. }
  43. // ClientFactory returns a new ssClientFactory instance.
  44. func (t *Transport) ClientFactory(stateDir string) (base.ClientFactory, error) {
  45. tStore, err := loadTicketStore(stateDir)
  46. if err != nil {
  47. return nil, err
  48. }
  49. cf := &ssClientFactory{transport: t, ticketStore: tStore}
  50. return cf, nil
  51. }
  52. // ServerFactory will one day return a new ssServerFactory instance.
  53. func (t *Transport) ServerFactory(stateDir string, args *pt.Args) (base.ServerFactory, error) {
  54. // TODO: Fill this in eventually, though obfs4 is better.
  55. return nil, fmt.Errorf("server not supported")
  56. }
  57. type ssClientFactory struct {
  58. transport base.Transport
  59. ticketStore *ssTicketStore
  60. }
  61. func (cf *ssClientFactory) Transport() base.Transport {
  62. return cf.transport
  63. }
  64. func (cf *ssClientFactory) ParseArgs(args *pt.Args) (interface{}, error) {
  65. return newClientArgs(args)
  66. }
  67. func (cf *ssClientFactory) Dial(network, addr string, dialFn base.DialFunc, args interface{}) (net.Conn, error) {
  68. // Validate args before opening outgoing connection.
  69. ca, ok := args.(*ssClientArgs)
  70. if !ok {
  71. return nil, fmt.Errorf("invalid argument type for args")
  72. }
  73. conn, err := dialFn(network, addr)
  74. if err != nil {
  75. return nil, err
  76. }
  77. dialConn := conn
  78. if conn, err = newScrambleSuitClientConn(conn, cf.ticketStore, ca); err != nil {
  79. dialConn.Close()
  80. return nil, err
  81. }
  82. return conn, nil
  83. }
  84. // Not yet implemented
  85. func (cf *ssClientFactory) OnEvent(f func(base.TransportEvent)) {}
  86. var _ base.ClientFactory = (*ssClientFactory)(nil)
  87. var _ base.Transport = (*Transport)(nil)